2.3 InterProcess Communication (IPC)

44
2.3 InterProcess 2.3 InterProcess Communication Communication (IPC) (IPC) Part B Part B

description

2.3 InterProcess Communication (IPC). Part B. IPC methods. 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. - PowerPoint PPT Presentation

Transcript of 2.3 InterProcess Communication (IPC)

Page 1: 2.3 InterProcess Communication (IPC)

2.3 InterProcess 2.3 InterProcess Communication (IPC)Communication (IPC)

Part BPart B

Page 2: 2.3 InterProcess Communication (IPC)

IPC methodsIPC methods

1.1. SignalsSignals2.2. Mutex (MUTual EXclusion)Mutex (MUTual EXclusion)

3.3. SemaphoresSemaphores4.4. Shared memoryShared memory5.5. Memory mapped filesMemory mapped files6.6. Pipes & named pipesPipes & named pipes7.7. SocketsSockets8.8. Message queuesMessage queues9.9. MPI (Message Passing Interface)MPI (Message Passing Interface)10.10. BarriersBarriers

Page 3: 2.3 InterProcess Communication (IPC)

SemaphoresSemaphores

Page 4: 2.3 InterProcess Communication (IPC)

(Bounded) Producer-(Bounded) Producer-ConsumerConsumer

►A A producerproducer produces some item and produces some item and stores it in a warehouse.stores it in a warehouse.

►A A consumerconsumer consumes an item by consumes an item by removing an item from the warehouse.removing an item from the warehouse.

►Notes (rules):Notes (rules):1.1. The producer must pause production if The producer must pause production if

the warehouse fills up (bounded).the warehouse fills up (bounded).2.2. If the warehouse is empty, the consumer If the warehouse is empty, the consumer

must wait for something to be produced.must wait for something to be produced.

Page 5: 2.3 InterProcess Communication (IPC)

(Bounded(Bounded) )

producer-producer-consumerconsumerproblemproblem

Danger, Will Robinson (a shared variable)!

Page 6: 2.3 InterProcess Communication (IPC)

(Bounded) Producer-consumer (Bounded) Producer-consumer problemproblem

► Buffer is initially empty.Buffer is initially empty.► Consumer checks count. It’s 0.Consumer checks count. It’s 0.► Scheduler interrupts consumer (puts Scheduler interrupts consumer (puts

consumer on ready queue).consumer on ready queue).► Producer runs.Producer runs.

Insert data into buffer.Insert data into buffer. Count is 1 so producer wakes up consumer.Count is 1 so producer wakes up consumer. But consumer is not asleep just yet! (The But consumer is not asleep just yet! (The

scheduler interrupted it right before the call to scheduler interrupted it right before the call to sleep().)sleep().)

Producer keeps inserting data into buffer until it’s Producer keeps inserting data into buffer until it’s full. Then producer goes to sleep!full. Then producer goes to sleep!

► Scheduler runs consumer. Consumer thinks Scheduler runs consumer. Consumer thinks count=0 so it goes to sleep!count=0 so it goes to sleep!

► Both sleep forever!Both sleep forever!

Page 7: 2.3 InterProcess Communication (IPC)

SemaphoresSemaphores

► Invented by Dutch computer scientist Invented by Dutch computer scientist Edsger Dijkstra.Edsger Dijkstra.

► Two basic operations:Two basic operations:1.1. UpUp

► increments the value of the semaphoreincrements the value of the semaphore► historically denoted as V (also known as signal)historically denoted as V (also known as signal)

2.2. DownDown► decrements the value of the semaphoredecrements the value of the semaphore► P (also known as wait)P (also known as wait)

Page 8: 2.3 InterProcess Communication (IPC)

SemaphoresSemaphores

Types:Types:1.1. POSIXPOSIX

► Shared only among Shared only among threadsthreads..

2.2. WindowsWindows► Can be system-widesystem-wide..

3.3. System VSystem V► Can be shared according to user-group-other Can be shared according to user-group-other

(can be (can be system-widesystem-wide).).

Page 9: 2.3 InterProcess Communication (IPC)

Binary semaphores = mutexBinary semaphores = mutex

►Create semaphore and initialize it to 1.Create semaphore and initialize it to 1. 1 = unlocked1 = unlocked 0 = locked0 = locked

