IPC Interprocess Communication - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

IPC Interprocess Communication

Description:

... two unrelated processes on the same machine, on the LAN or over the internet. ... Once the fork has occurred though, there is no longer a link between the parent ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 13
Provided by: Informatio55
Category:

less

Transcript and Presenter's Notes

Title: IPC Interprocess Communication


1
IPC Interprocess Communication
  • Chapter 28

2
Whats IPC?
  • Interprocess communication is used to transfer
    data from one process to another. This can be
    between parent/child, two unrelated processes on
    the same machine, on the LAN or over the
    internet. We will focus mainly on communicating
    data between parent child processes.

3
Family Talk
  • In the last lecture (chapter 27), we said the
    parent process passes all of its data to the
    child on a fork() call because the child is a
    duplicate of the parent. Once the fork has
    occurred though, there is no longer a link
    between the parent and child, so theres no
    built-in way to pass data.
  • There are a five ways to pass data once the fork
    has occurred. Each method below becomes
    progressively harder to accomplish as you go down
    the list.
  • Environment variables
  • Pipes
  • Signals
  • Sockets
  • Shared Memory

4
Environment Variables
  • The easiest way to pass data is through
    environment variables. Each process has the
    ability to set (setenv()) get (getenv())
    variables.
  • The disadvantage is that the processes
    communicating with environment variables must be
    run from the same shell login.
  • You also cannot block one process to wait for the
    other process when using environment variables.

5
Pipes
  • A pipe is a unidirectional communication link
    between two or more porcesses, which resembles an
    ordinary file from the process perspective.
    Processes can write data into one end of a pipe
    for others to read. Pipes do not work across a
    network.
  • Pipes are generally implemented as sockets, but
    much of the complexity is hidden from the
    programmer.
  • There are some important differences between a
    pipe and an ordinary file.
  • A pipe is a fixed size buffer. Once the buffer
    is full, any attempt to write to the buffer will
    cause the process to block, waiting for data to
    be removed from the pipe.
  • Reading from a pipe is destructive the data is
    removed from the pipe. Reading from an empty
    pipe will block until data is written into the
    pipe.
  • EOF only occurs when the last process with write
    access to the pipe closes it.
  • There are two types of pipes, named and unnamed.
  • Unnamed pipes can only be used to connect parent
    child.
  • Named pipes can be used to connect unrelated
    processes.

6
Unnamed Pipes
  • int pipe( int fd2 )
  • returns 0 on success, -1 on failure
  • This function creates a pipe, and two associated
    file descriptors. The first descriptor, fd0 is
    used for reading from the pipe, and fd1 is used
    for writing.
  • The pipe() function is called before a fork so
    that the child inherits copies of the file
    descriptors.
  • Normally, one process is designated as the
    reader and the other only writes to the pipe.
    Hence, each process would close one of the
    descriptors immediately after the fork().

7
  • include ltstdio.hgt
  • include ltstring.hgt
  • include ltunistd.hgt
  • define BUFF_SIZE 40
  • int main(int argc,char argv)
  • int fd2
  • char buffBUFF_SIZE1
  • if ( pipe(fd) 0 )
  • if ( fork() ! 0 )
  • / Parent reader /
  • close(fd1)
  • / Read message from parent /

8
Another Pipe
  • File popen( char command, char mode)
  • returns a pointer to the opened stream, or NULL
    on error.
  • The popen() function executes the specified
    command, and establishes a pipe connection to
    either its standard input (if mode is r) or
    its standard output (if mode is w).
  • You could use this to run another program such as
    ls which would get a directory listing for your
    program without displaying it to the user.
  • Files opened with popen() must be closed with
    pclose() not fclose()

9
Named Pipes
  • Named pipes can be used to connect unrelated
    process running on the same computer. They are
    referenced as a special file which can be opened
    by name just like an ordinary file.
  • int mknod(char path, mode_t mode, int dev)
  • Returns 0 on success, -1 on failure.
  • The dev argument is ignored for pipes.
  • int mkfifo(char path, mode_t mode)
  • Returns 0 on success, -1 on failure.
  • An easier way to make a named pipe, calls mknod()
    for you.
  • The mode argument specifies only the file
    permissions, since the file type is always a
    pipe.
  • Once created, the pipe can be opened with open()
    or fopen() just like a normal file.
  • It must be closed explicitly and removed with
    remove() or unlink().

10
Signals
  • You can communicate to your processes with
    signals like kill, terminate (TERM), hang up
    (HUP), interrupt (CTRL C), etc. In order to
    deal with these signals in a custom manner
    (instead of just dieing on ctrlc for instance)
    you need to specify handlers for each of the
    signals you want to catch.
  • Signals are asynchronous, meaning that they can
    arrive at any moment and not at any specified
    point.
  • Signal handling, sockets and shared memory(in the
    next slides) are complex, and beyond the scope of
    this course, so I will not go into detail. You
    do need to know that they exist.

11
Sockets
  • Sockets in C are just like those you dealt with
    in Client/Server programming. They allow you to
    send data either from one process to another on
    the same machine, or between processes on
    different machines across a network.
  • The general premise is to
  • create two sockets, A and B
  • connect the sockets
  • Set socket A to listen for connection requests
  • Send a connection request from B to A
  • Have A accept the connection request
  • send/receive data
  • Socket functions definitions are provided in
    ltsys/socket.hgt

12
Shared Memory
  • Shared memory is an alternative form of IPC that
    does not involve file descriptors. Shared memory
    objects are blocks of memory which can be
    accessed directly by more than one process.
  • For related processes, you would need just to
    create the shared memory object before calling
    fork() so that access is inherited by the child.
  • Shared memory objects are created by shmget() and
    are mapped into the process address space using
    shmat(). Releasing shared memory is done by
    shmdt().
  • Since shared memory isnt owned by any one
    process, it isnt freed when any one process
    exits. It must be explicitly freed from within
    your program using shmctl().
Write a Comment
User Comments (0)
About PowerShow.com