2.3 InterProcess Communication (IPC)

Slides:



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

1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania Fall 2003 Lecture Note 2.6: Message-Based Communication.
Prepared By Sarath S Menon S6 CSE.  Imagine a scenario in which there exists two Distinct processes both operating on a single shared data area.  One.
Unix IPC and Synchronization. Pipes and FIFOs Pipe: a circular buffer of fixed size written by one process and read by another int pipe(int fildes[2])
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
Shared Memory  Creating a Shared Memory Segment Allocated in byte amounts  Shared Memory Operations Create Attach Detach  Shared Memory Control Remove.
XSI IPC Message Queues Semaphores Shared Memory. XSI IPC Each XSI IPC structure has two ways to identify it An internal (within the Kernel) non negative.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Project 2 教學. Synchronization Functions in Windows HANDLE WINAPI CreateSemaphore ( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, __in LONG lInitialCount,
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Critical Sections and Semaphores A critical section is code that contains access to shared resources that can accessed by multiple processes. Critical.
1 Tuesday, June 27, 2006 "If the 8086 architects had designed a car, they would have produced one with legs, to be compatible with the horse." - Anonymous.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman
UNIX IPC CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of Concurrency2 Creating a UNIX process A process is created by making an exact.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
2.3 InterProcess Communication (IPC). IPC methods ► Signals ► Mutex (MUTual EXclusion) ► Semaphores ► Shared memory ► Memory mapped files ► Pipes & named.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Thread Synchronization with Semaphores
The Third Assignment COSC 4330/6310 Spring Implementing delays To be able to test the semaphores, we must run the program in real time To be able.
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.
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Win32 Programming Lesson 7: Kernel Objects. Abstract  Many of the concepts we’ll look at today won’t make complete sense until you use them  However,
Windows thread programming
Briefing for: Hacking Windows Internals Cesar Cerrudo Argeniss.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.
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.
1 Semaphores Chapter 7 from Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date: January.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
Semaphores Creating and Accessing Semaphore Sets Semaphore Operations
IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Lecture 5 Barriers and MPI Introduction Topics Barriers Uses implementations MPI Introduction Readings – Semaphore handout dropboxed January 24, 2012 CSCE.
CSNB334 Advanced Operating Systems 4. Concurrency : Mutual Exclusion and Synchronization.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
2.3 InterProcess Communication (IPC). IPC methods ► Signals ► Mutex (MUTual EXclusion) ► Semaphores ► Shared memory ► Memory mapped files ► Pipes & named.
Inter Process Comunication in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Semaphores Chapter 7 from Inter-process Communications in Linux:
© 2006 RightNow Technologies, Inc. Synchronization September 15, 2006 These people do not actually work at RightNow.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Termination Process executes last statement and asks the operating.
Semaphores Reference –text: Tanenbaum ch
Web Server Architecture Client Main Thread for(j=0;j
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
CSC Advanced Unix Programming, Fall, 2008 Monday, December 1 Thread local storage, POSIX:SEM semaphores and POSIX:XSI IPC.
Distributed and Parallel Processing George Wells.
Chien-Chung Shen CIS/UD
Semaphores Reference text: Tanenbaum ch
Process Synchronization
Shared Memory Dr. Yingwu Zhu.
Processes in Unix, Linux, and Windows
Waiting and Synchronization
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Synchronization Primitives – Semaphore and Mutex
26.
Synchronization.
Semaphores Reference text: Tanenbaum ch
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Presentation transcript:

2.3 InterProcess Communication (IPC) Part B

IPC methods Semaphores Shared memory Memory mapped files Signals Mutex (MUTual EXclusion) Semaphores Shared memory Memory mapped files Pipes & named pipes Sockets Message queues MPI (Message Passing Interface) Barriers

Semaphores

(Bounded) Producer-Consumer A producer produces some item and stores it in a warehouse. A consumer consumes an item by removing an item from the warehouse. Notes (rules): The producer must pause production if the warehouse fills up (bounded). If the warehouse is empty, the consumer must wait for something to be produced.

(Bounded) producer-consumer problem Danger, Will Robinson (a shared variable)!

(Bounded) Producer-consumer problem Buffer is initially empty. Consumer checks count. It’s 0. Scheduler interrupts consumer (puts consumer on ready queue). Producer runs. Insert data into buffer. Count is 1 so producer wakes up consumer. But consumer is not asleep just yet! (The scheduler interrupted it right before the call to sleep().) Producer keeps inserting data into buffer until it’s full. Then producer goes to sleep! Scheduler runs consumer. Consumer thinks count=0 so it goes to sleep! Both sleep forever!

Semaphores Invented by Dutch computer scientist Edsger Dijkstra. Two basic operations: Up increments the value of the semaphore historically denoted as V (also known as signal) Down decrements the value of the semaphore P (also known as wait)

Semaphores Types: POSIX Windows System V Shared only among threads. Can be system-wide. System V Can be shared according to user-group-other (can be system-wide).

Binary semaphores = mutex Create semaphore and initialize it to 1. 1 = unlocked 0 = locked Then to use this as a mutex: down c.s. up

Posix semaphores

POSIX Semaphores (shared among threads only) #include <semaphore.h> int sem_init ( sem_t* sem, int pshared, unsigned int value ); int sem_wait ( sem_t* sem ); int sem_trywait ( sem_t* sem ); int sem_post ( sem_t* sem ); int sem_getvalue ( sem_t* sem, int* sval ); int sem_destroy ( sem_t* sem );

POSIX Semaphores int sem_init ( sem_t* sem, int pshared, unsigned int value ); initialize pshared must be 0 on (some versions of) Linux semaphore is not shared by processes Value is initial value for semaphore.

POSIX Semaphores int sem_wait ( sem_t* sem ); down (if possible/blocking) int sem_trywait ( sem_t* sem ); nonblocking down Blocking?

POSIX Semaphores int sem_post ( sem_t* sem ); up (nonblocking) int sem_getvalue ( sem_t* sem, int* sval ); get the current semaphore value int sem_destroy ( sem_t* sem ); finish using the semaphore

Windows semaphores

Windows Semaphores HANDLE WINAPI CreateSemaphore ( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, __in LONG lInitialCount, __in LONG lMaximumCount, __in_opt LPCTSTR lpName ); DWORD WINAPI WaitForSingleObject ( __in HANDLE hHandle, __in DWORD dwMilliseconds ); //decrements count by 1 BOOL WINAPI ReleaseSemaphore ( __in HANDLE hSemaphore, __in LONG lReleaseCount, __out_opt LPLONG lpPreviousCount ); //increments count by lReleaseCount BOOL WINAPI CloseHandle ( __in HANDLE hObject );

System V semaphores

System V Semaphores (system-wide) #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semget ( key_t key, int nsems, int semflg ); create/access existing int semctl ( int semid, int semnum, int cmd, ... ); delete from system int semop ( int semid, struct sembuf* sops, unsigned nsops ); used for up and down

Create/access existing //using the key, get the semaphore id const int sid = semget( mySemKey, 1, IPC_CREAT | 0700 ); if (sid==-1) { perror( "semget " ); exit( -1 ); } printf( "sem id=%d \n", sid ); create if necessary system-wide permissions (In C/C++, octal values start with 0.) system-wide unique number

Create/access existing //using the key, get the semaphore id const int sid = semget( mySemKey, 1, IPC_CREAT | 0700 ); alternative (#include <sys/stat.h>): const int sid = semget( mySemKey, 1, IPC_CREAT | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ); create if necessary system-wide permissions (In C/C++, octal values start with 0.)

Access and delete //using the key, get the semaphore id const int sid = semget( mySemKey, 1, 0700 ); if (sid==-1) { perror( "semget " ); exit( -1 ); } printf( "sem id=%d \n", sid ); //delete the semaphore semctl( sid, 0, IPC_RMID, 0 );

Down function static void down ( const int whichSid ) { struct sembuf sem_lock; sem_lock.sem_num = 0; //semaphore number: 0 = first sem_lock.sem_op = -1; //semaphore operation sem_lock.sem_flg = 0; //operation flags if (semop(whichSid, &sem_lock, 1) == -1) { perror("semop "); exit(-1); }

Up function static void up ( const int whichSid ) { struct sembuf sem_unlock; sem_unlock.sem_num = 0; //semaphore number: 0 = first sem_unlock.sem_op = 1; //semaphore operation sem_unlock.sem_flg = 0; //operation flags if (semop(whichSid, &sem_unlock, 1) == -1) { perror("semop"); exit(-1); }

Note: 3 semaphores (one used as mutex)! Solution to (bounded) producer-consumer problem using semaphores (via up() and down()).

(bounded) Producer-consumer demo bounded.h, boundedProducer.cpp, boundedConsumer.cpp (bounded) Producer-consumer demo

Unbounded producer-consumer

(Unbounded) producer-consumer Every time the producer process runs, it produces one item. Every time the consumer runs, it consumes one item (or waits until one is available, and then consumes it).

//unbounded producer int main ( int argc, char* argv[] ) { //using the key, get the semaphore id const int sid = semget( mySemKey, 1, IPC_CREAT | 0700 ); if (sid==-1) { perror( "semget " ); exit( -1 ); } printf( "sem id=%d \n", sid ); puts( "producing" ); struct sembuf sem_unlock; sem_unlock.sem_num = 0; //semaphore number: 0 = first sem_unlock.sem_op = 1; //semaphore operation sem_unlock.sem_flg = 0; //operation flags if (semop(sid, &sem_unlock, 1) == -1) { perror("semop "); exit(-1); puts( "produced" ); return 0; Unbounded producer basically does an up() to indicate production.

//consumer int main ( int argc, char* argv[] ) { //using the key, get the semaphore id const int sid = semget( mySemKey, 1, IPC_CREAT | 0700 ); if (sid==-1) { perror( "semget " ); exit( -1 ); } printf( "sem id=%d \n", sid ); puts( "consuming" ); struct sembuf sem_lock; sem_lock.sem_num = 0; //semaphore number: 0 = first sem_lock.sem_op = -1; //semaphore operation sem_lock.sem_flg = 0; //operation flags if (semop(sid, &sem_lock, 1) == -1) { perror("semop "); exit(-1); puts( "consumed" ); return 0; Consumer basically does a down() to indicate consumption.

(Unbounded) Producer-consumer demo mySemKey.h, producer.cpp, consumerWait.cpp, consumerNoWait.cpp, bigProducer.cpp, delete.cpp (Unbounded) Producer-consumer demo

Shared Memory

Shared memory (Linux/Unix) Shared memory operations shmget allocates a shared memory segment shmctl allows the user to receive information on a shared memory segment, set the owner, group, and permissions of a shared memory segment, or destroy a segment

Shared memory Shared memory operations shmat shmdt attaches the shared memory segment (identified by shmid) to the address space of the calling process shmdt detaches the shared memory segment (located at the address specified by shmaddr) from the address space of the calling process

Shared memory From the Linux sem_init man page: The pshared argument indicates whether this semaphore is to be shared between the threads of a process, or between processes. If pshared has the value 0, then the semaphore is shared between the threads of a process, and should be located at some address that is visible to all threads (e.g., a global variable, or a variable allocated dynamically on the heap). If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory.

Memory mapped files

Memory mapped files “There comes a time when you want to read and write to and from files so that the information is shared between processes. Think of it this way: two processes both open the same file and both read and write from it, thus sharing the information. The problem is, sometimes it's a pain to do all those fseek()s and stuff to get around. Wouldn't it be easier if you could just map a section of the file to memory, and get a pointer to it? Then you could simply use pointer arithmetic to get (and set) data in the file. Well, this is exactly what a memory mapped file is. And it's really easy to use, too. A few simple calls, mixed with a few simple rules, and you're mapping like a mad-person.” http://beej.us/guide/ipc/mmap.html

Memory mapped files (Unix/Linux) mmap void* mmap ( void* start, size_t length, int prot, int flags, int fd, off_t offset ); map length bytes starting at offset offset from the file specified by the file descriptor fd into memory, preferably at address start

Memory mapped files (Unix/Linux) munmap int munmap ( void* start, size_t length ); The munmap system call deletes the mappings for the specified address range, and causes further references to addresses within the range to generate invalid memory references. The region is also automatically unmapped when the process is terminated. On the other hand, closing the file descriptor does not unmap the region.

Shared memory and memory mapped files (Windows) Windows combines both mechanisms into one set of function calls. If the file actually exists, then it will be memory-mapped (via call to CreateFileMapping). But if the hFile arg (to CreateFileMapping) is INVALID_HANDLE_VALUE, then the memory will be backed by an entry in the system paging file. (Windows refers to this are named shared memory.)

Memory mapped files (Windows) HANDLE WINAPI CreateFileMapping ( __in HANDLE hFile, __in_opt LPSECURITY_ATTRIBUTES lpAttributes, __in DWORD flProtect, __in DWORD dwMaximumSizeHigh, __in DWORD dwMaximumSizeLow, __in_opt LPCTSTR lpName ); LPVOID WINAPI MapViewOfFile ( __in HANDLE hFileMappingObject, __in DWORD dwDesiredAccess, __in DWORD dwFileOffsetHigh, __in DWORD dwFileOffsetLow, __in SIZE_T dwNumberOfBytesToMap

Creating shared memory via Windows. http://msdn. microsoft First process (creator): The first process creates the file mapping object by calling the CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object. Then the process uses the file mapping object handle (that CreateFileMapping returns) in a call to MapViewOfFile to create a view of the file in the process address space. The MapViewOfFile function returns a pointer to the file view, pBuf. The process then uses the CopyMemory function to write a string to the view that can be accessed by other processes. When the process no longer needs access to the file mapping object, it should call UnmapViewOfFile(pBuf) and then CloseHandle. When all handles are closed, the system can free the section of the paging file that the object uses.

Creating shared memory via Windows Second/other processes: A second process can access the string written to the shared memory by the first process by calling the OpenFileMapping function specifying the same name for the mapping object as the first process. Then it can use the MapViewOfFile function to obtain a pointer to the file view, pBuf.

Example: creating shared memory via Windows – first process (creator) #include <windows.h> #include <stdio.h> #include <conio.h> #include <tchar.h> #define BUF_SIZE 256 TCHAR szName[]=TEXT("Global\\MyFileMappingObject"); TCHAR szMsg[]=TEXT("Message from first process."); int _tmain ( ) { //sloppy/nonstandard main! HANDLE hMapFile; LPCTSTR pBuf; hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) BUF_SIZE, // maximum object size (low-order DWORD) szName); // name of mapping object if (hMapFile == NULL) { _tprintf(TEXT("Could not create file mapping object (%d).\n"), GetLastError()); return 1; } pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, BUF_SIZE); if (pBuf == NULL) { _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError()); CloseHandle(hMapFile); return 1; } CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR))); _getch(); UnmapViewOfFile(pBuf); return 0;

Example: creating shared memory via Windows – second/other processes #include <windows.h> #include <stdio.h> #include <conio.h> #include <tchar.h> #pragma comment(lib, "user32.lib") #define BUF_SIZE 256 TCHAR szName[]=TEXT("Global\\MyFileMappingObject"); int _tmain ( ) { HANDLE hMapFile; LPCTSTR pBuf; hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, // read/write access FALSE, // do not inherit the name szName); // name of mapping object if (hMapFile == NULL) { _tprintf(TEXT("Could not open file mapping object (%d).\n"), GetLastError()); return 1; } pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, BUF_SIZE); if (pBuf == NULL) { _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError()); CloseHandle(hMapFile); return 1; } MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK); UnmapViewOfFile(pBuf); return 0;