⇤ ← Версия 1 от 2020-03-20 21:35:46
944
Комментарий:
|
← Версия 2 от 2020-03-20 22:35:13 ⇥
2420
|
Удаления помечены так. | Добавления помечены так. |
Строка 4: | Строка 4: |
Use [[../16_IPC|lecture]] examples and ''modify them'' | |
Строка 9: | Строка 10: |
1. `killl.c` to send a signal * use `/bin/kill -l | head -16` and edit it to create signal names array * (!) challenge: use `sed s/regexp/replacement/` to eliminate handwork :) (spolier: /* `sed 's/\(.*\)/"\1",/` or even uglier `/bin/kill -l | head -16 | sed 's/\(.*\)/"\1",/;1s/^/char *names={"NONE", /;$s/,$/};/' | tr '\n' ' '`*/ |
1. `proc.c` that waits forever, periodically `printf`s it's PID via [[man2:getpid]] and increased counter. A parameter defines timeout between printfs * `./proc 5` printfs once a 5 seconds (using [[man3:sleep]] something like {{{ 26475: 0 26475: 1 26475: 2 … }}} 1. `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: /* `/bin/kill -l | head -16 | sed 's/\(.*\)/"\1",/` */ ) * much uglier version that does all: `/bin/kill -l | head -16 | sed 's/\(.*\)/"\1",/;1s/^/char *names[]={"NONE", /;$s/,$/};/' | tr '\n' ' '` |
Строка 13: | Строка 24: |
* use [[man3:perror]] if error occurred * print "No such signal" if NAME isn't found and returns 1 instead of 0 * see [[man2:kill]] and [[man3:strsignal]]; you need to search NAME over first '''30''' signal names 1. |
* see [[man2:kill]] * use [[man3: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 1. 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 [[man3:strsignal]]) when catching a signal instead of falling off (still printing messages once a 5 seconds) * note not all signals can be handled {{{#!highlight console $ ./catch 5 INT ABRT SEGV 26775: 0 ^C[Caught: Interrupt]26775: 1 26775: 2 [Caught: Segmentation fault]26775: 3 26775: 4 26775: 5 [Caught: Aborted]26775: 6 26775: 7 Illegal instruction $ }}} {{{#!highlight console $ kill -SEGV 26775 $ kill -ABRT 26775 $ kill -ILL 26772 }}} 1. 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 |
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