进程间通信系列 之 共享内存简单实例

 进程间通信系列 之 概述与对比   http://blog.csdn.net/younger_china/article/details/15808685
 进程间通信系列 之 共享内存及其实例   http://blog.csdn.net/younger_china/article/details/15961557
 进程间通信系列 之 共享内存简单实例   http://blog.csdn.net/younger_china/article/details/15991081
 进程间通信系列 之 信号(理论)   http://blog.csdn.net/younger_china/article/details/15976961
 进程间通信系列 之 信号实例   http://blog.csdn.net/younger_china/article/details/15968715
 进程间通信系列 之 信号综合实例   http://blog.csdn.net/younger_china/article/details/15980485
 进程间通信系列 之 命名管道FIFO及其应用实例   http://blog.csdn.net/younger_china/article/details/15808531
 进程间通信系列 之 管道(客户端和服务端通信)   http://blog.csdn.net/younger_china/article/details/15809281
 进程间通信系列 之 信号量详解及编程实例   http://blog.csdn.net/younger_china/article/details/15808531
 进程间通信系列 之 消息队列函数及其范例   http://blog.csdn.net/younger_china/article/details/15503871
 进程间通信系列 之 消息队列应用实例   http://blog.csdn.net/younger_china/article/details/15808501 
 进程间通信系列 之 socket套接字及其实例   http://blog.csdn.net/younger_china/article/details/15809163
 进程间通信系列 之 socket套接字实例   http://blog.csdn.net/younger_china/article/details/15809207


场景:

    两个进程,A进程创建共享内存并读取数据;B进程连接共享内存写入数据。输入 end 结束进程。

应用实例:

头文件:shm_com.h

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define	TEXT_SZ	2048

struct	shared_use_st{
	int	written_by_you;
	char	some_text[TEXT_SZ];
};


消费者程序:shm_customer.h

#include "shm_com.h"

int main(int argc,char **argv)
{
	int	running = 1;
	void *shared_memory = (void *)0;
	struct	shared_use_st *shared_stuff;
	int	shmid;

	srand((unsigned int)getpid());

	shmid = shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);
	if(shmid == -1){
		fprintf(stderr,"shmget failed\n");
		exit(-1);
	}
	
	shared_memory = shmat(shmid,(void *)0,0);
	if(shared_memory == (void *)-1){
		fprintf(stderr,"shmat failed\n");
		exit(-1);
	}

	shared_stuff = (struct shared_use_st *)shared_memory;
	shared_stuff->written_by_you = 0;
	while(running){
		if(shared_stuff->written_by_you){
			printf("You wrote: %s",shared_stuff->some_text);
			sleep(rand()%4);
			shared_stuff->written_by_you = 0;
			if(strncmp(shared_stuff->some_text,"end",3) == 0){
				running = 0;	
			}
		}
	}

	if(shmdt(shared_memory) == -1){
		fprintf(stderr,"shmdt failed!\n");
		exit(-1);
	}

	if(shmctl(shmid,IPC_RMID,0) == -1){
		fprintf(stderr,"shmctl(IPC_RMID) failed!\n");
		exit(-1);
	}

	exit(0);
}


生产者程序:shm_producer.c

#include "shm_com.h"

int main(int argc,char **argv)
{
	int	running = 1;
	void *shared_memory = (void *)0;
	struct	shared_use_st *shared_stuff;
	char	buffer[TEXT_SZ];
	int	shmid;

	shmid = shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);
	if(shmid == -1){
		fprintf(stderr,"Shmget failed!\n");
		exit(-1);
	}

	shared_memory = shmat(shmid,(void *)0,0);
	if(shared_memory == (void *)-1){
		fprintf(stderr,"Shmat failed!\n");
		exit(-1);
	}
	
	printf("Memory attached at %X\n",(int)shared_memory);

	shared_stuff = (struct shared_use_st *)shared_memory;
	while(running){
		while(shared_stuff->written_by_you == 1){
			sleep(1);
			printf("Waiting for client...\n");
		}
		printf("Enter some text: ");
		fgets(buffer,TEXT_SZ,stdin);

		strncpy(shared_stuff->some_text,buffer,TEXT_SZ);
		shared_stuff->written_by_you = 1;

		if(strncmp(buffer,"end",3) == 0){
			running = 0;
		}
	}

	if(shmdt(shared_memory) == -1){
		fprintf(stderr,"Shmdt failed\n");
		exit(-1);
	}

	exit(0);
}

 

转自:http://blog.csdn.net/nowdoit/article/details/7094745


 



posted on 2013-11-13 20:30  YoungerChina  阅读(255)  评论(0编辑  收藏  举报

导航