C语言实现消息队列的通讯
double_link.h文件
1 #include<stdio.h> 2 #include<sys/types.h> 3 #include<sys/ipc.h> 4 #include<sys/msg.h> 5 #include<stdio.h> 6 #include<stdlib.h> 7 struct DATA 8 { 9 int id; 10 char name[20]; 11 }; 12 typedef struct student 13 { 14 struct DATA data; 15 struct student *prior; 16 struct student *next; 17 }STU,*Pstu; 18 struct msgbuf 19 { 20 long type; 21 struct DATA buf; 22 }; 23 Pstu create_link(Pstu); 24 void show_link(Pstu); 25 void send_link(int,Pstu); 26 Pstu recv_link(int);
double_link.c文件
#include"double_link.h" Pstu create_link(Pstu head) { Pstu temp=(Pstu)malloc(sizeof(STU)); printf("please input id:"); scanf("%d",&temp->data.id); printf("please input name:"); scanf("%s",temp->data.name); if(head==NULL) { temp->next=NULL; temp->prior=NULL; } else { temp->next=head; temp->prior=NULL; head->prior=temp; } return temp; } void show_link(Pstu head) { printf("id\tname\n"); while(head!=NULL) { printf("%d\t%s\n",head->data.id,head->data.name); head=head->next; } } void send_link(int fd,Pstu head) { struct msgbuf mb; while(head!=NULL) { mb.type=1; mb.buf=head->data; msgsnd(fd,&mb,sizeof(struct DATA),0); head=head->next; } } Pstu recv_link(int fd) { Pstu head=NULL; struct msgbuf mb; while(msgrcv(fd,&mb,sizeof(struct DATA),1,IPC_NOWAIT)>0) { Pstu temp=(Pstu)malloc(sizeof(STU)); if(head!=NULL) { head->prior=temp; } temp->next=head; temp->prior=NULL; head=temp; temp->data=mb.buf; } return head; }
haxi.h文件
#include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #include<stdio.h> #include<stdlib.h> typedef struct teacher { int id; char name[20]; struct teacher *next; }tea,*Ptea; struct msgbuf1 { long type; struct teacher buf; }; int haxi(int id); void create_haxi(int id,char *name,Ptea array[]); void show_haxi(Ptea array[]); void send_haxi(int msgid,Ptea array[]); void recv_haxi(int msgid,Ptea array[]);
haxi.c文件
1 #include<string.h> 2 #include"haxi.h" 3 4 int haxi(int id) 5 { 6 return id%5; 7 } 8 void create_haxi(int id,char *name,Ptea array[]) 9 { 10 int key=haxi(id); 11 Ptea temp=(Ptea)malloc(sizeof(tea)); 12 temp->id=id; 13 strcpy(temp->name,name); 14 Ptea p=array[key]; 15 if(p==NULL) 16 temp->next=NULL; 17 else 18 temp->next=p; 19 array[key]=temp; 20 } 21 void show_haxi(Ptea array[]) 22 { 23 Ptea p; 24 for(int i=0;i<5;i++) 25 { 26 printf("第%d行\n",i); 27 p=array[i]; 28 while(p!=NULL) 29 { 30 printf("%d\t%s\n",p->id,p->name); 31 p=p->next; 32 } 33 } 34 } 35 void send_haxi(int msgid,Ptea array[]) 36 { 37 struct msgbuf1 mb; 38 Ptea p; 39 for(int i=0;i<5;i++) 40 { 41 p=array[i]; 42 while(p!=NULL) 43 { 44 mb.type=2; 45 mb.buf=*p; 46 msgsnd(msgid,&mb,sizeof(struct teacher),0); 47 p=p->next; 48 } 49 } 50 } 51 void recv_haxi(int msgid,Ptea array[]) 52 { 53 struct msgbuf1 mb; 54 Ptea temp; 55 int key; 56 while(msgrcv(msgid,&mb,sizeof(tea),2,IPC_NOWAIT)>0) 57 { 58 temp=(Ptea)malloc(sizeof(tea)); 59 *temp=mb.buf; 60 key=haxi(temp->id); 61 if(array[key]==NULL) 62 { 63 temp->next=NULL; 64 array[key]=temp; 65 } 66 else 67 { 68 temp->next=array[key]; 69 array[key]=temp; 70 } 71 } 72 return; 73 }
msgsend.c文件
1 #include<sys/types.h> 2 #include<sys/ipc.h> 3 #include<sys/msg.h> 4 #include"double_link.h" 5 #include"haxi.h" 6 int main() 7 { 8 int msgid; 9 msgid=msgget(0x2000,IPC_CREAT | 0777); 10 Pstu head=NULL; 11 for(int i=0;i<5;i++) 12 { 13 head=create_link(head); 14 } 15 show_link(head); 16 send_link(msgid,head); 17 printf("send haxi\n"); 18 int id; 19 char name[20]; 20 Ptea array[5]={NULL}; 21 for(int i=0;i<10;i++) 22 { 23 printf("input haxi id:"); 24 scanf("%d",&id); 25 printf("input haxi name:"); 26 scanf("%s",name); 27 create_haxi(id,name,array); 28 } 29 show_haxi(array); 30 send_haxi(msgid,array); 31 }
msgrecv.c文件
1 #include<sys/types.h> 2 #include<sys/ipc.h> 3 #include<sys/msg.h> 4 #include<stdio.h> 5 #include"double_link.h" 6 #include"haxi.h" 7 int main() 8 { 9 int msgid; 10 msgid=msgget(0x2000,IPC_CREAT |0777); 11 //struct msgbuf mb; 12 //msgrcv(msgid,&mb,sizeof(struct msgbuf)-sizeof(long),1,0); 13 //puts(mb.buf); 14 Pstu head=recv_link(msgid); 15 show_link(head); 16 17 Ptea array[5]={NULL}; 18 recv_haxi(msgid,array); 19 show_haxi(array); 20 return 0; 21 }
gcc double_link.c haxi.c msgsend.c -o send
gcc double_link.c haxi.c msgrecv.c -o recv
双向链表和哈希表传输数据,效果如下: