Linux C程序练习(3)进程通信之pipe、fifo、消息队列

22 篇文章 0 订阅

前几天练习的,补上,看了书做了这些练习之后,对之前盲目只根据例子写的,嵌入式linux中的函数和程序有了新的认识。

这些练习都是根据  Linux C程序设计(西安电子科技大学出版社)一书写的,比较基础,适合补一补linux的基础,无论是已经开始写linux下编程但没有一点理论的,或者是尚未动手打算入门的,个人感觉都是挺不错的一本书。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

typedef int fid_t;

int main(int argc,char **argv){
	
    fid_t fd[2];
    pid_t pd=0;
    char *buf=NULL;
    int len=0;
    
	buf=(char *)malloc(1024*sizeof(char));
	memset(buf,0,1024*sizeof(char));
	
	if(pipe(fd)<0){
		perror("pipe()");
		exit(1);
	}
	
	pd=fork();
	if(pd<0){
		perror("fork()");
		exit(1);
	}else if(pd==0){
		close(fd[0]);
		write(fd[1],"HelloParent!\n",14);
	}else{
		close(fd[1]);
		
		len=read(fd[0],buf,1024);
		if(len<0){
			perror("read()");
			exit(1);
		}else{
			printf("%s",buf);
		}
		wait();
	}
	exit(0);
}

pipe1.c 管道通信

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#define BUFSIZE 1024

int main(int argc,char **argv){
	
    int fd=0;
    char *buf=NULL;
    
    buf=(char *)malloc(1024*sizeof(char));
    memset(buf,0,BUFSIZE);
    
    if(access("/tmp/myfifo",F_OK)<0){
    	if(mkfifo("/tmp/myfifo",0666)<0){
    		perror("mkfifo()");
    		exit(1);
    	}
    }
    
    fd=open("/tmp/myfifo",O_RDONLY);
    if(fd<0){
    	perror("open()");
    	exit(1);
    }
    
    while(read(fd,buf,BUFSIZE)>0){
    	printf("read_fifo read: %s",buf);
    }
    
    free(buf);
    buf=NULL;
    close(fd);
	exit(0);
}

fiforead1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>

#define BUFSIZE 1024

int main(int argc,char **argv){
	int fd=0;
    int count=0;
    char *buf=NULL;
    int writeBytes=0;
    time_t tp;
    
    buf=(char *)malloc(BUFSIZE*sizeof(char));
    memset(buf,0,BUFSIZE);
    
    if(access("/tmp/myfifo",F_OK)<0){
    	if(mkfifo("/tmp/myfifo",0666)<0){
    		perror("mkfifo()");
    		exit(1);
    	}
    }
    
    fd=open("/tmp/myfifo",O_WRONLY);
    if(fd<0){
    	perror("open()");
    	exit(1);
    }
    
    for(count=0;count<10;count++){
    	time(&tp);
    	writeBytes=sprintf(buf,"write_fifo %d sends %s",getpid(),ctime(&tp));
    	printf("Send msg: %s",buf);
    	
    	if(write(fd,buf,writeBytes+1)<0){
    		perror("write()");
    		close(fd);
    		exit(1);
    	}
    	sleep(3);
    	memset(buf,0,BUFSIZE);
    }
    free(buf);
    buf=NULL;
    close(fd);
	exit(0);
}

fifowrite1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

#define MAX_TEXT 1024

typedef struct{
	long mytype;
	char msg_text[MAX_TEXT];
}Mymsg; 

int main(int argc,char **argv){
	int running=1;
	int msgid;
	Mymsg message;
	
	msgid=msgget((key_t)12345,0666|IPC_CREAT);
	if(msgid==-1){
		perror("msgget()");
		exit(1);
	}
	
	while(running){
		memset(message.msg_text,0,MAX_TEXT);
		if(msgrcv(msgid,(void *)&message,MAX_TEXT,0,0)==-1){
			perror("msgrcv()");
			exit(1);	
		}
		printf("receive message:%s",message.msg_text);
		if(strncmp(message.msg_text,"end",3)==0){
			running=0;
		}
	}
	if(msgctl(msgid,IPC_RMID,0)==-1){
		perror("msgctl()");
		exit(1);
	}
	exit(0);
}

messageQueueread1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

#define MAX_TEXT 1024

typedef struct{
	long mytype;
	char msg_text[MAX_TEXT];
}Mymsg; 

int main(int argc,char **argv){
	int running=1;
	int msgid;
	Mymsg message;
	
	msgid=msgget((key_t)12345,0666|IPC_CREAT);
	if(msgid==-1){
		perror("msgget()");
		exit(1);
	}
	
	while(running){
		printf("Enter the message to send:");
		
		memset(message.msg_text,0,MAX_TEXT);
		fgets(message.msg_text,MAX_TEXT,stdin);
		
		if(msgsnd(msgid,(void *)&message,MAX_TEXT,0)==-1){
			perror("msgsnd()");
			exit(1);	
		}
		printf("receive message:%s",message.msg_text);
		if(strncmp(message.msg_text,"end",3)==0){
			running=0;
		}
	}
	exit(0);
}
messageQueuewrite1.c
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值