因此,我一直在为我的OS类开发代码,在我的项目中,我必须在命令行中为每个文件创建一个新的子进程,孩子的父母。我们应该重新创建Unix的wc实用程序(我已经处理过这个部分)。用C语言为一个父类创建多个子进程
什么我迄今试图为:
for(i=0; i<argcount; i++){
int pid;
pid = fork();
if(pid == 0){
/* Child Process */
/* Close read pipes */
close(l_pipe[0]);
close(w_pipe[0]);
close(c_pipe[0]);
wc(lflag, wflag, cflag, filenames[i]);
} else {
/* Parent Process for piping */
/* Close write pipes */
close(l_pipe[1]);
close(w_pipe[1]);
close(c_pipe[1]);
/* Read from pipes */
read(l_pipe[0], &buffer, sizeof(count_t));
lines+=buffer;
read(w_pipe[0], &buffer, sizeof(count_t));
words+=buffer;
read(c_pipe[0], &buffer, sizeof(count_t));
bytes+=buffer;
}
}
然而,本作也有孩子,这显然是错误的,创建多的父母。我不确定我应该在哪里分叉。我有在子进程和父进程之间使用管道,并且我确信父进程需要read()的次数与子进程write()的次数一样多。
非常感谢您提供任何建议。
这是一个count_t缓冲区,当然。这看起来非常有帮助,我会玩你的建议。此外,我的wc()在stdin上打印它自己的计数,通过管道发送它们,并打印被调用的进程的PID。 – saturn