+ All Categories
Home > Documents > COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC...

COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC...

Date post: 06-Mar-2021
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
46
COP 4604 UNIX System Programming IPC Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University
Transcript
Page 1: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

COP 4604 UNIX System Programming

IPCDr. Sam Hsu

Computer Science & EngineeringFlorida Atlantic University

Page 2: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Interprocess Communicationn Interprocess communication (IPC) provides

two major functions/services:n Information exchange

n For data transfer among processes.

n Process synchronizationn For synchronization of process execution.

Page 3: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Various IPC Implementations

•••

SocketsStreams

•••

Message queuesSemaphoresShared memory

••

••

Stream pipes (FDX)Named stream pipes

••

••

••

Pipes (HDX)FIFOs (named pipes)

•••Signals

4.4BSDSVR4POSIX.1IPC Type

Page 4: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Signalsn Most widely known UNIX IPC facility.n Used primarily to inform processes the

occurrence of asynchronous events.n Posted by one process and received by

another or the same process.n The receiving process performs an action

appropriate to the signal received:n Default (SIG_DFL).n Ignored (SIG_IGN).n User-defined.

Page 5: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Pipesn A pipe is a one-way communication channel (HDX)

that couples one process to another.n Used only between processes that have a common

ancestor.n More specifically, for communication between parent

and child in general.n There are two types of pipes: named and unnamed

n Unnamed pipes are used for communication between related processes.

n Named pipes can be used for communication between unrelated processes.

Page 6: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

The pipe() System Call

n Syntaxpipe()

n Is used to create an unnamed pipe.n Needs an array of two int's for two fd's

(r/w)

Page 7: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Pipes Are Files

n In fact, a pipe is another generalization of the UNIX file concept.

n A pipe is a FIFO file.lseek() does not work on a pipe.

n Unnamed pipes come and go.n Named pipes (also known as FIFOs) are

permanent files.

Page 8: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Some Properties of Pipes (1/2)

n Write before readn If a read() is issued while the pipe is

empty, it will block.

n The size of a pipe is limited.n Max is PIPE_BUF.n If a write() is issued when the pipe is full, it

will block.

Page 9: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Some Properties of Pipes (2/2)n Data could be intermingled if several

processes are writing to the same pipe.n If all writers of a pipe are closed, a

reader will encounter EOF.n If all readers of a pipe are closed, a

writer will face a broken pipe.n Non-blocking reads and writes

n Issue fcntl() with O_NONBLOCK flag.

Page 10: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

FIFOs

n Also known as named pipes.n Can be used between unrelated

processes for data exchange.

Page 11: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

The popen()/pclose() Functionsn To create a pipe between the calling program

and the command to be executed. n Will handle dirty work such as:

n The creation of a pipe.n The fork of a child.n Closing the unused ends of the pipe.n Executing a shell to execute the command.n Waiting for the command to terminate.

Page 12: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Three SV IPC Facilities

n SV provides 3 unique IPC facilities (structures). n Semaphores

n For process synchronization.

n Shared memoryn For process communication.

n Message queuesn For process communication.

Page 13: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphoresn Semaphores are useful for process

synchronization and resource management. n A semaphore is an unsigned integer shared

among several competing processes.n Operating upon a semaphore involves adding or

subtracting from the underlying semaphore value.

n The UNIX implementation of semaphores is modeled after Dijkstra's semaphores.

Page 14: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Shared Memory

n Shared memory allows one or more process to share the same segment of memory.n Shared memory may be attached to more

than one process.n Once attached, it is part of a process's data

space.

n Shared memory is the fastest IPC.

Page 15: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Messages Queuesn A message queue allows processes to

send and receive discrete amounts of data, know as messages.n A message is a collection of bytes of varying

lengths, ranging from zero to a system imposed maximum.n The message content can be anything -- ASCII or

binary.

n Each message has a type associated with it.n A message type is a long integer.

Page 16: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Common Features of the Three IPC Facilities (1/3)

n An IPC facility must be created prior to use.n It can be created anytime before use.

n Permission and ownership for a facility are established at the time of creation.n But these attributes can be changed later.

n An IPC facility can exist with contents intact after all processes accessing it have exited.n Changes made to an IPC facility by a process persist

after the process exits.

n An IPC facility must be explicitly removed.n Removal of an IPC facility can be done by either the

owner or creator.

Page 17: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Common Features of the Three IPC Facilities (2/3)

n Shares a common data structure defined in <sys/ipc.h>