►Then to use this as a mutex:Then to use this as a mutex: downdown

►c.s.c.s.

upup

Page 10: 2.3 InterProcess Communication (IPC)

POSIX SEMAPHORESPOSIX SEMAPHORES

Page 11: 2.3 InterProcess Communication (IPC)

POSIX Semaphores POSIX Semaphores (shared among (shared among

threads only)threads only)

#include <semaphore.h>#include <semaphore.h>

1.1. int sem_init ( sem_t* sem, int pshared, int sem_init ( sem_t* sem, int pshared, unsigned int value );unsigned int value );

2.2. int sem_wait ( sem_t* sem );int sem_wait ( sem_t* sem );3.3. int sem_trywait ( sem_t* sem );int sem_trywait ( sem_t* sem );4.4. int sem_post ( sem_t* sem );int sem_post ( sem_t* sem );5.5. int sem_getvalue ( sem_t* sem, int* sval );int sem_getvalue ( sem_t* sem, int* sval );6.6. int sem_destroy ( sem_t* sem );int sem_destroy ( sem_t* sem );

Page 12: 2.3 InterProcess Communication (IPC)

POSIX SemaphoresPOSIX Semaphores

int sem_init ( sem_t* sem, int pshared, int sem_init ( sem_t* sem, int pshared, unsigned int value );unsigned int value );

initializeinitialize

pshared must be 0 on (some versions of) pshared must be 0 on (some versions of) LinuxLinux►semaphore is not shared by processessemaphore is not shared by processes

Value is initial value for semaphore.Value is initial value for semaphore.

Page 13: 2.3 InterProcess Communication (IPC)

POSIX SemaphoresPOSIX Semaphores

int sem_wait ( sem_t* sem );int sem_wait ( sem_t* sem ); down (if possible/blocking)down (if possible/blocking)

int sem_trywait ( sem_t* sem );int sem_trywait ( sem_t* sem ); nonblocking downnonblocking down

►Blocking?Blocking?

Page 14: 2.3 InterProcess Communication (IPC)

POSIX SemaphoresPOSIX Semaphores

int sem_post ( sem_t* sem );int sem_post ( sem_t* sem ); up (nonblocking)up (nonblocking)

int sem_getvalue ( sem_t* sem, int* int sem_getvalue ( sem_t* sem, int* sval );sval ); get the current semaphore valueget the current semaphore value

int sem_destroy ( sem_t* sem );int sem_destroy ( sem_t* sem ); finish using the semaphorefinish using the semaphore

Page 15: 2.3 InterProcess Communication (IPC)

WINDOWS SEMAPHORESWINDOWS SEMAPHORES

Page 16: 2.3 InterProcess Communication (IPC)

Windows SemaphoresWindows Semaphores

HANDLE WINAPI CreateSemaphore (HANDLE WINAPI CreateSemaphore (

__in_opt LPSECURITY_ATTRIBUTES __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,lpSemaphoreAttributes,

__in LONG lInitialCount,__in LONG lInitialCount,

__in LONG lMaximumCount,__in LONG lMaximumCount,

__in_opt LPCTSTR lpName__in_opt LPCTSTR lpName

););

DWORD WINAPI WaitForSingleObject (DWORD WINAPI WaitForSingleObject (

__in HANDLE hHandle,__in HANDLE hHandle,

__in DWORD dwMilliseconds__in DWORD dwMilliseconds

);); //decrements count by 1 //decrements count by 1

BOOL WINAPI ReleaseSemaphore (BOOL WINAPI ReleaseSemaphore (

__in HANDLE hSemaphore,__in HANDLE hSemaphore,

__in LONG lReleaseCount,__in LONG lReleaseCount,

__out_opt LPLONG lpPreviousCount__out_opt LPLONG lpPreviousCount

);); //increments count by //increments count by lReleaseCountlReleaseCount

BOOL WINAPI CloseHandle (BOOL WINAPI CloseHandle (

__in HANDLE hObject__in HANDLE hObject

););

Page 17: 2.3 InterProcess Communication (IPC)

SYSTEM V SEMAPHORESSYSTEM V SEMAPHORES

Page 18: 2.3 InterProcess Communication (IPC)

System V Semaphores System V Semaphores (system-wide)(system-wide)

#include <sys/types.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/ipc.h>#include <sys/sem.h>#include <sys/sem.h>

