Inter-Process Communication: Message Passing. Thomas Plagemann. Big Picture. message passing communication?

Similar documents
Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

INTER-PROCESS COMMUNICATION. UNIX Programming 2015 Fall by Euiseong Seo

COS 318: Operating Systems. Message Passing. Vivek Pai Computer Science Department Princeton University

Process. Program Vs. process. During execution, the process may be in one of the following states

Interprocess Communication. Bosky Agarwal CS 518

CSci 4061 Introduction to Operating Systems. IPC: Message Passing, Shared Memory

UNIT III- INTER PROCESS COMMUNICATIONS Part A

경희대학교컴퓨터공학과 조진성. UNIX System Programming

Lecture 8: Inter-process Communication. Lecturer: Prof. Zichen Xu

Computation structures. Support for problem-solving lesson #7

Interprocess Communication

INTER-PROCESS COMMUNICATION Tanzir Ahmed CSCE 313 Fall 2018

Department of Computer Science and Technology, UTU 2014

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

CS 385 Operating Systems Fall 2011 Homework Assignment 5 Process Synchronization and Communications

Prepared by Prof. Hui Jiang (COSC3221) 2/9/2007

CS 385 Operating Systems Fall 2013 Homework Assignment 2 Inter-Process Communications and Synchronization

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

CS 385 Operating Systems Spring 2013 Homework Assignment 2 Third Draft Inter-Process Communications and Synchronization

Message Queues POSIX

Inter Process Communication (IPC) Giorgio Richelli

COP 4604 UNIX System Programming IPC. Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

CS 361 Computer Systems Fall 2017 Homework Assignment 4 - Inter-Process Communications & I/O

Lecture 20. Log into Linux. Copy directory /home/hwang/cs375/lecture20 Project 5 due today. Project 6 posted, due Tuesday, April 8. Questions?

Shared Memory. By Oren Kalinsky

UNIX IPC. Unix Semaphore Unix Message queue

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Chapter 3: Processes

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

COS 318: Operating Systems. Message Passing. Kai Li and Andy Bavier Computer Science Department Princeton University

Chapter 3: Processes. Operating System Concepts 8th Edition, modified by Stewart Weiss

COMP 3100 Operating Systems

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

Signal Example 1. Signal Example 2

CSPP System V IPC 1. System V IPC. Unix Systems Programming CSPP 51081

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Chapter 3: Process Concept

Chapter 3: Process Concept

The Big Picture So Far. Chapter 4: Processes

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

The Big Picture So Far. Chapter 4: Processes

CHAPTER 3 - PROCESS CONCEPT

Diagram of Process State Process Control Block (PCB)

Chapter 3: Process Concept

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Chapter 3: Processes. Operating System Concepts 8th Edition

Part Two - Process Management. Chapter 3: Processes

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CSI Module 2: Processes

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

Chapter 4: Processes. Process Concept

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

Chapter 4: Processes

Processes. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & Apps 1

l Threads in the same process l Processes that share an address space l Inter-process communication (IPC) u Destination or source u Message (msg)

Chapter 3: Processes. Operating System Concepts 8 th Edition,

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication I

Dept. Of Computer Science, Colorado State University

Frequently asked questions from the previous class survey

ECE 650 Systems Programming & Engineering. Spring 2018

MC7412 NETWORK PROGRAMMING LABORATORY L T P C

Process Concept. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

Chapter 3: Processes. Operating System Concepts 8 th Edition,

전공핵심실습 1: 운영체제론 Chapter 6. Inter-process Communication (IPC)

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Lecture 2 Process Management

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

UNIT 7 INTERPROCESS COMMUNICATION

Dept. of CS, York Univ. 1

Babu Madhav Institute of Information Technology, UTU

Lecture 8: Other IPC Mechanisms. CSC 469H1F Fall 2006 Angela Demke Brown

Chapter 4: Processes

Inter-process communication (IPC)

COMP 2355 Introduction to Systems Programming

Topics. Lecture 8: Other IPC Mechanisms. Socket IPC. Unix Communication

! The Process Control Block (PCB) " is included in the context,

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Chapter 3: Processes. Operating System Concepts 9 th Edition

Chapter 4: Processes. Process Concept. Process State

message passing model

Process Concept Process Scheduling Operations On Process Inter-Process Communication Communication in Client-Server Systems

Part II Processes and Threads Process Basics

Noorul Islam College Of Engineering, Kumaracoil MCA Degree Model Examination (October 2007) 5 th Semester MC1642 UNIX Internals 2 mark Questions

Introduction. Interprocess communication. Terminology. Shared Memory versus Message Passing

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Lecture 15: I/O Devices & Drivers

Mid-Semester Examination, September 2016

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Outline. Interprocess Communication. Interprocess Communication. Communication Models: Message Passing and shared Memory.

Chapter 3: Processes. Operating System Concepts 9 th Edition

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

OS Lab Tutorial 1. Spawning processes Shared memory

Project 4 Inter-Process Communication and Process Management. COS 318 Fall 2016

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

CS420: Operating Systems. Interprocess Communication

Concurrency: Deadlock and Starvation

File Systems. Todays Plan

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

Transcription:

Inter-Process Communication: Message Passing Thomas Plagemann With slides from Pål Halvorsen, Kai Li, and Andrew S. Tanenbaum monitors sleep and wakeup critical regions Big Picture race conditions shared memory mutal exclusion mutexes semaphores message passing communication?

Message Passing API Generic API send( dest, &msg ) recv( src, &msg ) What should the dest and src be? pid file: e.g. a (named) pipe port: network address, pid, etc no src: receive any message src combines both specific and any What should msg be? Need both buffer and size for a variable sized message Issues Asynchronous vs. synchronous Direct vs. indirect How are links established? Can a link be associated with more than two processes? How many links can there be between any pair? What is the capacity of a link? What is the size of a message? Is a link unidirectional or bidirectional?

Asynchronous vs. Synchronous Synchronous (blocking): msg operation msg operation, unblock thread block thread, execute msg operation in another thread/kernel time thread is blocked until message primitive has been performed may be blocked for a very long time Asynchronous vs. Synchronous Asynchronous (non-blocking): msg operation, resume immediately execute msg operation in another thread/kernel thread gets control back immediately thread can run in parallel other activities thread cannot reuse buffer for message before message is received how to know when to start if blocked on full/empty buffer? poll interrupts/signals time

Asynchronous vs. Synchronous Send semantic: Receive semantic: Synchronous Will not return until data is out of its source memory Block on full buffer Asynchronous Return as soon as initiating its hardware Completion Require application to check status Notify or signal the application Block on full buffer Synchronous Return data if there is a message Block on empty buffer Asynchronous Return data if there is a message Return null if there is no message Buffering No buffering synchronous Sender must wait until the receiver receives the message Rendezvous on each message Buffering asynchronous or synchronous Bounded buffer Finite size Sender blocks when the buffer is full Use mesa-monitor to solve the problem? Unbounded buffer Infinite size Sender never blocks

Direct Communication Must explicitly name the sender/receiver ( dest and src ) processes A buffer at the receiver More than one process may send messages to the receiver To receive from a specific sender, it requires searching through the whole buffer A buffer at each sender A sender may send messages to multiple receivers Message Passing: Producer-Consumers Problem void producer(void) { } while (TRUE) { } produce item; send( consumer, item ); void consumer(void) { } while (TRUE) { } recv( producer, item ); consume item;

Message Passing: Producer-Consumers Problem with N messages Indirect Communication dest and src are a shared (unique) mailbox Use a mailbox to allow many-to-many communication Requires open/close a mailbox before using it Where should the buffer be? A buffer and its mutex and conditions should be at the mailbox

Using Message-Passing What is message-passing for? Communication across address spaces Communication across protection domains Synchronization Use a mailbox to communicate between a process/thread and an interrupt handler: fake a sender Keyboard mbox Receive Process Termination P waits for a message from Q, but Q has terminated Problem: P may be blocked forever Solution: P checks once a while Catch the exception and informs P Send ack message P sends a message to Q, but Q has terminated Problem: P has no buffer and will be blocked forever Solution: Check Q s state and cleanup Catch the exception and informs P

Message Loss & Corruption Unreliable service best effort, up to the user to Detection Acknowledge each message sent Timeout on the sender side Retransmission Sequence number for each message Retransmit a message on timeout Retransmit a message on out-of-sequence acknowledgement Remove duplication messages on the receiver side System V and Linux Mailboxes Messages are stored as a sequence of bytes System V IPC messages also have a type Mailboxes are implemented as message queues sorting messages according to FIFO Can be both blocking and non-blocking (IPC_NOWAIT) The Linux related slides have some simplified (pseudo) code Linux 2.4.18 several parts missing the shown code may block holding the queue lock waiting queues are more complex...

Mailboxes Example: msgsnd(a,,...) msgrcv(b,,...) A B C D... OS-kernel Messages in System V Four system call msgget( ) returns a message descriptor that designate a message queue msgctl( ) control a message descriptor msgsnd( ) send a message msgrcv( ) receive a message msgqid = msgget(key, flag) Create a new message queue (entry) or to retrieve an existing message queue key: a name chosen by user, represent a message queue. flag: such as create flag bit with different permission Return a kernel chosen descriptor which points to the message queue

msgget User calls msgget to creat a new descriptor Kernel searches array of message queues to see if one with the key exists If no entry with the given key: Allocate a new queue structure Initialize Return identifier Else: Check permissions and return Message Queues in System V Kernel stores messages on a linked list (queue) per descriptor msgqid is index into array of message queue headers Each message queue (or IPC entry) has a permissions structure Pointers to the first and last messages on a linked list The number of messages and the total number of data bytes on the linked list The maximum number of bytes of data that can be on the linked list The process IDs of the last processes to send and receive messages Time stamps of the last msgsnd, msgrc, and msgctl operation

Data Structures for Messages in System V Source: M. Bach: The Design of the Unix Operating System Message Queue Structure in Kernel

msgsnd (msgqid, msg, count, flag) msgqid descriptor of message queue returned by msgget msg pointer to integer and character array count size of the data array flag what should be done if not enough internal bufferspace Source: M. Bach: The Design of the Unix Operating System count = msgrcv(id, msg, maxcount, type, flag) id: message descriptor msg: the address of a user structure (message) maxcount: the size of the msg type: the message type that user wants to read flag: specifies what the kernel should do if no message are on the queue count: the number of bytes returned to the user

Source: M. Bach: The Design of the Unix Operating System Msgctl(id, cmd, mstatbuf) Query the status of a message descriptor, set its status, and remove a message queue id identifies the message descriptor cmd specifies the type of command mstatbuf contains control parameters or returns result of a query

Linux Mailboxes One msq_queue structure for each present queue: struct msg_queue { struct kern_ipc_perm q_perm; /* access permissions */ time_t q_stime; /* last msgsnd time */ time_t q_rtime; /* last msgrcv time */ time_t q_ctime; /* last change time */ unsigned long q_cbytes; /* current number of bytes on queue */ unsigned long q_qnum; /* number of messages in queue */ unsigned long q_qbytes; /* max number of bytes on queue */ pid_t q_lspid; /* pid of last msgsnd */ pid_t q_lrpid; /* last receive pid */ struct list_head q_messages; struct list_head q_receivers; struct list_head q_senders; }; Messages are stored in the kernel using the msg_msg structure: struct msg_msg { struct list_head m_list; long m_type; /* message type */ int m_ts; /* message text size */ struct msg_msgseg* next; /* next pointer */ }; NOTE: the message is stored immediately after this structure - no pointer is necessary Linux Mailboxes Create a message queue using the sys_msgget system call: long sys_msgget (key_t key, int msgflg) {... create new message queue and set access permissions... } To manipulate a queue, one uses the sys_msgctl system call: long sys_msgctl (int msqid, int cmd, struct msqid_ds *buf) {... switch (cmd) { case IPC_INFO: return info about the queue, e.g., length, etc. case IPC_SET: modify info about the queue, e.g., length, etc. case IPC_RMID: remove the queue }... }

