Semid not available in second process

Vinay L

Registered
Hi All,

I am using Semaphores for one of my application on Mac OS(10.6). I am able to successfully create the semaphores, but I am not able to get the semid in another process. The same works fine on Linux machine. I can see the list of semaphores using ipcs command.


The Code Snippet is as follows:

First_Process:


#include<stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <errno.h>

#define NUMSEMS 1

int main() {

int semid;

semid = semget("SEMTEST", NUMSEMS, 0666 | IPC_CREAT | IPC_EXCL);

printf("Sem id %d\n", semid);

return 0;
}

After running the above code’s executable, semaphore is created. I can verify using ipcs command.


$ipcs –s

Semaphores:

s 212860928 0x00001f68 --ra-ra-ra- offshore staff


After this I run my second process.

Second_Process:



#include<stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <errno.h>


#define NUMSEMS 1

int main() {

int semid;

int i;

semid = semget("SEMTEST", NUMSEMS, 0);

printf("----Semid=%d---\n", semid);

return 0;

}

The output of the second process is -1, i.e it is getting semid as -1 eventhough the semkey, NUMSEMS is same(as provided in first process).

Please let me know the changes to be done, so that we can get semid in Second Process also.



Thanks & Regards,
Vinay
 
Back
Top