1 Interprocess Communication CS 241 April 2, 2012 University of Illinois.

Slides:



Advertisements
Similar presentations
Processes Management.
Advertisements

IPC (Interprocess Communication)
Operating Systems Lecture 7.
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
Dr. Kalpakis CMSC 421, Operating Systems. Fall Processes.
1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
Abhinav Kamra Computer Science, Columbia University 4.1 Operating System Concepts Silberschatz, Galvin and Gagne  2002 Chapter 4: Processes Process Concept.
A. Frank - P. Weisberg Operating Systems Inter-Process Communication (IPC)
Adapted from slides ©2005 Silberschatz, Galvin, and Gagne Lecture 4: Processes.
Chapter 3: Processes.
Chapter 3 Processes.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
1/30/2004CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Chapter 3: Processes. Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server.
02/02/2004CSCI 315 Operating Systems Design1 Interprocesses Communication Notice: The slides for this lecture have been largely based on those accompanying.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
02/01/2010CSCI 315 Operating Systems Design1 Interprocess Communication Notice: The slides for this lecture have been largely based on those accompanying.
1/26/2007CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Inter Process Communication. Introduction Traditionally describe mechanism for message passing between different processes that are running on some operating.
Interprocess Communication. Process Concepts Last class.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems.
Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server.
Silberschatz, Galvin and Gagne  Operating System Concepts Cooperating Processes Independent process cannot affect or be affected by the execution.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.
Chapter 3 Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Jan 19, 2005 Chapter 3: Processes Process Concept.
AE4B33OSS Chapter 3: Processes. 3.2Silberschatz, Galvin and Gagne ©2005AE4B33OSS Chapter 3: Processes Process Concept Process Scheduling Operations on.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Outline n Process Concept n Process.
Chapter 3: Processes (6 th edition chap 4). 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Chapter 71 Deadlock Detection revisited. Chapter 72 Message Passing (see Section 4.5 in Processes Chapter)  A general method used for interprocess communication.
Chapter 3: Processes. 3.2CSCI 380 Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication.
1 11/1/2015 Chapter 4: Processes l Process Concept l Process Scheduling l Operations on Processes l Cooperating Processes l Interprocess Communication.
Share Memory Program Example int array_size=1000 int global_array[array_size] main(argc, argv) { int nprocs=4; m_set_procs(nprocs); /* prepare to launch.
3.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Interprocess Communication Processes within a system may be.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Processes. Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
CS212: OPERATING SYSTEM Lecture 2: Process 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Chapter 3 Process Scheduling Bernard Chen Spring 2007.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-4 Process Communication Department of Computer Science and Software.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 10 Processes II Read.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 3 Operating Systems.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Chapter 3: Process-Concept. Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
Operating Systems Yasir Kiani. 13-Sep Agenda for Today Review of previous lecture Interprocess communication (IPC) and process synchronization UNIX/Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
3.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Overview: Process Concept Process Scheduling Operations on Processes.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Jan 19, 2005 Chapter 3: Processes Process Concept.
Chapter 3: Processes-Concept. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes-Concept Overview Process Scheduling.
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
InterProcess Communication. Interprocess Communication Processes within a system may be independent or cooperating Cooperating process can affect or be.
 Process Concept  Process Scheduling  Operations on Processes  Cooperating Processes  Interprocess Communication  Communication in Client-Server.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally INTERPROCESS COMMUNICATION AND SYNCHRONIZATION SYNCHRONIZATION.
Chapter 3: Process-Concept. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 3: Process-Concept Process Concept Process Scheduling.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Termination Process executes last statement and asks the operating.
4.1 Operating System Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
Gokul Kishan CS8 1 Inter-Process Communication (IPC)
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos.
Processes Overview: Process Concept Process Scheduling
Applied Operating System Concepts
Chapter 4: Processes Process Concept Process Scheduling
Typically for using the shared memory the processes should:
Processes August 10, 2019 OS:Processes.
Presentation transcript:

1 Interprocess Communication CS 241 April 2, 2012 University of Illinois

2 Interprocess Communciation What is IPC? Mechanisms to transfer data between processes Why is it needed? Not all important procedures can be easily built in a single process

3 Two kinds of IPC “Mind meld”“Intermediary” Process OS Process Shared address space Shared memory Memory mapped files Message transported by OS from one address space to another Files Pipes FIFOs Today

4 Pipes

5 Google Chrome architecture (figure borrowed from Google) Separate processes for browser tabs to protect the overall application from bugs and glitches in the rendering engine Restricted access from each rendering engine process to others and to the rest of the system

6 Google Chrome architecture (figure borrowed from Google) A named pipe is allocated for each renderer process for communication with the browser process Pipes are used in asynchronous mode to ensure that neither end is blocked waiting for the other

7 Process A msg Process B Operating System private address space photo: theilr

8 UNIX Pipes #include int pipe(int fildes[2]); Create a message pipe  Anything can be written to the pipe, and read from the other end  Data is received in the order it was sent  OS enforces mutual exclusion: only one process at a time  Accessed by a file descriptor, like an ordinary file  Processes sharing the pipe must have same parent process Returns a pair of file descriptors  fildes[0] is the read end of the pipe  fildes[1] is the write end of the pipe

9 Pipe example #include #include #include #include #include int main(void) {... }

10 Pipe example int main(void) { int pfds[2]; char buf[30]; pipe(pfds); if (!fork()) { printf(" CHILD: writing to pipe\n"); write(pfds[1], "test", 5); printf(" CHILD: exiting\n"); } else { printf("PARENT: reading from pipe\n"); read(pfds[0], buf, 5); printf("PARENT: read \"%s\"\n", buf); wait(NULL); } return 0; } pfds[0] : read end of pipe pfds[1] : write end of pipe

11 A pipe dream ls | wc -l Can we implement a command-line pipe with pipe() ? How do we attach the stdout of ls to the stdin of wc ?

12 Duplicating a file descriptor #include int dup(int oldfd); Create a copy of an open file descriptor Put new copy in first unused file descriptor Returns: Return value  0 : Success. Returns new file descriptor Return value = -1: Error. Check value of errno Parameters: oldfd : the open file descriptor to be duplicated

13 Duplicating a file descriptor #include int dup2(int oldfd, int newfd); Create a copy of an open file descriptor Put new copy in specified location...after closing newfd, if it was open Returns: Return value  0 : Success. Returns new file descriptor Return value = -1: Error. Check value of errno Parameters: oldfd : the open file descriptor to be duplicated

14 Pipe dream come true: ls | wc –l #include #include #include int main(void) { int pfds[2]; pipe(pfds); if (!fork()) { close(1); /* close stdout */ dup(pfds[1]); /* make stdout pfds[1] */ close(pfds[0]); /* don't need this */ execlp("ls", "ls", NULL); } else { close(0); /* close stdin */ dup(pfds[0]); /* make stdin pfds[0] */ close(pfds[1]); /* don't need this */ execlp("wc", "wc", "-l", NULL); } return 0; } Run demo

