tags : Systems, Operating Systems, Processes
We can make two processes communicate with each other using IPC.
Shared memory (Indirect)
See 3.7. Shared Memory — Computer Systems Fundamentals
As a minor point of terminology, all forms of POSIX IPC, including shared memory, are referred to as objects. In contrast, System V shared memory is called a “segment” for historical purposes.
NOTE: The need to avoid
pointers
in shared memory
- shared memory regions cannot use pointers to refer to other parts of the region. For instance, assume the shared memory contains a linked list with the first node at the beginning of the region. The next node appears 256 bytes later. Using the addresses above, in one process, 0x40000000 must contain a pointer to address 0x40000100; in the other address, the pointer must point to 0x50008100, given the different base address.
- Instead of using
pointers
usepointer arithmetic
and make use of offset count from base addr in the two processes.
- One process creates an area in memory for others to access
- Fast,
no syscalls
needed but does need some kind of synchronization(Concurrency).- For applications that exchange large amounts of data, shared memory is far superior to message-passing techniques like message queues, which require system calls for every data exchange.
POSIX based
shm_open
shm_open()
creates the object but does not specify a sizeftruncate()
resizes the object to be large enough to store one instance of the struct permission.- now the processes maps the shared memory object into memory with mmap, making it a shared region.
- Because the file descriptor returned by
shm_open
is in-memory, thismmap
doesn’t create a file backed memory object as hence is safe to be used in a database environment. (PostgreSQL uses it for dynamic shared memory) - After we
mmap
, we no longer need to refer to the shared memory region using File Descriptors, we can use useful structs to map the mmaped data to.
- Because the file descriptor returned by
shm_unlink
- One could say since we use mmap anyway, why do we even need to use
shm_open
?shm_open
has useful flags which help in deciding what we want to do with the shared region(permissions etc)
- Using
shm_unlink()
provides a safety check that is not guaranteed with creating and mapping arbitrary files. Specifically,shm_unlink()
will not delete the object immediately if any other process also has an open connection (nice!)
Message Passing (Direct)
- Shared memory created in Kernel
- syscalls such as
send
,recieve
etc are involved. - Pipes make use of message passing. (unidirectional)
- See Message Passing
Signals
- Asynchronous, unidirectional communication between processes.
- Process receives a signal
- Processes the signal immediately
- Without finishing the currently running instruction
- We can list all signals in the system with
kill -l
andman 7 signal
Relationship w Interrupts
- The kernel may pass an interrupt as a signal to processes
Signal Handlers
- Signal handler executes in userspace.
Traps
- So signals are things that one process can send to another
- Steps
- A sends signal to B via a syscall
- Kernel intercepts the signal, checks if A can send the signal to B or no and goes on or drops based on that.
- If signal is SIGSTOP/SIGKILL it’ll send the signal regardless if B is blocking or not.
- Kernel sends the signal to B if B is not blocking the signal.
- Kernel sets a trap on the thread(B); adds it to the “pending signal set”
- Next time the thread(B) is scheduled to run, B handles the signal instead of processing itself.
- This means, if the context switch of B never happens, signal never will get processed.
- When handing the signal, B has few options
- Ignore the signal
- Let the defult thing happen
- Use a signal handler