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

2019-07-13 05:35发布

前几天练习的,补上,看了书做了这些练习之后,对之前盲目只根据例子写的,嵌入式linux中的函数和程序有了新的认识。这些练习都是根据  Linux C程序设计(西安电子科技大学出版社)一书写的,比较基础,适合补一补linux的基础,无论是已经开始写linux下编程但没有一点理论的,或者是尚未动手打算入门的,个人感觉都是挺不错的一本书。#include #include #include #include #include #include #include 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! ",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 #include #include #include #include #include #include #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 #include #include #include #include #include #include #include #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 #include #include #include #include #include #include #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 #include #include #include #include #include #include #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