#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; if((m = sem_open("mysemaphore",O_CREAT,0644,1)) == SEM_FAILED) { perror("Error"); exit(1); } int shmid; int *shm; sem_init(m,1,1); 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(m); } shmdt(shm); } else { shmid = shmget(2009,SHMSIZE,0666 | IPC_CREAT); shm = (int *)shmat(shmid,0,0); while(1) { sem_wait(m); *shm = *shm+1; printf("val_parent%d\n",*shm); sem_post(m); } shmdt(shm); } return 0; }