Inter Process Communication - PowerPoint PPT Presentation

1 / 75
About This Presentation
Title:

Inter Process Communication

Description:

order to put function calls together to create services. IPC ... To create two processes that communicate with the pipe ... Windows create an anonymous pipe ... – PowerPoint PPT presentation

Number of Views:245
Avg rating:5.0/5.0
Slides: 76
Provided by: greg66
Category:

less

Transcript and Presenter's Notes

Title: Inter Process Communication


1
Inter Process Communication
  • Dr. Gregory Vert

2
What to Know
  • This modules reading exposes you to UNIX
    programming of pipes, locks, messages etc
  • The lecture exposes you to Windows version of the
    UNIX reading
  • For study questions know
  • concepts
  • equivalent function calls in unix / windows
  • order to put function calls together to create
    services

3
IPC
  • Network programming is really about communication
    between processes
  • Dont need a wire to do this
  • First network programming was between processes
    on same computer

4
IPC
  • Process data space drawing
  • How do processes communicate
  • Most system processes are daemons
  • What is a daemon ?

5
IPC
  • Several types of IPC
  • pipes
  • named pipes
  • message queues
  • shared memory
  • semaphores

6
IPC
  • When to use IPC
  • locking of file information
  • crud in db
  • wild west update, delete of information
  • database example
  • access to resources
  • class examples

7
IPC
  • Types of Locking
  • Advisory
  • maintains a record of locks but does not prevent
    access
  • File level
  • lock the entire file
  • Record level
  • lock a small part of the file

8
IPC
  • Locking functions
  • System V
  • lockf()
  • a blocking call
  • what is blocking process goes to sleep and waits

9
IPC
  • Locking functions
  • The LockFile and LockFileEx functions lock a
    specified range of bytes in a file.
  • Locking part of a file prevents all other
    processes from reading or writing anywhere in the
    specified area.
  • Attempts to read from or write to a region locked
    by another process always fail.

10
IPC
  • Locking Functions
  • The LockFileEx function allows an application to
    specify either a shared lock or an exclusive
    lock.
  • An exclusive lock denies all other processes both
    read and write access to the specified region of
    the file.
  • A shared lock denies all processes write access
    to the specified region of the file, including
    the process that first locks the region. This can
    be used to create a read-only region in a file.

11
IPC
  • BOOL LockFile(
  • HANDLE hFile, // handle to file
  • DWORD dwFileOffsetLow, // low-order word of
    offset used with Offset high
  • DWORD dwFileOffsetHigh, // high-order word of
    offset
  • DWORD nNumberOfBytesToLockLow, // low-order word
    of length
  • DWORD nNumberOfBytesToLockHigh // high-order word
    of length
  • )
  • Note offsetlow and high are combined to create a
    larger value to lock
  • Note dw double word

12
Summary
  • Several types of IPC
  • pipes
  • named pipes
  • message queues
  • shared memory
  • Semaphores
  • Locking Functions

13
IPC
  • Simpler Function
  • include ltio.hgt
  • int lock (int handle, long start_position, long
    byte_count)
  • What are the limitations of this locking
    capability ?
  • All of the above functions return a value -1 if
    it fails to grab a lock
  • This function provides an interface to DOS
    file-sharing. Argument handle specifies a file
    handle, offset indicates the offset, and length
    indicates the file length.
  • Return Value Returns 0. If unsuccessful,
    returns -1 and sets errno to EACCES.

14
IPC
  • Client Server Communication
  • simple example
  • idea is that a service is rendered - IPC

15
IPC
  • Pipes
  • allow communication between two processes
  • processes exist in different process spaces
  • method of use is read / write to the pipe
  • A pipe is generally one directional SO you need
    to create a pipe to read and a pipe to write

