用管道分叉

发布于 2024-08-28 01:30:11 字数 1737 浏览 5 评论 0原文

我尝试在 main 中执行 fork() 和管道,它工作得很好,但是当我尝试在函数中实现它时,由于某种原因,我没有得到任何输出,这是我的代码:

void cmd(int **pipefd,int count,int type, int last);    

int main(int argc, char *argv[]) {
int pipefd[3][2];
int i, total_cmds = 3,count = 0;
int in = 1;

for(i = 0; i < total_cmds;i++){
 pipe(pipefd[count++]);
 cmd(pipefd,count,i,0);    
}

 /*Last Command*/
 cmd(pipefd,count,i,1);    

exit(EXIT_SUCCESS);
}

void cmd(int **pipefd,int count,int type, int last){    
    int child_pid,i,i2;

     if ((child_pid = fork()) == 0) {

            if(count == 1){
               dup2(pipefd[count-1][1],1); /*first command*/
            }
            else if(last!=1){
               dup2(pipefd[count - 2][0],0); /*middle commands*/
               dup2(pipefd[count - 1][1],1);
            }
            else if(last == 1){
               dup2(pipefd[count - 1][0],0); /*last command*/
            }


            for(i = 0; i < count;i++){/*close pipes*/
            for(i2 = 0; i2 < 2;i2++){
               close(pipefd[i][i2]);
            }}



            if(type == 0){
                execlp("ls","ls","-al",NULL);
            }
            else if(type == 1){
                execlp("grep","grep",".bak",NULL);
            }
            else if(type==2){
                               execl("/usr/bin/wc","wc",NULL);
            }
            else if(type ==3){
                         execl("/usr/bin/wc","wc","-l",NULL);
           }
            perror("exec");
            exit(EXIT_FAILURE);
    }
    else if (child_pid < 0) {
            perror("fork");
            exit(EXIT_FAILURE);
    }
}

我检查了文件描述符,它正在打开正确的,不确定问题是什么 可能是..

编辑:我修复了问题,但我有子进程,哪种方式是等待子进程的最佳方式, while(wait(NULL)!=-1);但那挂了

I have tried to do fork() and piping in main and it works perfectly fine but when I try to implement it in a function for some reason I don't get any output, this is my code:

void cmd(int **pipefd,int count,int type, int last);    

int main(int argc, char *argv[]) {
int pipefd[3][2];
int i, total_cmds = 3,count = 0;
int in = 1;

for(i = 0; i < total_cmds;i++){
 pipe(pipefd[count++]);
 cmd(pipefd,count,i,0);    
}

 /*Last Command*/
 cmd(pipefd,count,i,1);    

exit(EXIT_SUCCESS);
}

void cmd(int **pipefd,int count,int type, int last){    
    int child_pid,i,i2;

     if ((child_pid = fork()) == 0) {

            if(count == 1){
               dup2(pipefd[count-1][1],1); /*first command*/
            }
            else if(last!=1){
               dup2(pipefd[count - 2][0],0); /*middle commands*/
               dup2(pipefd[count - 1][1],1);
            }
            else if(last == 1){
               dup2(pipefd[count - 1][0],0); /*last command*/
            }


            for(i = 0; i < count;i++){/*close pipes*/
            for(i2 = 0; i2 < 2;i2++){
               close(pipefd[i][i2]);
            }}



            if(type == 0){
                execlp("ls","ls","-al",NULL);
            }
            else if(type == 1){
                execlp("grep","grep",".bak",NULL);
            }
            else if(type==2){
                               execl("/usr/bin/wc","wc",NULL);
            }
            else if(type ==3){
                         execl("/usr/bin/wc","wc","-l",NULL);
           }
            perror("exec");
            exit(EXIT_FAILURE);
    }
    else if (child_pid < 0) {
            perror("fork");
            exit(EXIT_FAILURE);
    }
}

I checked the file descriptors and it is opening the right ones, not sure what the problem
could be..

Edit: I Fixed the problem but I'm having child processes, which way would be the best to wait for the child , while(wait(NULL)!=-1); but that hangs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2024-09-04 01:30:11

问题是 pipefd 不是一个 int**,它是一个 int[3][2],所以当你将它传递给 < code>cmd,你会得到垃圾。您的编译器应该在每次调用cmd()时向您发出警告,例如:

warning: passing argument 1 of 'cmd' from incompatible pointer type

如果没有,请调高警告级别。

数组确实会衰减为指向其第一个元素的指针,因此您可以将一维数组传递给需要指针的函数,但这只适用于数组的第一个维度。特别是,二维数组不会衰减为指向指针的指针。它仅在第一级衰减,因此 pipefd 可以衰减为 int (*)[2] 类型,该类型被读作“指向 的数组 2 的指针” int"

所以,cmd的正确写法是这样的:

void cmd(int (*pipefd)[2],int count,int type, int last)

The problem is that pipefd is not an int**, it's an int[3][2], so when you pass it to cmd, you get garbage. Your compiler should be giving you a warning on each call to cmd(), such as something like this:

warning: passing argument 1 of 'cmd' from incompatible pointer type

If not, turn up your warning level.

It's true that arrays decay into pointers to their first elements, so you can pass a 1-D array to a function expecting a pointer, but that's only true for the first dimension of arrays. In particular, a 2D array does not decay into a pointer to a pointer. It decays at the first level only, so pipefd can decay into the type int (*)[2], which is read as "pointer to array 2 of int"

So, the correct way to write cmd is this:

void cmd(int (*pipefd)[2],int count,int type, int last)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文