IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /*-------------------------------------------------------------*/

Slides:



Advertisements
Similar presentations
Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory.
Advertisements

Operating Systems Lecture 7.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Lab 9 CIS 370 Umass Dartmouth.  A pipe is typically used as a one-way communications channel which couples one related process to another.  UNIX deals.
Synchronization and Deadlocks
CSCC69: Operating Systems
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Operating System Inter-Process Communication. IPC F How does one process communicate with another process? –semaphores -- signal notifies waiting process.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Concurrency. What is Concurrency Ability to execute two operations at the same time Physical concurrency –multiple processors on the same machine –distributing.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Informationsteknologi Friday, November 16, 2007Computer Architecture I - Class 121 Today’s class Operating System Machine Level.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Linux Operating System
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
S -1 Shared Memory. S -2 Motivation Shared memory allows two or more processes to share a given region of memory -- this is the fastest form of IPC because.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
Chapter 71 Deadlock Detection revisited. Chapter 72 Message Passing (see Section 4.5 in Processes Chapter)  A general method used for interprocess communication.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
Operating Systems Processes 1.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems Process Creation
(a) What is the output generated by this program? In fact the output is not uniquely defined, i.e., it is not necessarily the same in each execution. What.
Operating Systems Recitation 4, April th, 2002 Signals.
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
File Systems cs550 Operating Systems David Monismith.
Interprocess Communication
Chapter 7 - Interprocess Communication Patterns
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Introduction Contain two or more CPU share common memory and peripherals. Provide greater system throughput. Multiple processor executing simultaneous.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Operating Systems Review ENCE 360.
Topic 3 (Textbook - Chapter 3) Processes
Chapter 3: Process Concept
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Applied Operating System Concepts
Structure of Processes
Shared Memory Dr. Yingwu Zhu.
File redirection ls > out
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
Interprocess Communication
Inter-Process Communication
Unix System Calls and Posix Threads
Programming Assignment # 2 – Supplementary Discussion
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Process Description and Control in Unix
Process Description and Control in Unix
Presentation transcript:

IPC Programming

Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */ /* Ex1.c - Infinite loop. Compile using: cc -o forever Ex1.c */ /* */ #define TRUE 1 main() { while(TRUE) /* do nothing */ ; }

Process Model Compile using: cc -o forever Ex1.c Execute from the Korn shell (ksh) using: forever & Then, the parent process (ksh) creates a child process (forever): (ksh) | V (forever) This is the process model. In UNIX, a child is created by issuing a fork() system call.

Typical Uses of Fork A server forks off a slave process to do the work so that it can continue to process new requests. (Example: inetd and ftp) (ftp) > (inetd) < (ftp) | | | 2. | | | V (ftpd)

Typical Uses of Fork As shown above: 1.An ftp request is sent to the server (inetd). 2.The server forks off a child process to do the work (ftpd). 3.All subsequent communication is between the slave (ftpd) and the client (ftp). 4.Another client (ftp) can make a request and that request can be processed at the same time as the ongoing request. A process forks off a child to execute a command so that the process can return to its original state after the command is executed. (Example: shell programming (Ex4.c)).

Interprocess Communication 1.Pipes - provide a one-way flow of data. (A notable exception is SysVr4 which provides a two-way flow of data.). 2.Signals - provide a method to interrupt processes based on an external event, such as a timeout. 3.Shared Memory - provides shared data. 4.Semaphores - provides a way to synchronize events and process execution.

Signals To provide reliable network communication, we must have the ability to timeout and retransmit messages that get lost. To implement timeouts, signals can be used. Signals can be sent using kill(), either from the command-line (see kill(1) ), or from within a process (see kill(2) ). To see the relevant manual entries, type: man 1 kill, or man 2 kill. To print the entries: man 2 kill | col -b | lpr –P P1 etc. /* */ /* Ex2.c - Process blocks for 60 seconds. */ /* */ main() { printf("Sleeping for 1 minute \n"); sleep(60); printf("All done \n"); }

Signals To simulate the action of Ex2.c, we can use a signal handler: /* */ /* Ex3.c - Process blocks for 60 seconds (unless interrupted). */ /* */ #include void sighandler(int); /* Signal handling function prototype */ main() { printf("Sleeping for 1 minute \n"); signal(SIGALRM,sighandler); alarm(60);/* Send process a SIGALRM signal in 60 */ pause();/* Block until any signal is received */ printf("All done \n"); }

Signals void sighandler(int signo) { printf("Caught signal %d \n",signo); return; } The statement signal(SIGALRM,sighandler) ; informs the operating system that the sighandler function should be executed if a SIGALRM=14 signal is received for this process. Alarm(60) informs the operating system to send a SIGALRM signal to this process in 60 seconds. Finally, pause() simply blocks until any signal is received.

Pipes A typical example is what happens when you execute the command: $ ls | more In this case: A.the shell (ksh) creates a pipe, B.the shell forks off a child process to execute the "ls" code, and C.the shell forks off another child to execute the "more" code.

How to create pipes in the code? Prototype: int pipe (int * p); Example: int p[2]; if (pipe(p) == -1) { an error occurred, so exit }; Essentially, three values are returned from the pipe system call: 1.the value returned by the function is -1 if an error occured, 2.the value returned in p[0] is the descriptor opened for reading, and 3.the value returned in p[1] is the descriptor opened for writing. (An exception to this rule is UNIX SysVr4 in which the descriptors denote ends of the pipe and can be used for both reading and writing; that is, bidirectional communication.)

