ShareMemory1.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/sem.h>
#define SHMKEY 1234
#define SHMSIZE 4096
#define SEMKEY 1234
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
int sem_p(int id)
{
struct sembuf buf;
int ret;
buf.sem_num = 0;
buf.sem_op = -1;
buf.sem_flg = SEM_UNDO;
ret = semop(id,&buf,1);
if(-1 == ret)
{
perror("semop");
return 1;
}
return 0;
}
int sem_v(int id)
{
struct sembuf buf;
int ret;
buf.sem_num = 0;
buf.sem_op = 1;
buf.sem_flg = SEM_UNDO;
ret = semop(id,&buf,1);
if(-1 == ret)
{
perror("semop");
return 1;
}
return 0;
}
int main()
{
int shmid,semid,ret;
void *shmaddr;
int count = 0,tmp;
union semun union_sem;
shmid = shmget(SHMKEY,SHMSIZE,IPC_CREAT|IPC_EXCL);
if(-1 == shmid)
{
perror("shmget");
exit(1);
}
shmaddr = shmat(shmid,NULL,0);
if((void *)-1 == shmaddr)
{
perror("shmaddr");
exit(1);
}
semid = semget(SEMKEY,1,IPC_CREAT|IPC_EXCL);
if(-1 == semid)
{
perror("semget");
exit(1);
}
union_sem.val = 1;
ret = semctl(semid,0,SETVAL,union_sem);
if(-1 == ret)
{
perror("semctl");
exit(1);
}
*(int *)shmaddr = count;
while(1)
{
sem_p(semid);
tmp = *(int *)shmaddr;
if(tmp>20)
{
sem_v(semid);
break;
}
printf("process 1:count = %d\n",tmp);
sleep(1);
tmp++;
*(int *)shmaddr = tmp;
sem_v(semid);
}
ret = shmdt(shmaddr);
if(-1 == ret)
{
perror("shmdt");
exit(1);
}
sleep(1);
shmctl(shmid,IPC_RMID,NULL);
semctl(semid,0,IPC_RMID);
return 0;
}
ShareMemory2.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/sem.h>
#define SHMKEY 1234
#define SHMSIZE 4096
#define SEMKEY 1234
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
int sem_p(int id)
{
struct sembuf buf;
int ret;
buf.sem_num = 0;
buf.sem_op = -1;
buf.sem_flg = SEM_UNDO;
ret = semop(id,&buf,1);
if(-1 == ret)
{
perror("semop");
return 1;
}
return 0;
}
int sem_v(int id)
{
struct sembuf buf;
int ret;
buf.sem_num = 0;
buf.sem_op = 1;
buf.sem_flg = SEM_UNDO;
ret = semop(id,&buf,1);
if(-1 == ret)
{
perror("semop");
return 1;
}
return 0;
}
int main()
{
int shmid,semid,ret;
void *shmaddr;
int tmp;
union semun union_sem;
shmid = shmget(SHMKEY,SHMSIZE,0);
if(-1 == shmid)
{
perror("shmget");
exit(1);
}
shmaddr = shmat(shmid,NULL,0);
if((void *)-1 == shmaddr)
{
perror("shmaddr");
exit(1);
}
semid = semget(SEMKEY,1,0);
if(-1 == semid)
{
perror("semget");
exit(1);
}
while(1)
{
sem_p(semid);
tmp = *(int *)shmaddr;
if(tmp>20)
{
sem_v(semid);
break;
}
printf("process 2:count = %d\n",tmp);
sleep(1);
tmp++;
*(int *)shmaddr = tmp;
sem_v(semid);
}
ret = shmdt(shmaddr);
if(-1 == ret)
{
perror("shmdt");
exit(1);
}
return 0;
}
执行结果:
process 1:count = 0 process 2:count = 4
process 1:count = 1 process 2:count = 6
process 1:count = 2 process 2:count = 8
process 1:count = 3 process 2:count = 10
process 1:count = 5 process 2:count = 12
process 1:count = 7 process 2:count = 14
process 1:count = 9 process 2:count = 16
process 1:count = 11 process 2:count = 18
process 1:count = 13 process 2:count = 20
process 1:count = 15
process 1:count = 17
process 1:count = 19