IO 消息队列实现AB进程通话 共享内存实现一个进程打印一个进程逆置

A进程

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <fcntl.h>
#include<semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>

typedef struct {
	long mtype;
	char buf[20];

}data;

void* _w_mq(void *arg){
	data d;
	int mqid=*(int *)arg;
	memset(&d,0,sizeof(d));
	while(1){
		printf("请输入数据包类型:\n");
		scanf("%ld",&(d.mtype));
		printf("请输入数据:\n");
		scanf("%s",d.buf);
		msgsnd(mqid,&d,sizeof(d.buf),0);
		if(strcmp("quit",(d.buf))==0){
			break;
		}
		memset(&d,0,sizeof(d));
	}
	exit(0)
}

void* _r_mq(void *arg){
	data d;
	int mqid=*(int *)arg;
	while(1){
		msgrcv(mqid,&d,sizeof(d.buf),1,0);  //_w_mq收1包
		printf("%s\n",d.buf);
		if(strcmp("quit",(d.buf))==0){
			break;
		}
	}
		d.mtype=2;
		strcpy(d.buf,"quit");
		msgsnd(mqid,&d,sizeof(d.buf),0);
		exit(0);
	
}


void w(){
	
	key_t key=ftok("/home/ubuntu/",1);
	if(key==-1){
		perror("ftok");
		return;
	}

	int mqid= msgget(key,IPC_CREAT|0777);
	if(mqid<0){
		perror("msgget");
		return;
	}
	

	pthread_t tid1;
	pthread_t tid2;
	
	pthread_create(&tid1,NULL,_w_mq,(void*)&mqid);
	pthread_create(&tid2,NULL,_r_mq,(void*)&mqid);

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);




}



int main(){
w();
return 0;
}

B进程

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <fcntl.h>
#include<semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
typedef struct {
	long mtype;
	char buf[20];

}data;

void* _w_mq(void *arg){
	data d;
	int mqid=*(int *)arg;
	memset(&d,0,sizeof(d));
	while(1){
		printf("请输入数据包类型:\n");
		scanf("%ld",&(d.mtype));
		printf("请输入数据:\n");
		scanf("%s",d.buf);
		msgsnd(mqid,&d,sizeof(d.buf),0);
		if(strcmp("quit",(d.buf))==0){
			break;
		}
		memset(&d,0,sizeof(d));
	}
	exit(0);
}

void* _r_mq(void *arg){
	data d;
	int mqid=*(int *)arg;
	while(1){
		msgrcv(mqid,&d,sizeof(d.buf),2,0);  //_r_mq收w包
		printf("%s\n",d.buf);
		if(strcmp("quit",(d.buf))==0){
			break;
		}
	}
		d.mtype=1;
		strcpy(d.buf,"quit");
		msgsnd(mqid,&d,sizeof(d.buf),0);
	
	exit(0);
}


void w(){
	
	key_t key=ftok("/home/ubuntu/",1);
	if(key==-1){
		perror("ftok");
		return;
	}

	int mqid= msgget(key,IPC_CREAT|0777);
	if(mqid<0){
		perror("msgget");
		return;
	}
	

	pthread_t tid1;
	pthread_t tid2;
	
	pthread_create(&tid1,NULL,_w_mq,(void*)&mqid);
	pthread_create(&tid2,NULL,_r_mq,(void*)&mqid);

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);




}



int main(){
w();
return 0;
}

2.一个进程打印,一个进程逆置

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <fcntl.h>
#include<semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>


void test(){
		
	key_t key=ftok("/home/ubuntu/",1);
	if(key<0){
		perror("ftok");
		return;
	}

	int shmid=shmget(key,32,IPC_CREAT|0777);
	
	if(shmid<0){
		perror("shmget");
		return;
	}
	
	

	char buf[20]="123456";
	void* addr=shmat(shmid,NULL,0);
	int* faddr=(int *)addr;
	char* saddr=(char*)addr+4;
	*faddr=1;
	strcpy(saddr,buf);
	pid_t pid =fork();

	if(pid>0){

		//print
		while(1){
			if(*faddr==0){
			printf("%s\n",saddr);
			*faddr=1;	
			sleep(1);
			}
		}

	}else if(pid==0){
		//transfer
		while(1){
			if(*faddr==1){
				char *start;
				char *end;
				start=buf;
				end=buf;
		while(*end!='\0'){
			end++;
		}	
			end--;
		while(start<end){
			char temp=*start;
			*start=*end;
			*end=temp;
			start++;
			end--;
		}
			
	}
		*faddr=0;
		strcpy(saddr,buf);
}

}else{
		perror("fork");
		return;
	}

}





int main(){
test();	
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值