IO—消息队列+管道

使用消息队列实现的2个终端之间的互相聊天

并使用信号控制消息队列的读取方式:

当键盘按ctrl+c的时候,切换消息读取方式,一般情况为读取指定编号的消息,按ctr1+c之后,指定的编号不读取,读取其他所有编号的消息

wftok.c

cs 复制代码
#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

cs 复制代码
#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;
	}
}

运行结果:

相关推荐
深度Linux2 小时前
Linux网络编程中的零拷贝:提升性能的秘密武器
linux·linux内核·零拷贝技术
chian-ocean6 小时前
从理论到实践:Linux 进程替换与 exec 系列函数
linux·运维·服务器
拎得清n6 小时前
UDP编程
linux
敖行客 Allthinker6 小时前
从 UTC 日期时间字符串获取 Unix 时间戳:C 和 C++ 中的挑战与解决方案
linux·运维·服务器·c++
夏尔Gaesar8 小时前
Vim安装与配置教程(解决软件包Vim没有安装可候选)
linux·编辑器·vim
hunter2062068 小时前
如何监控ubuntu系统某个程序的运行状态,如果程序出现异常,对其自动重启。
linux·chrome·ubuntu
慕雪华年9 小时前
【Linux】opencv在arm64上提示找不到libjasper-dev
linux·运维·opencv
s_little_monster11 小时前
【Linux】从硬件到软件了解进程
linux·运维·服务器·经验分享·笔记·学习·学习方法
zyhhsss13 小时前
Ubuntu&Windows双系统安装
linux·运维·ubuntu
Marilynhom14 小时前
CentOs9新手教程
linux·centos