but wait wait....
Code:
unsigned short int get_semvalue(int sem_id) {
union semun argument;
semctl(sem_id,0,GETALL,argument);
return argument.array[0];
}
if I use this function the program doesn't work.....
why?
this is the complete program
Code:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
union semun {
int val;
struct semid_ds* buf;
unsigned short * array;
struct seminfo* __buf;
};
int get_semvalue(int sem_id) {
union semun argument;
semctl(sem_id,0,GETALL,argument);
return (int)argument.array[0];
}
int main() {
int sem_id;
union semun argument;
unsigned short int values[1];
values[0] = 12;
argument.array = values;
sem_id = semget(IPC_PRIVATE,1,IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR);
semctl(sem_id,0,SETALL,argument);
printf("%d\n",get_semvalue(sem_id)); //WRONG VALUE
union semun arg;
semctl(sem_id,0,GETALL,arg);
printf("<<<<<<%d>>>>>>>\n",arg.array[0]); //RIGHT VALUE
semctl(sem_id,1,IPC_RMID,argument);
return 0;
}
if i use the function it doesn't work, while with the second "manual" method it works.