Interprocess Communication

IPC - Single System

IPC - Different Systems

Single System IPC

File Locking

Advisory vs. Mandatory Locking

Record Locking

lockf() sytem call (Sys V)

Blocking vs. Non-Blocking System Calls

Back to lockf()

int lockf( int fd, int function, long size)
fd is a file descriptor (small integer)
function can be:

lockf() example

/* lock a file */
if ( lockf(file, F_LOCK, 0) ==1 )
  /* error can't lock the file */

/* unlock a file */
if ( lockf(file, F_ULOCK, 0) ==1 )
  /* error can't unlock the file */

POSIX file locking

Pipes

pipe() system call

int pipe( int filedes[2])

pipe()

pipe() followed by fork()

Cleanup with close()

2-way communication

Creating 2-way communication

int fd_a[2], fd_b[2];
if ( (pipe(fd_a) < 0) || (pipe(fd_b) < 0) )
   /* error opening pipes */
if (fork()==0){
   /* child process */
   close(fd_a[0]); close(fd_b[1]);
   /* write to parent via fd_a[1], read from fd_b[0] */
} else {
   /* parent process */
   close(fd_a[1]); close(fd_b[0]);
   /* write to child via fd_b[1], read from fd_a[0] */
}

2-way communication

IPC using pipes

FIFOs - First In, First Out

mknod() system call

opening a FIFO

Rules for reading FIFOs & Pipes

Rules for writing FIFOs & Pipes

Streams vs. Messages

Streams vs. Messages

System V IPC

FIFO Example

FIFO Example

Server

  • opens well known FIFO (fifo_wk) for reading.
  • reads name of temporary FIFO (fifo_tmp) from fifo_wk.
  • opens fifo_tmp for writing.
  • receives request from fifo_wk and sends response to fifo_tmp.
  • close fifo_tmp and go back to step 1.

    Client

  • create temporary FIFO fifo_tmp.
  • open well known FIFO fifo_wk for writing.
  • write name of temporary FIFO to fifo_wk.
  • open fifo_tmp for reading.
  • write request to fifo_wk
  • read response from fifo_tmp.
  • close FIFOs and delete fifo_tmp.

    Deadlock

    Homework #1

    Homework #1 Suggestions

    Homework #1 Multiplexed I/O