1.1. int semget ( key_t key, int nsems, int semflg );int semget ( key_t key, int nsems, int semflg ); create/access existingcreate/access existing

2.2. int semctl ( int semid, int semnum, int cmd, ... int semctl ( int semid, int semnum, int cmd, ... );); delete from systemdelete from system

3.3. int semop ( int semid, struct sembuf* sops, int semop ( int semid, struct sembuf* sops, unsigned nsops );unsigned nsops ); used for used for upup and and downdown

Page 19: 2.3 InterProcess Communication (IPC)

Create/access existingCreate/access existing

//using the key, get the semaphore id//using the key, get the semaphore idconst int sid = semget( mySemKey, 1, IPC_CREAT | const int sid = semget( mySemKey, 1, IPC_CREAT |

0700 );0700 );

if (sid==-1) {if (sid==-1) { perror( "semget " );perror( "semget " ); exit( -1 );exit( -1 );}}printf( "sem id=%d \n", sid );printf( "sem id=%d \n", sid );

create if necessary

system-wide permissions

(In C/C++, octal values start with 0.)

system-wide unique number

Page 20: 2.3 InterProcess Communication (IPC)

Create/access existingCreate/access existing

//using the key, get the semaphore id//using the key, get the semaphore idconst int sid = semget( mySemKey, 1, IPC_CREAT | const int sid = semget( mySemKey, 1, IPC_CREAT |

0700 );0700 );

alternative (#include <sys/stat.h>):alternative (#include <sys/stat.h>):const int sid = semget( mySemKey, 1, IPC_CREAT | const int sid = semget( mySemKey, 1, IPC_CREAT |

S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTHS_IWOTH ); );

create if necessary

system-wide permissions

(In C/C++, octal values start with 0.)

Page 21: 2.3 InterProcess Communication (IPC)

Access and deleteAccess and delete

//using the key, get the semaphore id//using the key, get the semaphore idconst int sid = semget( mySemKey, 1, 0700 );const int sid = semget( mySemKey, 1, 0700 );

if (sid==-1) {if (sid==-1) { perror( "semget " );perror( "semget " ); exit( -1 );exit( -1 );}}printf( "sem id=%d \n", sid );printf( "sem id=%d \n", sid );

//delete the semaphore//delete the semaphoresemctl( sid, 0, IPC_RMID, 0 );semctl( sid, 0, IPC_RMID, 0 );

Page 22: 2.3 InterProcess Communication (IPC)

Down functionDown function

static void down ( const int whichSid ) {static void down ( const int whichSid ) { struct sembuf sem_lock;struct sembuf sem_lock;

sem_lock.sem_num = 0; //semaphore number: 0 = firstsem_lock.sem_num = 0; //semaphore number: 0 = first sem_lock.sem_op = -1; //semaphore operationsem_lock.sem_op = -1; //semaphore operation sem_lock.sem_flg = 0; //operation flagssem_lock.sem_flg = 0; //operation flags

if (semop(whichSid, &sem_lock, 1) == -1) {if (semop(whichSid, &sem_lock, 1) == -1) { perror("semop ");perror("semop "); exit(-1);exit(-1); }}}}

Page 23: 2.3 InterProcess Communication (IPC)

Up functionUp function

static void up ( const int whichSid ) {static void up ( const int whichSid ) { struct sembuf sem_unlock;struct sembuf sem_unlock;

sem_unlock.sem_num = 0; //semaphore number: 0 = firstsem_unlock.sem_num = 0; //semaphore number: 0 = first sem_unlock.sem_op = 1; //semaphore operationsem_unlock.sem_op = 1; //semaphore operation sem_unlock.sem_flg = 0; //operation flagssem_unlock.sem_flg = 0; //operation flags

if (semop(whichSid, &sem_unlock, 1) == -1) {if (semop(whichSid, &sem_unlock, 1) == -1) { perror("semop");perror("semop"); exit(-1);exit(-1); }}}}

Page 24: 2.3 InterProcess Communication (IPC)

Solution to (bounded) producer-consumer problem using semaphores (via up() and down()).

Note: 3 semaphores (one used as mutex)!

Page 25: 2.3 InterProcess Communication (IPC)

(BOUNDED) PRODUCER-(BOUNDED) PRODUCER-CONSUMER DEMOCONSUMER DEMO

