Presentation is loading. Please wait.

Presentation is loading. Please wait.

Netzwerkprogrammierung – Network Programming

Similar presentations


Presentation on theme: "Netzwerkprogrammierung – Network Programming"— Presentation transcript:

1 Netzwerkprogrammierung – Network Programming
Interprocess Communication Signals and Pipes Madis Rumming Alexander Sczyrba Stefan Janssen

2 Interprocess communication
Parent and child processes can communicate via: signals pipes shared memory

3 Signals a technique to pass messages to a running process e.g. a division by zero causes signal SIGFPE processes can react to a signal by: ignoring it perfoming a default action (SIGFPE → terminate) calling a custom made function (signal handler) when signal is triggered

4 POSIX Signals (excerpt)
name value default meaning HUP 1 T Hangup detected INT 2 Interrupt from Keyboard ILL 4 Illegal Instruction FPE 8 T C Floating point exception KILL 9 T ! Termination signal PIPE 13 Write pipe with no readers TERM 15 CHLD 17 I Child terminated CONT 18 R Continue if stopped STOP 19 S ! Stop process T: terminate, C: continue, ! : can't be caught, R: resume, S : stop, C :core dump

5 os.killpg() or negated pid in os.kill()
In Python os.kill() sends a signal to another process: os.kill(pid, sig) sends signal sig to process pid sig is either the signals int number or its symbolic name (signal.SIG*) no return value, but error state if pid does not exist. Signalling a process group: os.killpg() or negated pid in os.kill()

6 signal handler must be registered signal.signal(signalnum, handler)
Catching signals signal handler is a function with exactly two arguments: 1. signal number, 2. interrupted stack frame signal handler must be registered signal.signal(signalnum, handler) signalnum: integer value of the signal handler: function to be called (signal handler) Note: do not use computationally demanding calls in a signal handler! C-Lib nicht re-entrant, daher Problem bei allem, was malloc() benutzt, während Handler läuft, da dann beim Rücksprung aus dem Handler mglw. Adressen im Speicher nicht mehr stimmen und es dann einen Coredump gibt.

7 How to terminate the above program?
Hands on! Write a Python program which infinitly prints „I'm sleeping“ every 5 seconds. How to terminate the above program? Extend your program such that it can react to „keyboard interrupt signals“ (SIGINT). It shall terminate after the THIRD catched SIGINT signals. #!/usr/bin/env python import time, sys, signal interrupted = 0 def sigint_handler(sig, frame): global interrupted interrupted += 1 print("Don't interrupt me!") print("You've already interrupted me %d times" % interrupted) if interrupted == 3: sys.exit(0) signal.signal(signal.SIGINT, sigint_handler) while(1): print("I'm sleeping!") time.sleep(5) import time, signal while(not interrupted == 3):

8 Pipes Original form of Unix Interprocess Communication (IPC) [1973] useful for many scenaries Short comming: no identifier, thus only usable by related processes. revised 1983, by introducing FIFOs (named pipes) Pipes and FIFOs are used by the common read and write file operation functions

9 Pipes in Python create a pipe via os.pipe() inpipe, outpipe = os.pipe() write to a pipe: os.write(outpipe, <MESSAGE>) read from a pipe: os.read(inpipe, <BUFFERSIZE>) fdHandle = os.fdopen(inpipe) line = fdHandle.readline() # a single line lines = fdHandle.readlines() # all lines

10 Pipes in Python pipes are buffered, always! → no real-time output. enforcing real time: os.read(inpipe, 1) remember to close your pipes! Pipes might be replaced by os.dup2(): stdin = sys.stdin.fileno() stdout = sys.stdout.fileno() parentStdin, childStdout = os.pipe() childStdin, parentStdout = os.pipe() if(os.fork() ==0): # child process os.close(parentStdin) os.close(parentStdout) os.dup2(childStdin, stdin) os.dup2(childStdout, stdout) print("Hallo Elternprozess!")

11 Named Pipes in Python Arbitrary (i.e. unrelated) processes communicate via named pipes. Named pipes are handled like files by the OS. if not os.path.exists(pipe_name): os.mkfifo(pipe_name) pipeout = os.open(pipe_name, os.O_WRONLY) os.write(pipeout, 'Number %03d\n' % counter) pipein = open(pipe_name, 'r') line = pipein.readline()

12 pipein, pipeout = os.pipe()
Opens a pair of file descriptors, which are connected by a pipe. Process WH RH Process Kernel Pipe

13 os.pipe() usually in combination with a fork()
parent opens first file handle and closes the other child acts reversly: closes first file handle and opens the other Parent WH RH Child WH RH fork() Prozess Kernel pipe

14 Hands on! Write a program, which opens a pipe, forks,
sends a message from the parent to the child, which ultimately prints the message. Parent WH RH WH RH Child fork() #!/usr/bin/env python import os, sys pipein, pipeout = os.pipe() if(os.fork()==0): os.close(pipeout) incoming = os.fdopen(pipein) while(1): line = incoming.readline().rstrip() if(line): print(line) else: sys.exit(0) os.close(pipein) os.write(pipeout, "Kluges Kind!\n") os.write(pipeout, "Das naechste bitte...") os.wait() Process Kernel pipe

15 Bidirectional Pipes presented pipes were half-duplex or unidirectional. passing information in one direction bi-directional communication requires two pipes → one per direction

16 Hands on! Write a program, that forks and can bi-directionally communicate between parent and child. Do so by: creating two pipes A and B os.fork() in parent close reader of pipe A close writer of pipe B in child close writer of pipe A close reader of pipe B print messages from the parent in the child and vice versa!

17 Hands on! WH RH WH RH pipe #2 pipe #1 Parent Child fork() process
kernel pipe #2 #!/usr/bin/env python import os, sys, time pipe1in, pipe1out = os.pipe() pipe2in, pipe2out = os.pipe() if(os.fork()==0): os.close(pipe1out) os.close(pipe2in) fh = os.fdopen(pipe1in) line = fh.readline().rstrip() print("Kind bekommt gesagt: "+line) os.write(pipe2out, "KUSS") os.close(pipe2out) sys.exit(0) else: os.close(pipe1in) print("Elter sagt: Kluges Kind!") os.write(pipe1out, "Kluges Kind!\n") print("Elter hat bekommen: "+os.read(pipe2in, 100)) os.wait() pipe #1


Download ppt "Netzwerkprogrammierung – Network Programming"

Similar presentations


Ads by Google