0805 练习

1)要求AB进程做通信

  1. A进程发送一句话,B进程接收打印
  2. 然后B进程发送给A进程一句话,A进程接收打印
  3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
    2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)

A进程:

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
struct msgbuf
{
	long mtype;
	char mtext[128];
};
int msqid;
void* send(void* arg)
{

	
	struct msgbuf snd;
	while(1)
	{		
		snd.mtype =1;
		printf("A发送的消息为:");
		fgets(snd.mtext,sizeof(snd.mtext),stdin);
		snd.mtext[strlen(snd.mtext)-1]=0;
		msgsnd(msqid,&snd,sizeof(snd.mtext),0);
		if(strcasecmp(snd.mtext,"quit")==0)
			exit(0);		
	}
}
void* receive(void* arg)
{


	struct msgbuf rcv;

	while(1)
	{
		msgrcv(msqid,&rcv,sizeof(rcv.mtext),2,0);
		printf("\rA收到的消息为:%s\n",rcv.mtext);
		printf("A发送到消息:");                               
		fflush(stdout);	
		if(strcasecmp(rcv.mtext,"quit")==0)
			exit(0);	
	}
}
	
int main(int argc, const char *argv[])
{
	key_t key =ftok("./",2);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	msqid=msgget(key,IPC_CREAT|0664);
	if(msqid<0)
	{
		perror("msgget");
		return -1;
	}
	
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,send,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&tid2,NULL,receive,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	system("ipcs -q");
	
	return 0;

}

B进程:

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
struct msgbuf
{
    long mtype;
    char mtext[128];                                   
};
int msqid;
void* send(void* arg)
{
	struct msgbuf snd;

	while(1)
    {
		snd.mtype=2;
        printf("B发送的消息为:");
        fgets(snd.mtext,sizeof(snd.mtext),stdin);
        snd.mtext[strlen(snd.mtext)-1]=0;
        msgsnd(msqid,&snd,sizeof(snd.mtext),0);
        if(strcasecmp(snd.mtext,"quit")==0)
            exit(0);
    }
}
void* receive(void* arg)
{
    struct msgbuf rcv;
    while(1)
    {
        msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0);
		printf("\rB收到的消息为:%s\n",rcv.mtext);
		printf("B发送到消息:");
		fflush(stdout);
        if(strcasecmp(rcv.mtext,"quit")==0)
            exit(0);
    }
       
}

int main(int argc, const char *argv[])
{
    key_t key =ftok("./",2);
    if(key<0)
    {
        perror("ftok");
        return -1;
    }
    msqid=msgget(key,IPC_CREAT|0664);
    if(msqid<0)
    {                                                 
        perror("msgget");
        return -1;
    }

    pthread_t tid1,tid2;
    if(pthread_create(&tid1,NULL,send,NULL)!=0)
    {
        perror("pthread_create");
        return -1;
    }
    if(pthread_create(&tid2,NULL,receive,NULL)!=0)
    {
        perror("pthread_create");
        return -1;
    }

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	system("ipcs -q");

    return 0;

}

  1. A进程写入一个整型,在该整型后,写入一个字符串
  2. B进程将共享内存中的整型以及字符串读取出来;

A进程:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc, const char *argv[])
{
	key_t key=ftok("./",3);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	printf("key=%d\n",key);
	int shmid=shmget(key,64,IPC_CREAT|0664);
	if(shmid<0)
	{
		perror("shmget");
		return -1;
	}

	void *p=shmat(shmid,NULL,0);
	if(p==(void*)-1)
	{
		perror("shmat");
		return -1;
	}
	printf("p=%p\n",p);

	int *q=(int*)p;
	printf("请输入一个整型:");
	scanf("%d",q);
	getchar();
	char *str=(char*)(q+1);
	printf("请输入一个字符串:");
	scanf("%s",str);
	getchar();

	return 0;

}

B进程:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc, const char *argv[])
{
    key_t key=ftok("./",3);
    if(key<0)
    {
        perror("ftok");
        return -1;
    }
    printf("key=%d\n",key);
    int shmid=shmget(key,64,IPC_CREAT|0664);
    if(shmid<0)
    {
        perror("shmget");
        return -1;
    }

    void *p=shmat(shmid,NULL,0);
    if(p==(void*)-1)
    {
        perror("shmat");
        return -1;
    }
    printf("p=%p\n",p);

    int *q=(int*)p;
	printf("%d\n",*q);
	char *str=(char*)(q+1);
	printf("%s\n",str);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值