开发者

execl only executing once in a forked process, C programming

For some reason that I am unaware of, my only my first execl statement is executing in the following code:

pid = fork();
if(pid < 0){
    fprintf(stderr, "Fork Failed.\n");
    exit(1);
    return;
}else if(pid==0){
        开发者_高级运维if(execl("/home/tropix/hw11-2","/home/tropix/hw11-2",semarg,pipe_to_p3,pipe_to_p4,(char*)0)){
            fprintf(stderr, "File Exexecution of hw11-2 failed.\n");
            exit(1);
        }
        if(execl("/home/tropix/hw11-3","/home/tropix/hw11-3",shmarg,semarg,pipe_from_p2,pipe_to_p5_1, (char*)0)){
            fprintf(stderr, "File Execution of hw11-3 failed.\n");
            exit(1);
        }
        if(execl("/home/tropix/hw11-4","/home/tropix/hw11-4",shmarg,semarg,pipe_from_p2_2,pipe_to_p5_2, (char*)0)){
            fprintf(stderr, "File Execution of hw11-4 failed.\n");
            exit(1);
        }
        if(execl("/home/tropix/hw11-5","/home/tropix/hw11-5",semarg,pipe_from_p3,pipe_from_p4,(char*)0)){
            fprintf(stderr, "File Execution of hw11-5 failed.\n");
            exit(1);
        }
} else (...parent stuff...)

Does anyone have an idea as to why this is?

Thanks :)


The exec family of functions shall replace the current process image with a new process image.

So, after the 1st execl, the 2nd does simply not exist.


The exec family of functions work by replacing your process with the one you specify, so the function never returns if it is successful.


As the other answers say, exec* replace the current process image with a new one, so you need to fork() once for every exec*() if you want to spawn several children processes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