22.07.2013 Views

Download File

Download File

Download File

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

main()<br />

{<br />

int shmid;<br />

int semid;<br />

key_t key;<br />

int *child_shm, *parent_shm;<br />

union semun arg;<br />

int i;<br />

struct sembuf operations[1];<br />

int status;<br />

int pid;<br />

/* Create the shared segment.& semaphore. */<br />

if ((shmid = shmget(SHM_KEY, SHMSZ, IPC_CREAT | 0666)) < 0)<br />

{<br />

perror("shmget");<br />

exit(1);<br />

}<br />

if ((semid = semget(SEM_KEY, 1, IPC_CREAT | 0666)) < 0)<br />

{<br />

perror("semget");<br />

exit(1);<br />

}<br />

/*<br />

* Initialize the semaphore to value 1. The idea is multiple processes<br />

* do semop() of -1. So only one is allowed in critical section.<br />

* initialize the shm to 0.<br />

*/<br />

arg.val = 1;<br />

if (semctl(semid, 0, SETVAL, arg) < 0)<br />

{<br />

perror("semctl");<br />

exit(1);<br />

}<br />

NP Lab Manual , RNEC page: 19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!