noel880
noel880

Reputation: 35

Exec won't call my second program

I created a test file to see if I could run a second program, but the code doesn't run the actual file even though it seems to compile. Is my syntax for exec incorrect?

coordinator.c

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

// Creates 2^n processes for n amount of values.
pid_t child = fork();

if(child < 0) //parent process
{
    perror("fork() system call failed.");
    exit(-1);
}

else if(child == 0) //Child Process, worker will be called here.
{
     execl("/worker", "worker", "Hello", NULL);
     printf("I'm the child %d, my parent is %d\n", getpid(), getpid());  
}
else
{
    printf("I'm the parent %d, my child is %d\n", getpid(), child);
    wait(NULL); // wait for child process to catch up
}

}

worker.c

int main(int argc, char *argv[])
{
  printf("Hi, I'm the worker file!");

  return 0;
}

Upvotes: 0

Views: 158

Answers (2)

Achal
Achal

Reputation: 11921

Let's say worker executable is in the same directory where you are running the main(coordinator) process then in child process while doing exec you should do ./worker instead of /worker, that shows current working directory.

see then man pages of exec() for other argument, it says

int execl(const char *path, const char *arg, ...);

child process should be like below

else if(child == 0) //Child Process, worker will be called here.
{
     printf("I'm the child %d, my parent is %d\n", getpid(), getpid());
     //execl("/worker", "worker", "Hello", NULL);/** It's wrong, check the below one **/
     execl("./worker", "./worker", NULL);
}

IF worker is in different directory then set the PATH variable, it seems it's in same directory because you are trying to do /worker instead of ./worker.

EDIT :

How to compile & execute :

coordinator.c

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
        pid_t child = fork();
        if(child < 0){
                perror("fork() system call failed.");
                exit(-1);
        }
        else if(child == 0) {
                printf("I'm the child %d, my parent is %d\n", getpid(), getpid());
                execl("./worker", "./worker", NULL);
        }
        else {
                printf("I'm the parent %d, my child is %d\n", getpid(), child);
                wait(NULL); // wait for child process to catch up
        }
}

worker.c

int main(int argc, char *argv[])
{
        printf("Hi, I'm the worker file!");
        return 0;
}

First create the worker executable/binary as

gcc -Wall worker.c -o worker

Next, create the main executable and run it

gcc -Wall coordinator.c
./a.out

Upvotes: 2

Andrea Tulimiero
Andrea Tulimiero

Reputation: 18519

The problem is in the PATH argument you're passing to execl(). In fact, if you do insert a / at the beginning of the string passed as the first argument, the function is going to seek the program at the root of your file system. To let it look for the worker executable in your current directory, just specify the name of it, thus execl("worker", ... ), or execl("./worker", ... )

Take a look here to understand how the function works https://www.systutorials.com/docs/linux/man/3-execl/

Upvotes: 3

Related Questions