国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
有名管道
2、  有名管道
有名管道可用于兩個無關(guān)的進(jìn)程之間的通信。它的實(shí)現(xiàn)函數(shù)是:
int mkfifo(const char *filename, mode_t mode)
//創(chuàng)建一個名為filename的管道,模式可選為讀或?qū)懛绞?,阻塞或非阻塞方式等?br>下面一個實(shí)例演示了mkfifo的使用。Fifo_read.c不斷從管道文件里讀數(shù)據(jù),fifo_write.c往管道文件里寫數(shù)據(jù)。改變sleep的值也會產(chǎn)生類似上面進(jìn)程同步的問題,而會發(fā)現(xiàn)一些緩沖區(qū)的特性。
兩個程序用gcc編譯后在兩個終端里運(yùn)行。
//---------------------------------------------------------------------------------------------------
//fifo_read.c
//創(chuàng)建有名管道,演示兩個不相關(guān)的進(jìn)程之間的通信
//int mkfifo(const char *filename, mode_t mode)
#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO "/home/huang/myfifo"
int main(int argc,char **argv)
{
       int fd;
       int nread;
       char buf_r[100];
       if(mkfifo("/home/huang/myfifo",O_CREAT|O_EXCL)
       {
              perror("mkfifo:");
              printf("cann't create fifoserver\n");
              return -1;
       }
       printf("Preparing for reading bytes...\n");
       fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
       if(fd==-1)
       {
              perror("open!\n");
              exit(1);
       }
       while(1)
       {
              memset(buf_r,0,sizeof(buf_r));
              if((nread=read(fd,buf_r,sizeof(buf_r)))==-1)
              {
                     if(errno==EAGAIN)
                            printf("no data yet\n");
              }
              printf("read %s from FIFO\n",buf_r);
              sleep(1);
       }
       pause();
       unlink(FIFO);
}
//------------------------------------------------------------------------------------------------------------------
//fifo_write.c
//創(chuàng)建有名管道,演示兩個不相關(guān)的進(jìn)程之間的通信
//int mkfifo(const char *filename, mode_t mode)
#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO "/home/huang/myfifo"
int main(int argc, char **argv)
{
       int fd;
       char w_buf[100];
       int nwrite;
       if(argc==1)
       {
              printf("please send some message\n");
              exit(1);   
       }
       fd=open(FIFO,O_WRONLY|O_NONBLOCK,0);
       if(fd==-1)
       {
              if(errno==ENXIO)
                     printf("open error;no reading process\n");
              perror("open:");
              return -1;
       }
       memset(w_buf,'a',sizeof(w_buf));
       printf("sizeof(w_buf)=%d\n",sizeof(w_buf));
       while(1)
       {
              if((nwrite=write(fd,w_buf,strlen(w_buf)))==-1)
              {
                     if(errno==EAGAIN)
                            printf("The FIFO has not been write yet.\n");
                     perror("write");
              //     else
                     //     printf("error in writting!\n");
              }
              else
                     printf("write %s to the FIFO\n",w_buf);
              sleep(2);
       }
       close(fd);
}

 
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Linux的進(jìn)程通訊之命名管道
有名管道和無名管道
C語言進(jìn)程間通信(二)
管道通信
多進(jìn)程編程通信——管道
進(jìn)程間通信之管道(pipe、fifo)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服