Everyone, I'm here to ask you how can I share an array of semaphores between 2 process??Since I created an array with semget(..) but I can't use shmat(..) on it !
With shared memory segment I usually use shmget() first and then shmat(..) so the child process can access to it.
But How it works with an array of semaphores?
I can't find any similar method able to attach() !!!
Here I Use the semget() to create an array of 5 semaphores:
/* allocate semaphores */
if ((semid = semget(IPC_PRIVATE,5,IPC_CREAT|0666)) == -1) {
printf("shmget() fallita sem id\n");
perror("semget()");
//releaseAll(bufferid,Tid,Did,semid);
exit(-4);
Here in another process I try to attach this array before use it (but i'm using shmat and seems not working..)
sem_t* addr1;
if((addr1=(sem_t*)shmat(semid,NULL,0))==-1){
printf("shmat() fallita sem id\n");
perror("shmat() for content");
exit(-1);// +1 per \0 finale
}
IPC_PRIVATE, use a real key returned byftokinstead, in both processes. See e.g. here.