Intended Schedule V. IPC: Inter-Process Communication Date Lecture Hand out Submission 0 20.04. Introduction to Operating Systems Course registration 1 27.04. Systems Programming using C (File Subsystem) 1. Assignment 2 04.05. Systems Programming using C (Process Control) 2. Assignment 1. Assignment 3 11.05. Process Scheduling 3. Assignment 2. Assignment 4 18.05. Process Synchronization 4. Assignment 3. Assignment 5 25.05. Inter Process Communication 5. Assignment 4. Assignment 6 01.06. Pfingstmontag 6. Assignment 5. Assignment 7 08.06. Input / Output 7. Assignment 6. Assignment 8 15.06. Memory Management 8. Assignment 7. Assignment 9 22.06. 9. Assignment 8. Assignment Filesystems 10 29.06. 10. Assignment 9. Assignment 11 06.07. Special subject: Transactional Memory 10. Assignment 12 13.07. Special subject: XQuery your Filesystem 13 20.07. Wrap up session 27.07. First examination date 12.10. Second examination date 1 2 Synchronization vs. Communication Synchronization Synchronization vs. Communication OS primitives used for agreement on a point in time between twp processes little or no additional information exchanged Communication often requires synchronization as well main purpose lies in exchange of (sometimes significant amounts of) data IPC may often be used for both 3 4
IPC: Overview PL vs. OS Functionality Synchronization Semaphores,... Signals Communication memory-based shared memory message-based synchronous vs. asynchronous uni- vs. bidiretional buffered vs. unbuffered 5 Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term 2009 Glatz 2005 6 Mechanism for processes to communicate and to synchronize their actions Message system processes communicate with each other without resorting to shared variables IPC facility provides two operations: send(message) message size fixed or variable receive(message) If P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive Implementation of communication link physical (e.g., shared memory, hardware bus) logical (e.g., logical properties) How are links established? Can a link be associated with more than two processes? How many links can there be between every pair of communicating processes? What is the capacity of a link? Is the size of a message that the link can accommodate fixed or variable? Is a link unidirectional or bi-directional? Silberschatz, Galvin and Gagne 2005 7 Silberschatz, Galvin and Gagne 2005 8
indirect direct Processes must name each other explicitly: send (P, message) send a message to process P receive(q, message) receive a message from process Q Properties of communication link Links are established automatically A link is associated with exactly one pair of communicating processes Between each pair there exists exactly one link The link may be unidirectional, but is usually bi-directional Silberschatz, Galvin and Gagne 2005 9 Silberschatz, Galvin and Gagne 2005 10 Messages are directed and received from mailboxes (also referred to as ports) Each mailbox has a unique id Processes can communicate only if they share a mailbox Properties of communication link Link established only if processes share a common mailbox A link may be associated with many processes Each pair of processes may share several communication links Link may be unidirectional or bi-directional Operations create a new mailbox send and receive messages through mailbox destroy a mailbox Primitives are defined as: send(a, message) send a message to mailbox A receive(a, message) receive a message from mailbox A Silberschatz, Galvin and Gagne 2005 11 Silberschatz, Galvin and Gagne 2005 12
Mailbox sharing P 1, P 2, and P 3 share mailbox A P 1, sends; P 2 and P 3 receive Who gets the message? Solutions Allow a link to be associated with at most two processes Allow only one process at a time to execute a receive operation Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was. Message passing may be either blocking or non-blocking Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available Non-blocking is considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null Silberschatz, Galvin and Gagne 2005 13 Silberschatz, Galvin and Gagne 2005 14 Queue of messages attached to the link; implemented in one of three ways 1. Zero capacity 0 messages Sender must wait for receiver (rendezvous) 2. Bounded capacity finite length of n messages Sender must wait if link full 3. Unbounded capacity infinite length Sender never waits Message-Based Communication Silberschatz, Galvin and Gagne 2005 15 16
Unix Pipes Command Chaining Two variants: unnamed pipes named pipes Unidirectional (exception: streams) Synchronization via blocking writer blocks when pipe is full reader blocks when pipe is empty API: like files Main use in Unix: Command chaining Standard-In and -Out of a single process Command chaining 17 18 Unnamed Pipes Pipes: Interface unidirectional link between two processes pipe created by parent (or ancestor) process file descriptors for write-(in) and read-(out) end of pipe inherited from parent process Use: whenever processes in a joint hierarchy need to communicate Advantage: no cleanup on process termination necessary 19 Glatz 2005 20
Example: Producer -- Consumer void main() { int fds[2]; pipe(fds); if (fork() == 0) { dup2(fds[1], 1); close(fds[0]); printf("kind 1 bei execlp\n"); execlp("./erzeuger", "erzeuger", 0); else if (fork() == 0) { dup2(fds[0], 0); close(fds[1]); printf("kind 2 bei execlp\n"); execlp("./verbraucher", "verbraucher", 0); else { close(fds[0]); close(fds[1]); wait(0); wait(0); Producer Child Process #include <stdio.h> #define BUFLEN 80 #define NSEKUNDEN 3 void main (int argc, char* argv[]) { char* text; char buffer [BUFLEN]; int anzsaetze = 25; int satznr; int anz; /*printf("kind 1 meldet sich"); funktioniert nicht! */ text = "Kind 1 meldet sich\n"; write(1,text,18); /*Text in Pipe schreiben*/ for (satznr = 1; satznr <= anzsaetze; satznr++) { anz=sprintf(buffer, "==> Erzeuger %d Satznr. %6d ##",getpid(),satznr); write(1, buffer, anz); /*write(1,"ende\0",5); Wozu dient diese Zeile?*/ sleep(nsekunden);/* -> zur Beobachtung */ close(1); exit(0); Glatz 2005 21 Glatz 2005 22 #define BUFLEN 30 #define EOF 0 #define NSEKUNDEN 5 void main() { char buffer [BUFLEN+1]; Consumer Child Process buffer [BUFLEN] = 0; /*String abschliessen mit NULL */ printf("kind 2 meldet sich\n"); while (read(0, buffer, BUFLEN)!= EOF) { printf("verbraucher %d : %s \n",getpid(),buffer); sleep(nsekunden);/* -> zur Beobachtung */ printf("\nverbraucher hat ganze Pipe geleert\n"); exit(0); Named Pipes (FIFOs) Name appears as a special file in the filesystem (yet, no data is ever stored on disk!) unidirectional link between two processes created by arbitrary process carries standard file permissions every process knowing the name can connect (permissions allowing) Use: whenever arbitrary processes need to exchange data disadvantage: cleanup upon process termination necessary Glatz 2005 23 24
Named Pipes: Interface #include <stdio.h> #include <fcntl.h> int main (int argc, char* argv[]) { char * text = "Hallo da!\n"; int fd, wr; Example: Creator Process (Producer) if (mkfifo ("./pipe", 0700) == 0) printf("process 1: named pipe created successfully\n"); fd = open ("./pipe", O_WRONLY); wr = write(fd, text, strlen(text)); if (wr > 0) printf ("process 1: %d byte written to pipe.\n", wr); close(fd); printf ("process 1: all set - good bye!\n"); exit(0); Glatz 2005 25 Glatz 2005 26 Example: Consumer Process Bi-directional Communication #include <stdio.h> #include <fcntl.h> #define BUFLEN 80 int main (int argc, char* argv[]) { char* text; char buffer [BUFLEN]; int fd, cnt=0; fd = open ("./pipe", O_RDONLY); do { cnt = read(fd, buffer, BUFLEN); buffer[cnt]=0; if (cnt > 0) printf ("process 2: %d byte read from pipe, text is: %s", cnt, buffer); else printf("process 2: EOF read from pipe\n"); while (cnt > 0); close (fd); unlink("./pipe"); printf ("process 2: all set - good bye\n"); exit(0);... needs two pipes, one for each direction Glatz 2005 27 Glatz 2005 28
Client-Server Operation Client-Server Messaging w/ Feedback Glatz 2005 29 Glatz 2005 30 POSIX Message Queues (1) POSIX Message Queues (2) Glatz 2005 31 Glatz 2005 32
Unix Signals... can be considered a very basic communication primitive (... or as a synchronization primitive as well) A process can send one of a few signals to another. There are terminating and non-terminating signals i.e., the process receiving the signal may be (abnormally) ended A process may install handlers (pointers to functions to be called) for certain signals. Unix systems generate a couple of signals automatically resume after stop, child process changes state, wants to generate output while in background,... 33 Some Standard Signals (see <signal.h>) No Name Default Action Description 1 SIGHUP terminate process terminal line hangup 2 SIGINT terminate process interrupt program 3 SIGQUIT create core image quit program 4 SIGILL create core image illegal instruction 5 SIGTRAP create core image trace trap 6 SIGABRT create core image abort program (formerly SIGIOT) 7 SIGEMT create core image emulate instruction executed 8 SIGFPE create core image floating-point exception 9 SIGKILL terminate process kill program 10 SIGBUS create core image bus error 11 SIGSEGV create core image segmentation violation 12 SIGSYS create core image non-existent system call invoked 13 SIGPIPE terminate process write on a pipe with no reader 14 SIGALRM terminate process real-time timer expired 15 SIGTERM terminate process software termination signal 16 SIGURG discard signal urgent condition present on socket 17 SIGSTOP stop process stop (cannot be caught or ignored) 18 SIGTSTP stop process stop signal generated from keyboard 19 SIGCONT discard signal continue after stop 20 SIGCHLD discard signal child status has changed 21 SIGTTIN stop process background read attempted from control terminal 22 SIGTTOU stop process background write attempted to control terminal 23 SIGIO discard signal I/O is possible on a descriptor (see fcntl(2)) [more on signals: see Assignment 5] 34 Shared Memory Memory-Based Communication OS offers functionality to allocate shared memory part of the virtual memory of multiple processes extremely fast communication, since zero overhead involved mutually exclusive access not guaranteed extra mechanism, such as semaphore, needed (see last lecture) Unix, Windows: typically realized via memory-mapped files application example: see last lecture 35 36
POSIX Shared Memory Shared Memory & Virt. Address Space Glatz 2005 37 Glatz 2005 38 Example: POSIX Shared Memory #include <sys/mman.h> int shmd; void *shmp; // Shared Memory anlegen mit Schreib/Lesezugriffsrechten für Benutzer shmd = shm_open ( /shmem 1, O_CREAT O_RDWR,S_IRWXU); // Grösse festlegen auf 16384 Byte if (ftruncate(shmd, 16384) < 0) { /* Fehlerbehandlung */ ; // Ganzen Bereich in eigenen Prozessadressraum einblenden auf Adresse 0 shmp = mmap(0, 16384, PROT_READ PROT_WRITE, MAP_SHARED, shmd, 0); if (shmp == MAP_FAILED) { /* Fehlerbehandlung */ ; // Erhaltene Startadresse zur Kontrolle ausgeben printf( Startadresse = %x, (unsigned long)shmp); // Shared Memory benutzen.. // Shared Memory Einblendung aufheben close(shmd); // Shared Memory Object vernichten shm_unlink( /shmem 1 ); Process Communication Across Machines Glatz 2005 39 40
Berkeley Sockets as Comm. Endpoints A socket is defined as an endpoint for communication Concatenation of IP address and port The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 Communication consists between a pair of sockets Silberschatz, Galvin and Gagne 2005 41 Glatz 2005 42 Socket Functions Sockets: Principle of Operation socket() socket() bind() listen() connect() accept() read() write() write() read() close() close() Glatz 2005 43 Glatz 2005 44
Socket Addresses struct in_addr { u_long s_addr; /* 32 Bit Hostadresse */ struct sockaddr_in { short sin_family; /* 16 Bit Adressfamilientyp: AF_INET(=2) */ u_short sin_port; /* 16 Bit Portnummer */ struct in_addr sin_addr; /* 32 Bit Hostadresse */ char sin_zero[8]; /* Unbenutzt */ ; struct sockaddr_un { short sun_family; /* 16 Bit Adressfamilientyp: AF_UNIX(=1) */ char sun_path[108]; /* Pfad */ ; More Communication Primitives [Sockets-Programmbeispiel: siehe Glatz] Glatz 2005 45 46 Remote Procedure Calls (RPC) RPC Execution a.k.a. Remote Method Invocation (RMI) allow a process an machine A (client) to invoke a procedure/function/method on machine B (server) also involves data communication (e.g., input parameters & return values) depending on the type of that data, marshalling and unmarshalling may be necessary Depending on system and language environments, a lot of such mechanisms can be found if those environments are even different on both ends, crossplatform rpc/rmi becomes a major challenge... 47 48
Marshalling Parameters Monitors, Rendezvous, etc. We have come across those in the previous lecture, as highlevel means for synchronization. They may also be considered (language-dependent) communication tools. They need to be implemented using more low-level mechanisms, such as the ones we ve mentioned here. 49 50 Intended Schedule Date Lecture Hand out Submission 0 20.04. Introduction to Operating Systems Course registration 1 27.04. Systems Programming using C (File Subsystem) 1. Assignment 2 04.05. Systems Programming using C (Process Control) 2. Assignment 1. Assignment 3 11.05. Process Scheduling 3. Assignment 2. Assignment 4 18.05. Process Synchronization 4. Assignment 3. Assignment 5 25.05. Inter Process Communication 5. Assignment 4. Assignment 6 01.06. Pfingstmontag 6. Assignment 5. Assignment 7 08.06. Input / Output 7. Assignment 6. Assignment 8 15.06. Memory Management 8. Assignment 7. Assignment 9 22.06. 9. Assignment 8. Assignment Filesystems 10 29.06. 10. Assignment 9. Assignment 11 06.07. Special subject: Transactional Memory 10. Assignment 12 13.07. Special subject: XQuery your Filesystem 13 20.07. Wrap up session 27.07. First examination date 12.10. Second examination date VI. Input/Output 51 52