2.设计内容
把下面的几个网址的内容看懂,关于Linux的通信机制就会有个基本的了解了,后面的这几个代码也应该可以看得懂了。
- 管道通信:
- 消息队列通信:
共享内存通信:
2)实现一个管道通信程序。话不多说,直接贴代码。
#include#include #include #include #include #include #include #include #include #include #include #include #include #include #define BUFFER_SIZE 8192int pipe_used_size(int pipe[2],sem_t *pipe_mutex){ int status=0; int count=0; char buffer[1]; sem_wait(pipe_mutex); while(status>=0){ status=read(pipe[0], buffer,1); if(status>=0){count=count+1;} } for(int i=0;i =0){ status=write(pipe[1],buffer,1); if(status>=0){count=count+1;} } for(int i=0;i =0){ status = write(pipe_fd[1], buffer, 128);//怎么那个管道的最大是多大?为啥出来是653....,这个是查看默认的管道的空间大小是多少。 if(status>=1){ pipe_size += 128; } } printf("pipe size:%d\n", pipe_size); //pipe_used_size(pipe_fd,pipe_mutex); status=0; while(status>=0){ status = read(pipe_fd[0], buffer, 128); } printf("EAGAIN %d\n", EAGAIN); /*flags = fcntl(pipe_fd[1], F_GETFL); fcntl(pipe_fd[1], F_SETFL, flags ^ O_NONBLOCK); flags = fcntl(pipe_fd[0], F_GETFL); fcntl(pipe_fd[0], F_SETFL, flags ^ O_NONBLOCK);*/ status = 0; if((pid1 = fork()) == 0){ while(status>=0){ sem_wait(pipe_mutex); printf("child1 start writing\n"); status=write(pipe_fd[1], buffer, 128); printf("child1 write %d\n", status); //sleep(1); sem_post(pipe_mutex); } exit(0); }else if((pid2 = fork()) == 0){ while(status>=0){ sem_wait(pipe_mutex); printf("child2 start writing\n"); status=write(pipe_fd[1], buffer, 2047); printf("child2 write %d\n", status); //sleep(1); sem_post(pipe_mutex); } exit(0); }else if((pid3 = fork()) == 0){ while(status>=0){ sem_wait(pipe_mutex); printf("child3 start writing\n"); status=write(pipe_fd[1], buffer, 4200); printf("child3 write %d\n", status); //sleep(1); sem_post(pipe_mutex); } exit(0); }else{ getchar();//这个语句在这里的作用是什么? sem_post(pipe_mutex); wait(0); wait(0); wait(0); pipe_left_size(pipe_fd,pipe_mutex);//这两个函数的作用又是什么? sem_unlink("pipe_mutex"); } return 0;}
3)利用Linux的消息队列通信机制实现线程的通信,也是直接上代码。
#include#include #include #include #include #include #include #include #include #include #include #define MSG_MAX 100struct my_msgbuf{ long int mtype; char mtext[MSG_MAX];};void *sender1(){ int n; struct my_msgbuf message; char buf[MSG_MAX]; sem_t *mutex = sem_open("mutex", O_CREAT | O_RDWR, 0666, 0); sem_t *sender1_over = sem_open("sender1_over", O_CREAT | O_RDWR, 0666, 0); sem_t *receive1_over = sem_open("receive1_over", O_CREAT | O_RDWR, 0666, 0); int msqid = msgget((key_t)8088, 0666 | IPC_CREAT); if( msqid == -1){ printf("create failed"); exit(-1); } while(1){ sem_wait(mutex); printf("sender1 wirte :"); scanf("%s", &buf); printf("\n"); message.mtype = 1; if(strcmp(buf,"exit") == 0){ strcpy(message.mtext,"end1"); n = msgsnd(msqid, (void *)&message, 100, 0);//还有关于这个接收消息的类型? sem_wait(receive1_over); n = msgrcv(msqid, (void *)&message, 100, 2, 0); printf("%s\n", message.mtext); sem_post(sender1_over); sem_post(mutex); sleep(1); return 0; } else{ strcpy(message.mtext,buf); n = msgsnd(msqid, (void *)&message, 100, 0); sem_post(mutex); sleep(1); } }}void *sender2(){ int n; struct my_msgbuf message; char buf[MSG_MAX]; sem_t *mutex = sem_open("mutex", O_CREAT | O_RDWR, 0666, 0); sem_t *sender2_over = sem_open("sender2_over", O_CREAT | O_RDWR, 0666, 0); sem_t *receive2_over = sem_open("receive2_over", O_CREAT | O_RDWR, 0666, 0); int msqid = msgget((key_t)8088, 0666 | IPC_CREAT); if( msqid == -1){ printf("create failed"); exit(-1); } while(1){ sem_wait(mutex); printf("sender2 wirte :"); scanf("%s", &buf); printf("\n"); message.mtype = 1; if(strcmp(buf,"exit") == 0){ strcpy(message.mtext,"end2"); n = msgsnd(msqid, (void *)&message, 100, 0); sem_wait(receive2_over); n = msgrcv(msqid, (void *)&message, 100, 3, 0); printf("%s\n", message.mtext); sem_post(sender2_over); sem_post(mutex); sleep(1); return 0; } else{ strcpy(message.mtext,buf); n = msgsnd(msqid, (void *)&message, 100, 0); sem_post(mutex); sleep(1); } }}void *receive(){ int n; int over1=0; int over2=0; struct my_msgbuf message; char buf[MSG_MAX]; sem_t *sender1_over = sem_open("sender1_over", O_CREAT | O_RDWR, 0666, 0); sem_t *receive1_over = sem_open("receive1_over", O_CREAT | O_RDWR, 0666, 0); sem_t *sender2_over = sem_open("sender2_over", O_CREAT | O_RDWR, 0644, 0); sem_t *receive2_over = sem_open("receive2_over", O_CREAT | O_RDWR, 0666, 0); int msqid = msgget((key_t)8088, 0666 | IPC_CREAT); if( msqid == -1){ printf("create failed"); exit(-1); } while(1){ n = msgrcv(msqid, (void *)&message, 100, 0, 0); if(n > 0){ printf("\n receive %s\n", message.mtext); if( strcmp(message.mtext,"end1") == 0 ){ message.mtype = 2; strcpy(message.mtext,"over1"); n = msgsnd(msqid, (void *)&message, 100, 0); if( n == 0 ){ sem_post(receive1_over); sem_wait(sender1_over); } over1 = 1; }else if( strcmp(message.mtext,"end2") == 0 ){ message.mtype = 3; strcpy(message.mtext,"over2"); n = msgsnd(msqid, (void *)&message, 100, 0); if( n == 0 ){ sem_post(receive2_over); sem_wait(sender2_over); } over2 = 1; } } if(over1==1 && over2==1){ msgctl(msqid, IPC_RMID, 0); exit(0); } sleep(1); }}int main(){ int msqid = msgget((key_t)8088, 0666 | IPC_CREAT); msgctl(msqid, IPC_RMID, 0); sem_unlink("mutex"); sem_unlink("sender1_over"); sem_unlink("sender2_over"); sem_unlink("receive1_over"); sem_unlink("receive2_over"); sem_t *mutex = sem_open("mutex", O_CREAT | O_RDWR, 0666, 0); sem_t *sender1_over = sem_open("sender1_over", O_CREAT | O_RDWR, 0666, 0); sem_t *receive1_over = sem_open("receive1_over", O_CREAT | O_RDWR, 0666, 0); sem_t *sender2_over = sem_open("sender2_over", O_CREAT | O_RDWR, 0666, 0); sem_t *receive2_over = sem_open("receive2_over", O_CREAT | O_RDWR, 0666, 0); pthread_t pt1,pt2,pt3; pthread_create(&pt1, NULL, sender1, NULL); pthread_create(&pt2, NULL, sender2, NULL); pthread_create(&pt3, NULL, receive, NULL); sem_post(mutex); pthread_join(pt1, NULL); pthread_join(pt2, NULL); pthread_join(pt3, NULL); return 0;}
4)利用Linux的共享内存通信机制实现两个进程间的通信。
//为消息接收程序#include#include #include #include #include #include #include #include #include #include #include int main(){ char buf[50]; int shmid = shmget((key_t)8888, 50, IPC_CREAT | 0666); sem_t *mutex1 = sem_open("mutex1", O_CREAT | O_RDWR, 0666, 0); sem_t *mutex2 = sem_open("mutex2", O_CREAT | O_RDWR, 0666, 0); sem_wait(mutex1); char *m =(char *)shmat(shmid, NULL, 0); printf("receive: %s \n",m); strcpy(buf, "over"); memcpy(m,buf,sizeof(buf)); shmdt(shmid); sem_post(mutex2); return 0;}
//为消息发送程序#include#include #include #include #include #include #include #include #include #include #include int main(){ char buf[50]; int shmid = shmget((key_t)8888, 50, IPC_CREAT | 0666); shmctl((key_t)8888, IPC_RMID, NULL); sem_unlink("mutex1"); sem_unlink("mutex2"); sem_t *mutex1 = sem_open("mutex1", O_CREAT | O_RDWR, 0666, 0); sem_t *mutex2 = sem_open("mutex2", O_CREAT | O_RDWR, 0666, 0); shmid = shmget((key_t)8888, 50, IPC_CREAT | 0666); if( shmid == -1){ printf("create failed"); exit(-1); } char *m = (char *)shmat(shmid, NULL, 0); printf("sender writing:"); scanf("%s", buf); memcpy(m,buf,sizeof(buf)); sem_post(mutex1); sem_wait(mutex2); printf("\n%s\n/", m); shmdt(shmid); shmctl((key_t)8888, IPC_RMID, NULL); sem_unlink("mutex1"); sem_unlink("mutex2"); return 0;}