欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

進程通信--管道聽課筆記

系統 1771 0
進程間通信(IPC)作用
1.數據傳輸
2.共享資源
3.通知事件
4.進程控制

IPC的方式 6種
1.管道(pipe)和有名管道(FIFO)
2.信號(signal)
3.消息隊列
4.共享內存
5.信號量
6.套接字

管道
一進程寫入管道尾部,另一進程從管道頭讀出
讀空管道,進程被阻塞
寫滿管道,進程被阻塞

進程通信--管道聽課筆記

無名管道 多用于父子進程間的通信
    
int pipe(int filedis[2])//創建無名管道

  

filedis文件描述符
filedis[0]用于讀管道
filedis[1]用于寫管道

    
//管道的創建和關閉
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
    int pipe)fd[2];
    //創建pipe
    if(pipe(pipe_fd)<0){
        printf("pipe create error\n");
        return -1;
    }else{
        printf("pipe create success\n"):
    }
    //關閉pipe
    close(pipe_fd[0]);
    colse(pipe_fd[1]);
}

  


通常 先創建一個管道, 再通過fork創建一個子進程 ,子進程會繼承父進程所創建的管道

進程通信--管道聽課筆記

有名管道
    
#include <sys/types.h>
#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode)

  

pathname:FIFO的路徑

讀寫管道時: 非阻塞標志O_NONBLOCK,非阻塞時,出錯立即返回,errno是ENXIO

刪除管道
    
unlink(const char *pathname)

  


讀有名管道
    
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"

main(int argc,char** argv)
{
	char buf_r[100];
	int  fd;
	int  nread;
	
	/* 創建管道 */
	if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
		printf("cannot create fifoserver\n");
	
	printf("Preparing for reading bytes...\n");
	
	memset(buf_r,0,sizeof(buf_r));
	
	/* 打開管道 */
	fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
	if(fd==-1)
	{
		perror("open");
		exit(1);	
	}
	while(1)
	{
		memset(buf_r,0,sizeof(buf_r));
		
		if((nread=read(fd,buf_r,100))==-1)
		{
			if(errno==EAGAIN)
				printf("no data yet\n");
		}
		printf("read %s from FIFO\n",buf_r);
		sleep(1);
	}	
	pause(); /*暫停,等待信號*/
	unlink(FIFO); //刪除文件
}

  


寫有名管道
    
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_SERVER "/tmp/myfifo"

main(int argc,char** argv)
{
	int fd;
	char w_buf[100];
	int nwrite;
		
	/*打開管道*/
	fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
	
	if(argc==1)
	{
		printf("Please send something\n");
		exit(-1);
	}
	
	strcpy(w_buf,argv[1]);
	
	/* 向管道寫入數據 */
	if((nwrite=write(fd,w_buf,100))==-1)
	{
		if(errno==EAGAIN)
			printf("The FIFO has not been read yet.Please try later\n");
	}
	else 
		printf("write %s to the FIFO\n",w_buf);
}

  

進程通信--管道聽課筆記


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲国产系列 | 精品一区二区三区在线观看视频 | 不卡国产一区二区三区四区 | 在线欧美日韩 | 欧美一级毛片免费播放器 | 国产精品久久久久影院色老大 | 久久精品国产免费中文 | 好吊妞gao988在线播放 | 四虎影视免费观看免费观看 | 国产精品久久久久久久久久红粉 | 日韩成人免费电影 | 五月深爱婷婷 | 欧美日韩中文在线观看 | 久久女人被添全过程A片 | 国产chinese视频在线观看 | 天堂资源av | 99久久精品国产导航 | 中文一区二区 | 国产在线一区二区三区 | 精品日韩在线观看 | 久久不卡一区二区三区 | 久久久久久国产精品 | 五月激情六月婷婷 | 亚洲成人一区 | 亚洲天堂网站 | 欧美成人免费高清网站 | 夜夜爽天天狠狠九月婷婷 | 四虎国产视频 | 午夜看片网 | 午夜影院福利社 | 91国色 | 国产福利在线免费 | 欧美a一级大片 | 久久久欧美综合久久久久 | 欧美黄色一区 | 欧美高清不卡午夜精品免费视频 | 久草在线草a免费线看 | 亚洲一区在线免费 | 奇米影视亚洲精品一区 | 欧美中文字幕 | 精品日韩视频 |