如何在c Linux中创建4处理和管道消息? [英] how can i create 4 Process and Piping a message in c Linux?
本文介绍了如何在c Linux中创建4处理和管道消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C,Linux的开头...请问我怎样才能在c Linux中创建4处理和管道消息?
process1通过pipe1将结果发送到process2 ...进程2通过pipe2将结果发送到process3然后process3通过pipe3将结果提供给process4并且process4保存结果到文件..
等待你的回答...
非常感谢
-lida
Hi,
i m in the start of C, Linux ... may u help me please how can i create 4 Process and Piping a message in c Linux?
process1 give the result by pipe1 to process2 ... process 2 by pipe2 send the result to process3 then process3 give the result to process4 by pipe3 and process4 save the result into a file..
wait for ur kind answers...
many thanks
-lida
推荐答案
请参阅:
http ://linux.die.net/man/2/pipe [ ^ ],
http://forum.codecall.net/topic/60502-using-pipe-in-c-over-linux/#axzz2Ds7vJI7h [ ^ ]。
-SA
Please see:
http://linux.die.net/man/2/pipe[^],
http://forum.codecall.net/topic/60502-using-pipe-in-c-over-linux/#axzz2Ds7vJI7h[^].
—SA
请参阅以下链接
http://home.agh.edu.pl/~wojnicki/thesis/node52.html
仅用于3个进程: -
Its for only 3 processes :-
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MSGSIZE 14
char *message = "hello, world!";
main() {
char inbuf[MSGSIZE];
int p[3];
pid_t pid;
pid_t pid1;
if (pipe(p) == -1){
perror("pipe call");
exit(1);
}
pid = fork();
pid1 = fork();
if(pid == -1){
perror("Fork failed");
exit(1);
}
if(pid | pid1 == 0)//process A
{
close(p[0]);
write(p[1], message, MSGSIZE);
read(p[1], message, MSGSIZE);
write(p[2], message, MSGSIZE);
}
/*else if(pid1 == 0){
close(p[1]);
//read(p[1], message, MSGSIZE);
write(p[2], message, MSGSIZE);
}*/
else{
//parent process C
close(p[2]);
read(p[0], inbuf, MSGSIZE);
printf("Pipelined message return:%s\n", inbuf);
wait(NULL);
}
exit(0);
}
这篇关于如何在c Linux中创建4处理和管道消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文