Lecture Slides on Unix IPC - UNIX System Programming | COP 4604, Exams of Computer Science

Material Type: Exam; Professor: Hsu; Class: UNIX System Programming; Subject: Computer Programming; University: Florida Atlantic University; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-tsg-1
koofers-user-tsg-1 🇺🇸

5

(1)

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
COP 4604 UNIX System Programming
IPC
Dr. Sam Hsu
Computer Science & Engineering
Florida Atlantic University
2
UNIX IPC
Signals
Pipes
FiFOs
Semaphores
Shared Memory
Messages Queues
3
Interprocess Communication
Interprocess communication (IPC) provides
two major functions/services:
Information exchange
For data transfer among processes.
Process synchronization
For synchronization of process execution.
4
Various IPC Implementations
UDS
UDS
MacOS X
10.3
opt
opt, UDS
opt, UDS
Linux
2.4.22
XSI option
Sockets
STREAMS
XSI
XSI
XSI
Message queues
Semaphores
Shared memory
, UDS
, UDS
, UDS
UDS
Allowed
XSI option
Full-duplex (FDX) pipes
Named FDX pipes
(full)
(full)
Half-duplex (HDX) pipes
FIFOs (named pipes)
Signals
Solaris
9
FreeBSD
5.2.1
SUSIPC Type
5
Signals
Most widely known UNIX IPC facility.
Used primarily to inform processes the
occurrence of asynchronous events.
Posted by one process and received by
another or the same process.
The receiving process performs an action
appropriate to the signal received:
Default (SIG_DFL).
Ignored (SIG_IGN).
User-defined.
6
Pipes
A pipe is a communication channel that couples one
process to another.
Historically, a pipe is one way (HDX) only. However, newer
implementations support two way (FDX) pipes.
Pipes are used between processes that have a common
ancestor.
More specifically, for communication between parent and child
in general.
There are two types of pipes: named and unnamed
Unnamed pipes are used for communication between related
processes.
Named pipes can be used for communication between
unrelated processes.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Slides on Unix IPC - UNIX System Programming | COP 4604 and more Exams Computer Science in PDF only on Docsity!

COP 4604 UNIX System Programming

IPC

Dr. Sam Hsu

Computer Science & Engineering

Florida Atlantic University

UNIX IPC

„ Signals

„ Pipes

„ FiFOs

„ Semaphores

„ Shared Memory

„ Messages Queues

Interprocess Communication

„ Interprocess communication (IPC) provides

two major functions/services:

„ Information exchange

„ For data transfer among processes.

„ Process synchronization

„ For synchronization of process execution.

Various IPC Implementations

UDS

UDS

MacOS X

opt

opt, UDS

opt, UDS

Linux

XSI option

Sockets

STREAMS

XSI

XSI

XSI

Message queues

Semaphores

Shared memory

  • , UDS
  • , UDS
  • , UDS

UDS

Allowed

XSI option

Full-duplex (FDX) pipes

Named FDX pipes

(full)

(full)

Half-duplex (HDX) pipes

FIFOs (named pipes)

Signals • • •

Solaris

FreeBSD

IPC Type SUS

Signals

„ Most widely known UNIX IPC facility.

„ Used primarily to inform processes the

occurrence of asynchronous events.

„ Posted by one process and received by

another or the same process.

„ The receiving process performs an action

appropriate to the signal received:

„ Default (SIG_DFL).

„ Ignored (SIG_IGN).

„ User-defined.

Pipes

„ A pipe is a communication channel that couples one

process to another.

„ Historically, a pipe is one way (HDX) only. However, newer

implementations support two way (FDX) pipes.

„ Pipes are used between processes that have a common

ancestor.

„ More specifically, for communication between parent and child

in general.

„ There are two types of pipes: named and unnamed

„ Unnamed pipes are used for communication between related

processes.

„ Named pipes can be used for communication between

unrelated processes.

The pipe() System Call

„ Syntax

pipe()

„ Is used to create an unnamed pipe.

„ Needs an array of twoint's for twofd's

(r/w)

