Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- int main() {
- const char *IPC_PATH="/home/jason/ipcfile";
- int *buf;
- int ret, shm, semid;
- key_t memKey, semaphoreKey;
- union semun {
- int val;
- struct semid_ds *buf;
- unsigned short *array;
- } arg;
- arg.val=42;
- /* set up the semaphore, set its value */
- semaphoreKey = ftok(IPC_PATH, 1);
- semid = semget(semaphoreKey, 1, 0660 | IPC_CREAT);
- ret = semctl(semid, 0, SETVAL, arg);
- /* set up the shared memory */
- memKey = ftok(IPC_PATH, 1);
- shm = shmget(memKey, sizeof(int), 0660 | IPC_CREAT);
- buf = shmat(shm, NULL, 0);
- /* Stick the semaphore id in shared mem for the client */
- *buf = semid;
- printf("Semaphore id %i put in shared mem %i for client\n", semid, shm);
- printf("Enter a character to stop.\n");
- getc(stdin);
- /* detach & remove shared mem, remove semaphore */
- ret = shmdt(buf);
- ret = shmctl(shm, IPC_RMID, 0);
- ret = semctl(semid, 0, IPC_RMID);
- printf("Done.\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment