【发布时间】:2013-12-17 05:50:12
【问题描述】:
大家好,所以我的代码应该创建无限数量的进程,每个进程都运行自己的程序并通信抛出的管道..
这是以递归方式完成的,因此第一个父级创建一个管道,然后是一个子级。在此之后,子级执行相同的操作,创建一个 pipe2 和 child2 等......
问题是父进程不想等待所有子进程被创建以及当我尝试插入时
waitpid(childpid, NULL, 0);
它有点等待永远......所以要么我希望最后一个孩子发送某种 STOP WAIT 信号,要么我想要另一种方法来解决这个问题!
代码如下:
/* workForce.c
*
* Program created by ----Secret-----
*
* Input has to be the programs you wish the workForce to execute with following parameters seperated by '0's
*
* example input:
* printenv 0 grep L 0 sort
*
* Will result in the command: printenv | grep L | sort | chosen PAGER
*
* The program will pipe each unit of the workForce to the next unit untill it reaches the last unit where
* it will printout the result to STDOUT.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define PIPE_READ_SIDE ( 0 )
#define PIPE_WRITE_SIDE ( 1 )
pid_t childpid; /* för child-processens PID vid fork() */
/*
* The generateWorkforce method automaticly creates it's own workforce with one process per taskset
* Each unit in the work force will execute the parameters provided
* As long as it's not the end of the workforce the unit will pipe its output to the next unit
*/
void generateWorkforce(int taskpointer, int totaltasks, int taskset[], char* tasks[]){
/* Create pipe then fork */
int pipe_filedesc[ 2 ];
int return_value;
return_value = pipe( pipe_filedesc );
if( -1 == return_value ) {perror( "Cannot create pipe" ); exit( 1 );}
childpid = fork();
if( 0 == childpid )
{
/* Redirecting STDIN to now read from pipe instead and removing the pipe */
return_value = dup2( pipe_filedesc[ PIPE_READ_SIDE], STDIN_FILENO );
if( -1 == return_value){perror( "Cannot dup" ); exit( 1 );}
return_value = close( pipe_filedesc[PIPE_READ_SIDE] );
if( -1 == return_value ){perror( "Cannot close read end" ); exit( 1 );}
return_value = close( pipe_filedesc[ PIPE_WRITE_SIDE ] );
if( -1 == return_value ){perror( "Cannot close write end" ); exit( 1 );}
if(taskpointer < totaltasks-1){
generateWorkforce((taskpointer+taskset[taskpointer]), totaltasks, taskset, tasks);
}
else {
kill(getppid, SIGINT);
execlp(tasks[taskpointer], tasks[taskpointer], (char *) 0);
/* exec only returns if an error occured */
perror( "Cannot exec execute[pointer]" );
exit( 1 );
}
}
else
{
if( -1 == childpid ){ perror( "Cannot fork()" ); exit( 1 );}
/* Redirecting STDOUT to write to pipe instead and thereafter removes the pipe */
return_value = dup2( pipe_filedesc[ PIPE_WRITE_SIDE], STDOUT_FILENO );
if( -1 == return_value){perror( "Cannot dup" ); exit( 1 );}
return_value = close( pipe_filedesc[ PIPE_WRITE_SIDE ] );
if( -1 == return_value ){perror( "Cannot close write end" ); exit( 1 );}
return_value = close( pipe_filedesc[ PIPE_READ_SIDE ] );
if( -1 == return_value ){perror( "Cannot close read end" ); exit( 1 );}
waitpid(childpid, NULL, 0);
/* Will define what task to execute and how many parameters it needs */
if(taskset[taskpointer] > 1){
char* execute[taskset[taskpointer]+1];
int i;
char* executeHead = tasks[taskpointer];
execute[0] = tasks[taskpointer];
for(i = 1;i<taskset[taskpointer];i++){
execute[i] = tasks[taskpointer+i];
}
execute[taskset[taskpointer]] = (char *) 0;
(void) execvp( executeHead, execute );
}
else{
(void) execlp( tasks[taskpointer], tasks[taskpointer], (char *) 0 );
}
/* exec only returns if an error occured */
perror( "Cannot exec tasks[taskpointer]" );
exit( 1 );
}
}
int main( int argc, char * argv[] )
{
/* The following block will determine how many tasksets was in the input */
char * separator = "0";
int i;
int j;
int tasknr = 1;
for(i = 1, j = 1;i<(argc);i++)
{
if(!(strcmp(argv[(i)], separator ))){
j++;
}
else{
tasknr++;
}
}
char* execute[tasknr];
int taskset[j+1];
/* Will fill the taskset array and execute array */
if(getenv("PAGER")) execute[(tasknr-1)] = getenv("PAGER");
else execute[(tasknr-1)] = "less";
taskset[j] = 1;
taskset[0] = 0;
int k = 0;
for(i = 1, j = 0;i<(argc);i++)
{
if(!(strcmp(argv[(i)], separator ))){
j++;
taskset[j] = 0;
}
else{
execute[k] = argv[i];
taskset[j]++;
k++;
}
}
generateWorkforce(0, tasknr, taskset, execute);
exit( 0 );
}
编辑:kill(getppid, SIGINT);试图停止等待!
【问题讨论】:
-
-1 != waitpid( pid, &status, WNOHANG )
-
更改了我为此设置的 waitpid,但结果与根本不等待一样。
-
所以 main(starter) 进程创建了一个子进程,而子进程创建了另一个子进程——就像一个进程列表,对吗?而且您希望主进程根本不等待,对吗?
-
我希望它等到最后一个孩子完成,但之后继续执行
-
与
wait()问题无关:我发现如果进程是在循环中创建的,则更容易理解,例如,请参阅Connecting n commands with pipes in a shell?。以及基于它的代码:pipeline-three-processes.c