7.24作业

第一题: 使用消息队列实现2个终端之间的互相聊天功能

#include "/home/a/Desktop/test/gencls/GenFunc.h"

int mid = -1;
typedef struct msgdata_main
{
	long type;
	char data[1024];
}msgdata;
void signalMain(int sig)
{
	if(SIGCHLD == sig)
		while(-1 != waitpid(-1,0,WNOHANG));
	else if(SIGINT == sig)
	{
		if(mid >= 0)
		{
			msgctl(mid,IPC_RMID,0);
			mid = -1;
		}
		exit(0);
	}
}
void* run(void* data)
{
	while(1)
	{
		msgdata data;
		//data.type =  2;
		if(0 < msgrcv(mid,(void*)&data,1024,2,0))
		{
			printf("%s\n",data.data);
		}	
	}
}
int main(int argc, const char *argv[])
{
	signal(SIGCHLD,signalMain);
	signal(SIGINT,signalMain);
	srand(time(0));

	if(access("./msg",0) < 0) return -1;
	int key = ftok("./msg",1);
	if(key < 0) return -1;
	mid = msgget(key,IPC_CREAT | 0666);
	if(mid < 0) return -1;
	pthread_t mt;
	pthread_create(&mt,0,run,0);
	pthread_detach(mt);
	while(1)
	{
		msgdata data;
		data.type =  1;
		printf("请输入:");
		GetLineFunc(data.data,1023);
		msgsnd(mid,(void*)&data,1024,0);
	}
	msgctl(mid,IPC_RMID,0);
	return 0;
}
#include "/home/a/Desktop/test/gencls/GenFunc.h"

int mid = -1;
typedef struct msgdata_main
{
	long type;
	char data[1024];
}msgdata;
void signalMain(int sig)
{
	if(SIGCHLD == sig)
		while(-1 != waitpid(-1,0,WNOHANG));
	else if(SIGINT == sig)
	{
		if(mid >= 0)
		{
			msgctl(mid,IPC_RMID,0);
			mid = -1;
		}
		exit(0);
	}
}
void* run(void* data)
{
	while(1)
	{
		msgdata data;
		//data.type =  1;
		if(0 < msgrcv(mid,(void*)&data,1024,1,0))
		{
			printf("%s\n",data.data);
		}	
	}
}
int main(int argc, const char *argv[])
{
	signal(SIGCHLD,signalMain);
	signal(SIGINT,signalMain);
	srand(time(0));

	if(access("./msg",0) < 0) return -1;
	int key = ftok("./msg",1);
	if(key < 0) return -1;
	mid = msgget(key,IPC_CREAT | 0666);
	if(mid < 0) return -1;
	pthread_t mt;
	pthread_create(&mt,0,run,0);
	pthread_detach(mt);
	while(1)
	{
		msgdata data;
		data.type =  2;
		printf("请输入:");
		GetLineFunc(data.data,1023);
		msgsnd(mid,(void*)&data,1024,0);
	}
	msgctl(mid,IPC_RMID,0);
	return 0;
}

第二题: 创建一对父子进程,父进程循环输出 A,子进程循环输出a 使用信号灯集同步这对父子进程,实现效果 A_a_A_a ..... _表示休眠1秒

#include "/home/a/Desktop/test/gencls/GenFunc.h"
int sem = -1;
void signalMain(int sig)
{
	if(SIGCHLD == sig)
		while(-1 != waitpid(-1,0,WNOHANG));
	else if(SIGINT == sig)
	{
		if(sem >= 0)
		{
			semctl(sem,0,IPC_RMID,0);
			sem = -1;
		}
		exit(0);
	}
}
void* run(void* data)
{
	while(1)
	{
		semwait(sem,1);
		printf("a");
		fflush(stdout);
		sleep(1);
		sempost(sem,0);	
	}
}
int main(int argc, const char *argv[])
{
	signal(SIGCHLD,signalMain);
	signal(SIGINT,signalMain);
	srand(time(0));

	if(access("./msg",0) < 0) return -1;
	int keysem = ftok("./msg",1);
	if(keysem < 0) return -1;
	sem =  semget(keysem,2,IPC_CREAT | 0666);
	if(sem < 0) return -1;
	semctl(sem,0,SETVAL,1);
	semctl(sem,1,SETVAL,0);
	
	int pid = fork();
	if(pid < 0)
	{
		semctl(sem,0,IPC_RMID,0);
		sem = -1;
		return -1;
	}else if(pid == 0) run(NULL);
	else{
		while(1)
		{
			semwait(sem,0);
			printf("A");
			fflush(stdout);
			sleep(1);
			sempost(sem,1);
		}
	}

	semctl(sem,0,IPC_RMID,0);
	sem = -1;
	return 0;
}

