栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Linux命名管道实现进程通信C语言

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux命名管道实现进程通信C语言

进程通信
  • fifo_write.c
  • fifo_read.c

fifo_write.c
#include 
#include 

#include //the two head files are concerned with int mkfifo(const char *pathname, mode_t mode).
#include 

#include 
#include 
#include 
#include//use time(time_t *ptr) to get different time from 1970.1.1.00:00 to make srand(unsigned int seed) function produce different seed.a
#define LEN2  30
int length_total=0;//additional l or L represent it's a long integer.

void perform(int sig);
void set_data(char *ptrbuf,int len);

int main(){
    
    remove("fifo1");//unlink("fifo") function deletes a fifo pipe
    unlink("fifo2");
    int ret1 = mkfifo("fifo1",0644),
        ret2 = mkfifo("fifo2",0644);
         
    
	if((ret1 < 0) || ( ret2 < 0))
    {
        perror("make fifo error");
        exit(1);
    }
    
	int fd1 = open("fifo1",O_WRONLY),//open the pipes
	    fd2 = open("fifo2",O_RDONLY);
    if((fd1 < 0) || (fd2 < 0))
    {
        perror("open fifo error");
        exit(1);
    }
    
    char buf1[BUFSIZ];//BUFSIZ=8192 Bytes
    int timer=0;

	signal(SIGINT,perform);
	for(;;)
	{
	    time_t t;
	    char buf_time[20];
	    time(&t);
	    ctime_r(&t,buf_time);//get the time difference from 1970
	    
		srand((unsigned int)time(NULL));//different seeds make different random sequence.
      
	    int timer,len=rand()%(BUFSIZ/sizeof(char)/10-10)+1;//BUFSIZ/sizeof(char)-10
	    
	    set_data(buf1,len);
	    printf("seq=%dnlength=%dbytesntime=%sdata:%sn",++timer,(int)(len*sizeof(char)),buf_time,buf1);
        write(fd1,buf1,len);//write into pipe
        length_total+=len;
		sleep(2);
        char buf2[LEN2]={0};
        int rd2=read(fd2,buf2,LEN2);
        if(rd2 > 0)
        {
            printf("I'm receiving the result......n");
            sleep(2);
            printf("%snreceive success!nnn",buf2);
        }
        sleep(1);
	}
    return 0;
}

void set_data(char *ptrbuf,int len)
{
	int i,val=0;
	for(i=0;i
		if(0==rand()%2)
		{
			ptrbuf[i]='0';
			val^=0;
		}
		else
		{
			ptrbuf[i]='1';
			val^=1;
		}
	}
	ptrbuf[i++]=(char)(48+val);
	ptrbuf[i]='';
}

void perform(int sig)
{
	printf("Send %d bytes data in total!n",length_total);
	signal(sig,SIG_DFL);
	kill(getpid(),SIGINT);
}
fifo_read.c
#include 
#include 
#include //the two head files are concerned with int mkfifo(const char *pathname, mode_t mode).
#include 
#include 
#include 
#include 
#include

#define LEN2  30

void feedback(int flag);

int main(){
    int fd1 = open("fifo1",O_RDONLY),
        fd2 = open("fifo2",O_WRONLY);
 
    if ((fd1 < 0) || (fd2 < 0))
    {
        perror("open fifo error");
        exit(1);
    }
    
    int timer=0;
    while(1)
    {
        time_t t;
        char buf_time[20];
	    time(&t);
	    ctime_r(&t,buf_time);//get the time difference from 1970
	    
        char buf1[BUFSIZ]={0};
        int ret1 = read(fd1,buf1,sizeof(buf1)),
            sign=0,
            len=strlen(buf1);
        
        if(ret1>0){
            printf("seq=%dnlength=%dbytesntime=%sdata:%sn",++timer,(int)(len*sizeof(char)),buf_time,buf1);
            int i;
            for(i=0;isign^=(buf1[i]-48);}
            if(sign+48==buf1[i])
            {
                printf("Check right!nThe check result is sending......"); 
                char buf2[LEN2]="Check right!";
                write(fd2,buf2,LEN2);
            }
            else
            {
                printf("Check wrong!nThe check result is sending......");        
                char buf2[LEN2]="Check wrong!";
                write(fd2,buf2,LEN2);
            }
            sleep(1);
            printf("nsend success!nnn");
        }
    }
	return 0;    
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1034559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号