OS: IPC I. Cooperating Processes. CIT 595 Spring 2010. Message Passing vs. Shared Memory. Message Passing: Unix Pipes



Similar documents
Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

REAL TIME SYSTEMS. Piotr MALECKI

RTAI API Inter Process Communication

Unix System Calls. Dept. CSIE

Lecture 24 Systems Programming in C

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

Shared Memory Introduction

System Calls Related to File Manipulation

System Calls and Standard I/O

Message based Process-Process Synchronization and Communication

Introduction. What is an Operating System?

Programmation Systèmes Cours 7 IPC: FIFO

Networks. Inter-process Communication. Pipes. Inter-process Communication

LOW LEVEL FILE PROCESSING

Operating Systems and Networks

Lecture 16: System-Level I/O

1 Posix API vs Windows API

Shared Memory Segments and POSIX Semaphores 1

Operating Systems. Privileged Instructions

Operating System Components

CSC 2405: Computer Systems II

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

UNIX File Management (continued)

NDK: NOVELL NSS AUDIT

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

1 Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term IPC may often be used for both

IPC. Semaphores were chosen for synchronisation (out of several options).

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

Programmation Systèmes Cours 9 UNIX Domain Sockets

MatrixSSL Porting Guide

Process definition Concurrency Process status Process attributes PROCESES 1.3

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

1 Abstract Data Types Information Hiding

Device Management Functions

MetroPro Remote Access OMP-0476F. Zygo Corporation Laurel Brook Road P.O. Box 448 Middlefield, Connecticut 06455

Grundlagen der Betriebssystemprogrammierung

Operating System Structure

Introduction. dnotify

Linux Driver Devices. Why, When, Which, How?

Have both hardware and software. Want to hide the details from the programmer (user).

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 5 - DBMS Architecture

REAL TIME OPERATING SYSTEM PROGRAMMING-II: II: Windows CE, OSEK and Real time Linux. Lesson-12: Real Time Linux

System calls. Problem: How to access resources other than CPU

Lab 4: Socket Programming: netcat part

Illustration 1: Diagram of program function and data flow

Real Time Programming: Concepts

I/O. Input/Output. Types of devices. Interface. Computer hardware

Angels (OpenSSL) and D(a)emons. Athula Balachandran Wolfgang Richter

CSE543 - Introduction to Computer and Network Security. Module: Reference Monitor

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Windows Security. CSE497b - Spring 2007 Introduction Computer and Network Security Professor Jaeger.

Chapter 6, The Operating System Machine Level

Tutorial on Socket Programming

About the File Manager 2

HP-UX Essentials and Shell Programming Course Summary

Stack Allocation. Run-Time Data Structures. Static Structures

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

This document was derived from simulation software created by Steve Robbins which was supported by NSF DUE

UNIX Sockets. COS 461 Precept 1

I/O Device and Drivers

Socket Programming. Srinidhi Varadarajan

COMMMUNICATING COOPERATING PROCESSES

POSIX. RTOSes Part I. POSIX Versions. POSIX Versions (2)

Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

Operating System Components and Services

SRFax Fax API Web Services Documentation

Jorix kernel: real-time scheduling

Lab 2 : Basic File Server. Introduction

CIS 551 / TCOM 401 Computer and Network Security

MatrixSSL Developer s Guide

Linux Kernel Architecture

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Giving credit where credit is due

CS2510 Computer Operating Systems

CS2510 Computer Operating Systems

Virtuozzo Virtualization SDK

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

: provid.ir

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Xillybus host application programming guide for Linux

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

Algorytm przydzielania bloku

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Discussion Board. Accessing Discussion Board

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

Computer Networks UDP and TCP

Listeners. Formats. Free Form. Formatted

Priority Based Implementation in Pintos

Sensors and the Zaurus S Hardware

Assignment 5: Adding and testing a new system call to Linux kernel

OS-9 for 68K Samba File Manager. Reference manual

Lecture 25 Systems Programming Process Control

Design: Metadata Cache Logging

Transcription:

Cooperating Processes Independent processes cannot affect or be affected by the execution of another process OS: IPC I CIT 595 Spring 2010 Cooperating process can affect or be affected by the execution of another process Advantages Information sharing Computational speed-up Disadvantages of process cooperation Data corruption, deadlocks Requires processes to synchronize their processing Need a mechanisms for facilitating communications and synchronization between processes Involve the OS for communication Message Passing vs. Shared Memory Shared Memory Multiple processes map a region of memory into their address spaces Changes made by one show up instantly in the address space of all others sharing the same region This is the idea behind threading Synchronization of access to shared structures must be handled E.g. Using Semaphore Message Passing Think of multiple processes operating by passing small chunks of information (message) between them Message Passing: Unix Pipes Pipe sets up communication channel between two (related) processes One process writes to the pipe, the other reads from the pipe Looks exactly the same as reading from/to a file Use systems calls read and write System call: int fd[2] ; pipe(fd) ; //creates the pipe fd[0] now holds descriptor to read from pipe fd[1] now holds descriptor to write into pipe 1

