進(jìn)程間通信之:管道
4.管道使用實(shí)例
在本例中,首先創(chuàng )建管道,之后父進(jìn)程使用fork()函數創(chuàng )建子進(jìn)程,之后通過(guò)關(guān)閉父進(jì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í),Linux將不保證寫(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)文章:通信原理
評論