<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > 進(jìn)程間通信之:管道

進(jìn)程間通信之:管道

作者: 時(shí)間:2013-09-13 來(lái)源:網(wǎng)絡(luò ) 收藏

本文引用地址:http://dyxdggzs.com/article/257127.htm

4.使用實(shí)例

在本例中,首先創(chuàng )建,之后父使用fork()函數創(chuàng )建子,之后通過(guò)關(guān)閉父的讀描述符和子進(jìn)程的寫(xiě)描述符,建立起它們之間的通信。

/*pipe.c*/

#includeunistd.h>

#includesys/types.h>

#includeerrno.h>

#includestdio.h>

#includestdlib.h>

#defineMAX_DATA_LEN256

#defineDELAY_TIME1

intmain()

{

pid_tpid;

intpipe_fd[2];

charbuf[MAX_DATA_LEN];

constchardata[]=PipeTestProgram;

intreal_read,real_write;

memset((void*)buf,0,sizeof(buf));

/*創(chuàng )建管道*/

if(pipe(pipe_fd)0)

{

printf(pipecreateerrorn);

exit(1);

}

/*創(chuàng )建一子進(jìn)程*/

if((pid=fork())==0)

{

/*子進(jìn)程關(guān)閉寫(xiě)描述符,并通過(guò)使子進(jìn)程暫停1s等待父進(jìn)程已關(guān)閉相應的讀描述符*/

close(pipe_fd[1]);

sleep(DELAY_TIME*3);

/*子進(jìn)程讀取管道內容*/

if((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0)

{

printf(%dbytesreadfromthepipeis'%s'n,real_read,buf);

}

/*關(guān)閉子進(jìn)程讀描述符*/

close(pipe_fd[0]);

exit(0);

}

elseif(pid>0)

{

/*父進(jìn)程關(guān)閉讀描述符,并通過(guò)使父進(jìn)程暫停1s等待子進(jìn)程已關(guān)閉相應的寫(xiě)描述符*/

close(pipe_fd[0]);

sleep(DELAY_TIME);

if((real_write=write(pipe_fd[1],data,strlen(data)))!=-1)

{

printf(Parentwrote%dbytes:'%s'n,real_write,data);

}

/*關(guān)閉父進(jìn)程寫(xiě)描述符*/

close(pipe_fd[1]);

/*收集子進(jìn)程退出信息*/

waitpid(pid,NULL,0);

exit(0);

}

}

將該程序交叉編譯,下載到開(kāi)發(fā)板上的運行結果如下所示:

$./pipe

Parentwrote17bytes:'PipeTestProgram'

17bytesreadfromthepipeis'PipeTestProgram'

5.管道讀寫(xiě)注意點(diǎn)

n 只有在管道的讀端存在時(shí),向管道寫(xiě)入數據才有意義。否則,向管道寫(xiě)入數據的進(jìn)程將收到內核傳來(lái)的SIGPIPE信號(通常為Brokenpipe錯誤)。

n 向管道寫(xiě)入數據時(shí),將不保證寫(xiě)入的原子性,管道緩沖區一有空閑區域,寫(xiě)進(jìn)程就會(huì )試圖向管道寫(xiě)入數據。如果讀進(jìn)程不讀取管道緩沖區中的數據,那么寫(xiě)操作將會(huì )一直阻塞。

n 父子進(jìn)程在運行時(shí),它們的先后次序并不能保證,因此,在這里為了保證父子進(jìn)程已經(jīng)關(guān)閉了相應的文件描述符,可在兩個(gè)進(jìn)程中調用sleep()函數,當然這種調用不是很好的解決方法,在后面學(xué)到進(jìn)程之間的同步與互斥機制之后,請讀者自行修改本小節的實(shí)例程序。

linux操作系統文章專(zhuān)題:linux操作系統詳解(linux不再難懂)

linux相關(guān)文章:linux教程


數字通信相關(guān)文章:數字通信原理


通信相關(guān)文章:通信原理




評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>