struct ipc_perm {uid_t uid; /* owner's user id */gid_t gid; /* owner's group id */uid_t cuid; /* creator's user id */gid_t cgid; /* creator's group id */mode_t mode; /* access modes */uint_t seq; /* slot usage sequence number */key_t key; /* key */

}

Page 18: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Common Features of the Three IPC Facilities (3/3)

n Needs an IPC key to gain access to an IPC facility.n All processes wishing to use the same IPC facility must

specify the same key.n This key must uniquely identify the IPC facility.

n An IPC facility is identified by a nonnegative integer.n In need of an IPC key to generate such a numeric ID.

n Three ways to generate an IPC key:n IPC_PRIVATEn Use a predefined key and store it in a common header file.n Use key_t ftok(const char *path, int id) to generate an IPC

key.

Page 19: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Three Major Categories of IPC System Calls

n get(): Is used to create or access an IPC facility. It returns an IPC identifier.n semget(), shmget(), msgget()

n ctl(): Is used to determine status, set options and/or permissions, or remove an IPC facility.n semctl(), shmctl(), msgctl()

n op(): Is used to operate on an IPC identifier.n semop()n shmat(), shmdt()n msgsnd(), msgrcv()

Page 20: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

End-user Level Commands

n ipcsn Used to report inter-process

communication facilities status

n ipcrmn Used to remove a message queue,

semaphore set, or shared memory ID.

Page 21: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphore Basics (1/3)

n A semaphore is an integer variable that can be accessed only through two standard atomic operations:n Proberen – waitn P(S): while S <= 0

do skip;S ß S-1;

n Verhogen – signal V(S): S ß S+1;

Page 22: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphore Basics (2/3)

n Semaphores are used to control access to shared resource(s).n An important mechanism to ensure mutual

exclusion to protect critical sections in a program.

n To obtain a shared resource, a process needs to test the semaphore that controls the resource.n 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.

Page 23: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphore Basics (3/3)

n If the value tested is 0, the process goes to sleep until the semaphore value is greater than 0.

n 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.

n Semaphores are typically implemented inside the kernel.n To ensure that the test of a semaphore’s value and

the de/incrementing of the value are implemented as atomic operations.

Page 24: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

2 Types of Semaphores (1/2)

n Binary (lock/unlock)n Having a value of 0 or 1.n Usually used for locking a resource.

n N -ary (counting) n N is the available number of an intended

resource on a system.n Usually used for maintaining a pool of

resources.

Page 25: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

2 Types of Semaphores (2/2)n Note that the type/nature of a semaphore

(whether binary of N-ary) is imposed by the application (user-defined), not by the system.

n In the binary case, a process waits for the semaphore to become one; in the N-ary case, a process waits for a non-zero value. (The latter can be considered as a more general case of the former.)

Page 26: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphore Implementation (1/2)

n A semaphore is a sharable unsigned short integer variable.

n Semaphores are grouped together to form sets.n No standalone semaphores.

n A semaphore set (array) contains:n 1 to SEMMSL semaphores.n 1 data structure, struct semid_ds, containing

administrative information about the set.

Page 27: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphore Implementation (2/2)

n Each semaphore set is uniquely identified by a nonnegative integer.

n One or more semaphores in a set can be accessed at the same time.

n All semaphores involved in an operation are changed "atomically".

n Only one process can access a semaphore set at any given time.

Page 28: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Basic Semaphore Operations

n Incrementing.n Decrementing.n Testing for zero.

Page 29: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphore System Calls (1/2)n semget():

n To create or gain access to a set of one or more semaphores.

n semctl(): n To get or set the value of a semaphore(s) in a set.n To get or modify the status of a semaphore set. n To identify process waiting for a specific

semaphore value.n To identify process that did the last operation on a

semaphore.n To remove a semaphore set.

Page 30: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphore System Calls (2/2)n semop():

n To wait for the value of a semaphore(s) to become 0.

n To increment semaphore value(s).n To decrement semaphore value(s).n IPC_NOWAIT for non-blocking.n SEM_UNDO for undo operations.n Note: sleep() and wakeup() are used internally.

Page 31: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Blocking/nonblocking Operations

n A semaphore operation can be either: n Blocking: Processes wait for resources to

be released.n Non-blocking: Processes return an error

code -1, and the external errno variable is set accordingly. (IPC_NOWAIT is set.)

n UNIX puts the blocked process to sleep to eliminate the busy waiting phenomenon.

Page 32: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Semaphore system calls

semid = semget()

semctl()

union semun {int valstruct semid_ds *buf;unshort *array;

};

semop()

struct sembuf {ushort sem_num;short sem_op;short sem_flg;

};

Semaphore Data Structureskernel data space

struct semid_ds {struct ipc_perm sem_perm;struct sem *sem_baseushort_t sem_nsemstime_t sem_otime; time_t sem_ctime;int sem_binary;

};

struct sem {ushort_t semval;pid_t sempidushort_t semncnt ushort_t semzcnt;

} [nsems];

(retained by kernel)

Note:In union semun

int val is used for SETVALstruct semid_ds *buf is used

for IPC_STAT and IPC_SETushort *array is used for

GETALL and SETALL

In struct sembufshort sem_op has the value:

0: zero test,+n.: release n, or-n: request n

short sem_flg has the value: IPC_NOWAIT or SEM_UNDO

struct sem is the data structure of a semaphore. It comes in an array.

Page 33: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Shared Memory Basics

n Shared memory allows multiple processes to share memory.n Shared memory is used to hold data

needed by two or more processes.n Look-up tables, form layouts, etc.

Page 34: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Shared Memory Implementation

n The implementation of shared memory is machine dependent. n It depends on the memory management of

the host machine.n System configuration values determine the

minimum and maximum sizes of a segment.

n The number of segments that may be attached to a process is also restricted.

Page 35: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Shared Memory Creation & Attachment

n Typically one process creates a shared memory segment, maps the segment into its data space and initializes the segment.n The term for mapping a shared memory segment

into a user's data space is called attachment.n The creator process specifies R/W permissions

associated for the shared memory segment.n Other processes may then attach the shared

memory segment and access its contents.n Unrelated processes can thus communicate via shared

memory.

Page 36: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Shared Memory Detachment

n A process using a shared memory segment may detach it explicitly when finished.n If not done explicitly, an exit() call will

detach the segment automatically.n Detachment does not remove the shared

memory.

Page 37: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Explicit Removal & Synchronization

n Shared memory must be removed explicitly.n Usually by the same process that does the

creation.

n Synchronized access to shared memory must be done explicitly.n Usually used with semaphores.

Page 38: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Shared Memory System Calls (1/2)

n shmget():n To obtain a shared memory identifier.

n shmat():n To attach a shared memory segment to a

process data segment.n shmdt():

n To detach a shared memory segment from a process data segment.

Page 39: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Shared Memory System Calls (2/2)

n shmctl():n To determine the status of a shared

memory segment.n To change permissions or ownership of a

shared memory segment.n To remove a shared memory.

Page 40: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

A Conceptual View of Shared Memory

argv[] & envp[]

BSS

data

text

heap

stack

argv[] & envp[]

BSS

data

text

heap

stack

shared memory

Page 41: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Message Queues Basicsn Message queues are for exchange of data

among processes.n The data exchanged are in discrete portions known

as messages.n A message is a sequence of bytes, from zero to the

system limit, with a message type.n The structure of a message is up to the programmer.

n Once a message queue is created, processes can send/receive messages to/from the queue.

Page 42: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Reading Messages

n A process can selectively read messages on a queue:n First message on the queue.n First message of a specific type on the

queue.n First message from a range of types (from

1 to upper bound of range) on the queue.

Page 43: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Some Message Queue Propertiesn Messages queues are similar to unnamed

pipes; however, n Each message has a type associated with it.n Data in a pipe is just a sequence of types with no

header of format.n Unread data in a pipe vanishes when the last

reader process close a file descriptor associated with the pipe's read end.

n A message queue sticks around.n It will remain active until it is explicitly removed.

Page 44: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Message Queues System Calls (1/2)

n msgget():n To create or to gain access to a message

queue.

n msgctl():n To determine the status of a message queue.n To change the permission or ownership of a

message queue.n To change the maximum size.n To remove a message queue.

Page 45: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Message Queues System Calls (2/2)

n msgsend():n To send a message.

n msgrcv():n To receive a message.

Page 46: COP 4604 UNIX System Programmingroy/cop4604.04f/notes/ipc.pdf · n Most widely known UNIX IPC facility. n Used primarily to inform processes the occurrence of asynchronous events.

Some Comments on the Three SV IPC Facilities

n The SV IPC structures are system-wide.n They stick around until explicitly removed.

n Not compatible with standard UNIX file manipulation facilities.n Can’t use open(), close(), read(), write(), etc. n Almost a dozen of new system calls were added to

the kernel to support these IPC facilities.

n Basically, they are intra-system IPC facilities.


Recommended