15 Pipe dream come true: ls | wc –l 0 Parent file descriptor table stdin 1 stdout 2 3 pfds[0] pfds[1] pipe pipe(pfds);

16 Pipe dream come true: ls | wc –l 0 Child file descriptor table Parent file descriptor table stdin 1 stdout 2 3 pfds[0] pfds[1] pipe pipe(pfds); fork();

17 Pipe dream come true: ls | wc –l 0 Child file descriptor table Parent file descriptor table stdin 1 stdout 2 3 pfds[0] pfds[1] pipe pipe(pfds); fork(); close(0); close(1);

18 Pipe dream come true: ls | wc –l 0 Child file descriptor table Parent file descriptor table stdin 1 stdout 2 3 pfds[0] pfds[1] pipe pipe(pfds); fork(); close(0); dup(pfds[0]); close(1); dup(pfds[1]);

19 Pipe dream come true: ls | wc –l 0 Child file descriptor table Parent file descriptor table stdin 1 stdout 2 3 pfds[0] pfds[1] pipe pipe(pfds); fork(); close(0); dup(pfds[0]); close(pfds[1]); execlp("wc", "wc", "-l", NULL); close(1); dup(pfds[1]); close(pfds[0]); execlp("ls", "ls", NULL);

20 FIFOs

21 FIFOs A pipe disappears when no process has it open FIFOs = named pipes Special pipes that persist even after all the processes have closed them Actually implemented as a file #include int status;... status = mkfifo("/home/cnd/mod_done", /* mode=0644 */ S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);