Examples: Ex7.c and Ex8.c In Ex7.c, the parent creates a pipe, and forks off a child. The child writes to the pipe ( write(p[1],&x,sizeof x); ), and the parent reads from the pipe ( read(p[0],&x,sizeof x); ). The descriptors are integers, p[0]=3 and p[1]=4 in the example. Whenever a process is executed, three descriptors are automatically assigned to the process: 0 for standard input ( stdin ), 1 for standard output ( stdout ), and 2 for standard error ( stderr ). In UNIX, a user can redirect stdin, stdout and stderr. For example: $ ls > out causes standard out to be redirected to the file " out ". That is, my directory listing is put in the file " out ".

Pipes Pipes provide a stream of bytes with NO message boundaries. For example, the child could write 8 bytes and the parent could read 4 bytes 2 times, etc. This is illustrated by Ex8.c. Limitations: The pipe must be created before forking off the children because the children must inherit the descriptors to be used in the communication. This is one advantage of using "named pipes" instead of pipes.

Quiz Problem 1. How many process are created when the following code is executed? What happens? $ ls | lp -dms processes are created... $ ls > more - 1 process is created... Problem 2. What happens when the following code is executed? What is printed? x=3. int p[2]; int x=3; pipe(p); write(p[1],&x,sizeof(int)); x=4; read(p[0],&x,sizeof(int)); printf("x = %d \n",x);

Signals and Timeouts To provide reliable network communication, we must have the ability to timeout and retransmit messages that get lost. To implement timeouts, signals can be used. Signals can be sent using kill(), either from the command-line (see kill(1) ), or from within a process (see kill(2) ). To see the relevant manual entries, type: man 1 kill, or man 2 kill. To print the entries: man 2 kill | col -b | lp –dP1 Example 5 (Ex5.c) shows how signals and alarms can be used to implement a timeout.

Signals and Timeouts The statement signal(SIGALRM,sighandler) ; informs the operating system that the sighandler function should be executed if a SIGALRM=14 signal is received for this process. alarm(seconds) informs the operating system to send a SIGALRM signal to this process in a fixed number of seconds. Finally, alarm(0) turns the alarm off. In the scanf() is interrupted by a signal, then it returns an error ( -1 ) and sets errrno=EINTR. Thus, we check to see if the read operation was interrupted. If so, it is re-executed in the while loop.

Semaphores Semaphores can be used to synchronize processes (or events within processes) and to control access to shared resources (such as shared memory). Def: A semaphore is a structure (record) that consists of 2 parts: struct semaphore { int count; queue Q; } an integer counter - count, and a queue - Q which is used to hold processes that are blocked on this semaphore.

Semaphores There are 2 semaphore operations: sem_wait() and sem_signal(). semaphore S; sem_wait(S): if (S.count > 0) then S.count = S.count - 1; else block the process in S.Q; sem_signal(S): if (S.Q is non-empty) then wakeup a process in S.Q; else S.count = S.count + 1;

Examples of Semaphores Mutual Exclusion: Several processes want to have exclusive access to a shared resource. semaphore S = 1 ; All processes execute the following code to access the resource: sem_wait(S); (critical section - access the resource) sem_signal(S);

Examples of Semaphores Process Synchronization: Process A must finish executing before process B begins executing. S = 0 A > B Note: the semaphore represents the constraint A before B. semaphore S = 0; Process A: do work... sem_signal(S); Process B: sem_wait(S); do work... In this case, B must be sure that A is finished before starting to work.

Implementation In the actual implementation, we need 4 system calls: 1.semid = sem_create(key, initial_value) ; - to create and initialize a semaphore. 2.sem_wait(semid) ; 3.sem_signal(semid) ; 4.sem_rm(semid) ; - to remove a semaphore from the IPC table. If the process bombs, we need to manually remove the entries from the ipc table: use " ipcs " to view the ipc table, and " ipcrm -s " to remove an entry. Ex9.c, the parent creates a semaphore and initializes it to 1. Then, two child processes are created. They use the semaphore to ensure mutually exclusive access to a shared file called "balance". (see Mutual Exclusion above).

Shared Memory Allows processes to have access to the same address space. Access MUST be synchronized; for example, by using semaphores. Comparison with Pipes: Consider an ftp "get" operation. Pipes require the data to be moved 4 times, whereas shared memory only requires the data to be moved 2 times. Pipes impose synchronization automatically because a process will be blocked if there is nothing to be read. However, processes using shared memory MUST be synchronized by using other means, such as semaphores. Shared Memory Operations: 1.int shminit(key, size) - initialize a shared memory segment. 2.char *shmat(shmid,0,0) - attach to a shared memory segment. 3.void shmkill(shmid) - remove shared memory segment.

IPC Table Clean-Up Example 10. Entries in the IPC table can be removed by using: $ ipcs $ ipcrm -m $ ipcrm -s

Quiz Problem 1. What happens? What will be printed if the error is removed? 5 will be printed if the erroneous line is removed. int shmid; char *shmaddr; int *xp; shmid = shminit (1234, 8); shmaddr = shmat (shmid, 0, 0); *xp = 3;  this line is bad news and will cause a segmentation fault because xp doesn't point to memory allocated to this process. xp = (int *) (shmaddr + 4); *xp = 5; printf("%d \n",*(shmaddr+4)); shmkill(shmid);

Quiz Problem 2. Ditto from above. 5 will be printed. Note that shmaddr[1] is shorthand for *(shmaddr + 1). #include #include "shared_mem.h" main() { int shmid; int *shmaddr; int *xp; shmid = shminit (1234, 8); shmaddr = (int *) shmat (shmid, 0, 0); xp = shmaddr + 1; *xp = 5; printf("%d \n",shmaddr[1]); shmkill(shmid); }