日期:2014-05-16 浏览次数:20827 次
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
int main(void)
{
int *buff = (int *)malloc(sizeof(int));
pid_t pid;
sem_t *sem_empty = sem_open("empty", O_CREAT, 0644, 1), *sem_full = sem_open("full", O_CREAT, 0644, 0);
pid = fork();
switch (pid)
{
case (-1):
{
printf("Failure!\n");
break;
}
case (0):
{
sem_wait(sem_empty);
*buff = 1234;
sem_post(sem_full);
break;
}
default:
{
sem_wait(sem_full);
printf("%d\n", *buff);
sem_post(sem_empty);
break;
}
}
wait(NULL);
sem_unlink("empty");
sem_unlink("full");
return (0);
}