SIG_SETMASK means to "mask out" -- or block -- any signal in the given set. ie, "I'm NOT
interested in the following signals..."
Line 9 tells the kernel that you want to block every signal that is blockable, so in other words, you
don't want to receive any signal.
Presumably, it was possible for the program to receive a signal that would cause sigreceived to
get set even before line 9 executed, so line 10 checks to see if a signal was already received.
If it wasn't, then line 11 tells the kernel to suspend the process until it receives a signal that it
is masked in the parameter. The only signal not masked in maskmost is SIGUSR1. Therefore,
sigsuspend() waits until SIGUSR1 is received.
Line 12 then restores the process' signal mask to what it was before line 9 changed it.
More info can be found if you man sigprocmask and sigsuspend.
Thanks jsmith! I still have this doubt ... SIG_SETMASK does both block & populate the out prameter ? i.e.
sigprocmask(SIG_SETMASK, &maskall, &maskold);
and if this is done , it sets the new mask and unblocks the pending signals?
sigprocmask(SIG_SETMASK, &maskold, NULL);
And why is this required ? Since i understand, sigsuspend resets the signal mask as originally it was.
Early on all signals are blocked , then all signals except sigusr1 is blocked, on receipt the state is restored to the last configuration i.e all signals blocked. So we explicitly need to restore the old state.
No, both can write and both can read. Think of it as a two-lane highway. Cars (data)
can flow in one directly only per lane, but there are two lanes, so you can achieve
bidirectional data.
I am trying to understand which IPC mechanism should be preferred over another. I have read up a deal of it on Beej's guide & a book i picked up recently (UNIX Systems Programming: Communication, Concurrency and Threads).
While each of these books do a wonderful job of explaining of how to setup & use each of the IPC mechanisms; they do not touch on performance implications of the same.The interwebs is not helping either.
Well it's obvious that a mechanism like socket will be used if the system is distributed , but what is the case when the processes reside on the same system?
I.e Mainly , i am looking for advantages/disadvantages between the following mechanisms :
- Named Pipes
- Message Queues
- Shared Memory
- Local Sockets
I would love it if you guys can give some practical scenarios where you have encountered them & any reason why one should be preferred over another. All examples i have come across are typical process A writes int and B reads it.
Also apart from access to shared memory/hardware resources , is there any other reason where semaphores are used?
(System V) message queues should be avoided simply because whereas everything
else in UN*X land is a file descriptor, message queues are accessed through message
queue IDs and not file descriptors.
I tend to avoid shared memory and named pipes because both have state that is
external to the program (ie, if the program dies, the state remains).
I prefer sockets because I can use all the "standard" system calls on them -- select, read,
write, close, etc.