16. Inter-process communications

Make 16_IPC directory. Code must reside here.

Use lecture examples and modify them

  1. 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)

  2. 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
      
  3. 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

  4. 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
         1 $ ./catch 5 INT ABRT SEGV
         2 26775: 0
         3 ^C[Caught: Interrupt]26775: 1
         4 26775: 2
         5 [Caught: Segmentation fault]26775: 3
         6 26775: 4
         7 26775: 5
         8 [Caught: Aborted]26775: 6
         9 26775: 7
        10 Illegal instruction
        11 $
      
         1 $ kill -SEGV 26775
         2 $ kill -ABRT 26775
         3 $ kill -ILL 26772
      
  5. 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:

HSE/ProgrammingOS/Lab_16_IPC (последним исправлял пользователь FrBrGeorge 2020-03-20 22:35:13)