I’m trying to create a several programs, where the first one initiates all the required IPC elements. The problem I am having is executing the subsidiary programs with the values from the first. I have tried the following:
int sem_id = semget (key1, 4, IPC_CREAT | 0666);
int shmid = shmget(key2, 1024, 0644 | IPC_CREAT);
int pipe1[2];
if (pipe (pipe1)){
printf("Error: %s",strerror(errno));
exit(1);
}
if(execl("/home/tropix/program-3","/home/tropix/program-3", sem_id, shmid, pipe1, (char*)0) == 0){
fprintf(stderr, "File Execution of program 3 failed.\n");
exit(1);
}
Program 3 is not executing, so I obviously have a problem with my execl statement. If I remove the variables sem_id, shmid and pipe2 it runs.
Thanks for any suggestions.
Edit: added statements for creating shmid and sem_id. I can print these int’s to stdout, so I’m sure they have a value. And as far as knowing whether program 3 is executing, see program 3:
main(int argc, char *argv[]){
int x;
printf("Number of args = %d", argc);
for(x=0;x<argc;x++){
printf("arg#: %d, %s\n",x,argv[x]);
}
}
Another note: I have compiled program 3. If I remove the execl arguments and replace them with quoted words (“test”), they show up as arguments in the child page. But in this case, NOTHING is printing from program 3.
The arguments to a program must all be strings.
Therefore, to pass the semaphore ID, shared memory ID and file descriptor to the executed program, you need the calling program code to convert the numbers into strings, and the executed program to convert the strings back into number which it then uses appropriately.
Your code doesn’t show ‘pipe2’…I’ve assumed it was a simple
int, but if it was an array (likepipe1is), then you need to subscript it with 0 or 1 to get the relevant file descriptor:It is not clear why you have
pipe1created; you need to do appropriate plumbing with it. If theexecl()returns, it failed. You don’t need to test its return value.From the comments:
Simplicity in the question is a good idea…
I assume that between the call to
pipe()and theexecl(), there was afork()that was omitted. Otherwise, thepipe()makes no sense whatsoever.Presumably, the intent of creating the pipe is to let the child process talk to the parent, or for the parent process to talk to the child. There are bi-directional pipes, but they’re neither standard nor portable; therefore, I assume you have a regular uni-directional pipe. Since you are passing the file descriptor number as an argument to the child, we are not redirecting standard input or standard output for the child – which is what you often (but not always) do with a pipe.
So, your revised code could be (blank lines removed to avoid scroll bar):
Clearly, if the pipe is read by the parent and written by the child, you need to juggle the closes and the specified file descriptor number.
Almost always, you end up closing at least one of the two file descriptors returned from
pipe(). If you redirect the pipe to standard input or standard output, you normally usedup2()to duplicate the appropriate end of the pipe, and then close both file descriptors returned frompipe(). Since all of this is messing with pipes, it is colloquially known as plumbing.