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字节