#include #include #include #include #include #include #include #include #include #include #include /* Needed for the wait function */ #include /* needed for the fork function */ #include /* needed for the strcat function */ #include #include #include #include #include #define SHMSIZE 27 int main(){ sem_t *m; sem_t *p; if((m = sem_open("mysemaphore",O_CREAT,0644,1)) == SEM_FAILED) { perror("Error"); exit(1); } if((p = sem_open("mysemaphore2",O_CREAT,0644,1)) == SEM_FAILED) { perror("Error"); exit(1); } int shmid; int *shm; sem_init(m,1,1); sem_init(p,1,0); if (fork() == 0) { int i; shmid = shmget(2009,SHMSIZE,0); shm = (int *)shmat(shmid,0,0); *shm =1000; while(1) { sem_wait(m); *shm = *shm -1; printf("val_child=%d\n",*shm); sem_post(p); } shmdt(shm); } else { shmid = shmget(2009,SHMSIZE,0666 | IPC_CREAT); shm = (int *)shmat(shmid,0,0); while(1) { sem_wait(p); *shm = *shm+1; printf("val_parent%d\n",*shm); sem_post(m); } shmdt(shm); } return 0; }