16
Unix Pipes
  • Programs normally have some input and some
    output. That is that some data has to be entered
    and some kind of analysis will come out
    afterwards.
  • Programs for UNIX are usually written as though
    input is typed in by the user, and output appears
    on the screen. The "redirection" of the data, to
    or from files, to printers, to other programs is
    controlled by "pipes".
  • The pipes channel streams of data to different
    places. For instance, there is a program (or UNIX
    command) in UNIX which reports who is logged onto
    the system
  • who
  • This can be piped to the printer who lpr

17
Unix Pipes
  • Unix pipes allow communication between two
    processes using a buffer.
  • ls l grep test
  • One process writes to the buffer.
  • One process reads from the buffer.
  • The communication is unidirectional.
  • The buffer is accessed using a file descriptor.
  • The difference between a file and a pipe pipe
    is a data structure in the kernel.
  • Typical size is 512 bytes (Minimum limit defined
    by POSIX)

18
IPC
  • Unix Pipes A pipe is created by using the pipe
    system call
  • int pipe(int filedes)
  • - Two file descriptors are returned
  • filedes0 is open for reading
  • filedes1 is open for writing
  • use
  • write (pipefd1..
  • read (pipefd0.

19
IPC
  • To create two processes that communicate with the
    pipe
  • fork a child process and use the pipe fds to
    read / write
  • In UNIX and Windows the operator creates pipes
    between the outputs of OS commands

20
IPC
  • Window Pipes
  • A pipe is a section of shared memory that
    processes use for communication. The process that
    creates a pipe is the pipe server . A process
    that connects to a pipe is a pipe client MSDN
  • Types
  • Anonymous (more common)- lives as long as the
    process
  • Named (used for IPC) - exists beyond the life of
    the process

21
IPC
  • Windows create an anonymous pipe
  • A traditional pipe is "unnamed" because it exists
    anonymously and persists only for as long as the
    process is running.
  • An anonymous pipe is an unnamed, one-way pipe
    that typically transfers data between a parent
    process and a child process.
  • Anonymous pipes are always local they cannot be
    used for communication over a network.

22
IPC
  • BOOL CreatePipe( PHANDLE hReadPipe, // read
    handle PHANDLE hWritePipe, // write handle
  • LPSECURITY_ATTRIBUTES lpPipeAttributes, //
    security attributes DWORD nSize // pipe size )
  • Use Readfile / Write file to read and write to
    the pipe
  • For more information, search in MSDN on Pipes,
    sample code can be found

23
IPC
  • Named Pipes
  • One-way or duplex pipe for communication between
    the pipe server and one or more pipe clients.
  • All instances of a named pipe share the same pipe
    name, but each instance has its own buffers and
    handles
  • Used for client / server communication
  • A named pipe is system-persistent and must be
    "unlinked" or deleted once it is no longer being
    used.
  • Multiple pipe clients to use the same named pipe
    simultaneously.

24
IPC
  • Named Pipes (called FIFOs in UNIX)
  • Any process can access named pipes, subject to
    security checks
  • Named pipes can be used to provide communication
    between processes on the same computer or between
    processes on different computers across a
    network.
  • Any process can act as both a server and a
    client, making peer-to-peer communication
    possible.

25
IPC
  • Pipe Commands
  • CallNamedPipe Connects to a message-type pipe,
    writes to and reads from the pipe, and then
    closes the pipe.
  • ConnectNamedPipe Enables a named pipe server
    process to wait for a client process to connect
    to an instance of a named pipe.
  • CreateNamedPipe Creates an instance of a named
    pipe and returns a handle for subsequent pipe
    operations.
  • CreatePipe Creates an anonymous pipe.
  • DisconnectNamedPipe Disconnects the server end of
    a named pipe instance from a client process.

26
IPC
  • Pipe Commands
  • GetNamedPipeHandleState Retrieves information
    about a specified named pipe.
  • GetNamedPipeInfo Retrieves information about the
    specified named pipe.
  • PeekNamedPipe Copies data from a named or
    anonymous pipe into a buffer without removing it
    from the pipe.
  • SetNamedPipeHandleState Sets the read mode and
    the blocking mode of the specified named pipe.

27
IPC
  • Pipe Commands
  • TransactNamedPipe Combines the functions that
    write a message to and read a message from the
    specified named pipe into a single network
    operation.
  • WaitNamedPipe Waits until either a time-out
    interval elapses or an instance of the specified
    named pipe is available for a connection.

28
Step
  • Client Server Communication
  • Server
  • Create a pipe
  • Connect
  • Read/Write
  • Closes handle
  • Client
  • Opens Handle
  • Waits
  • Write/Read
  • Closes handle

29
IPC
  • Order of Pipe functions calls for a Server
  • CreateNamedPipe()
  • ConnectNamedPipe() wait for client to connect,
    a blocking call
  • Readfile() - using the handle of the pipe
  • Writefile() - using the handle of the pipe
  • TransactNamedPipe read/write
  • FlushFileBuffers
  • DisconnectNamedPipe
  • CloseHandle(pipe handle)

30
IPC
  • The CreateNamedPipe (Windows) function creates an
    instance of a named pipe and returns a handle for
    subsequent pipe operations.
  • A named pipe server process uses this function
    either to create the first instance of a specific
    named pipe and establish its basic attributes or
    to create a new instance of an existing named
    pipe.

31
IPC
  • HANDLE CreateNamedPipe(
  • LPCTSTR lpName, // pipe name
  • DWORD dwOpenMode, // pipe open mode
  • DWORD dwPipeMode, // pipe-specific modes
  • DWORD nMaxInstances, // maximum number of
    instances
  • DWORD nOutBufferSize, // output buffer size
  • DWORD nInBufferSize, // input buffer size
  • DWORD nDefaultTimeOut, // time-out interval
    LPSECURITY_ATTRIBUTES lpSecurityAttributes // SD
  • )

32
IPC
  • Sample code on named pipes in MSDN
  • Search for pipes
  • http//msdn2.microsoft.com/en-us/library/aa365780.
    aspx

33
IPC
  • The TransactNamedPipe function is most commonly
    used by a pipe client to write a request message
    to the named pipe server and read the server's
    response message.
  • The pipe client must specify GENERIC_READ
    GENERIC_WRITE access when it opens its pipe
    handle by calling the CreateFile function.
  • The pipe client sets the pipe handle to
    message-read mode by calling the
    SetNamedPipeHandleState function.
  • The client can read the remainder of the message
    by calling either the ReadFile, ReadFileEx, or
    PeekNamedPipe function.

34
IPC
  • Order of Pipe functions calls for a Client
  • Createfile opens handle to pipe
  • CreateFile function fails if the access specified
    is incompatible with the access specified
    (duplex, outbound, or inbound)
  • WaitNamedPipe waits for instance of pipe to
    become availble
  • SetNamedPipeHandleState set message mode to
    same as server
  • WriteFile / ReadFile
  • CloseHandle use handle of pipe

35
SetNamedPipeHandleState
  • Sets the read mode and the blocking mode of the
    specified named pipe. If the specified handle is
    to the client end of a named pipe and if the
    named pipe server process is on a remote
    computer, the function can also be used to
    control local buffering.
  • BOOL SetNamedPipeHandleState
  • ( HANDLE hNamedPipe, //A handle to the named pipe
    instance.
  • LPDWORD lpMode, //a combination of a read-mode
    flag and a wait-mode flag
  • LPDWORD lpMaxCollectionCount, //The maximum
    number of bytes collected on the client
    computer before transmission to the server.
  • LPDWORD lpCollectDataTimeout //The maximum time,
    in milliseconds, that can pass before
    a remote named pipe transfers information
    over the network. )
  • Return Value
  • If the function succeeds, the return value is
    nonzero.
  • If the function fails, the return value is zero.

36
Announcement
  • There will be a short quiz on Wednesday covering
    this chapter from slide 1 to the last slide
    covered.

37
Summary
  • Pipes in Unix.
  • Pipes in Windows (named and anonymous).
  • Several functions to create, connect pipes.

38
Message Queues
Two (or more) processes can exchange information
via access to a common system message queue.
39
Basic Message Passing
  • IPC messaging lets processes send and receive
    messages, and queue messages.
  • Unlike the data flow of pipes, each IPC message
    has an explicit length.
  • Messages can be assigned a specific type.
  • A server process can direct message traffic
    between clients on its queue by using the client
    process PID as the message type.
  • For single-message transactions, multiple server
    processes can work in parallel on transactions
    sent to a shared message queue.

40
Message Queues
  • System V UNIX has a more generalized message
    queue system
  • Messages are stored in kernel
  • Goal of message queues
  • mux the message for multiple clients from one
    queue
  • pid can allow message to go to specific processes

41
Message Queues
  • Before a process can send or receive a message,
    the queue must be initialized (through the msgget
    function)
  • Operations to send and receive messages are
    performed by the msgsnd() and msgrcv() functions,
    respectively.

42
Message Queues
  • When a message is sent, its text is copied to the
    message queue.
  • The msgsnd() and msgrcv() functions can be
    performed as either blocking or non-blocking
    operations.
  • Non-blocking operations allow for asynchronous
    message transfer -- the process is not suspended
    as a result of sending or receiving a message.
  • In blocking or synchronous message passing the
    sending process cannot continue until the message
    has been transferred or has even been
    acknowledged by a receiver.

43
Message Queues
  • A blocked message operation remains suspended
    until one of the following three conditions
    occurs
  • The call succeeds.
  • The process receives a signal.
  • The queue is removed.

44
Message Queues
  • Functions
  • note all of these use msqid to identify the
    queue to access
  • msgget creates a queue or access it
  • access to message specified by flag owner, group,
    world

45
Initialize the Queue
  • int msgget(key_t key, int msgflg)
  • returns the message queue ID (msqid) of the queue
    corresponding to the key argument.
  • msgflg must be an octal integer with settings for
    the queue's permissions and control flags.

46
Functions
  • The following code illustrates the msgget()
    function.
  • include ltsys/ipc.hgt
  • include ltsys/msg.hgt
  • ...
  • key_t key / key to be passed to msgget() /
  • int msgflg / msgflg to be passed to msgget() /
  • int msqid / return value from msgget() / ...
  • key ...
  • msgflg ...
  • if ((msqid msgget(key, msgflg)) -1)
  • perror("msgget msgget failed") exit(1)
  • else (void) fprintf(stderr, ldquomsgget
    succeeded")

47
Message Queues
  • msgsnd puts a message onto a queue
  • int msgsnd(int msqid,
  • const void msgp,
  • size_t msgsz,
  • int msgflg)
  • msgrcv reads messages from a message queue
  • int msgrcv(int msqid,
  • void msgp,
  • size_t msgsz,
  • long msgtyp,
  • int msgflg)
  • returns number of bytes in message
  • ptr to message data

48
Functions
  • The msqid argument must be the ID of an existing
    message queue. The msgp argument is a pointer to
    a structure that contains the type of the message
    and its text.
  • Example of a user-defined buffer
  • struct mymsg
  • long mtype / message type /
  • char mtextMSGSZ / message text of length
    MSGSZ /
  • The msgsz argument specifies the length of the
    message in bytes.

49
Functions
  • msgtype is the received message's type as
    specified by the sending process.
  • msgflg specifies the action to be taken if
  • The number of bytes already on the queue is equal
    to msg_qbytes.
  • The total number of messages on all queues
    system-wide is equal to the system-imposed limit.
  • Ex calling process will return, process
    suspended.
  • msgctl a function that allows you to control a
    message queue.
  • Link to code http//www.cs.cf.ac.uk/Dave/C/no
    de25.html

50
Message Queues
  • Message can be written to the kernel by a process
    that terminates and then read by another process
    at some later time
  • Messages in windows are really routed from the OS
    to a given GUI application where the message is
    processed by a procedure

51
Message Queues
  • SendMessage calls the window procedure for the
    specified window and does not return until the
    window procedure has processed the message.
  • To use, you need the handle to the window
  • sends the type of messages we see in MFC message
    maps
  • PostMessage function, in contrast, posts a
    message to a thread's message queue and returns
    immediately.

52
Message Queues
  • LRESULT SendMessage( HWND hWnd, UINT Msg, WPARAM
    wParam, LPARAM lParam )
  • Parameters
  • hWnd
  • in Handle to the window whose window procedure
    will receive the message. If this parameter is
    HWND_BROADCAST, the message is sent to all
    top-level windows in the system, including
    disabled or invisible unowned windows, overlapped
    windows, and pop-up windows but the message is
    not sent to child windows.
  • Msg
  • in Specifies the message to be sent.
  • wParam
  • in Specifies additional message-specific
    information.
  • lParam
  • in Specifies additional message-specific
    information.

53
Synchronization
  • Need to coordinate activities between threads and
    processes
  • Used mostly for access to data and resources

54
Communicating Processes
  • Problem Controlling access to shared resource.
  • Suppose two processes share access to a file, or
    shared memory segment, and at least one of these
    processes can modify the data in this shared area
    (critical section) of memory.
  • We must ensure that only one process execute a
    critical section of code at a time.
  • The Critical Section Problem is to design a
    protocol that the processes can use to coordinate
    their activities when one wants to enter its
    critical section of code.

55
Semaphores
  • Semaphores are counters of resources
  • Processes wait on a semaphore to access a
    resource
  • Semaphores are not owned by a process

56
Semaphores
  • Semaphore example
  • 3 serial ports on a computer
  • every time a process uses a port, the count is
    decremented
  • every time a process releases a port the count is
    incremented
  • when count 0, a process waits to use port until
    it is greater than 0

57
Example
  •   if (0 lt V)       V--
  • We can use the semaphore to synchronize
  • our processes       // Entry Section      
    wait(S)       // Critical Section       if (0
    lt V)           V--       // Exit
    Section       signal(S)

wait(S)       while(S 0)      S--and
signal is given bysignal(S)       S--
58
Semaphores
  • UNIX
  • semget creates a semaphore
  • can have a set of semaphores created
  • semop performs operations on the semaphore
  • e.g. changing the value of the semaphore
  • semctl performs control operations on a
    semaphore
  • e.g. remove a semaphore from system

59
Semget()
  • PurposeRequest a semaphore
  • includeltsys/types.hgtincludeltsys/ipc.hgtinclude
    ltsys/sem.hgt
  • int  semget(key_t key, int numsems, int flag)
  • key   key valuenumsems    number of
    semaphoresflag    IPC_CREAT or IPC_EXCL
  • Returns-1 on error semaphore ID if successful
  • Errors
  • ENOMEM  No memory availableEEXIST  structure
    exists and IPC_EXCL set

60
semop
  • Implement a predefined action on a semaphore set
  • int semop(int semaid,
  • struct sembuf semopsarray,
  • size_t nops)
  • semaid    semaphore IDsemopsarray   
    operations to be performed on the
    semaphoresnops    length of semopsarray
  • Returns-1 on error or SUCCESS
  • Errors
  • EAGAIN  Process would wait and IPC_NOWAIT set 

61
Example
  • // Entry Section       semop(semaid, WAIT,
    1)       // Critical Section       if (0 lt
    V)           V--       // Exit Section      
    semop(semaid, SIGNAL, 1)

62
semctl
  • Mainly to remove a semaphore structure from the
    system, and to set the values of each semaphore
    in the structure.
  • includeltsys/types.hgtincludeltsys/ipc.hgtinclude
    ltsys/sem.hgt
  • int semctl(int semaid, int semnum, int cmd, union
    semun arg)
  • semaid    semaphore IDsemnum    between 0 and
    numsems-1cmd    SETVAL or SETALLarg    union
    semun
  • Returns 0, for the two commands aboveErrors
    NONE

63
Code
  •   // Entry Section       semop(semaid, WAIT,
    1)       // Critical Section       if (0 lt
    V)           V--       // Exit Section      
    semop(semaid, SIGNAL, 1)

