Semaphoren (semget, semop, semctl)

Post Reply
Message
Author
terrorgfx

Semaphoren (semget, semop, semctl)

#1 Post by terrorgfx »

Hoi!

Hab ein Referat über Semaphoren geschrieben ... da stimmt aber einiges nicht.

Weil ich selber keine Ahnung von dem ganze habe und auf Quellen im Inet angewiesen bin, weiß ich nicht, wie ich die Mängel des Referats beheben kann.

Und zwar:

Mit dem semop()-Befehl wird die Semaphore selbst bearbeitet

Code: Select all

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

int semop&#40;int semid, struct sembuf sem_array&#91;&#93;, size_t n_op&#41;;
Folgende Argumente werden beim semop()-Befehl übergeben:
- semid: die ID der Semaphore, die man bei semget() erhalten hat
- sem_array[]: die Adresse eines Arrays von der Struktur sembuf, die je eine Semaphorenoperation beschreiben
- n_op: gibt an, wie viele Elemente in dem Array (2. Parameter) stehen

sembuf beinhaltet folgende Felder:

Code: Select all

struct sembuf &#123;
    short sem_num;  /* semaphore number&#58; 0 = first */
    short sem_op;   /* semaphore operation */
    short sem_flg;  /* operation flags */
&#125;;
Bei sem_op hab ich jetzt folgendes stehen:

sem_op: bestimmt die Operation selbst. Folgendes ist möglich:
- semop > 0: Der Wert der Semaphore wird um den Wert, den semop hat, erhöht. Diese Operation blockiert nie
- semop < 0: Der Wert der Semaphore wird um den Wert von semop verringert.
Wird die Semaphore durch diese Operation negativ, wird der kritische Bereich gesperrt.
Versucht nun ein anderer Prozess, darauf zuzugreifen, hängt die Reaktion von dem sem_flag ab.
Wurde IPC_NOWAIT gesetzt, bricht semop() mit einem Fehler ab, ansonsten wartet der Prozess, bis die Ressource wieder freigegeben ist.
- semop == 0: ist der Wert der Semaphore semval Null, dann läuft die Operation einfach durch. Ansonsten blockiert dieser Prozess und wartet, bis das Semaphor Null wird.

Anscheinend stimmen aber die Beschreibungen für
semop == 0
und semop > 0 nicht ... !

Weitere Frage die der Lehrer fragt:
Was passiert, wenn bei semop nur ein Teil der sembuf-Spezifikationen erfüllt werden kann?

Und noch weiter:
Was ist nun key wirklich?

Meine Beschreibung wäre gewesen:
Der Key ist quasi die „magische Nummer“, also eine Zahl, die von den Programmen vereinbart wird, die über die Semaphore kommunizieren wollen.

Reicht aber anscheinend nicht aus ...

Irgend jemand hier im Forum, der sich da besser auskennt und meine Fragen beantworten kann?[/code]

User avatar
jochen
prolinux-forum-admin
Posts: 699
Joined: 14. Jan 2000 15:37
Location: Jülich
Contact:

#2 Post by jochen »

An naheliegendsten wäre da doch ein Blick in die man-Pages, nicht?

Was passiert, wenn nur ein Teil der Operationen ausgeführt werden können?
The set of operations contained in sops is performed atomically, that is, the operations are performed at the same
time, and only if they can all be simultaneously performed. The behaviour of the system call if not all operations
can be performed immediately depends on the presence of the IPC_NOWAIT flag in the individual sem_flg fields, as noted
below.
Fall sem_op==0:
If sem_op is zero, the process must have read permission on the semaphore set. This is a "wait-for-zero" operation:
if semval is zero, the operation can immediately proceed. Otherwise, if IPC_NOWAIT is specified in sem_flg, semop()
fails with errno set to EAGAIN (and none of the operations in sops is performed). Otherwise semzcnt (the count of
processes waiting until this semaphore's value becomes zero) is incremented by one and the process sleeps until one of
the following occurs:

o semval becomes 0, at which time the value of semzcnt is decremented.

o The semaphore set is removed: semop() fails, with errno set to EIDRM.

o The calling process catches a signal: the value of semzcnt is decremented and semop() fails, with errno set to
EINTR.

o The time limit specified by timeout in a semtimedop() call expires: semop() fails, with errno set to EAGAIN.
Falls semop < 0:
If sem_op is less than zero, the process must have alter permission on the semaphore set. If semval is greater than
or equal to the absolute value of sem_op, the operation can proceed immediately: the absolute value of sem_op is sub-
tracted from semval, and, if SEM_UNDO is specified for this operation, the system updates the process undo count
(semadj) for this semaphore. If the absolute value of sem_op is greater than semval, and IPC_NOWAIT is specified in
sem_flg, semop() fails, with errno set to EAGAIN (and none of the operations in sops is performed). Otherwise semncnt
(the counter of processes waiting for this semaphore's value to increase) is incremented by one and the process sleeps
until one of the following occurs:

o semval becomes greater than or equal to the absolute value of sem_op, at which time the value of semncnt is
decremented, the absolute value of sem_op is subtracted from semval and, if SEM_UNDO is specified for this
operation, the system updates the process undo count (semadj) for this semaphore.

o The semaphore set is removed from the system: semop() fails, with errno set to EIDRM.

o The calling process catches a signal: the value of semncnt is decremented and semop() fails, with errno set to
EINTR.

o The time limit specified by timeout in a semtimedop() call expires: the system call fails, with errno set to
EAGAIN.

On successful completion, the sempid value for each semaphore specified in the array pointed to by sops is set to the
process ID of the calling process. In addition, the sem_otime is set to the current time.
Falls sem_op > 0:
If sem_op is a positive integer, the operation adds this value to the semaphore value (semval). Furthermore, if
SEM_UNDO is specified for this operation, the system updates the process undo count (semadj) for this semaphore. This
operation can always proceed -- it never forces a process to wait. The calling process must have alter permission on
the semaphore set.
Ansonsten würde ich in solchen Bereichen der Bibbliothek mal einen Besuch abstatten. Zu empfehlen wäre

W. Richard Stevens, Programmierung in der UNIX-Umgebung

und

Marc J. Rochkind, UNIX-Programmierung für Fortgeschrittene

Das Internet ist prima, aber manchmal sind Lehrbücher das bessere Mittel der Wahl.

Jochen
Die grösste Lüge der EDV? "Mal eben..."

Post Reply