bounded.h,bounded.h,

boundedProducer.cpp,boundedProducer.cpp,

boundedConsumer.cppboundedConsumer.cpp

Page 26: 2.3 InterProcess Communication (IPC)

UNBOUNDED PRODUCER-UNBOUNDED PRODUCER-CONSUMERCONSUMER

Page 27: 2.3 InterProcess Communication (IPC)

(Unbounded) producer-(Unbounded) producer-consumerconsumer

►Every time the producer process runs, Every time the producer process runs, it produces one item.it produces one item.

►Every time the consumer runs, it Every time the consumer runs, it consumes one item (or waits until one consumes one item (or waits until one is available, and then consumes it).is available, and then consumes it).

Page 28: 2.3 InterProcess Communication (IPC)

//unbounded producer//unbounded producerint main ( int argc, char* argv[] ) {int main ( int argc, char* argv[] ) { //using the key, get the semaphore id//using the key, get the semaphore id const int sid = semget( mySemKey, 1, IPC_CREAT | 0700 );const int sid = semget( mySemKey, 1, IPC_CREAT | 0700 ); if (sid==-1) {if (sid==-1) { perror( "semget " );perror( "semget " ); exit( -1 );exit( -1 ); }} printf( "sem id=%d \n", sid );printf( "sem id=%d \n", sid );

puts( "producing" );puts( "producing" ); struct sembuf sem_unlock;struct sembuf sem_unlock; sem_unlock.sem_num = 0; //semaphore number: 0 = firstsem_unlock.sem_num = 0; //semaphore number: 0 = first sem_unlock.sem_op = 1; //semaphore operationsem_unlock.sem_op = 1; //semaphore operation sem_unlock.sem_flg = 0; //operation flagssem_unlock.sem_flg = 0; //operation flags

if (semop(sid, &sem_unlock, 1) == -1) {if (semop(sid, &sem_unlock, 1) == -1) { perror("semop ");perror("semop "); exit(-1);exit(-1); }} puts( "produced" );puts( "produced" );

return 0;return 0;}}

Unbounded producer basically does an up() to indicate production.

Page 29: 2.3 InterProcess Communication (IPC)

//consumer//consumerint main ( int argc, char* argv[] ) {int main ( int argc, char* argv[] ) {

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

puts( "consuming" );puts( "consuming" ); struct sembuf sem_lock;struct sembuf sem_lock; sem_lock.sem_num = 0; //semaphore number: 0 = firstsem_lock.sem_num = 0; //semaphore number: 0 = first sem_lock.sem_op = -1; //semaphore operationsem_lock.sem_op = -1; //semaphore operation sem_lock.sem_flg = 0; //operation flagssem_lock.sem_flg = 0; //operation flags if (semop(sid, &sem_lock, 1) == -1) {if (semop(sid, &sem_lock, 1) == -1) { perror("semop ");perror("semop "); exit(-1);exit(-1); }} puts( "consumed" );puts( "consumed" );

return 0;return 0;}}

Consumer basically does a down() to indicate consumption.

Page 30: 2.3 InterProcess Communication (IPC)

(UNBOUNDED) (UNBOUNDED) PRODUCER-CONSUMER PRODUCER-CONSUMER DEMODEMO

mySemKey.h,mySemKey.h,

producer.cpp, consumerWait.cpp, consumerNoWait.cpp, producer.cpp, consumerWait.cpp, consumerNoWait.cpp, bigProducer.cpp,bigProducer.cpp,

delete.cppdelete.cpp

Page 31: 2.3 InterProcess Communication (IPC)

Shared MemoryShared Memory

Page 32: 2.3 InterProcess Communication (IPC)

Shared memory (Linux/Unix)Shared memory (Linux/Unix)

►Shared memory operationsShared memory operations

shmgetshmget►allocates a shared memory segmentallocates a shared memory segment

shmctlshmctl►allows the user to receive information on a allows the user to receive information on a

shared memory segment, set the owner, shared memory segment, set the owner, group, and permissions of a shared memory group, and permissions of a shared memory segment, or destroy a segmentsegment, or destroy a segment

Page 33: 2.3 InterProcess Communication (IPC)

Shared memoryShared memory

►Shared memory operationsShared memory operations

shmatshmat►attaches the shared memory segment attaches the shared memory segment

(identified by shmid) to the address space of (identified by shmid) to the address space of the calling processthe calling process

shmdtshmdt►detaches the shared memory segment detaches the shared memory segment

(located at the address specified by shmaddr) (located at the address specified by shmaddr) from the address space of the calling processfrom the address space of the calling process

Page 34: 2.3 InterProcess Communication (IPC)

Shared memoryShared memory

►From the Linux sem_init man page:From the Linux sem_init man page: The pshared argument indicates whether this The pshared argument indicates whether this

semaphore is to be shared between the threads semaphore is to be shared between the threads of a process, or between processes.of a process, or between processes.

1.1. If pshared has the value 0, then the semaphore is If pshared has the value 0, then the semaphore is shared between the threadsshared between the threads of a process, and of a process, and should be should be located at some address that is visible to located at some address that is visible to all threadsall threads (e.g., a global variable, or a variable (e.g., a global variable, or a variable allocated dynamically on the heap).allocated dynamically on the heap).

2.2. If pshared is nonzero, then the semaphore is If pshared is nonzero, then the semaphore is shared between processesshared between processes, and should be , and should be located located in a region of shared memoryin a region of shared memory..

Page 35: 2.3 InterProcess Communication (IPC)

Memory mapped filesMemory mapped files

Page 36: 2.3 InterProcess Communication (IPC)

Memory mapped filesMemory mapped files

““There comes a time when you want to read and write to There comes a time when you want to read and write to and from files so that the information is shared and from files so that the information is shared between processes. Think of it this way: two between processes. Think of it this way: two processes both open the same file and both read and processes both open the same file and both read and write from it, thus sharing the information. The write from it, thus sharing the information. The problem is, sometimes it's a pain to do all those problem is, sometimes it's a pain to do all those fseek()s and stuff to get around. Wouldn't it be easier fseek()s and stuff to get around. Wouldn't it be easier if you could just map a section of the file to memory, if you could just map a section of the file to memory, and get a pointer to it? Then you could simply use and get a pointer to it? Then you could simply use pointer arithmetic to get (and set) data in the file.pointer arithmetic to get (and set) data in the file.

Well, this is exactly what a memory mapped file is. And Well, this is exactly what a memory mapped file is. And it's really easy to use, too. A few simple calls, mixed it's really easy to use, too. A few simple calls, mixed with a few simple rules, and you're mapping like a with a few simple rules, and you're mapping like a mad-person.”mad-person.”

http://beej.us/guide/ipc/mmap.htmlhttp://beej.us/guide/ipc/mmap.html

Page 37: 2.3 InterProcess Communication (IPC)

Memory mapped files Memory mapped files (Unix/Linux)(Unix/Linux)

►mmapmmap

void* mmap ( void* start, size_t length, int void* mmap ( void* start, size_t length, int prot, int flags, int fd, off_t offset );prot, int flags, int fd, off_t offset );

map map lengthlength bytes starting at offset bytes starting at offset offsetoffset from the file specified by the file from the file specified by the file descriptor descriptor fdfd into memory, preferably at into memory, preferably at address address startstart

Page 38: 2.3 InterProcess Communication (IPC)

Memory mapped files Memory mapped files (Unix/Linux)(Unix/Linux)

► munmapmunmap

int munmap ( void* start, size_t length );int munmap ( void* start, size_t length );

The munmap system call deletes the mappings for The munmap system call deletes the mappings for the specified address range, and causes further the specified address range, and causes further references to addresses within the range to generate references to addresses within the range to generate invalid memory references.invalid memory references.

The region is also automatically unmapped when the The region is also automatically unmapped when the process is terminated.process is terminated.

On the other hand, closing the file descriptor does not On the other hand, closing the file descriptor does not unmap the region.unmap the region.

Page 39: 2.3 InterProcess Communication (IPC)

Shared memory and memory Shared memory and memory mapped files (Windows)mapped files (Windows)

►Windows combines both mechanisms into Windows combines both mechanisms into one set of function calls.one set of function calls.

If the file actually exists, then it will be memory-If the file actually exists, then it will be memory-mapped (via call to CreateFileMapping).mapped (via call to CreateFileMapping).

