简体   繁体   English

带有execlp的c中的匿名管道

[英]anonymous pipes in c with execlp

i have a homework for my university in which i must have a main process and 3 child processes. 我要为我的大学做作业,其中必须有一个主要过程和三个子过程。 I must read an expression in the main process from the user and then pass it through an anonymous pipe at p1 process with redirection of standard input. 我必须在用户的主进程中读取一个表达式,然后在p1进程中通过重定向标准输入将其通过匿名管道传递。 Then i must modify it and pass it with another pipe to p2. 然后,我必须修改它,并将其与另一个管道传递给p2。 Then the same thing happens with p3 with another pipe. 然后,使用另一个管道的p3也会发生相同的情况。 Finally i must return the final expression from p3 to main process using another pipe. 最后,我必须使用另一个管道将最终表达式从p3返回到主进程。 When i create p1,p2,p3 processes in the main process by calling fork, i use execlp to run the code of each of these process. 当我通过调用fork在主进程中创建p1,p2,p3进程时,我使用execlp来运行每个进程的代码。

What i managed to do until now(apart from each process' logic which was really complicated), is to send the expression of the user from the main process to p1 process. 到目前为止,我设法做到的(除了每个流程的逻辑非常复杂之外),是将用户的表达从主流程发送到p1流程。 Inside the code of p1 in the main process, i redirected to the input of the p1 to the read end of the first pipe and i successfully read the expression from main process. 在主流程的p1代码内部,我将p1的输入重定向到第一个管道的读取端,并且我成功地从主流程读取了表达式。

My problem is with the other processes. 我的问题是其他过程。 I really got stuck on how to correctly redirect the inputs and outputs of each pipe to communicate with all processes. 我真的迷上了如何正确重定向每个管道的输入和输出以与所有进程进行通信的方法。 I really would appreciate any help here. 我真的很感谢这里的任何帮助。 Thank you in advance. 先感谢您。

From your description I think you want to chain forking. 根据您的描述,我认为您想进行链式分叉。 Eg instead of having main fork three times to create p1, p2 and p3, instead let main fork once to create p1, then let p1 fork once to create p2 which also forks once to create p3. 例如,不要让主叉创建3次来创建p1,p2和p3,而是让主叉一次创建p1,然后让p1叉一次创建p2,也让一次叉创建p3。

in this EXAMPLE you can see the execlp() function and the fork() function: 在此示例中,您可以看到execlp()函数和fork()函数:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
   pid_t pid;
   char *pathvar;
   char newpath[1000];

   pathvar = getenv("PATH");
   strcpy(newpath, pathvar);
   strcat(newpath, ":u/userid/bin");
   setenv("PATH", newpath);

   if ((pid = fork()) == -1)
      perror("fork error");
   else if (pid == 0) {
      execlp("newShell", "newShell", NULL);
      printf("Return not expected. Must be an execlp error.n");
   }
}

Here you can see how to make a pipe between two process: 在这里,您可以看到如何在两个过程之间建立管道:

/*
 “ls -R | grep <pat>” where <pat> is a pattern
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv [])
{
    int pipe1[2];
    int pid1;
    int pid2;
    char stringa[100];

    pipe(pipe1); // creo la pipe

    if((pid1=fork())==0)
    {
        close(pipe1[0]);
        dup2(pipe1[1],1);
        close(pipe1[1]);
        execlp("ls","ls","-R",NULL);
    }

    if((pid2=fork())==0)
    {
        close(pipe1[1]);
        dup2(pipe1[0],0);
        close(pipe1[0]);
        execlp("grep","grep",argv[1],NULL); 
    }

    close(pipe1[0]);
    close(pipe1[1]);
    waitpid(pid1,NULL,0);
    waitpid(pid2,NULL,0);
    exit(0);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM