实现生产组和消费者的机制,利用共享内存实现文件的打开和读写操作,PV操作。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zm1_1zm/article/details/77193606

/*shm_com.h*/
#include
#include
#include
#include
#include
#include
#include

#define SHM_BUFF_SZ  2048
struct shm_buff
{
	int pid;
	char buffer[SHM_BUFF_SZ];
};

/*sem_com.h*/
#include
#include
#include
#include
#include
union semun
{
	int val;
	struct semid_ds *buf;
	unsigned short *array;
};
/*信号量初始化(赋值)函数*/
int init_sem(int sem_id,int init_value);
/*从系统中删除信号量的函数*/
int del_sem(int sem_id);
/*p操作函数*/
int sem_p(int sem_id);
/*V操作函数*/
int sem_v(int sem_id);


/*sem_com.c*/
#include"sem_com.h"


/*信号量初始化(赋值)函数*/
int init_sem(int sem_id,int init_value)
{
	union semun sem_union;
	sem_union.val=init_value;  /*init_value为初始值*/
	if(semctl(sem_id,0,SETVAL,sem_union)==-1)
	{
		perror("Initialize semaphore");
		return -1;
	}
	return 0;
}

/*从系统中删除信号量的函数*/
int del_sem(int sem_id)
{
	union semun sem_union;
	if(semctl(sem_id,0,IPC_RMID,sem_union)==-1)
	{
		perror("Delete semaphore");
		return -1;
	}
}

/*p操作函数*/
int sem_p(int sem_id)
{
	struct sembuf sem_b;
	sem_b.sem_num=0;  /*单个信号量的编号应该为0*/
	sem_b.sem_op=-1;  /*取值为-1,表示p操作*/
	sem_b.sem_flg=SEM_UNDO; /*系统自动释放将会在系统中残留的信号量*/
	if(semop(sem_id,&sem_b,1)==-1)
	{
		perror("P operationn");
		return -1;
	}
	return 0;
}

/*V操作函数*/
int sem_v(int sem_id)
{
	struct sembuf sem_b;
	sem_b.sem_num=0;  /*单个信号量的编号应该为0*/
	sem_b.sem_op=1;  /*取值为+1,表示v操作*/
	sem_b.sem_flg=SEM_UNDO; /*系统自动释放将会在系统中残留的信号量*/
	if(semop(sem_id,&sem_b,1)==-1)
	{
		perror("V operationn");
		return -1;
	}
	return 0;
}

/*producer.c*/
#include"shm_com.h"
#include"sem_com.h"
#include
int ignore_signal(void)
{ /*忽略一些信号,以免非法突出程序*/
	signal(SIGINT,SIG_IGN);
	signal(SIGSTOP,SIG_IGN);
	signal(SIGQUIT,SIG_IGN);
	return 0;
}

int main()
{
	char *shared_memory;
	struct shm_buff *shm_buff_inst;
	char buffer[BUFSIZ];
	int shmid,semid;
	/*定义信号量,用于实现访问共享内存的进程间的互斥*/
	ignore_signal();  /*用于防止程序非正常退出*/
	semid=semget(ftok(".",'a'),1,0666|IPC_CREAT);/*创建一个信号量*/
	init_sem(semid,1); /*初始值为1*/

	/*创建共享内存*/
	shmid=shmget(ftok(".",'c'),sizeof(struct shm_buff),0666|IPC_CREAT);
	if(shmid==-1)
	{
		perror("shmget failed");
		del_sem(semid);
		exit(1);
	}

	/*将共享内存地址映射到当前进程地址空间*/
	shared_memory=shmat(shmid,0,0);
	if(shared_memory==(void*)-1)
	{
		perror("shmat");
		del_sem(semid);
		exit(1);
	}

	printf("Memory attached at %p\n",shared_memory);
	/*获取共享内存的映射地址*/
	shm_buff_inst=(struct shm_buff *)shared_memory;
	do
	{
		sem_p(semid);
		printf("Emter some text to the shared memory(enter 'quit' to exit):");
		/*向共享内存写入数据*/
		if(fgets(shm_buff_inst->buffer,SHM_BUFF_SZ,stdin)==NULL)
		{
			perror("fgets");
			sem_v(semid);
			break;
		}
		shm_buff_inst->pid=getpid();
		sem_v(semid);
	}while(strncmp(shm_buff_inst->buffer,"quit",4)!=0);

	/*删除信号量*/
	del_sem(semid);

	/*删除共享内存到当前进程地址空间中的映射*/
	if(shmdt(shared_memory)==-1)
	{
		perror("shmdt");
		exit(1);
	}

	exit(0);
}

/* customer.c */

#include"shm_com.h"
#include"sem_com.h"

int main()
{
	char *shared_memory=NULL;
	struct shm_buff *shm_buff_inst;
	int shmid,semid;

	/*获得信号量*/
	semid=semget(ftok(".",'a'),1,0666);
	if(semid==-1)
	{
		perror("Producer isn't exist");
		exit(1);
	}
	/*获得共享内存*/
	shmid=shmget(ftok(".",'c'),sizeof(struct shm_buff),0666|IPC_CREAT);
	if(shmid==-1)
	{
		perror("shmget");
		exit(1);
	}

	/*将共享内存地址映射到当前进程地址空间*/
	shared_memory=shmat(shmid,0,0);
	if(shared_memory==(void *)-1)
	{
		perror("shmat");
		exit(1);
	}
	printf("Memory attached at %p\n",shared_memory);

	/*获得共享内存的映射地址*/

	shm_buff_inst=(struct shm_buff *)shared_memory;
	do
	{
		sem_p(semid);
		printf("Shared memory was written by process %d : %s",shm_buff_inst->pid,shm_buff_inst->buffer);
		if(strncmp(shm_buff_inst->buffer,"quit",4)==0)
		{
			perror("strncmp");
			exit(1);
		}
		shm_buff_inst->pid=0;
		memset(shm_buff_inst->buffer,0,SHM_BUFF_SZ);
		sem_v(semid);
	}while(1);

	/*删除共享内存到当前进程地址空间中的映射*/
	if(shmdt(shared_memory)==-1)
	{
		perror("shmdt");
		exit(1);
	}

	/*删除共享内存*/
	if(shmctl(shmid,IPC_RMID,NULL)==-1)
	{
		perror("shmctl");
		exit(1);
	}
	exit(0);
}




猜你喜欢

转载自blog.csdn.net/zm1_1zm/article/details/77193606
今日推荐