信息队列及共享内存练习题

1、要求实现AB进程对话

A进程先发送一句话给B进程,B进程接收后打印

B进程再回复一句话给A进程,A进程接收后打印

重复上诉步骤,当对方输入quit后,退出AB进程删除消息队列

提示:用一个消息队列,两种类型即可

A进程

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>

struct msgbuf {
	long mtype;       /* message type, must be > 0 */
	char mtext[128];    /* message data */
}send,receive;

	int msqid; 
	pthread_t tid_r,tid_w;

void* task_read(void* arg){
	while(1){
		bzero(receive.mtext,sizeof(receive.mtext));		
		//B发送的到信息队列的信息权限为2
		msgrcv(msqid,&receive,sizeof(receive.mtext),2,0);
		//读取到quit时退出
		if(strcmp(receive.mtext,"quit") == 0)
			break;
		printf("B : %s\n",receive.mtext);
	}
	//该线程退出前请求另一个线程退出
	pthread_cancel(tid_w);
	pthread_exit(NULL);
}

void* task_write(void* arg){
	send.mtype=1;//A发送的到信息队列的信息权限为1
	while(1){
		bzero(send.mtext,sizeof(send.mtext));
		fgets(send.mtext,sizeof(send.mtext),stdin);
		send.mtext[strlen(send.mtext)-1] = 0;//将'\n'换为'\0'
		msgsnd(msqid,&send,sizeof(send.mtext),0);
		//终端输入quit后退出线程
		if(strcmp(send.mtext,"quit") == 0)
			break;
	}
	//该线程退出前请求另一个线程退出
	pthread_cancel(tid_r);
	pthread_exit(NULL);
}

int main(int argc,const char *argv[])
{
	key_t key;
	key = ftok("./",1);
	if(key < 0){
		perror("ftok");
		return -1;
	}
	//创建信息队列
	msqid =msgget(key,IPC_CREAT|0664);
	if(msqid < 0){
		perror("msgget");
		return -1;
	}
	
	//创建两个线程
	if(pthread_create(&tid_r,NULL,task_read,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid_w,NULL,task_write,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	
	//回收线程资源
	pthread_join(tid_r,NULL);
	pthread_join(tid_w,NULL);
	
	//删除信息队列
	msgctl(msqid,IPC_RMID,NULL);
	return 0;
}

B进程

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>

struct msgbuf {
	long mtype;       /* message type, must be > 0 */
	char mtext[128];    /* message data */
}send,receive;

	int msqid; 
	pthread_t tid_r,tid_w;

void* task_read(void* arg){
	while(1){
		bzero(receive.mtext,sizeof(receive.mtext));		
		//A发送的到信息队列的信息权限为1
		msgrcv(msqid,&receive,sizeof(receive.mtext),1,0);
		//读取到quit时退出
		if(strcmp(receive.mtext,"quit") == 0)
			break;
		printf("A : %s\n",receive.mtext);
	}
	//该线程退出前请求另一个线程退出
	pthread_cancel(tid_w);
	pthread_exit(NULL);
}

void* task_write(void* arg){
	send.mtype=2;//B发送的到信息队列的信息权限为1
	while(1){
		bzero(send.mtext,sizeof(send.mtext));
		fgets(send.mtext,sizeof(send.mtext),stdin);
		send.mtext[strlen(send.mtext)-1] = 0;//将'\n'换为'\0'
		msgsnd(msqid,&send,sizeof(send.mtext),0);
		//终端输入quit后退出线程
		if(strcmp(send.mtext,"quit") == 0)
			break;
	}
	//该线程退出前请求另一个线程退出
	pthread_cancel(tid_r);
	pthread_exit(NULL);
}

int main(int argc,const char *argv[])
{
	key_t key;
	key = ftok("./",1);
	if(key < 0){
		perror("ftok");
		return -1;
	}
	//创建信息队列
	msqid =msgget(key,IPC_CREAT|0664);
	if(msqid < 0){
		perror("msgget");
		return -1;
	}
	
	//创建两个线程
	if(pthread_create(&tid_r,NULL,task_read,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid_w,NULL,task_write,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	
	//回收线程资源
	pthread_join(tid_r,NULL);
	pthread_join(tid_w,NULL);
	
	//删除信息队列
	msgctl(msqid,IPC_RMID,NULL);
	return 0;
}

2、创建两个进程,定义一个共享内存,内存中存储char str[10]= "1234567";要求如下

A循环打印str;

B循环倒置str; 不能使用辅助数组;

要求出现的结果没有乱序,只能出现 1234567 7654321

不允许使用sleep函数

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <unistd.h>
#include <wait.h>
int main(int argc,const char *argv[])
{
	//创建key值
	key_t key = ftok("./",1);
	if(key < 0){
		perror("ftok");
		return -1;
	}
	//创建共享内存
	int shmid = shmget(key,32,IPC_CREAT|0664);
	if(shmid < 0){
		perror("shmget");
		return -1;
	}
	
	char str[] = "1234567";

	//创建子进程
	pid_t cpid;
	cpid = fork();
	if(cpid > 0)    //父进程打印
	{
		void * attr = shmat(shmid,NULL,0);
		if((void*)-1 == attr){
			perror("shmat");
			wait(NULL);
			return -1;
		}
		*(char*)attr = 1;
		strcpy((char*)attr+1,str);
		while(1){
			if(*((char*)attr) == 1){   //为1时打印
				printf("%s\n",(char*)attr+1);
				*(char*)attr = 2;
			}
		}
	}
	else if(cpid == 0)   //子进程倒置
	{
		void * attr = shmat(shmid,NULL,0);
		if((void*)-1 == attr){
			perror("shmat");
			return -1;
		}
		while(1){
			if(*((char*)attr) == 2){   //为2时倒置
				for(int i=1;i<4;i++){
					char temp = *((char*)attr+i);
					*((char*)attr+i) = *((char*)attr+8-i);
					*((char*)attr+8-i) = temp;
				}
				*(char*)attr = 1;
			}			
		}
	}
	else
	{
		perror("fork");
		return -1;
	}
	wait(NULL);
	//删除共享内存
	shmctl(shmid,IPC_RMID,NULL);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值