设计网站页面要注意什么公司展厅
news/
2025/9/22 20:28:19/
文章来源:
设计网站页面要注意什么,公司展厅,小学做试题网站,山西大同网站建设哪家好pipe函数 管道函数 man pipe
#include unistd.h
int pipe(int pipefd[2]);参数介绍#xff1a;pipefd读写文件描述符#xff0c;0-代表读#xff0c; 1-代表写父子进程实现pipe通信#xff0c;实现ps aux | grep bash 功能 经常出现的问题#xff1a; 父进程认为…pipe函数 管道函数 man pipe
#include unistd.h
int pipe(int pipefd[2]);参数介绍pipefd读写文件描述符0-代表读 1-代表写父子进程实现pipe通信实现ps aux | grep bash 功能 经常出现的问题 父进程认为写端存在就有可能还有人发送数据继续等待 所以尽量保持管道数据的流向保持一致所以先在子进程关闭读端 父进程关闭写端这样管道流向就是 子进程写父进程读。
#include stdio.h
#include string.h
#include sys/types.h
#include sys/wait.h
#include unistd.h
#include sys/stat.h
#include fcntl.hint main() {int fd[2];pipe(fd);pid_t pid fork();if(pid 0) {// sonclose(fd[0]);dup2(fd[1], STDOUT_FILENO);execlp(ps, ps, aux, NULL);} else if(pid0){// fatherclose(fd[1]);dup2(fd[0], STDIN_FILENO);execlp(grep, grep, bash, NULL);}return 0;
}pipe管道的行为
读管道 a.写端全部关闭 -- read读到0相当于读到文件末尾b.写端没有全部关闭b1.有数据 -- read读到数据b2.没有数据 -- read阻塞 fcntl函数可以更改为非阻塞
写管道 a. 读端全部关闭 产生一个信号SIGPIPEkill - 13程序异常终止b. 读端没有全部关闭b1. 管道已满 --write阻塞b2. 管道未满 --write正常写入pipe 管道的大小 通过ulimit -a
pipe size (512 bytes, -p) 8pipe 管道缺点 只能单向通信 双向通信需要建立两个管道 只有血缘关系的进程才能通信兄弟进程实现 ps aux | grep bash 功能
#include stdio.h
#include sys/types.h
#include sys/wait.h
#include unistd.h
int main() {int fd[2];pipe(fd);pid_t pid;int i0, n2;for(i0; in; i) {pid fork();if(pid 0) {break;}}if(i 0) {close(fd[0]);dup2(fd[1], STDOUT_FILENO);execlp(ps, ps, aux, NULL);} else if(i 1 ) {close(fd[1]);dup2(fd[0], STDIN_FILENO);execlp(grep, grep, bash, NULL);} else if(i 2) {close(fd[0]); close(fd[1]);wait(NULL);wait(NULL);}return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910270.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!