19 Apr 2021 - Programming
In the last blog, I have discussed two IPC methods: Pipe and FIFO. In this blog, I will continue this topic and talk about two other IPC methods: Signal and Semaphores.
There are several ways for IPC, including pipe, FIFO, message queue, signal, and socket etc.
Signals are a mechanism used to notify a process that some condition has happened, e.g.,
a user has pressed Ctrl-C
to terminate the process.
A signal can come from hardware or software (e.g., sent from another process).
Once a process receives a signal, it has three options to deal with the signal:
SIGINT
signal (typically generated by pressing Ctrl-C
), the default action is to terminate.SIGKILL
and SIGSTOP
cannot be ignored.SIGKILL
and SIGSTOP
cannot be catched by user-defined functions.A semaphore is a counter that provides access to shared resources for multiple processes. To access shared resources, a process must do the following:
There are two primitives defined for semaphore operations: P
and V
.
The operation P
(also called wait(semaphore)
) decreases the semaphore and V
(also called signal(semaphore)
) increases the semaphore.
Both operations are atomic. For this reason, they are typically implemented inside the kernel.
There is another concept called “Mutex”. Sometimes people are confused with the two concepts and think they are the same. In fact, “Mutex” and “Semaphores” are different concepts. Mutex is a kind of locking mechanism that makes sure only one process or one thread can accquire the resource (i.e., exclusively use the resource) at a time. Others must wait for the user who owns that resource at that time to release the lock. However, semaphore is a kind of signaling mechanism, which means a process or a thread that is waiting on a semaphore can be signaled by any one who releases the shared resources. Therefore, a mutex is always a binary value (0 or 1), but a semaphore can be any non-positive value (much more like a counter).
Reference:
(To be continued…)
«Prev | More About | Next» |
---|---|---|
Natas18 - Improper Session Management Part 1 | Programming | Natas19 - Improper Session Management Part 2 |