操作系统 实验31 共享内存通信

1、共享内存通信

源程序:

shmmutexwrite.c

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define BUFFER_SIZE 10
#define sem_name "mysem"
int main()
{
struct Stu
{
char name[10];
int score;
};
int shmid;	
	sem_t*sem;								int score =60,i=1;					
	char buff[BUFFER_SIZE];
	key_t shmkey;							shmkey=ftok("shmmutexread.c",0);
	sem=sem_open(sem_name,O_CREAT,0644,1);

	if (sem == SEM_FAILED) {  
    	perror("sem_open");  
    	exit(EXIT_FAILURE);  
	}
	shmid=shmget(shmkey,1024,0666|IPC_CREAT);
	/*创建IPC键值为shmkey的共享内存,其大小为1024字节,允许读写*/
	if(shmid==-1)
 		printf("creat shm is fail\n");
	struct Stu *
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值