Beej’s Guide to Interprocess Communication
Technical Books
In Progress
My notes on Beej’s Guide to Interprocess Communication by Beej
Notes
Chapter 2. A fork() Primer
- A parent process
fork()‘s a child process (the new one) and the child process gets a copy of the parents’ data. - When a process dies it doesn’t really go away, but instead a small remnant is waiting around for the parent process to pick up. The remnant contains the return value from the child process. So after a parent process
fork()s a child process, it mustwait()orwaitpid()for that child process to exit. This is what allows all remnants of a child to vanish. - The parent can ignore the
SIGCHLDsignal and then it won’t have towait()
- If a chile process dies and has not been
wait()ed on, it will show up inpslisting as<defunct> - Oh interesting, so if a the parent dies before it
wait()s for the child, the child is reparented to theinitprocess (PID 1). - Always ensure your parent process either ignores
SIGCHLDorwait()s for all child itfork()s. wait()waits for whatever child process exists first, usewaitpid()if you want to wait for a specific child
Chapter 3. Signals
- A signal is just a way to interrupt another process.
SIGSTOPis one signal you can send to stop that process and you can continue withSIGCONT.- Many signals are predefined and the process has a default signal handler to deal with it.
SIGINTis the interrupt signal that a process receives when the user hitsCTRL-C. The default handler causes the process to exit.SIGKILLis a signal you can’t add your own handler for. Same with theSIGTSTOP.SIGTERMis the default signal that gets sent from thekillcommandSIGUSR1andSIGUSR2are not reserved and are free to use for whatever.kill()is also the name of the sys call used to sends signalsraise()is another way to raise a signal within the same processsigaction()is used to catch signalswrite()is async signal safe whileprintf()is not.