22 Communication Over a FIFO First open blocks until second process opens the FIFO Can use O_NONBLOCK flag to make operations non-blocking FIFO is persistent : can be used multiple times Like pipes, OS ensures atomicity of writes and reads OS address space Process A Process B Private address space

23 FIFO Example: Producer-Consumer Producer Writes to FIFO Consumer Reads from FIFO Outputs data to file FIFO ensures atomicity of write

24 FIFO Example #include #include "restart.h" int main (int argc, char *argv[]) { int requestfd; if (argc != 2) { /* name of consumer fifo on the command line */ fprintf(stderr, "Usage: %s fifoname > logfile\n", argv[0]); return 1; }

25 FIFO Example /* create a named pipe to handle incoming requests */ if ((mkfifo(argv[1], S_IRWXU | S_IWGRP| S_IWOTH) == -1) && (errno != EEXIST)) { perror("Server failed to create a FIFO"); return 1; } /* open a read/write communication endpoint to the pipe */ if ((requestfd = open(argv[1], O_RDWR)) == -1) { perror("Server failed to open its FIFO"); return 1; } /* Write to pipe like you would to a file */... }

26 What if there are multiple producers? Examples Multiple children to compute in parallel; wait for output from any Network server connected to many clients; take action as soon as any one of them sends data Problem Can use read / write scanf, but..... problem? Blocks waiting for that one file, even if another has data ready & waiting! Solution Need a way to wait for any one of a set of events to happen Something similar to wait() to wait for any child to finish, but for events on file descriptors

27 Select & Poll

28 Select and Poll: Waiting for input Similar parameters Set of file descriptors Set of events for each descriptor Timeout length Similar return value Set of file descriptors Events for each descriptor Notes Select is slightly simpler Poll supports waiting for more event types Newer variant available on some systems: epoll

29 Select int select (int num_fds, fd_set* read_set, fd_set* write_set, fd_set* except_set, struct timeval* timeout); Wait for readable/writable file descriptors. Return: Number of descriptors ready -1 on error, sets errno Parameters: num_fds :  number of file descriptors to check, numbered from 0 read_set, write_set, except_set :  Sets (bit vectors) of file descriptors to check for the specific condition timeout :  Time to wait for a descriptor to become ready

30 File Descriptor Sets Bit vectors Often 1024 bits, only first num_fds checked Macros to create and check sets fds_set myset; void FD_ZERO (&myset); /* clear all bits */ void FD_SET (n, &myset); /* set bits n to 1 */ void FD_CLEAR (n, &myset); /* clear bit n */ int FD_ISSET (n, &myset); /* is bit n set? */

31 File Descriptor Sets Three conditions to check for Readable  Data available for reading Writable  Buffer space available for writing Exception  Out-of-band data available (TCP)

