IPC Introduction
Introduction
IPC means communication among processes, traditionally it used various types of method to transfer messages, but lately new methods are introduced such as shared memory. So following sections will describe various types of synchronization among processes.
For the past 30 years, transferring message in Unix O/S has been changed as below.
1. Pipes
were the first widely used form of IPC, available both within programs and from the shell. The problem with pipes is that they are usable only between processes that have a common ancestor.
2. System V message queues
were added to System V kernels in the early 1980s. These can be used between related or unrelated processes on a given host.
3. Posix message queues
were added by Posix realtime standard.
4. Remote Procedure Calls
were introduced in mid 80s. It is being used to call remote function call between heterogeneous systems.
Processes, Threads, and the Sharing of Information
Threads
For long time, Unix used process method. The concept of thread was introduced lately. Posix. 1 thread standard is accepted in 1995. Since threads are sharing information by global variable, there are several methods to synchronize shared data.
Persistence of IPC Objects
1. Process-persistent IPC
It exists till the last process, which owns the IPC object, closes the IPC object.
2. Kernel-persistent IPC
It exists till kernel is rebooted or IPC object is removed forcefully.
It exists till kernel is rebooted or IPC object is removed forcefully.
3. Filesystem-persistent IPC
It exists till it is deleted forcefully.
System V IPC
Introduction
Three types of IPCs are as below.
· System V message queues
· System V semaphores
· System V shared memory
Following is api lists which can control each IPC object.
Message queues | Semaphores | Shared memory | |
Header | sys/sem.h | ||
Function to create or open | msgget | semget | shmget |
Function for control operations | msgctl | semctl | shmctl |
Functions for IPC operations | msgsnd msgrcv | semop | shmat shmdt |
Summary of System V IPC functions.
key_t Keys and ftok Function
key_t type might be defined 32bit integer in sys/types.h. ftok function accepts integer type id and pathname and returns key_t type.
#include
key_t ftok(const char *pathname, int id);
returns: IPC key if OK, -1 or error
It will generate IPC key combining low-order 8bit of pathname and id.
ipc_perm Structure
Kernal manages IPC object same like file and the structure is as below.
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; // read-write permissions
ulong_t seq; // slot usage sequence number
key_t key; // IPC key
};
Creating and Opening IPC Channels
Three get_xxx functions can be used to create or open IPC object and all functions get integer identifier using IPC key value.
There are two ways to pass function argument.
There are two ways to pass function argument.
1. Calls ftok function using pathname and id.
2. Specify Key as IPC_PRIVATE then new IPC object will be uniquely created.
Generating IPC identifiers from IPC keys |
All get_xxx functions have read-write permissions.
1. If key is IPC_PRIVATE, it guarantees creating unique IPC object.
2. If oflag is set as IPC_CREATE, it will create new entry to specific key if entry doesn't exist, otherwise it will return existing entry.
2. If oflag is set as IPC_CREATE, it will create new entry to specific key if entry doesn't exist, otherwise it will return existing entry.
3. if oflag is set IPC_CREATE and IPC_EXCL, it only create new entry when there is no entry otherwise it will return EEXIST.
Following is summery as diagram.
Logic for creating or opening an IPC object |
Shared Memory
Shared Memory Introduction
Shared memory is most fast among IPC since it doesn't need kernel help. You may understand easily with following diagram.
Flow of file data from server to client |
Copying file data from server to client using shared memory |
System V Shared Memory
Introduction
All shared memory segment has shmid_ds struct which is defined in sys/shm.h.
struct shmid_ds
{
struct ipc_perm shm_perm; // operation permission struct
size_t shm_segsz; // segment size
pid_t shm_lpid; // pid of last operation
pid_t shm_cpid; // creator pid
shmatt_t shm_nattch; // current # attached
shmat_t shm_cnattch; // in-core # attached
time_t shm_atime; // last attach time
time_t shm_dtime; // last detach time
time_t shm_ctime; // last change time of this structure
}
shmget Function
Creates shared memory or accesses existing shared memory.
#include
int shmget(key_t key, size_t size, int oflag);
returns: shared memory identifier if OK, -1 on error
Return value would be shared memory identifier and will be used to access shared memory by other three shm_xxx functions.
Size is size of shared memory segment. Oflag means permission for read-write.
shmat Function
shmat function can access shared memory after it is opened or created.
#include
void *shmat(int shmid, const void *shmaddr, int flag);
returns: starting address of mapped region if OK, -1 on error
shmid is return value from shmget. The return value of shmat is shared memory starting address.
shmdt Function
Detaches from shared memory when the process finishes.
#include
int shmdt(const void *shmaddr);
returns: 0 if OK, -1 on error
Calling shmdt doesn't mean that it removes shared memory segment.
shmctl Function
Provides various functions related shared memory.
#include
int shmctl(int shmid, int cmd, struct shmid_ds *buff);
returns: 0 if OK, -1 on error
IPC_RMID | Remove the shared memory segment identified by shmid from the system and destroy the shared memory segment |
IPC_SET | Set the following three members of the shmid_ds structure for the shared memory segment from the corresponding members in the structure pointed to by the buff argument: shm_perm.uid, shm_perm.gid, and shm_perm.mode. The shm_ctime value is also replaced with the current time. |
IPC_STAT | Return to the caller(through the buff argument) the current shmid_ds structure for the specified shared memory segment |
* referenced by
Unix Network Programming (Interprocess Communications) by W. Richard Stevens
No comments:
Post a Comment