/[cvs]/joko/Uni/BSArch/02/bakery.c
ViewVC logotype

Annotation of /joko/Uni/BSArch/02/bakery.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Sun May 28 02:40:05 2006 UTC (18 years, 6 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +12 -3 lines
File MIME type: text/plain
minor changes and comments

1 joko 1.2
2     #include <stdlib.h>
3     #include "bakery.h"
4    
5 joko 1.4 /* Hilfsfunktion, um höchsten Wert (integer) in einem Array zu bestimmen */
6 joko 1.3 int maxValueInList(int list[], int list_length) {
7     int i;
8     int max_value = 0;
9     for (i = 0; i < list_length; i++) {
10     if (list[i] > max_value)
11     max_value = list[i];
12     }
13     return max_value;
14     }
15    
16 joko 1.4 /* Initialisiert Struktur zur Verwaltung kritischer Abschnitte */
17 joko 1.2 void My_InitializeCriticalSection( MY_LPCRITICAL_SECTION * sec ) {
18    
19     int i;
20    
21     /* Datenstruktur "erzeugen" */
22     *sec = malloc(sizeof(struct _crit_sec));
23    
24 joko 1.4 /* Felder der Datenstruktur mit "0" initialisieren */
25 joko 1.2 for (i = 0; i < MAX_THREADS; i++) {
26     (*sec)->choosing[i] = 0;
27     (*sec)->number[i] = 0;
28     }
29    
30     }
31    
32 joko 1.4 /* Gibt Speicher zur Verwaltung kritischer Abschnitte wieder frei */
33 joko 1.2 void My_DeleteCriticalSection( MY_LPCRITICAL_SECTION sec ) {
34    
35     /* Datenstruktur freigeben */
36     free(sec);
37    
38     }
39 joko 1.3
40 joko 1.4 /* Eintritt in kritischen Abschnitt */
41 joko 1.3 void My_EnterCriticalSection( int ThId, MY_LPCRITICAL_SECTION sec ) {
42    
43     int j;
44    
45     /* Beginn der Ticket-ID Berechnung */
46     sec->choosing[ThId] = 1;
47    
48 joko 1.4 /* Neue Ticket-ID berechnen und zuweisen */
49 joko 1.3 sec->number[ThId] = maxValueInList(sec->number, MAX_THREADS) + 1;
50    
51     /* Ende der Ticket-ID Berechnung */
52     sec->choosing[ThId] = 0;
53    
54     /* Alle Threads durchlaufen */
55     for (j = 0; j < MAX_THREADS; j++) {
56    
57 joko 1.4 /* Warten, falls ein anderer Thread momentan eine Ticket-ID berechnet (busy wait) */
58 joko 1.3 while (sec->choosing[j] == 1);
59    
60 joko 1.4 /* Bakery Algorithmus: Warten, bis man an der Reihe ist (busy wait) */
61 joko 1.3 while ( (sec->number[j] != 0) &&
62     (
63     (sec->number[j] < sec->number[ThId]) ||
64     ((sec->number[j] == sec->number[ThId]) && j < ThId)
65     )
66     );
67    
68     }
69    
70     }
71    
72 joko 1.4 /* Austritt aus kritischem Abschnitt */
73 joko 1.3 void My_LeaveCriticalSection( int ThId, MY_LPCRITICAL_SECTION sec ) {
74 joko 1.4
75     /* Ticket löschen */
76 joko 1.3 sec->number[ThId] = 0;
77 joko 1.4
78 joko 1.3 }

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed