Principles of Operating Systems Lecture 14

Slides:



Advertisements
Similar presentations
Operating Systems Lecture 7.
Advertisements

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.
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Assistant Professor, University of Maryland Baltimore County.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 3 – Process Concepts Outline 3.1 Introduction 3.1.1Definition of Process 3.2Process States:
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
3.5 Interprocess Communication
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.
Posix Message Queues Courtesy of W. Richard Stevens Unix Network Programming Volume 2: Interprocess Communication.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Interprocess Communication. Process Concepts Last class.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
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.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Operating Systems Yasir Kiani. 13-Sep Agenda for Today Review of previous lecture Interprocess communication (IPC) and process synchronization UNIX/Linux.
Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns
CSNB334 Advanced Operating Systems 4. Concurrency : Mutual Exclusion and Synchronization.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Interprocess Communication
Named Pipes. Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Lecture 5 Systems Programming: Unix Processes: Orphans and Zombies
G.Jyostna.
CSNB334 Advanced Operating Systems 4
Chapter 3 – Process Concepts
Using Processes.
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Threads and Cooperation
Applied Operating System Concepts
Chapter 3: Processes.
Structure of Processes
Chapter 4: Processes Process Concept Process Scheduling
Interprocess Communication (IPC)
Threads and Data Sharing
Making Virtual Memory Real: The Linux-x86-64 way
תרגול 8 – ק/פ ותקשורת תהליכים ב-Linux
Interprocess Communication
Operating Systems Lecture 8.
Unix programming Term: Unit-V I PPT Slides
IPC Prof. Ikjun Yeom TA – Hoyoun
Inter-Process Communication ENCE 360
Advanced UNIX progamming
System Programming: Process Management
Presentation transcript:

Principles of Operating Systems Lecture 14 Abhishek Dubey Daniel Balasubramanian Inter Process Communication Slides Based On Power points and book material from William Stallings Fall2014

Inter process communication(IPC) IPC refers to a set of services/methods provided by the operating system used for exchanging information/messages between processes. IPC mechanisms can be roughly divided into Synchronization (semaphores/monitors) Message passing (can be used to exchange information as well as for synchronization) Shared memory signalsg Shared files (we will cover file system at a later time.)

Message Passing When processes interact with one another two fundamental requirements must be satisfied: Message Passing is one approach to providing both of these functions works with distributed systems and shared memory multiprocessor and uniprocessor systems synchronization to enforce mutual exclusion communication to exchange information When processes interact with one another, two fundamental requirements must be satisfied: synchronization and communication. Processes need to be synchronized to enforce mutual exclusion; cooperating processes may need to exchange information. One approach to providing both of these functions is message passing. Message passing has the further advantage that it lends itself to implementation in distributed systems as well as in shared-memory multiprocessor and uniprocessor systems.

Message Passing The actual function is normally provided in the form of a pair of primitives: send (destination, message) Blocking/non blocking receive (source, message) Blocking send and receive can be used for synchronization as well as mutual exclusion Non-blocking mode is often referred to as asynchronous communication A process sends information in the form of a message to another process designated by a destination A process receives information by executing the receive primitive, indicating the source and the message Message-passing systems come in many forms. In this section, we provide a general introduction that discusses features typically found in such systems. The actual function of message passing is normally provided in the form of a pair of primitives: send (destination, message) receive (source, message) This is the minimum set of operations needed for processes to engage in message passing. A process sends information in the form of a message to another process designated by a destination . A process receives information by executing the receive primitive, indicating the source and the message .

Signals 1/2 A form of asynchronous notification. A software mechanism that informs a process of the occurrence of asynchronous events Similar to a hardware interrupt, but does not employ priorities keyboard (ctrl-C, ctrl-Z)  (SIGINT, SIGTSTP) hardware exceptions (segmentation fault, illegal instruction)  (SIGSEGV, SIGILL) interprocess signals (using the kill system call)   timer  (SIGALRM) child termination  (SIGCHLD)

Signals 2/2 A signal is delivered by updating a field in the process table for the process to which the signal is being sent A process may respond to a signal by: performing some default action executing a signal-handler function ignoring the signal

UNIX Signals Table 6.2 lists signals defined for UNIX SVR4.

Signal API Sender side Recepient side int kill(pid_t pid, int sig); Register a signal handler. Note that this is possible only if the signal can be trapped sighandler_t signal(int signum, sighandler_t handler); Where sighandler_t is typedef void (*sighandler_t)(int); int pause(void); Wait until a signal is delievered that either terminates the process or causes the invocation of a signal handler.

Signal Example int main() { if(signal(SIGUSR2, signal_handler) == SIG_ERR) printf("error in registering signal handler for SIGUSR2\n"); return -1; } if(signal(SIGUSR1, signal_handler) == SIG_ERR) printf("error in registering signal handler for SIGUSR1\n"); while(flag!=SIGUSR1 && flag !=SIGUSR2) sleep(1); printf("in the loop\n"); printf("returning due to signal %d\n",flag); return 0; long flag=0 void signal_handler{int signum) { switch(signnum) case SIGUSR1: printf("received SIGUSR1\n"); flag=SIGUSR1; return; case SIGUSR2: printf("received SIGUSR2\n"); flag=SIGUSR2; default: printf("received unknown signal, %d\n",signum); }; }

Pipes Named Unnamed Two types: Circular buffers allowing two processes to communicate on the producer-consumer model first-in-first-out queue, written by one process and read by another Named Unnamed Two types: One of the most significant contributions of UNIX to the development of operating systems is the pipe. Inspired by the concept of coroutines [RITC84], a pipe is a circular buffer allowing two processes to communicate on the producer–consumer model. Thus, it is a first-in-first-out queue, written by one process and read by another. When a pipe is created, it is given a fixed size in bytes. When a process attempts to write into the pipe, the write request is immediately executed if there is sufficient room; otherwise the process is blocked. Similarly, a reading process is blocked if it attempts to read more bytes than are currently in the pipe; otherwise the read request is immediately executed. The OS enforces mutual exclusion: that is, only one process can access a pipe at a time. There are two types of pipes: named and unnamed. Only related processes can share unnamed pipes, while either related or unrelated processes can share named pipes.

IPC: Pipes & FIFOs (1/2) Pipes & FIFOs (Named Pipes) provide unidirectional communication between a read end and a write end Only different in how they are created and who can access them Pipes are created with the popen library routine, and access is limited to related processes FIFOs are named objects in the file system created by mkfifo and may be opened by any process

IPC: Pipes & FIFOs (2/2) Communication over pipes is a stream of bytes; applications must Use a common message format Indicate/detect message boundaries Pipes have limited capacity Writing to a full/reading from an empty pipe will block There are, of course, some exceptions

Sidebar: File Descriptors A File Descriptor is a handle provided by the OS when interacting with files Typically an index into a data structure in kernel memory Used for all interactions with that file In UNIX, almost everything is treated like a file – files, pipes, sockets, you name it Typical ways to interact with a file descriptor: open – creates a new file descriptor that points to a resource write – Write a stream of bytes to that resource Read – Read a stream of bytes from that resource Close – Indicate you no longer need access to the resource

Sidebar: File Descriptors All processes have at least three open file descriptors: 0 – standard input 1 – standard output 3 – standard error A particular file descriptor can be reopened and pointed at a different resource

Named Pipe Creation Creates a special file at the given pathname int mkfifo (const char *pathname, mode_t mode) Creates a special file at the given pathname Any process that has access to the file may open it Access permissions can be controlled with the mode parameter This file may then be opened in read mode or write mode Both sides (read and write) must be opened by at least one process Open will typically block until the other side has opened

Pipe Creation int pipe (int pipefd[2]); int pipe2 (int pipefd[2], int flags) Creates a read and write pipe, placing the file descriptors in pipefd pipefd[0] is the read end pipefd[1] is the write end Pipe2 has flags that control two properties: O_NONBLOCK – full/empty pipes do not cause read/write to block O_CLOEXEC The pipes are closed on exec. man 2 pipe

A bit more about pipes Pipes are half duplex That is the data flows typically from a writer to a reader in only one direction. The pipes can only be used between processes with common ancestor Parent-child Child-child Reading from a pipe whose write end has been closed returns 0 (end of file) Writing to a pipe whose read end has been closed generates SIGPIPE

A bit more about pipes The pipe on child side can be associated with the STDIN using dup2 system call. int dup2(int oldfd, int newfd); int fd[2]; pid_t pid; pipe(fd); //fildes[0] is open for reading and //fildes[1] is open for writing pid = fork(); if(pid == 0) { dup2(fd[0], STDIN_FILENO); exec(<your program>); } Anything written by parent on the pipe will be read by the child on stdin.

Using pipes Signal events (one pipe) „Synchronize (one or two pipes) „Wait for a message „Synchronize (one or two pipes) „Wait for a message or set of messages „You send me a message when you are ready, then I’ll send you a message when I am ready „Communicate (one or two pipes) „Send messages back and forth

Message Queues Linked list of messages stored in the kernel A message is a block of bytes. A message queue is like a mailbox (with maximum size) Senders can add messages Receivers can take message Often the operating system guarantees that the action of taking 1 message and adding 1 message is mutually exclusive. Senders have an option of blocking forever or wait until timeout if queue is full. Receivers block or wait for a specified time if the queue is empty.

POSIX Message Queues: Creation mqd_t mq_open (const char *name, int oflag); mqd_t mq_open (const char *name, int oflag, mode_t mode, struct mq_attr *attr Creates (or accesses an existing) message queue identified by a name Oflag works similarly to POSIX semaphores (O_CREAT, O_EXCL) Can be created in nonblocking form (O_NONBLOCK Oflag also specifies the direction of the message queue (O_RDONLY, O_WRONLY, O_RDWR) Second form allows you to set advanced options (access restrictions, size of queue, size of message) Like semaphores, MQs exist until they are unlinked with mq_unlink

POSIX Message Queues: Sending int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio); int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio, const struct timespec *abs_timeout); Sends a message (msg_ptr) of a specified length (msg_len) to a message queue (mqdes) A priority is specified (msg_prio). Messages are delivered in priority FIFO order. Both forms will block (if the MQ is not nonblocking) if the queue is full. The timed form accepts an absolute timeout.

POSIX Message Queues: Receiving ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio); ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio, const struct timespec *abs_timeout); Receives a message into a buffer (msg_ptr) or specified length (msg_len) from a specific queue (mqdes). Buffer must be at least the maximum size of a message. Will block (if the MQ is not nonblocking) if the queue is empty.

Shared Memory Fastest form of interprocess communication Common block of virtual memory shared by multiple processes Permission is read-only or read-write for a process Mutual exclusion constraints are not part of the shared-memory facility but must be provided by the processes using the shared memory The fastest form of interprocess communication provided in UNIX is shared memory. This is a common block of virtual memory shared by multiple processes. Processes read and write shared memory using the same machine instructions they use to read and write other portions of their virtual memory space. Permission is read-only or read-write for a process, determined on a per-process basis. Mutual exclusion constraints are not part of the shared-memory facility but must be provided by the processes using the shared memory.

Typical Sequence of Activity Open shared memory segment shm_open system call Set the size of the shared memory segment ftruncate system call Map the shared memory segment in the virtual address space of the calling process mmap Typically the output of mmap is stored into a pointer Use the pointer associated with the memory segment Unlink the shared memory segment.

Shared Memory: Initialization int shm_open(const char *name, int oflag, mode_t mode); Creates a new or opens an existing named shared memory segment The oflag parameter allows the calling process to specify access and creation restrictions: O_RDWR/O_RDONLY: Read/write access or read-only access O_CREAT/O_EXCL: Controls behavior of the segment does/does not exist O_TRUNCATE: If a segment exists, erase its contents The mode parameter provides access control: it is possible to restrict access to other users/groups Returns a file descriptor that identifies the shared memory segment

Sidebar: File Descriptors (1/2) A file descriptor is an abstract handle identifying an open file or resource UNIX-like Oss use integers Other Oss may use some other data type Intended to be opaque to the process – typically used as an index into a kernel data structure In UNIX-like OSs, almost everything is a file or file-like object Files Devices Sockets (for network communication)

Sidebar: File Descriptors (2/2) The file descriptor is passed to any system call that operates on files Open – constructs a file descriptor that points to a file like object Read/write – read data from/write data to a file-like object Close – indicate that the process is no longer interested in that resource Many others

shm_open example // Open the shared memory segment int shm_fd = shm_open ("/SHM_example", O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); O_CREAT and O_EXCL are similar to what we discussed earlier in the context of pipes and message queues.

ftruncate #include <unistd.h> #include <sys/types.h> int ftruncate(int fd, off_t length); Truncates a file pointed by file descriptor fd to the specified length. Necessary to set the size of the shared memory.

Ftruncate example if (ftruncate (shm_fd, sizeof (mymessage)) != 0) { perror ("ftruncate"); ret = -1; goto exit; }

mmap mmap() creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in addr. The length argument specifies the length of the mapping. Used to map the contents of a file descriptor into a memory segment located in the virtual address space of calling process.

mmap #include <sys/mman.h> void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); Flags MAP_FIXED Do not select a different address than the one specified. MAP_SHARED Share this mapping with all other processes that map this object. MAP_PRIVATE Create a private copy-on-write mapping. Stores to the region do not affect the original file. MAP_PRIVATE and MAP_SHARED cannot be used together.

mmap #include <sys/mman.h> void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); prot PROT_EXEC Pages may be executed. PROT_READ Pages may be read. PROT_WRITE Pages may be written. PROT_NONE Pages may not be accessed. Fd: file to map starting at the specified offset.

mmap #include <sys/mman.h> void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); If start is 0, it returns the best location where the memory segment map has been created. Do not pass MAP_FIXED in that case Return MAP_FAILED on error. Exact error code is available with errno Example shm_segment =(mymessage *) mmap (0, sizeof (mymessage), (PROT_READ | PROT_WRITE), MAP_SHARED, shm_fd, 0))

unlink Similar to pipes/semaphores/message queues Deletes the shared memory segment.

Example – producer consumer