Linux Mailboxes Send a message to the queue using the sys_msgsnd system call: long sys_msgsnd (int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg) { msq = msg_lock(msqid);... if ((msgsz + msq->q_cbytes) > msq->q_qbytes) insert message the tail of msq->q_messages *msgp, update msq else put sender in waiting senders queue (msq->q_senders) } msg_unlock(msqid);... Linux Mailboxes Receive a message from the queue using the sys_msgrcv system call: long sys_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp, int msgflg) { msq = msg_lock(msqid);... search msq->q_messages for first message matching msgtype if (msg) store message in msgbuf *msgp, remove message from msgbuf *msgp, update msq else put receiver in waiting receivers queue (msq->q_receivers) } msg_unlock(msqid);... the msgtyp parameter and msgflg flag determine which messages to retrieve: = 0: return first message > 0: first message in queue with msg_msg.m_type = msgtyp > 0 & MSG_EXCEPT: first message in queue with msg_msg.m_type!= msgtyp

Our Mailbox We make it simpler than Linux Deliver messages in FIFO order Mailbox buffer space Finite space Main purpose: Send Receive Maintenance: Init Open Close Statistics Linux Pipes Classic IPC method under UNIX: > ls -l more shell runs two processes ls and more which are linked via a pipe the first process (ls) writes data (e.g., using write) to the pipe and the second (more) reads data (e.g., using read) from the pipe the system call pipe( fd[2] ) creates one file descriptor for reading (fd[0]) and one for writing (fd[1]) - allocates a temporary inode and a memory page to hold data struct pipe_inode_info { wait_queue_head_t wait; char *base; unsigned int len; unsigned int start; unsigned int readers, writers; unsigned int waiting_readers, waiting_writers; unsigned int r_counter, w_counter; } ls more

Linux: Mailboxes vs. Pipes Are there any differences between a mailbox and a pipe? Message types mailboxes may have messages of different types pipes do not have different types Buffer pipes one or more pages storing messages contiguously mailboxes linked list of messages of different types Termination pipes exists only as long as some have open the file descriptors mailboxes must often be closed More than two processes a pipe often (not in Linux) implies one sender and one receiver many can use a mailbox Performance Performance is an important issue (at least when sender and receiver is on one machine), e.g.: shared memory and using semaphores mailboxes copying data from source to mailbox and from mailbox to receiver Can one somehow optimize the message passing?

Shared Memory in System V Processes can communicate directly by sharing parts of their virtual address space System calls for manipulating shared memory are similar to calls for messages Read and write with standard calls Shared Memory in System V (cont.) shmget create a new region of shared memory or returns an existing one shmid = shmget (key, size, flag) shmat logically attacheds a shared memory to the virtual address space of a process virtaddr = shmat (id, addr, flags) shmdt detaches a shared memory from the virtual address shmdt(addr) shmctl manipulate various parameters associated with the shared memory shmctl(id, cmd, shmstatbuf)

Attaching Shared Memory

Remote Procedure Call Message passing uses I/O Idea of RPC is to make function calls Small libraries (stubs) and OS take care of communication Remote Procedure Call Implementation Issues: Cannot pass pointers - call by reference becomes copy-restore Marshaling - packing parameters Weakly typed languages - client stub cannot determine size Not always possible to determine parameter types Cannot use global variables - may get moved to remote machine/protection domain

Summary Many ways to perform IPC on a machine Direct message passing Message passing using mailboxes Pipes Shared memory RPC To learn more about IPC in the Internet: INF 3190 Data communication INF 5040 Open Distributed Processing INF 5050 Protocols and Routing in the Internet INF 5090 Advanced Topics in Distributed Systems