Skip to content

Instantly share code, notes, and snippets.

@kotaroito
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save kotaroito/9071625 to your computer and use it in GitHub Desktop.

Select an option

Save kotaroito/9071625 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <sys/sem.h>
int main(void)
{
int semid;
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
// semaphoreの集合(ここでは要素数1)を得る
if ((semid = semget(IPC_PRIVATE, 1, 0666)) < 0) {
perror("semget");
}
// semaphore集合の要素番号0に対して、1をセットする
arg.val = 1;
if (semctl(semid, 0, SETVAL, arg) < 0) {
perror("semctl:SETVAL");
}
printf("semaphore:%d\n", semctl(semid, 0, GETVAL));
// semaphoreを削除する
if (semctl(semid, 0, IPC_RMID) < 0) {
perror("semctl:IPC_RMID");
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment