System V IPC: Unix Systems Programming CSPP 51081
System V IPC: Unix Systems Programming CSPP 51081
' $
System V IPC
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 2
' $
System V IPC
Overview
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 3
' $
• System V IPC structures can be shared among any processes on the same
system.
• Each IPC mechanism has a common interface for creating, controling and
removing.
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 4
' $
SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
Return:
0 on success -1 on error
Possible errors are due to problems with path. See man 3 ftok.
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 8
' $
Permission Structure
• All IPC structures have an owner, the user ID of the process which
created the structure, and group, the group ID of the process which
created the structure.
• All IPC structures have a 9-bit permission access mode for owner,
group and everyone else. The permission flags are just as in open,
except only read and write permissions make sense. For semaphores,
permission to write to the semaphore set means permission to alter
the semaphore.
• Permissions are set in the mode field of XXXget.
• The flags IPC_CREAT and IPC_EXCL have the same meaning as with
open and are bit-wise or’d to the permissions. IPC_CREAT creates the
IPC structure if it does no exist; the combination IPC_CREAT and
IPC_EXCL ensure the structure is new, or XXXget returns an error.
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 10
' $
Semaphore Sets
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 11
' $
Semaphores
• A semaphore is a shared counting variable (of type integer) which
restricts the number of processes which access a shared data object.
If access must be exclusive to a single process, the semaphore is
called a mutex, and otherwise is called a counting semaphore.
• The steps for obtaining access to a shared resource using a
semaphore is as follows
1. Test the semaphore to see if it is positive.
2. If the value of the semaphore is positive, the process can access
the resource, and decrements the value of the semaphore.
3. If the value of the semaphore is zero, the process waits on a queue
until the semaphore value is greater than zero and the process is
given permission to decrement the value of the semaphore.
4. When the process finishes with the shared resource, it increments
the semaphore value (allowing another process to pass the
semaphore.) This step never blocks.
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 12
' $
Semaphore Sets
• The number of semaphores in the set are determined when the set is
created.
SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
Return:
semid, identifier on success -1 on error
see man 2 semget for possible errors
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 13-1
Let nsems be the number of semaphores in the set and perm be the permissions
and key be a key value. Then there are 4 ways to obtain a semaphore set
• Let the kernel choose the key and guarantee the semaphore set is new
semid = semget(IPC_PRIVATE, nsems, perm);
• Obtain the semaphore set associated with key, creating if it does not exist
semid = semget(key, nsems, IPC_CREAT | perm);
• Obtain the semaphore set associated with keyguaranteeing it is new or
return with error EEXIST
semid = semget(key, nsems, IPC_CREAT | IPC_EXCL | perm);
SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
Return:
0 on success -1 on error
semctl has many different uses, all return non-negative on success. We
will only be using the system call to initialize semaphore sets and remove
semaphore sets. See man 2 semctl
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 15
' $
There are ten possible values to cmd argument of semctl. We will only
be interested in three here. Some of these arguments require the fourth
argument and others ignore it. The union type, union semun, is probably
not defined so you will need to define it in your program:
union semun {
int val; /* used for SETVAL */
struct semid_ds *buf; /* We won’t use */
ushort *array; /* used for SETALL */
}
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 16
' $
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 17
' $
Semaphore sets remain in the system until removed. Since there is a small
system-wide limit to the number of semaphore sets, it is important to
remove them when not using them. There are two ways of removing a
semaphore set
• On the shell command line, to remove the semaphore set with the
identifier id,
ipcrm sem id
• In a running process, to remove a semaphore set with identifier id.
semctl(id, 0, IPC_RMID);
Note that it is not necessary to specify the final argument.
If a semaphore is removed by a process, then all processes which
were waiting on a semaphore will return with the error ERMID
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 18
' $
SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
Return:
0 on success -1 on error
where struct sembuf include the following members
short sem_num; /* semaphore number: 0 = first */
short sem_op; /* semaphore operation */
short sem_flg; /* IPC_NOWAIT, SEM_UNDO */
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 19
' $
Semaphore Operations
Semaphore Operations
• sem_op is zero then the process will wait until the value of the
semaphore is 0 before returning.
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 21
' $
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 22
' $
semopr.sem_num = semnum2;
semopr.sem_op = 1;
semopr.sem_flg = 0;
& %
CSPP 51081 Unix Systems Programming
CSPP 51081 System V IPC 23
' $