64
Semaphores
  • Window Order of Function Calls
  • The CreateSemaphore function creates or opens a
    named or unnamed semaphore object.
  • HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES
    lpSemaphoreAttributes, // SD
  • LONG lInitialCount, //
    initial count LONG lMaximumCount, //
    maximum count LPCTSTR lpName //
    object name
  • )

65
Semaphores
  • Use the handle from CreateSemaphore in a number
    of Wait functions
  • Wait functions wait for the semaphore to hit be
    signaled (greater than 0)
  • they sleep in the mean time
  • when signaled the semaphore is decreased by 1
  • Multiple objects can use the same semaphores
    handle to synchronize

66
Semaphores
  • Wait Functions in Windows/ Acquire Sempahore
  • SignalObjectAndWait, WaitForSingleObject, and
    WaitForSingleObjectEx functions require a handle
    to one synchronization object.
  • These functions return when one of the following
    occurs
  • The specified object is in the signaled state.
  • The time-out interval elapses. The time-out
    interval can be set to INFINITE to specify that
    the wait will not time out.

67
Semaphores
  • How to Share a semaphore with other processes
  • The OpenSemaphore function opens an existing
    named semaphore object.
  • HANDLE OpenSemaphore(
  • DWORD dwDesiredAccess, // access
  • BOOL bInheritHandle, // inheritance option
    LPCTSTR lpName // object name
  • )

