advanced unix ipc facilities

11
Advanced UNIX IPC Facilities After Haviland, et al.’s book

Upload: arlene

Post on 04-Jan-2016

65 views

Category:

Documents


1 download

DESCRIPTION

After Haviland, et al.’s book. Advanced UNIX IPC Facilities. Introduction and Basic Concepts. UNIX offers a variety of advanced IPC mechanisms This makes UNIX an extremely rich system in the IPC area - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced UNIX IPC Facilities

Advanced UNIX IPC Facilities

After Haviland, et al.’s book

Page 2: Advanced UNIX IPC Facilities

Introduction and Basic Concepts

• UNIX offers a variety of advanced IPC mechanisms

• This makes UNIX an extremely rich system in the IPC area

• Allows developer to use a variety of approaches when programming a system made up of cooperating tasks

Page 3: Advanced UNIX IPC Facilities

Categories of advanced IPC

• Message passing• Semaphores• Shared memory

Page 4: Advanced UNIX IPC Facilities

IPC facility keys

• UNIX has made the 3 categories of IPC as similar as possible for programming convenience

• The most (?) important similarity is the IPC key• Keys are numbers used to identify an IPC object

on a UNIX system• Much like a file name being used to identify files• Key allows IPC object to be shared by several

processes

Page 5: Advanced UNIX IPC Facilities

Difference between keys and file names

• Keys are not file names and carry less meaning

• Actual key type is determined by implementation, and defined by key_t in <sys/types.h>

• UNIX uses a simple library funciton that maps a file’s pathname to a key– Routine is called ftok( )

Page 6: Advanced UNIX IPC Facilities

IPC get operation

• With the get operation, a program uses a key to – Create an IPC, or,– Gain access to an existing one

Mqid = msgget((key_t)0100, 0644|IPC_CREAT|IPC_EXCL);

Msg queue key

Non-neg id returned if successful

For semaphores: semgetFor shared mem: shmget

Page 7: Advanced UNIX IPC Facilities

Other IPC Operations• Control operations

– Get status information– Set control values– Actual calls are msgctl, semctl, shmctl

• More specific operations to perform various functions– Do interesting work– Called “IPC operations”– For example, for message queues we have:

• Msgsend• msgrcv

Page 8: Advanced UNIX IPC Facilities

Status data structures

• When an IPC object is created, the system also creates an IPC facility status structure

• This contains any administrative info assoc with object

• There is one type of status structure for messages, semaphores and shared mem

• However all 3 contain a common permission structure, ipc_perm

Page 9: Advanced UNIX IPC Facilities

Message passing

• A message is basically a sequence of characters of bytes, not necessarily null-terminated

• One process creates a message queue using msgget

• One a queue is established, a process with the right permissions can put messages into it with msgsnd

• Another process can then read this message with msgrcv

Page 10: Advanced UNIX IPC Facilities

Form of msgget

#include <sys/msg.h>int msgget(key_t key, int permflags);

Page 11: Advanced UNIX IPC Facilities

Permflags• Determines the exact action perform by msgget

• Two constants are relevant here, defined in <sys/ipc.h>

• Can be used alone or ORed together

– IPC_CREAT• Tells msgget to create a message queue for the value

key if one doesn't exist• Message queue will not be overwritten if it already

exists• If not set, then msgget returns id of existing

msgqueue, if exists

– IPC_EXCL• If this and IPC_CREAT are both set, then only intend

to create new msgqueue• Return -1 if already exists