~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Testing/diningPhilosophers.m

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
 
1
/*
2
2
   diningPhilosophers.h
3
3
 
4
4
   Five hungry philosophers testing locks and threads
8
8
 
9
9
   Author:  Scott Christley <scottc@net-community.com>
10
10
   Date: 1996
11
 
   
 
11
 
12
12
   This file is part of the GNUstep Application Kit Library.
13
13
 
14
14
   This library is free software; you can redistribute it and/or
15
15
   modify it under the terms of the GNU Library General Public
16
16
   License as published by the Free Software Foundation; either
17
17
   version 2 of the License, or (at your option) any later version.
18
 
   
 
18
 
19
19
   This library is distributed in the hope that it will be useful,
20
20
   but WITHOUT ANY WARRANTY; without even the implied warranty of
21
21
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
23
 
24
24
   If you are interested in a warranty or support for this source code,
25
25
   contact Scott Christley <scottc@net-community.com> for more information.
26
 
   
 
26
 
27
27
   You should have received a copy of the GNU Library General Public
28
28
   License along with this library; if not, write to the Free
29
29
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
30
 
*/ 
 
30
*/
31
31
 
32
32
#include <Foundation/NSLock.h>
33
33
#include <Foundation/NSThread.h>
40
40
 
41
41
// NSLocks ... umm I mean forks
42
42
id forks[5];
 
43
id fork_lock;
43
44
 
44
45
//
45
46
// A class of hungry philosophers
69
70
        // Its a constant battle to feed yourself
70
71
        while (1)
71
72
        {
 
73
 
 
74
                [fork_lock lockWhenCondition:FOOD_SERVED];
 
75
 
72
76
                // Get the fork to our left
73
77
                [forks[chair] lockWhenCondition:FOOD_SERVED];
74
78
 
75
79
                // Get the fork to our right
76
80
                [forks[(chair + 1) % 5] lockWhenCondition:FOOD_SERVED];
77
81
 
 
82
                [fork_lock unlock];
 
83
 
78
84
                // Start eating!
79
 
                printf("Philosopher %d can start eating.\n", chair);
 
85
                printf("Philosopher %d can start eating.\n", chair); fflush(stdout);
80
86
 
81
87
                for (i = 0;i < 100000; ++i)
82
88
                {
83
89
                        if ((i % 10000) == 0)
84
 
                                printf("Philosopher %d is eating.\n", chair);
 
90
                                printf("Philosopher %d is eating.\n", chair); fflush(stdout);
85
91
                }
86
92
 
87
93
                // Done eating
88
 
                printf("Philosopher %d is done eating.\n", chair);
 
94
                printf("Philosopher %d is done eating.\n", chair); fflush(stdout);
89
95
 
90
96
                // Drop the fork to our left
91
97
                [forks[chair] unlock];
120
126
  // Create the locks
121
127
  for (i = 0;i < 5; ++i)
122
128
  {
123
 
          forks[i] = [[NSConditionLock alloc] 
 
129
          forks[i] = [[NSConditionLock alloc]
124
130
                  initWithCondition:NO_FOOD];
125
131
          [forks[i] lock];
126
132
  }
127
133
 
 
134
        fork_lock = [[NSConditionLock alloc]
 
135
                initWithCondition:NO_FOOD];
 
136
        [fork_lock lock];
 
137
 
128
138
  // Create the philosophers
129
139
  for (i = 0;i < 5; ++i)
130
140
          p[i] = [[Philosopher alloc] init];
135
145
                  toTarget:p[i] withObject: [NSNumber numberWithInt: i]];
136
146
 
137
147
  // Now let them all eat
 
148
        [fork_lock unlockWithCondition:FOOD_SERVED];
138
149
  for (i = 0;i < 5; ++i)
139
150
          [forks[i] unlockWithCondition:FOOD_SERVED];
140
 
  
 
151
 
141
152
  while (1);
142
153
  [arp release];
143
154
}