68
Semaphore
  • In Windows, ReleaseSemaphore() is used release
    the semaphore.
  • BOOL ReleaseSemaphore( HANDLE hSemaphore, LONG
    lReleaseCount, LPLONG lpPreviousCount )
  • hSemaphore is a pointer to the handle of the
    semaphore.
  • lReleaseCount is the semaphore count incremented
    by the specified amount.
  • lpPreviousCount is the pointer to the variable
    where the previous semaphore count is returned.

69
Mapping
  • Windows Linux Process
  • CreateSemaphore semget
    semctl
  • OpenSemaphore semget
  • WaitForSingleObject semop
  • ReleaseSemaphore semop
  • CloseHandle semctl

70
Shared Memory
  • Allows two or more processes to share memory
    without involving the kernel for IPC calls
  • Can use semaphores to coordinate access to shared
    memory
  • Can create a client / server model

71
Shared Memory
  • UNIX
  • shmget creates shared memory segment
  • returns a shared memory identifier
  • specify read / write by owner, group, world
  • shmat attaches memory and makes it available
  • returns pointer to shared memory used for read /
    write
  • shmdt detaches memory when done with using it
  • shmctl deletes a shared memory segment

72
Shared Memory
  • Reading and writing to shared memory is done via
  • using ptr returned from shmat in a read / write
    function or others that take a pointer to a
    buffer
  • e.g. fgets, fputs

73
Shared Memory
  • Windows
  • File mapping can be used to share a file or
    memory between two or more processes.
  • To share a file or memory, all of the processes
    must use the name or the handle of the same
    file-mapping object.
  • To share a file, the first process creates or
    opens a file by using the CreateFile function.
  • Next, it creates a file-mapping object by using
    the CreateFileMapping function, specifying the
    file handle and a name for the file-mapping
    object.

74
Shared Memory
  • The easiest way for other processes to obtain a
    handle of the file-mapping object created by the
    first process is to use the OpenFileMapping
    function and specify the object's name. This is
    referred to as named shared memory

75
Shared Memory
  • Each process uses the UnmapViewOfFile function to
    invalidate the pointer to the mapped memory.
  • This destroys the file view in the address space
    of the process. If any pages of the file view
    have changed since the file view was mapped,
    UnmapViewOfFile also copies the changed pages to
    the file on disk.
  • Use CloseHandle to complete clean up
Write a Comment
User Comments (0)
About PowerShow.com