Example Pipe for pipeline #include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <string.h> char *message = "This is a message!!!" ; int main(){ char buf[1024] ; int fd[2]; pipe(fd); /*create pipe*/ if (fork()!= 0) { /* I am the parent */ write(fd[1], message, strlen (message) + 1) ; printf("parent %d sent the message\n", getpid()); else { /*Child code */ read(fd[0], buf, 1024) ; printf("got this from Parent %d: %s\n",getppid(), buf) ; return 0; Note: child inherits file descriptors Can set up pipeline E.g. who sort lpr A B c D Process A writes to pipe AB, Process B reads from AB and writes to BC Process C reads from BC and writes to CD.. who sort lpr Steps involved in pipeline Shell creates a pipe Use fork() to make three children Children inherit the descriptors Close the pipe links which are not needed Duplicates file descriptor using dup2(..) making them aliases and then deleting the old file descriptor E.g. output of who is which is to written to stdout is now written to the pipe dup2( fd[1], 1) //connect the write end of a pipe to stdout Replace children by programs using exec() Multiple Readers and Writes Perfectly possible to have multiple readers and writers attached to a pipe Can cause confusion or problems Solution: process closes the link it does not need 2

Size of a pipe is finite Pipe size, full or empty Only a certain amount of bytes can remain in the pipe without being read read() & write() are blocking by default If pipe to read is empty If pipe to write is full or there not enough room in the pipe Since write() can block in middle of its output, then output from multiple writers may be mixed up Can make them non-blocking May want to return an error instead Want to poll several pipes in turn until one has data Using fcntl() Limitations of Pipes Processing using a pipe must come from a common ancestor E.g. parent and child Pipes are not permanent Disappear when process terminates Communication is one way (half duplex) Readers and Writers do not know each other Message Queue Provides standard interface for creating/opening message queue to sending and receive messages Similar to pipes in that they are opened and closed and have readers and writers POSIX Message Queue Implementation Header: mqueue.h The header defines the mqd_t (message queue descriptor type), a long integer to identify the queue mqd_t is used by other subroutines to refer to that message queue Different compared to pipes in that messages have structure (attributes), and message have associate priorities Attributes: maximum size, size of each message, number of messages currently in queue etc. 3

Creating a queue mqd_t mq_open(const char *name, int oflag, mode_t mode,struct mq_attr *attr); Creates a message queue or opens an existing one name of the queue processes can operate on the same queue by passing the same name to mq_open() oflag is operation flags controls whether the queue is created or merely accessed Defined constants to specify operation by a process mode determines the mode Used to set rwx permissions Defined constants described d in <sys/stat.h> attr struct that contains size of message, queue length etc. If argument is NULL, then default attributes are selected Returns mqd_t mq_open Example Option 1 mqd_t mq;.. mq = mq_open(q_name, O_RDONLY O_CREAT O_EXCL, S_IRWXU, NULL); Oflag constants: Opened in read only exclusive mode S_IRWXU: read, write, execute by user/owner NULL for mq_attr (implies use default attributes) Option 2 mq = mq_open(q_name, O_WRONLY, S_IRWXU, NULL); Message queue by name Q_NAME opened in write only mode Functions for send and receive mqd_t mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio); adds the message referred by msg_ptr to the message queue specified by mqdes msg_len is the length of the message in bytes referred by msg_ptr. msg_prio is a non-negative negative integer that specifies the priority of this message Messages are placed on the queue in decreasing order of priority size_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio); receive the oldest of the highest priority message(s) from the message queue message is removed from the queue and copied to the buffer referred by the msg_ptr argument returns the length of the selected message in bytes and the message is removed from the queue Queue full or empty The sending/receiving process is blocked Cleanup More mqueue functions mq_close(mqd_t mqdes) removes the association between the message queue descriptor, mqdes,, and its message queue int mq_unlink(const char *name); Removes the message queue refered by name Is blocking if another process(es) has the mqueue open After a successful call to mq_unlink() with name, a call to mq_open() with name fails if the flag O_CREAT is not set in flags Getting Attributes int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat) Similarly there is setattr to set attributes 4

Synchronous vs. Asynchronous Communication Synchronous One makes a connection to another, sends a request and waits for a reply E.g. a user sends a request for a web page and then waits for a reply Asynchronous Idea is that an application may need to notify another that an event has occurred, but does not need to wait for a response We will use this model for the next assignment Sender and receiver of the message do not need to interact with the message queue at the same time Messages are placed onto the queue and are stored until the recipient retrieves them mqueue files Since mqueue are stored as files you can see the message queues in the filesystem But you have to mount the mqueue filesystem mkdir /dev/mqueue mount t mqueue none /dev/mqueue Queues can be cleaned up by simply deleting the appropriate file in that directory Note that you can only delete queues that you have created Also the OS only lets you create a very limited number of queues So make sure you close and unlink all queues before you quit the application 5