程序也就是这么个程序,能够实现进程间通讯,自己也是费了不少功夫想出来的,感觉设计确实是比实现复杂不少。
发到这里当个备份吧。
发到这里当个备份吧。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <string.h> 5 #include <sys/ipc.h> 6 #include <sys/msg.h> 7 #define size 512 8 struct msg{ 9 int sign; int a1; int a2; 10 char message[size]; 11 }mymsg; 12 int main(){ 13 int pid; 14 char name[size]; 15 int id; 16 printf("please input the name:\n"); 17 fgets(name,size,stdin); 18 id=getpid(); 19 int sid=msgget(9967,0777|IPC_CREAT); 20 mymsg.a1=id; 21 mymsg.sign=1; 22 strcpy(mymsg.message,name); 23 if(msgsnd(sid,&mymsg,size,0)==-1){ 24 perror("msgsnd"); 25 exit(1); 26 } 27 28 pid=fork(); 29 if(pid==0){ 30 int lid=msgget(id,0777|IPC_CREAT); 31 while(1){ 32 if(msgrcv(lid,&mymsg,size,0,0)==-1){ 33 perror("msgrcv"); 34 exit(1); 35 } 36 if(mymsg.sign==1){ 37 printf("%d : %s",mymsg.a1,mymsg.message); 38 }else if(mymsg.sign==2){ 39 printf("id=%d , name=%s",mymsg.a1,mymsg.message); 40 }else if(mymsg.sign==5){ 41 printf("warning : no such user online!!!!!!\n"); 42 } 43 } 44 }else{ 45 int sid=msgget(9967,0777|IPC_CREAT); 46 int sign1; 47 while(1){ 48 printf("input 2 for get the info of login.\n"); 49 printf("input 3 for send the message to somebody\n"); 50 printf("input 4 for quit\n"); 51 scanf("%d",&sign1); 52 if(sign1==2){ 53 mymsg.sign=2; 54 mymsg.a1=id; 55 if(msgsnd(sid,&mymsg,size,0)==-1){ 56 perror("msgsnd"); 57 exit(1); 58 } 59 }else if(sign1==3){ 60 mymsg.sign=3; 61 mymsg.a1=id; 62 printf("please input the id you want to send to:\n"); 63 scanf("%d",&mymsg.a2); 64 getchar(); 65 printf("please input the message:\n"); 66 fgets(mymsg.message,size,stdin); 67 if(msgsnd(sid,&mymsg,size,0)==-1){ 68 perror("msgsnd"); 69 exit(1); 70 } 71 }else if(sign1==4){ 72 mymsg.sign=4; 73 mymsg.a1=id; 74 printf("goodbye~\n"); 75 if(msgsnd(sid,&mymsg,size,0)==-1){ 76 perror("msgsnd"); 77 exit(1); 78 } 79 exit(1); 80 } 81 82 } 83 } 84 return 0; 85 } 86 87 88 89 90 91 92
1 #include <stdio.h> 2 #include <fcntl.h> 3 #include <stdlib.h> 4 #include <unistd.h> 5 #include <string.h> 6 #include <sys/ipc.h> 7 #include <sys/msg.h> 8 #include <time.h> 9 #define size 512 10 struct msg{ 11 int sign; int a1; int a2; 12 char message[size]; 13 }mymsg; 14 struct list{ 15 int id; 16 char name[size]; 17 struct list *next; 18 }mylist; 19 void del(int del){ 20 struct list *e; 21 struct list *opp; 22 for(opp=&mylist;;opp=opp->next){ 23 if(opp->id==del){ 24 for(e=&mylist;e->next!=opp;e=e->next){ 25 } 26 e->next=opp->next; 27 free(opp); 28 return; 29 } 30 if(opp->next==NULL) break; 31 } 32 return; 33 34 } 35 int check(int nnn){ 36 struct list *oop; 37 for(oop=&mylist;;oop=oop->next){ 38 if(oop->id==nnn){ 39 return 1; 40 } 41 if(oop->next==NULL) return 0; 42 43 } 44 return 0; 45 } 46 int main(){ 47 int sid=msgget(9967,0777|IPC_CREAT); 48 int lid; 49 struct list *op; 50 mylist.next=NULL; 51 while(1){ 52 if(msgrcv(sid,&mymsg,size,0,0)==-1){ 53 perror("msgrcv"); 54 exit(1); 55 } 56 if(mymsg.sign==1){ 57 time_t tp; 58 tp=time(&tp); 59 FILE *fp; 60 if((fp=fopen("./log.txt","a+w"))==NULL){ 61 perror("open"); 62 } 63 printf("%s",ctime(&tp)); 64 65 printf("log in:id=%d,name=%s",mymsg.a1,mymsg.message); 66 fprintf(fp,"%s",ctime(&tp)); 67 fprintf(fp,"log in:id=%d,name=%s",mymsg.a1,mymsg.message); 68 fclose(fp); 69 struct list *p=(struct list *)malloc(sizeof(struct list)); 70 for(op=&mylist;op->next!=NULL;op=op->next); 71 p->id=mymsg.a1; 72 strcpy(p->name,mymsg.message); 73 p->next=NULL; 74 op->next=p; 75 76 }else if(mymsg.sign==2){ 77 mymsg.sign=2; 78 lid=msgget(mymsg.a1,0777|IPC_CREAT); 79 for(op=mylist.next;;op=op->next){ 80 strcpy(mymsg.message,op->name); 81 mymsg.a1=op->id; 82 if(msgsnd(lid,&mymsg,size,0)==-1){ 83 perror("msgsnd"); 84 exit(1); 85 } 86 if(op->next==NULL) break; 87 } 88 }else if(mymsg.sign==3){ 89 mymsg.sign=1; 90 if(check(mymsg.a2)==0){ 91 lid=msgget(mymsg.a1,0777|IPC_CREAT); 92 mymsg.sign=5; 93 if(msgsnd(lid,&mymsg,size,0)==-1){ 94 perror("msgsnd"); 95 exit(1); 96 } 97 continue; 98 } 99 lid=msgget(mymsg.a2,0777|IPC_CREAT); 100 if(msgsnd(lid,&mymsg,size,0)==-1){ 101 perror("msgsnd"); 102 exit(1); 103 } 104 }else if(mymsg.sign==4){ 105 struct list *e; 106 del(mymsg.a1); 107 } 108 } 109 return 0; 110 }