使用消息隊列實現的2個終端之間的互相聊天
并使用信號控制消息隊列的讀取方式:
當鍵盤按ctrl+c的時候,切換消息讀取方式,一般情況為讀取指定編號的消息,按ctr1+c之后,指定的編號不讀取,讀取其他所有編號的消息
wftok.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
void* run(void* arg);
typedef struct msgp{long mtype;char mtext[128];
}msgpstr;
int mtype=1;
void handler(int signum);
int main(int argc, const char *argv[])
{pthread_t id;int prtval;prtval= pthread_create(&id,0,run,0);if(prtval==-1){perror("pthread_create");return 1;}signal(SIGINT,handler);//創建消息隊列秘鑰key_t frtval=ftok("./ipc_ftok",1);if(frtval==-1){perror("ftok");return 1;}//訪問創建的消息秘鑰int mrtval=msgget(frtval,IPC_CREAT|0666);if(mrtval==-1){perror("msgget");return 1;}//寫入消息msgpstr msg;while(1){bzero(msg.mtext,128);msg.mtype=mtype;//確定消息編號puts("A:");scanf("%128s",msg.mtext);while(getchar()!=10);int msrtval=msgsnd(mrtval,&msg,128,0);}return 0;
}
void* run(void* arg){//創建消息隊列秘鑰key_t frtval=ftok("./ipc_ftok",1);if(frtval==-1){perror("ftok");return NULL;}//訪問創建的消息秘鑰int mrtval=msgget(frtval,IPC_CREAT|0666);if(mrtval==-1){perror("msgget"); return NULL;}//readmsgpstr msg;while(1){bzero(msg.mtext,128);msgrcv(mrtval,&msg,128,mtype+1,0);printf("B:%s\n",msg.mtext);}}
void handler(int signum){if(signum==SIGINT){printf("cut number\n");mtype=2;}
}
rftok.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
void* run(void* arg);
typedef struct msgp{long mtype;char mtext[128];
}msgpstr;
int mtype=1;
void handler(int signum);
int main(int argc, const char *argv[])
{pthread_t id;int prtval;prtval= pthread_create(&id,0,run,0);if(prtval==-1){perror("pthread_create");return 1;}signal(SIGINT,handler);//創建消息隊列秘鑰key_t frtval=ftok("./ipc_ftok",1);if(frtval==-1){perror("ftok");return 1;}//訪問創建的消息秘鑰int mrtval=msgget(frtval,IPC_CREAT|0666);if(mrtval==-1){perror("msgget");return 1;}//readmsgpstr msg;while(1){bzero(msg.mtext,128);msgrcv(mrtval,&msg,128,mtype,0);printf("A:%s\n",msg.mtext);}return 0;
}
void* run(void* arg){//創建消息隊列秘鑰key_t frtval=ftok("./ipc_ftok",1);if(frtval==-1){perror("ftok");return NULL;}//訪問創建的消息秘鑰int mrtval=msgget(frtval,IPC_CREAT|0666);if(mrtval==-1){perror("msgget");return NULL;}//寫入消息msgpstr msg;while(1){bzero(msg.mtext,128);msg.mtype=mtype+1;//確定消息編號puts("B:");scanf("%128s",msg.mtext);while(getchar()!=10);int msrtval=msgsnd(mrtval,&msg,128,0);}
}
void handler(int signum){if(signum==SIGINT){printf("cut number\n");mtype=2;}
}
運行結果:
?