c - Linux System V IPC: Unable to initialize semaphore value -
i have created simple semaphore initialization example demo purpose:
#include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <unistd.h> #include <stdlib.h> int init_semaphore(int semid, int semnum, int initval) { union semun pack; pack.val = initval; return semctl(semid,semnum,setval,pack); }
but getting error:
error: aggregate ‘init_semaphore(int, int, int)::semun pack’ has incomplete type , cannot defined
i not able understand why compiler throwing error. headers included properly.
you have explicitly declare union semun
yourself.
the semctl() function provides variety of semaphore control operations specified cmd. fourth argument optional , depends upon operation requested. if required, of type
union semun
, which application shall explicitly declare:union semun { int val; struct semid_ds *buf; unsigned short *array; } arg;
per linux man page:
this function has 3 or 4 arguments, depending on
cmd
. when there four, fourth has typeunion semun
. calling program must define union follows:union semun { int val; /* value setval */ struct semid_ds *buf; /* buffer ipc_stat, ipc_set */ unsigned short *array; /* array getall, setall */ struct seminfo *__buf; /* buffer ipc_info (linux-specific) */ };