Linux 进程通信 共享内存,消息队列

#include "common.h"
//write
void main()
{
	int fp;
	int shmid;    //share memory id
	char *shmaddr;   //share memory address
	int mid;    //message id
	struct message msg;

	//open file 
	fp = open(filePath2, O_RDWR | O_CREAT, 0777);
	if (-1 == fp)
	{
		printf("%s open error!\n", filePath2);
		exit(1);
	}
	//init share memory
	shmid = shmget(SHMKEY, BUF_SIZE, 0777 | IPC_CREAT);
	if (shmid<0)
	{
		printf("shmget error!\n");
		close(fp);
		exit(1);
	}
	//reflect share memory
	shmaddr = shmat(shmid, 0, 0);
	if (shmaddr<(char*)0)
	{
		printf("shmat error!\n");
		close(fp);
		exit(1);
	}
	//init message queue
	mid = msgget(MSGKEY, IPC_CREAT | 0666);
	if (mid<0)
	{
		printf("msgget error!\n");
		close(fp);
		shmdt(shmaddr);
	}
	//file transfer
	while (1)
	{
		//receive msg from read
		memset(&msg, 0, sizeof(msg));
		msgrcv(mid, &msg, sizeof(msg) - sizeof(msg.msg_type), MESSAGE_TYPE_READ, 0);
		//read close shmaddr,the msg_text will be null,the process will be break;
		if (0 != strcmp(msg.msg_text, content[1]))
		{
			printf("over\n");
			break;
		}

		//write to filePath2 from shmaddr
		write(fp, shmaddr, msg.msg_len);

		//send to read msg
		memset(&msg, 0, sizeof(msg));
		msg.msg_type = MESSAGE_TYPE_WRITE;
		strcpy(msg.msg_text, content[0]);
		msgsnd(mid, &msg, sizeof(msg) - sizeof(msg.msg_type), 0);

	}
	close(fp);
	shmdt(shmaddr);
	msgctl(mid, IPC_RMID, NULL);
}
#ifndef _COMMON_H_
#define _COMMON_H_

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
//定义了两个固定的KEY
#define SHMKEY 170811
#define MSGKEY 170810

#define BUF_SIZE 1024
#define MIN_STRING 32
//运行程序前在目录/tmp下准备了pic.jpg文件
const char filePath1[] = { "/tmp/pic.jpg" };
const char filePath2[] = { "/tmp/bic.jpg" };

struct message
{
	long msg_type;
	char msg_text[MIN_STRING];
	int msg_len;
};

enum messageType 
{
	MESSAGE_TYPE_READ = 1,
	MESSAGE_TYPE_WRITE,
	MESSAGE_TYPE_MAX,
};

const char content[][MIN_STRING] = {
	"read done",
	"write done",
};

#endif // !_COMMON_H_


#include "common.h"
//read
void main()
{
	int fp;    
	int shmid;    //share memory id
	char *shmaddr;   //share memory address
	int readsize;
	int mid;    //message id
	struct message msg;
	
	//open file 
	fp = open(filePath1, O_RDONLY, 0777);
	if (-1 == fp)
	{
		printf("%s open error!\n", filePath1);
		exit(1);
	}
	//init share memory
	shmid = shmget(SHMKEY, BUF_SIZE, 0777 | IPC_CREAT);
	if (shmid<0)
	{
		printf("shmget error!\n");
		close(fp);
		exit(1);
	}
	//reflect share memory
	shmaddr = shmat(shmid, 0, 0);
	if (shmaddr<(char*)0)
	{
		printf("shmat error!\n");
		close(fp);
		exit(1);
	}
	//init message queue
	mid = msgget(MSGKEY, IPC_CREAT | 0666);
	if (mid<0)
	{
		printf("msgget error!\n");
		close(fp);
		shmdt(shmaddr);
	}
	//file transfer
	while (1)
	{
		//read file to share memory
		readsize = read(fp, shmaddr, BUF_SIZE);

		//send to write programe,data is ready,can be read
		memset(&msg, 0, sizeof(msg));
		msg.msg_type = MESSAGE_TYPE_READ;
		msg.msg_len = readsize;
		strcpy(msg.msg_text, content[1]);
		msgsnd(mid, &msg, sizeof(msg) - sizeof(msg.msg_type), 0);

		//receive massage from write programe,can be write 
		memset(&msg, 0, sizeof(msg));
		msgrcv(mid, &msg, sizeof(msg) - sizeof(msg.msg_type), MESSAGE_TYPE_WRITE, 0);
		if (0 != strcmp(msg.msg_text,content[0]))
		{
			printf("system error");
			break;
		}

		//the file was read empty
		if (readsize<BUF_SIZE)
		{
			break;
		}
	}
	close(fp);
	shmdt(shmaddr);
	msgctl(mid, IPC_RMID, NULL);
}

猜你喜欢

转载自blog.csdn.net/zhangzc1026/article/details/80583139
今日推荐