IO—消息队列+管道

使用消息队列实现的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;
	}
	//read
	msgpstr 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;
    }
	//read
	msgpstr 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;
	}
}

运行结果:
 

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值