If I do not define LINUX_ENV macro,everything goes well(especially,the IPC_RMID cmd return 0). but if I define LINUX_ENV(I am running on linux system-ubuntu10.04),the last semctl's IPC_RMID cmd return EINVAL,and says Invalid argument, i.e. the semaphore is not removed. it seems earlier semctl SEM_INFO cmd causes later IPC_RMID cmd return Invalid argument on linux system Where goes wrong in my code?Can anyone help me with this.Thanks in advance.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define LINUX_ENV
#ifdef LINUX_ENV
#define _GNU_SOURCE
#endif
union semun{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
int main(int argc,char* argv[])
{
key_t key;
int semid;
int nsems;
int proj_id;
struct semid_ds semid_ds_buf;
union semun semun_buf;
struct seminfo* sem_info;
proj_id=rand();
key=ftok(argv[0],proj_id);
nsems=2;
semid=semget(key,nsems,IPC_CREAT|IPC_EXCL|0666);
if(semid==-1)
{
perror("semget failed");
return -1;
}else
{
printf("key(%s,%d) semaphore id:%d\n",argv[0],proj_id,semid);
}
semun_buf.buf=&semid_ds_buf;
//nsems is ignored
semctl(semid,0,IPC_STAT,&semid_ds_buf);
printf("current number of semaphores:%lu\n",semid_ds_buf.sem_nsems);
#ifdef LINUX_ENV
if(semctl(semid,0,SEM_INFO,&semun_buf)==-1)
{
printf("semctl SEM_INFO failed");
return -2;
}
sem_info=(struct seminfo*)(&semid_ds_buf);
printf("max entries in semaphore map:%d\n",sem_info->semmap);
#endif
if(semctl(semid,0,IPC_RMID,0)==-1)
{
perror("semctl IPC-RMID failed");
return -3;
}
return 0;
}