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

 进程间通信系列 之 概述与对比    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);
}

转自:Unix/Linux 共享内存简单实例 (POSIX)_NowDoIT的博客-CSDN博客


 


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YoungerChina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值