16. Inter-process communications
Make 16_IPC directory. Code must reside here.
Use lecture examples and modify them
- processes
ps -ef / pstree / ps axu (BSD-style format) / ps xf (BSD style own process only)
pidof program
ls /proc
kill proc / kill -STOP proc / kill -HUP proc (use two terminals)
proc.c that waits forever, periodically printfs it's PID via getpid and increased counter. A parameter defines timeout between printfs
./proc 5 printfs once a 5 seconds (using sleep something like
26475: 0 26475: 1 26475: 2 …
killn.c to send a signal
use /bin/kill -l | head -16 and edit it's output to create signal names array
challenge: use sed s/regexp/replacement/ to eliminate handwork (spoiler here:
)- much uglier version that does all:
/bin/kill -l | head -16 | sed 's/\(.*\)/"\1",/;1s/^/char *names[]={"NONE", /;$s/,$/};/' | tr '\n' ' '
./killn PID NAME sends PID process signal NAME
see kill
use perror if error occurred
- print "No such signal" if NAME isn't found and returns 1 instead of 0
try to ./killn running proc 4, non-existent process, foreign process
Copy proc.c to catch.c and modify it, adding a signal handler
./catch 5 SIGNAL_NAME1 SIGNAL_NAME2 … should print corresponded signals' description (via strsignal) when catching a signal instead of falling off (still printing messages once a 5 seconds)
- note not all signals can be handled
Join catch.c with child-control program from lecture, name the result childctl.c.
./childctl timeout signalQ signal1 … signaln should:
print a message once in timeout seconds
catch signal1 … signaln and print message
peacefully exit when got signalQ
H/W
Modify last program to:
Exit after getting signalQ three times
- Check every syscall return values on error state