利用pipe实现兄弟进程之间的通信
来源:互联网 发布:qq mac版 编辑:程序博客网 时间:2024/05/20 11:49
利用pipe实现兄弟进程之间的通信
进程A创建2个子进程B,C。然后B C执行的程序利用pipe来通信:ls的输出通过管道到达另一个进程用wc统计单词数,字节数,行数。
execve系统调用用于执行指定的程序,其他的exec系列函数都是基于它。APUE-188
int execve(const char *filename, char *const argv[],char *const envp[]);
execve() executes the program pointed to by filename. filename must be either a binary executable, or a script starting with a line of the form:
#! interpreter [optional-arg]
argv is an array of argument strings passed to the new program. By convention, the first of these strings should contain the filename associated with the file being executed. envp is an array of strings, conventionally of the form key=value, which are passed as environment to the new program. Both argv and envp must be terminated by a NULL pointer.dup2复制现有的描述符,实现重定向,底层就是改变文件号和struct file的绑定。
int dup2(int oldfd, int newfd);
dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following:- If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.
- If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
After a successful return from one of these system calls, the old and new file descriptors may be used interchangeably. They refer to the same open file description and thus share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the descriptors, the offset is also changed for the other.
The two descriptors do not share file descriptor flags (the close-on-exec flag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is off.也就是APUE60所说,新描述符的close-on-exec标志总被dup函数清楚
ls命令下面俩选项要注意:
-1 list one file per line
-l use a long listing format
代码:
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/time.h>#include <sys/resource.h>#include <sys/wait.h>int main(){ int childB, childC; int pipefds[2]; char *args1[] = {"//usr/bin/wc", NULL}; char *args2[] = {"/bin/ls", "-l", NULL}; //=======Process A====== pipe(pipefds); if(!(childB = fork())){ //======== Process B ======== close(pipefds[1]);// close write pipe end // redirect stdin close(0); dup2(pipefds[0], 0); close(pipefds[0]); // execute the target execve("/usr/bin/wc", args1, NULL); printf("pid %d: I got here, sth is WRONG!\n", getpid()); } // Process A continue close(pipefds[0]); if(!(childC = fork())){ //======== Process C ======== //redirect stdout close(1); dup2(pipefds[1], 1); close(pipefds[1]); // execute the target execve("/bin/ls", args2, NULL); //ERROR: If run to here printf("pid %d: I got here, sth is WRONG!\n", getpid()); } close(pipefds[1]); wait4(childB, NULL, 0, NULL); printf("%s\n", "OVER!"); return 0;}
运行效果:
参考:
《Linux内核源码情景分析 P702》
- 利用pipe实现兄弟进程之间的通信
- 利用油槽实现进程之间的通信
- Pipe,利用匿名管道实现进程间通信
- Linux pipe函数 进程之间的管道通信
- 利用命名管道实现进程之间的通信 .........
- linux中利用有名管道实现进程之间的通信
- Linux进程间通信分类 以及 pipe的原理实现
- Linux进程间通信分类 以及 pipe的原理实现
- Linux进程间通信分类 以及 pipe的原理实现
- Linux进程间通信分类 以及 pipe的原理实现
- Linux进程间通信分类 以及 pipe的原理实现
- Linux进程间通信分类 以及 pipe的原理实现
- 通过共享内存,利用循环队列实现两个进程A,B之间的通信
- 利用Messenger实现同一进程Activity和Service之间的通信
- 进程之间的通信-邮槽实现
- 如何实现Linux进程之间的通信?
- LINUX 实现两个进程之间的通信
- linux 线程或进程之间通过管道通信(pipe)
- Java 注解的简单学习与应用 Annotation
- 如何用PHP通过拼接数据到显示在数据库图表上
- github
- [iOS] dom解析xml数据,拿到<>里面的值
- bootstrap 学习(1)
- 利用pipe实现兄弟进程之间的通信
- POJ 1995 Raising Modulo Numbers【快速幂】
- iOS学习笔记-Objective-C-继承和实例化、对象初始化
- POJ3273--Monthly Expense
- java反射机制功能之---检查类的结构
- acm p1753 小明 A+B
- Java- 构造方法的执行顺序
- HN OJ 13375 Flowery Trails (spfa的路径遍历)
- c的rand()函数,相同的种子,windows和android下产生的随机数列不一样