Reputation: 21
arg.val = 1;
if (sem_id = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT) == -1 ){
perror("Creating semaphore failed");
exit(1);
}
else {
printf("Creating a semaphore with ID: %d \n",sem_id);
if (semctl(sem_id, 0, SETVAL, arg) == -1 ) {
perror("Initialization of semaphore failed\n");
exit(1);
}
}
I am trying to create and initialize a semaphore and when I am compiling my program it returns me:
"Initialization of semaphore failed
:Identifier removed
Could you explain me the reason why this happens??
Upvotes: 2
Views: 989
Reputation: 452
The operation == has a higher precedence than the operation =. I believe this means this means that sem_id is getting set to a true value. You should group the operation in the if statement.
if((sem_id = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT)) == -1){
Upvotes: 1
Reputation: 15114
if (sem_id = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT) == -1 )
is parsed (because ==
binds stronger than =
)
if (sem_id = (semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT) == -1) )
you probably want
if ((sem_id = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT)) == -1 )
Upvotes: 1