1

This is not running on my Mac for some reasons that I can't figure out. the output I am getting is only from the main.c the output is

Parent PID 4066
Child PID 4067
Process 4067 exited with status 5

I need the main.c to execute counter.c and pass the argument 5 which I will then have to use it inside the for a loop, but I can't get exec to run at all no matter what path I put.

//main.c

int main(int argc, char *argv[]) {

    pid_t childOrZero = fork();

    if (childOrZero < 0){
        perror("Error happened while trying to fork\n");
        exit(-1);
    }


    if (childOrZero == 0){
        printf("Child PID %d\n", (int)getpid());
        execl("./counter", "5", NULL);
        exit(5);
    }

    // THis must be parent
    printf("Parent PID %d\n", (int)getpid());
    int status = 0;
    pid_t childpid = wait(&status);
    int childReturnedValue = WEXITSTATUS(status);
    printf("Process %d exited with status %d\n", (int)childOrZero, childReturnedValue);

    return 0;
}

counter.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main (int argc, char *argv[]){
    for (int i = 0; i<5; i++){
        printf("Process: %d  %d\n", (int)getpid(),i);
        //sleep(3);
    }
}
8
  • 1
    Call perror() or print errno after execl call. Commented Jun 5, 2017 at 5:28
  • No such file or directory. I tried using the absolute path and it is still not working Commented Jun 5, 2017 at 5:30
  • 1
    What is the precise way you compile counter.c? Can you execute it from the command line yourself after compiling it? Commented Jun 5, 2017 at 5:32
  • Please show your full log from compiling both programs to running the main program. Commented Jun 5, 2017 at 5:33
  • ⭕ p1$ 😬 gcc counter.c ⭕ p1$ 😬 ./a.out Process: 4144 0 Process: 4144 1 Process: 4144 2 Process: 4144 3 Process: 4144 4 Commented Jun 5, 2017 at 5:34

2 Answers 2

1

In a comment, you mention you compile counter.c into an executable called a.out. This is the default executable name when you do not provide an output name explicitly to the compiler. Thus, if you compile both counter.c and main.c, only one of them will be the a.out.

You can provide gcc an option to name your executable different from the default:

gcc -o counter counter.c

Also, your invocation of execl is not quite correct. The first argument is the path to the executable, but the remaining arguments will become argv[0], argv[1], etc. Thus, you really want to invoke execl this way:

     execl("./counter", "counter", "5", NULL);
Sign up to request clarification or add additional context in comments.

4 Comments

this can also be a comment :/
I know this and it still will give me a 'bad address' pereor message.
@iali87: There was a mistake in your invocation of execl.
@iali87: Regarding I know this..., I did ask you for the precise way you compiled counter.c, because I wanted to know if you had generated an executable with the correct name. Your answer to my question led me to conclude you had not.
0

Read documentation of execl for MacOSX and the POSIX specification of execl

It can fail (and that is the only case when it is returning). So code:

if (childOrZero == 0){
    printf("Child PID %d\n", (int)getpid());
    execl("./counter", "5", NULL);
    perror("execl counter");
    exit(5);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.