[ Pobierz całość w formacie PDF ]

sem nsems
Number of semaphores in the semaphore set (the array)
48 CHAPTER 6. LINUX INTERPROCESS COMMUNICATIONS
Kernel sem structure In the semid ds structure, there exists a pointer to the base of
the semaphore array itself. Each array member is of the sem structure type. It is also
defined inlinux/sem.h:
/* One semaphore structure for each semaphore in the system. */
struct sem {
short sempid; /* pid of last operation */
ushort semval; /* current value */
ushort semncnt; /* num procs awaiting increase in semva
ushort semzcnt; /* num procs awaiting semval = 0 */
};
sem pid
The PID (process ID) that performed the last operation
sem semval
The current value of the semaphore
sem semncnt
Number of processes waiting for resources to become available
sem semzcnt
Number of processes waiting for 100% resource utilization
SYSTEM CALL: semget()
In order to create a new semaphore set, or access an existing set, the semget() system
call is used.
SYSTEM CALL: semget();
PROTOTYPE: int semget ( key_t key, int nsems, int semflg );
RETURNS: semaphore set IPC identifier on success
-1 on error: errno = EACCESS (permission denied)
EEXIST (set exists, cannot create (IPC_EXCL))
EIDRM (set is marked for deletion)
ENOENT (set does not exist, no IPC_CREAT was
ENOMEM (Not enough memory to create new set)
ENOSPC (Maximum set limit exceeded)
NOTES:
The first argument to semget() is the key value (in our case returned by a call to
ftok()). This key value is then compared to existing key values that exist within the
kernel for other semaphore sets. At that point, the open or access operation is dependent
upon the contents of thesemflgargument.
IPC CREAT
Create the semaphore set if it doesn t already exist in the kernel.
IPC EXCL
When used with IPC CREAT, fail if semaphore set already exists.
6.4. SYSTEM V IPC 49
If IPC CREAT is used alone, semget() either returns the semaphore set identifier
for a newly created set, or returns the identifier for a set which exists with the same key
value. If IPC EXCL is used along with IPC CREAT, then either a new set is created, or
if the set exists, the call fails with -1. IPC EXCLis useless by itself, but when combined
withIPC CREAT, it can be used as a facility to guarantee that no existing semaphore set
is opened for access.
As with the other forms of System V IPC, an optional octal mode may be OR d into the
mask to form the permissions on the semaphore set.
The nsems argument specifies the number of semaphores that should be created in a
new set. This represents the number of printers in our fictional print room described earlier.
The maximum number of semaphores in a set is defined in  linux/sem.h as:
#define SEMMSL 32 /*
Note that thensemsargument is ignored if you are explicitly opening an existing set.
Let s create a wrapper function for opening or creating semaphore sets:
int open_semaphore_set( key_t keyval, int numsems )
{
int sid;
if ( ! numsems )
return(-1);
if((sid = semget( mykey, numsems, IPC_CREAT | 0660 )) == -1)
{
return(-1);
}
return(sid);
}
Note the use of the explicit permissions of0660. This small function either returns a
semaphore set identifier (int), or -1 on error. The key value must be passed to it, as well
as the number of semaphores to allocate space for if creating a new set. In the example
presented at the end of this section, notice the use of the IPC EXCL flag to determine
whether or not the semaphore set exists or not.
SYSTEM CALL: semop()
SYSTEM CALL: semop();
PROTOTYPE: int semop ( int semid, struct sembuf *sops, unsigned nsops);
RETURNS: 0 on success (all operations performed)
-1 on error: errno = E2BIG (nsops greater than max number of ops all
EACCESS (permission denied)
EAGAIN (IPC_NOWAIT asserted, operation could no
EFAULT (invalid address pointed to by sops argu
EIDRM (semaphore set was removed)
EINTR (Signal received while sleeping)
EINVAL (set doesn t exist, or semid is invalid)
ENOMEM (SEM_UNDO asserted, not enough memory to
undo structure necessary)
ERANGE (semaphore value out of range)
NOTES:
50 CHAPTER 6. LINUX INTERPROCESS COMMUNICATIONS
The first argument to semget() is the key value (in our case returned by a call to
semget). The second argument (sops) is a pointer to an array of operations to be per-
formed on the semaphore set, while the third argument (nsops) is the number of opera-
tions in that array.
Thesopsargument points to an array of typesembuf. This structure is declared in
linux/sem.has follows:
/* semop system call takes an array of these */
struct sembuf {
ushort sem_num; /* semaphore index in array */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */
};
sem num
The number of the semaphore you wish to deal with
sem op
The operation to perform (positive, negative, or zero)
sem flg
Operational flags
If sem op is negative, then its value is subtracted from the semaphore. This cor-
relates with obtaining resources that the semaphore controls or monitors access of. If [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • g4p.htw.pl
  •