32 Select: Example fd_set my_read; FD_ZERO(&my_read); FD_SET(0, &my_read); if (select(1, &my_read, NULL, NULL) == 1) { ASSERT(FD_ISSET(0, &my_read); /* data ready on stdin */

33 Poll #include int poll (struct pollfd* pfds, nfds_t nfds, int timeout); Poll file descriptors for events. Return: Number of descriptors with events -1 on error, sets errno Parameters: pfds :  An array of descriptor structures. File descriptors, desired events and returned events nfds :  Length of the pfds array timeout :  Timeout value in milliseconds

34 Descriptors Structure struct pollfd { int fd;/* file descriptor */ short events;/* queried event bit mask */ short revents;/* returned event mask */ Note: Any structure with fd < 0 is skipped

35 Event Flags POLLIN : data available for reading POLLOUT : Buffer space available for writing POLLERR : Descriptor has error to report POLLHUP : Descriptor hung up (connection closed) POLLVAL : Descriptor invalid

36 Poll: Example struct pollfd my_pfds[1]; my_pfds[0].fd = 0; my_pfds[0].events = POLLIN; if (poll(&my_pfds, 1, INFTIM) == 1) { ASSERT (my_pfds[0].revents & POLLIN); /* data ready on stdin */

37 Bonus Slides!

38 IPC Solutions Two options Support some form of shared address space  Shared memory, memory mapped files Use OS mechanisms to transport data from one address space to another  Pipes, FIFOs  Messages, signals

39 Message-based IPC Message system  Enables communication without resorting to shared variables To communicate, processes P and Q must  Establish a communication link between them  Exchange messages Two operations  send(message)  receive(message)

40 Message Passing Process AProcess B Direct Process AProcess C Indirect Process B

41 Direct Message Passing Processes must name each other explicitly  send (P, message) Send a message to process P  receive(Q, message) Receive a message from process Q  receive(&id, message) Receive a message from any process Link properties  Established automatically  Associated with exactly one pair of processes  There exists exactly one link between each pair Limitation  Must know the name or ID of the process(es)

42 Indirect Message Passing Process names a mailbox (or port)  Each mailbox has a unique id  Processes can communicate only if they share a mailbox Link properties  Established only if processes share a common mailbox  May be associated with many processes  Each pair of processes may share multiple links  Link may be unidirectional or bi-directional

43 Mailbox Ownership Process Only the owner receives messages through mailbox Other processes only send. When process terminates, any “owned” mailboxes are destroyed System Process that creates mailbox owns it (and so may receive through it) but may transfer ownership to another process.

44 Indirect Message Passing Mailboxes are a resource Create and Destroy Primitives send(A, message)  Send a message to mailbox A receive(A, message)  Receive a message from mailbox A

45 Indirect Message Passing Mailbox sharing  P1, P2, and P3 share mailbox A  P1, sends; P2 and P3 receive  Who gets the message? Options  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 arbitrarily select the receiver and notify the sender

46 IPC and Synchronization Blocking == synchronous  Blocking send Sender blocks until the message is received  Blocking receive Receiver blocks until a message is available Non-blocking == asynchronous  Non-blocking send Sender sends the message and continues  Non-blocking receive Receiver receives a valid message or null

47 Buffering IPC message queues 1. Zero capacity No messages may be queued Sender must wait for receiver 2. Bounded capacity Finite buffer of n messages Sender blocks if link is full 3. Unbounded capacity Infinite buffer space Sender never blocks

48 Buffering Is a buffer needed? P1:send(P2, x) P2:receive(P1, x) receive(P2, y)send(P1, y) Is a buffer needed? P1:send(P2, x) P2: send(P1, x) receive(P2, y) receive(P1, y)

49 Example: Message Passing void Producer() { while (TRUE) { /* produce item */ build_message(&m, item); send(consumer, &m); receive(consumer, &m); /* wait for ack */ } void Consumer { while(TRUE) { receive(producer, &m); extract_item(&m, &item); send(producer, &m); /* ack */ /* consume item */ }

50 Signals == Messages Signals are a simple form of message passing Non-blocking No buffering

51 Communication Over a Pipe OS address space Process A Process B Private address space msg

52 Select and Poll: Prototypes Select Wait for readable/writable file descriptors #include int select (int num_fds, fd_set* read_set, fd_set* write_set, fd_set* except_set, struct timeval* timeout); Poll Poll file descriptors for events #include int poll (struct pollfd* pfds, nfds_t nfds, int timeout);