2011-05-08 27 views
1

出于某种原因,我不知道,我只是我的第一个execl的语句下面的代码执行的:EXECL只在派生进程中执行一次,C编程

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...) 

没有人有一个想法,为什么这是?

谢谢:)

回答

2

exec函数族通过用您指定的函数替换您的进程来工作,所以如果函数成功,函数永远不会返回。

0

正如其他答案所说,exec*用一个新的替换当前的过程映像,所以如果你想产生几个子进程,你需要fork()一次exec*()