Tag:

C中的管道和叉子

我试图在C中使用管道和叉子编写程序。我对如何实现这一点感到有点困惑。 我需要根据用户指定的输入创建n个进程(也就是说,如果用户输入2,我将通过fork创建2个进程)。 然后,通过fork创建的每个新进程都会执行一些操作。 然而,现在管道出现了。 我有管道从每个叉子获取信息。 我的问题是 – 如何为每个叉子创建多个管道? 如果我有多个叉子,我怎样才能从父母角度处理多个管道? 此外,假设我在父进程中有一个字符指针数组,当一个新进程通过fork生成时,这个进程可以访问该数组,还是不存在? 谢谢!

从管道C读取

我需要创建一个新进程来运行xxx程序,加载xxx程序,发送’xxx’从管道读取的数据,但我收到此错误,“错误:无法读取stdin”每当我尝试编译。 pid_t pid; pid = fork(); int fd[2]; int ret; ret = pipe(fd); if (ret == -1) { perror(“pipe failed”); exit(1); } if (pid == -1) { perror(“fork failed”); exit(1); } else if (pid == 0) //Child { char buff[10]; int validate; close(fd[1]); dup2(fd[0], STDIN_FILENO); close(fd[0]); read(STDIN_FILENO, buff, sizeof(buff)); xxx = execlp(“./xxx”, “./xxx”, NULL); //run […]

ls | wc使用C不起作用

我写了一个C程序,它使用多个管道来模拟shell。 问题是我可以运行大多数命令,如ls | cat ls | cat等,但我无法使用ls | wc ls | wc 。 什么情况下wc不起作用? int pipefd[4]; int p1 = pipe(pipefd); // Open pipe 1 int p2 = pipe(pipefd + 2); // Open pipe 2 pid_t pid; for(i = 0; i < n_commands; i++) { fflush(stdout); pid = fork(); if(pid == 0) { int command_no = […]

管道损坏错误

我在FTP实现中的打开的数据套接字上使用write()来发送文件。 但在写完一些数据之后,它已经悬挂了一段时间; 然后它返回Broken管道错误。 任何帮助都将非常感谢。 我的进程从一个buff读取数据包并写入套接字。 我注意到带宽增加的问题。 如果我增加了要处理的数据包的数量,那么问题就出现了。 我正在使用FreeBSD。 我使用两个线程一个读取数据包并写入缓冲区…第二个线程从缓冲区读取这些数据包并写入套接字。 谢谢你的帮助亚历山大

C命名管道(fifo)。 父进程陷入困境

我想创建一个简单的程序,该fork,并且子进入命名管道,父进程从命名管道读取和显示。 问题是它进入了父进程,进行了第一次printf然后它变得奇怪,它没有做任何其他事情,没有进入第二个printf,它只是在控制台中输入的方式。 #include #include #include #include #include #include #include void main() { char t[100]; mkfifo(“myfifo”,777); pid_t pid; pid = fork(); if (pid==0) { //execl(“fifo2″,”fifo2”,(char*)0); char r[100]; printf(“scrie2->”); scanf(“%s”,r); int fp; fp = open(“myfifo”,O_WRONLY); write(fp,r,99); close(fp); printf(“exit kid \n”); exit(0); } else { wait(0); printf(“entered parent \n”); // <- this it prints // whats below this […]

无限管疯狂

我正在尝试创建一组infinte管道,以便从左边的流程遍历到正确的流程。 我正在使用fd保留前面的fd并将其输入到新进程。 任何人都可以看到我出错的地方。 在这一点上看起来应该很简单。 我记录得很好。 //Keep the previous out fd for the in of the subsequent process int prev_out_fd; for (x = 0; x < prog_count; ++x) { //Create a pipe for both processes to share int pipefd[2]; if (x != prog_count -1) { pipe(pipefd); } prog_defs[x].pid = fork(); if(prog_defs[x].pid == 0) { //If this […]

使用pipe():如何允许多个子进程同时执行

我正在使用pipe()按索引拆分文件,将该索引发送到子进程,让子进程计算其指定文件块中数字的总和,并将其总和返回给父进程。 我的孩子似乎按顺序执行,我希望他们同时执行,以使这个过程更有效率。 这是我正在使用的代码: #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int numchild; struct timeval stop, start; int i, j, len, ret, fpos=0, val, count=0, total=0, alltotal=0; pid_t pid; int nums = 1000; FILE * file; printf(“How many children to use: “); scanf(“%d”, &numchild); printf(“\nWill use %d child process(es).\n”, numchild); […]

我没有关闭管道的一端,有什么不对的?

我使用管道在父proc和子proc之间进行通信。 我读过的那本书说在父母的proc中,我必须关闭pipefd [1],但我没有这样做,没有其他事情发生,所以我的问题是“如果我不关闭pipefd [1],有什么不受控制的?” 最好的祝福! int pipefd[2]; if(pipe(pipefd) == -1) { perror(“pipe communication error”); exit(EXIT_FAILURE); } int fd = fork(); if(fd < 0) { perror("fork child process error"); exit(EXIT_FAILURE); } if(fd != 0)//run in parent proc { int a = -1; int i = 1; //close(pipefd[1]); ## here! ## while(i) { read(pipefd[0], &a, sizeof(a)); printf("%d\n", […]

将一个命令的输出作为输入管道输入另一个命令

我至少知道管道的基本知识。 但是,我不明白如何使用C管道在C中实现此任务。 我不知道如何将一个程序的输出作为另一个程序的输入等等。 例如: ls | wc | ./add 这里ls列出文件, wc给出列出的文件的计数,而./add添加wc给出的数字。 请帮忙! 编辑 :这是一项任务。 确切的问题陈述如下: “编写C程序以读取两个(或更多)可执行程序的名称,并将第一个程序的输出重定向到第二个程序的输入,将第二个程序的输出重定向到第三个程序的输入,依此类推……”

通过C中的管道向bc写一个术语

我遇到了这个C练习的问题。 任务是创建两个进程。 这两个管道连接两个管道,终止于孩子的stdin和stdout。 然后用bc替换子进程。 然后我应该从父进程到子进程(bc)写一个术语(例如1 + 2)。 管道正在做他们应该做的事情,但是,bc似乎不喜欢输入。 当我写入管道时,bc会响应以下多行: (standard_in) 1: illegal character: ^@ 到目前为止这是我的解决方案: #include #include #include #include int main(int argc, char *argv[]) { /*Create two pipes: One from parent to child (pipe_pc) and one from child to parent (pipe_cp). */ int pipe_pc[2], pipe_cp[2]; int cid; if (pipe(pipe_pc) == -1 || pipe(pipe_cp) == -1) […]