Pipes Are Files

„ In fact, a pipe is another generalization

of the UNIX file concept.

„ A pipe is a FIFO file.

lseek() does not work on a pipe.

„ Unnamed pipes come and go.

„ Named pipes (also known as FIFOs) are

permanent files.

Some Properties of Pipes (1/2)

„ Write beforeread

„ If a read() is issued while the pipe is

empty, it will block.

„ The size of a pipe is limited.

„ Max is PIPE_BUF.

„ If a write() is issued when the pipe is full, it

will block.

Some Properties of Pipes (2/2)

„ Data could be intermingled if several

processes are writing to the same pipe.

„ If all writers of a pipe are closed, a

reader will encounter EOF.

„ If all readers of a pipe are closed, a

writer will face a broken pipe.

„ Non-blocking reads and writes

„ Issue fcntl() with O_NONBLOCK flag.

FIFOs

„ Also known as named pipes.

„ Can be used between unrelated

processes for data exchange.

The popen()/pclose() Functions

„ To create a pipe between the calling program

and the command to be executed.

„ Will handle dirty work such as:

„ The creation of a pipe.

„ The fork of a child.

„ Closing the unused ends of the pipe.

„ Executing a shell to execute the command.

„ Waiting for the command to terminate.

Common Features of the Three

IPC Facilities (3/3)

„ Needs an IPC key to gain access to an IPC

facility.

„ All processes wishing to use the same IPC facility must

specify the same key.

„ This key must uniquely identify the IPC facility.

„ An IPC facility is identified by a nonnegative integer.

„ In need of an IPC key to generate such a numeric ID.

„ Three ways to generate an IPC key:

„ IPC_PRIVATE

„ Use a predefined key and store it in a common header file.

„ Use key_t ftok(const char *path, int id) to generate an IPC

key.

Three Major Categories of IPC

System Calls

„ get() : Is used to create or access an IPC

facility. It returns an IPC identifier.

„ semget(), shmget(), msgget()

„ ctl() : Is used to determine status, set options

and/or permissions, or remove an IPC facility.

„ semctl(), shmctl(), msgctl()

„ op() : Is used to operate on an IPC identifier.

„ semop()

„ shmat(), shmdt()

„ msgsnd(), msgrcv()

End-user Level Commands

„ ipcs

„ Used to report inter-process

communication facilities status

„ ipcrm

„ Used to remove a message queue,

semaphore set, or shared memory ID.

General Concepts of Semaphores (1/3)

„ A semaphore is an integer variable that

can be accessed only through two

standard atomic operations:

„ P roberen – wait

„ P(S): while S <= 0

do skip;

S Å S-1;

„ V erhogen – signal

V(S): S Å S+1;

General Concepts of Semaphores (2/3)

„ Semaphores are used to control access to

shared resource(s).

„ An important mechanism to ensure mutual

exclusion to protect critical sections in a

program.

„ To obtain a shared resource, a process needs to

test the semaphore that controls the resource.

„ If the semaphore value is positive, the process can

use the resource. The process decrements the

semaphore value by 1, indicating it has used one

unit of the resource.

General Concepts of Semaphores (3/3)

„ If the value tested is 0, the process goes to sleep

until the semaphore value is greater than 0.

„ The controlling semaphore value is incremented by

1 when a process is done with the shared resource.

If any other processes are asleep, waiting for the

semaphore, they are awakened.

„ Semaphores are typically implemented inside

the kernel.

„ To ensure that the test of a semaphore’s value and

the de/incrementing of the value are implemented

as atomic operations.

2 Types of Semaphores (1/2)

„ Binary (lock/unlock)

„ Having a value of 0 or 1.

„ Usually used for locking a resource.

„ N -ary (counting)

„ N is the available number of an intended

resource on a system.

„ Usually used for maintaining a pool of

resources.

2 Types of Semaphores (2/2)

„ Note that the type/nature of a semaphore

(whether binary ofN-ary) is imposed by the

application (user-defined), not by the system.

„ In the binary case, a process waits for the

semaphore to become one; in theN-ary case,

a process waits for a non-zero value. (The

latter can be considered as a more general

case of the former.)

Semaphore Implementation (1/2)

„ A semaphore is a sharable unsigned short

integer variable.

„ Semaphores are grouped together to form

sets.

„ No standalone semaphores.

„ A semaphore set (array) contains:

„ 1 to SEMMSL semaphores.

„ 1 data structure, struct semid_ds, containing

administrative information about the set.

Semaphore Implementation (2/2)

„ Each semaphore set is uniquely

identified by a nonnegative integer.

„ One or more semaphores in a set can

be accessed at the same time.

„ All semaphores involved in an operation

are changed "atomically".

„ Only one process can access a

semaphore set at any given time.

Basic Semaphore Operations

„ Incrementing.

„ Decrementing.

„ Testing for zero.

Semaphore System Calls (1/2)

„ semget():

„ To create or gain access to a set of one or more

semaphores.

„ semctl():

„ To get or set the value of a semaphore(s) in a set.

„ To get or modify the status of a semaphore set.

„ To identify process waiting for a specific

semaphore value.

„ To identify process that did the last operation on a

semaphore.

„ To remove a semaphore set.

Shared Memory Detachment

„ A process using a shared memory

segment may detach it explicitly when

finished.

„ If not done explicitly, an exit() call will

detach the segment automatically.

„ Detachment does not remove the shared

memory.

Explicit Removal & Synchronization

„ Shared memory must be removed

explicitly.

„ Usually by the same process that does the

creation.

„ Synchronized access to shared memory

must be done explicitly.

„ Usually used with semaphores.

Shared Memory System Calls (1/2)

„ shmget():

„ To obtain a shared memory identifier.

„ shmat():

„ To attach a shared memory segment to a

process data segment.

„ shmdt():

„ To detach a shared memory segment from

a process data segment.

Shared Memory System Calls (2/2)

„ shmctl():

„ To determine the status of a shared

memory segment.

„ To change permissions or ownership of a

shared memory segment.

„ To remove a shared memory.

A Conceptual View of Shared

Memory

argv[] & envp[]

BSS

data

text

heap

stack

argv[] & envp[]

BSS

data

text

heap

stack

shared memory

Message Queues

„ Message queues are for exchange of data

among processes.

„ The data exchanged are in discrete portions known

as messages.

„ A message is a sequence of bytes, from zero to the

system limit, with a message type.

„ The structure of a message is up to the programmer.

„ Once a message queue is created, processes

can send/receive messages to/from the queue.

Reading Messages

„ A process can selectively read

messages on a queue:

„ First message on the queue.

„ First message of a specific type on the

queue.

„ First message from a range of types (from

1 to upper bound of range) on the queue.

Some Message Queue Properties

„ Messages queues are similar to unnamed

pipes; however,

„ Each message has a type associated with it.

„ Data in a pipe is just a sequence of types with no

header of format.

„ Unread data in a pipe vanishes when the last

reader process close a file descriptor associated

with the pipe's read end.

„ A message queue sticks around.

„ It will remain active until it is explicitly removed.

Message Queues System Calls (1/2)

„ msgget():

„ To create or to gain access to a message

queue.

„ msgctl():

„ To determine the status of a message queue.

„ To change the permission or ownership of a

message queue.

„ To change the maximum size.

„ To remove a message queue.

Message Queues System Calls (2/2)

„ msgsend():

„ To send a message.

„ msgrcv():

„ To receive a message.

Some Comments on the Three

SV IPC Facilities

„ The SV IPC structures are system-wide.

„ They stick around until explicitly removed.

„ Not compatible with standard UNIX file

manipulation facilities.

„ Can’t use open(), close(), read(), write(), etc.

„ Almost a dozen of new system calls were added to

the kernel to support these IPC facilities.

„ Basically, they are intra-system IPC facilities.

Recommended Reading

„ Read Chapter 15,Advanced

Programming in the UNIX Environment,

2ed., by W. Richard Stevens and

Stephen Rago, Addison Wesley, 2005.