第三题: 使用共享内存 + 信号灯集实现2个终端之间互相聊天

#include "/home/a/Desktop/test/gencls/GenFunc.h"
typedef struct msgdata_main
{
	char data[1024];
}msgdata;
int sem = -1,shm = -1;
msgdata* pdata = NULL;
void signalMain(int sig)
{
	if(SIGCHLD == sig)
		while(-1 != waitpid(-1,0,WNOHANG));
	else if(SIGINT == sig)
	{
		if(sem >= 0 || shm >= 0 || NULL != pdata)
		{
			shmdt((void*)pdata);
			semctl(sem,0,IPC_RMID,0);
			shmctl(shm,IPC_RMID,0);
			sem = -1;
			shm = -1;
			pdata = NULL;
		}
		exit(0);
	}
}
void* run(void* data)
{
	while(1)
	{
		semwait(sem,1);
		printf("%s\n",pdata->data);	
	}
}
int main(int argc, const char *argv[])
{
	signal(SIGCHLD,signalMain);
	signal(SIGINT,signalMain);
	srand(time(0));

	if(access("./msg",0) < 0) return -1;
	int keyshm = ftok("./msg",1);
	int keysem = ftok("./msg",2);
	if(keyshm < 0 || keysem < 0) return -1;
	sem =  semget(keysem,2,IPC_CREAT | 0666);
	shm = shmget(keyshm,1024, IPC_CREAT | 0666);
	if(sem < 0 || shm < 0)
	{
		semctl(sem,0,IPC_RMID,0);
		shmctl(shm,IPC_RMID,0);
		return -1;
	}
	semctl(sem,0,SETVAL,0);
	semctl(sem,1,SETVAL,0);
	pdata = shmat(shm,0,0);
	if(NULL == pdata)
	{
		semctl(sem,0,IPC_RMID,0);
		shmctl(shm,IPC_RMID,0);
		return -1;
	}
	
	pthread_t mt;
	pthread_create(&mt,0,run,0);
	pthread_detach(mt);
	while(1)
	{
		GetLineFunc(pdata->data,1023);
		sempost(sem,0);
	}
	shmdt((void*)pdata);
	semctl(sem,0,IPC_RMID,0);
	shmctl(shm,IPC_RMID,0);
	sem = -1;
	shm = -1;
	pdata = NULL;
	return 0;
}
#include "/home/a/Desktop/test/gencls/GenFunc.h"
typedef struct msgdata_main
{
	char data[1024];
}msgdata;
int sem = -1,shm = -1;
msgdata* pdata = NULL;
void signalMain(int sig)
{
	if(SIGCHLD == sig)
		while(-1 != waitpid(-1,0,WNOHANG));
	else if(SIGINT == sig)
	{
		if(sem >= 0 || shm >= 0 || NULL != pdata)
		{
			shmdt((void*)pdata);
			semctl(sem,0,IPC_RMID,0);
			shmctl(shm,IPC_RMID,0);
			sem = -1;
			shm = -1;
			pdata = NULL;
		}
		exit(0);
	}
}
void* run(void* data)
{
	while(1)
	{
		semwait(sem,0);
		printf("%s\n",pdata->data);	
	}
}
int main(int argc, const char *argv[])
{
	signal(SIGCHLD,signalMain);
	signal(SIGINT,signalMain);
	srand(time(0));

	if(access("./msg",0) < 0) return -1;
	int keyshm = ftok("./msg",1);
	int keysem = ftok("./msg",2);
	if(keyshm < 0 || keysem < 0) return -1;
	sem =  semget(keysem,2,IPC_CREAT | 0666);
	shm = shmget(keyshm,1024, IPC_CREAT | 0666);
	if(sem < 0 || shm < 0)
	{
		semctl(sem,0,IPC_RMID,0);
		shmctl(shm,IPC_RMID,0);
		return -1;
	}
	semctl(sem,0,SETVAL,0);
	semctl(sem,1,SETVAL,0);
	pdata = shmat(shm,0,0);
	if(NULL == pdata)
	{
		semctl(sem,0,IPC_RMID,0);
		shmctl(shm,IPC_RMID,0);
		return -1;
	}
	
	pthread_t mt;
	pthread_create(&mt,0,run,0);
	pthread_detach(mt);
	while(1)
	{
		GetLineFunc(pdata->data,1023);
		sempost(sem,1);
	}
	shmdt((void*)pdata);
	semctl(sem,0,IPC_RMID,0);
	shmctl(shm,IPC_RMID,0);
	sem = -1;
	shm = -1;
	pdata = NULL;
	return 0;
}

思维导图

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值