But if the hFile arg (to CreateFileMapping) is But if the hFile arg (to CreateFileMapping) is INVALID_HANDLE_VALUE, then the memory will INVALID_HANDLE_VALUE, then the memory will be backed by an entry in the system paging file. be backed by an entry in the system paging file. (Windows refers to this are (Windows refers to this are named shared named shared memorymemory.).)

Page 40: 2.3 InterProcess Communication (IPC)

Memory mapped files Memory mapped files (Windows)(Windows)

HANDLE WINAPI CreateFileMapping (HANDLE WINAPI CreateFileMapping (

__in HANDLE hFile,__in HANDLE hFile,

__in_opt LPSECURITY_ATTRIBUTES lpAttributes,__in_opt LPSECURITY_ATTRIBUTES lpAttributes,

__in DWORD flProtect,__in DWORD flProtect,

__in DWORD dwMaximumSizeHigh,__in DWORD dwMaximumSizeHigh,

__in DWORD dwMaximumSizeLow,__in DWORD dwMaximumSizeLow,

__in_opt LPCTSTR lpName__in_opt LPCTSTR lpName

););

LPVOID WINAPI MapViewOfFile (LPVOID WINAPI MapViewOfFile (

__in HANDLE hFileMappingObject,__in HANDLE hFileMappingObject,

__in DWORD dwDesiredAccess,__in DWORD dwDesiredAccess,

__in DWORD dwFileOffsetHigh,__in DWORD dwFileOffsetHigh,

__in DWORD dwFileOffsetLow,__in DWORD dwFileOffsetLow,

__in SIZE_T dwNumberOfBytesToMap__in SIZE_T dwNumberOfBytesToMap

););

Page 41: 2.3 InterProcess Communication (IPC)

Creating shared memory via Creating shared memory via Windows*Windows*

*http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551%28v=vs.85%29.aspx*http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551%28v=vs.85%29.aspx

► First process (creator):First process (creator):1.1. The first process creates the file mapping object by calling the The first process creates the file mapping object by calling the

CreateFileMapping function with INVALID_HANDLE_VALUE and a CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object.name for the object.

2.2. Then the process uses the file mapping object handle (that Then the process uses the file mapping object handle (that CreateFileMapping returns) in a call to MapViewOfFile to create a CreateFileMapping returns) in a call to MapViewOfFile to create a view of the file in the process address space. The MapViewOfFile view of the file in the process address space. The MapViewOfFile function returns a pointer to the file view, pBuf.function returns a pointer to the file view, pBuf.

3.3. The process then uses the CopyMemory function to write a string The process then uses the CopyMemory function to write a string to the view that can be accessed by other processes.to the view that can be accessed by other processes.

4.4. When the process no longer needs access to the file mapping When the process no longer needs access to the file mapping object, it should call UnmapViewOfFile(pBuf) and then object, it should call UnmapViewOfFile(pBuf) and then CloseHandle.CloseHandle.

► When all handles are closed, the system can free the When all handles are closed, the system can free the section of the paging file that the object uses.section of the paging file that the object uses.

Page 42: 2.3 InterProcess Communication (IPC)

Creating shared memory via Creating shared memory via WindowsWindows

► Second/other processes:Second/other processes:1.1. A second process can access the string written to the A second process can access the string written to the

shared memory by the first process by calling the shared memory by the first process by calling the OpenFileMapping function specifying the same name for OpenFileMapping function specifying the same name for the mapping object as the first process.the mapping object as the first process.

2.2. Then it can use the MapViewOfFile function to obtain a Then it can use the MapViewOfFile function to obtain a pointer to the file view, pBuf.pointer to the file view, pBuf.

Page 43: 2.3 InterProcess Communication (IPC)

Example: creating shared memory Example: creating shared memory via Windows – first process (creator)via Windows – first process (creator)

#include <windows.h>#include <stdio.h>#include <conio.h>#include <tchar.h>

#define BUF_SIZE 256TCHAR 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, 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);

CloseHandle(hMapFile);

return 0;}

Page 44: 2.3 InterProcess Communication (IPC)

Example: creating shared memory Example: creating shared memory via Windows – second/other via Windows – second/other processesprocesses#include <windows.h>

#include <stdio.h>#include <conio.h>#include <tchar.h>#pragma comment(lib, "user32.lib")

#define BUF_SIZE 256TCHAR 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, 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);

CloseHandle(hMapFile);

return 0;}