semop issue
Hi All,
Have written a sample program which uses shared memory and semaphores to protect those ...
typedef struct{
int a;
float b;
char c[10];
} locStr;
main(int ctr, char* arr[]){
int shmId, keyId ;
struct sembuf locSem;
locStr *locPtr = NULL;
locSem.sem_num = 0;
locSem.sem_op = -1;
locSem.sem_flg = IPC_NOWAIT;
shmId = shmget(666, sizeof(locStr), IPC_CREAT);
keyId = semget(atoi(arr[1]), 1, 0777|IPC_CREAT);
if(keyId == -1)
printf("MAAN %d and <%s>\n", keyId, strerror(errno));
else
printf("MAAN %d and <%s>\n", keyId, strerror(errno));
printf("The val of the sem is <%d>\n",semctl(keyId, 0, GETVAL));
if(semop(keyId, &locSem, 1) == -1)
printf("MAAN %d and <%s>\n", keyId, strerror(errno));
sleep(5);
locSem.sem_op = -1;
if(semop(keyId, &locSem, 1) == -1)
printf("MAAN %d and <%s>\n", keyId, strerror(errno));
if( shmId > 0)
{
locPtr = (locStr *)shmat(shmId,NULL,0);
}
else
printf("GONE 4 A TOSS\n");
semctl(keyId, 0 , IPC_RMID);
}
The first semop (in bold) operation always returns "Resource temporarily unavailable", though i have just created the semaphore ( i also check using a semctl call that the semapaphore is a 0) and am locking the sem by decreasing the value by 1.Please help
Last edited by pumplove; 01-21-2008 at 07:15 AM.
|