网站地图 模板,域名备案有什么用,海口建站,东莞黄页大全#include unistd.h int pipe(int pipefd[2]); 功能#xff1a;创建一个匿名管道#xff0c;用于进程间通信 参数#xff1a; -int pipefd[2]:传出参数 pipefd[0]对应的是管道的读端 pipefd[0]对应的是管道的写端 返回值#xff1a; 成功返回0#xff0c;失败返回-… #include unistd.h int pipe(int pipefd[2]); 功能创建一个匿名管道用于进程间通信 参数 -int pipefd[2]:传出参数 pipefd[0]对应的是管道的读端 pipefd[0]对应的是管道的写端 返回值 成功返回0失败返回-1
管道默认是阻塞的如果管道中没有数据read阻塞如果管道满了write阻塞 注意匿名管道只能用于具有关系的进程之间的通信父子进程、兄弟进程
1.子进程写数据父进程读数据并输出
//子进程发送数据给父进程父进程读取到数据输出
#include unistd.h
#includesys/types.h
#includestdlib.h
#includestdio.h
#includestring.h
int main() {//在fork之前创建管道int pipefd[2];int ret pipe(pipefd);if(ret -1) {perror(pipe);exit(0);}//创建子进程pid_t pid fork();if(pid 0) {//父进程//从管道读取端读取数据char buf[1024] {0};int len read(pipefd[0], buf, sizeof(buf));printf(parent recv : %s, pid: %d\n, buf, getpid());} else if(pid 0){//子进程//在管道中写入数据char * str hello, i am child;write(pipefd[1], str, strlen(str));}return 0;
} 2.子进程持续写数据父进程持续接收
//子进程发送数据给父进程父进程读取到数据输出
#include unistd.h
#includesys/types.h
#includestdlib.h
#includestdio.h
#includestring.h
int main() {//在fork之前创建管道int pipefd[2];int ret pipe(pipefd);if(ret -1) {perror(pipe);exit(0);}//创建子进程pid_t pid fork();if(pid 0) {//父进程//从管道读取端读取数据printf(i am parent process, pid: %d\n, getpid());char buf[1024] {0};while(1) {int len read(pipefd[0], buf, sizeof(buf));printf(parent recv : %s, pid: %d\n, buf, getpid());}} else if(pid 0){//子进程printf(i am child process, pid: %d\n, getpid());while(1) {//在管道中写入数据char * str hello, i am child;write(pipefd[1], str, strlen(str));sleep(1);}}return 0;
}
3.父子进程都能读和写数据
//子进程发送数据给父进程父进程读取到数据输出
#include unistd.h
#includesys/types.h
#includestdlib.h
#includestdio.h
#includestring.h
int main() {//在fork之前创建管道int pipefd[2];int ret pipe(pipefd);if(ret -1) {perror(pipe);exit(0);}//创建子进程pid_t pid fork();if(pid 0) {//父进程//从管道读取端读取数据printf(i am parent process, pid: %d\n, getpid());char buf[1024] {0};while(1) {//在管道中读取数据int len read(pipefd[0], buf, sizeof(buf));printf(parent recv : %s, pid: %d\n, buf, getpid());//在管道中写入数据char * str hello, i am parent;write(pipefd[1], str, strlen(str));sleep(1);}} else if(pid 0){//子进程printf(i am child process, pid: %d\n, getpid());char buf[1024] {0};while(1) {//在管道中写入数据char * str hello, i am child;write(pipefd[1], str, strlen(str));sleep(1);//在管道中读取数据int len read(pipefd[0], buf, sizeof(buf));printf(child recv : %s, pid: %d\n, buf, getpid());}}return 0;
}
4.查看管道缓冲大小 #include unistd.h
#includesys/types.h
#includestdlib.h
#includestdio.h
#includestring.hint main() {int pipefd[2];int ret pipe(pipefd);//获取管道大小long size fpathconf(pipefd[0], _PC_PIPE_BUF);printf(pipe size: %ld\n, size);return 0;
}
文章转载自: http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.cmzcp.cn.gov.cn.cmzcp.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.srjbs.cn.gov.cn.srjbs.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.rqbr.cn.gov.cn.rqbr.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.kgkph.cn.gov.cn.kgkph.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.jlrym.cn.gov.cn.jlrym.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.mxnhq.cn.gov.cn.mxnhq.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.rnngz.cn.gov.cn.rnngz.cn http://www.morning.ssjry.cn.gov.cn.ssjry.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.hwbf.cn.gov.cn.hwbf.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn