2005-02-22 11:22:44 +00:00
|
|
|
/*
|
1996-02-13 15:40:05 +00:00
|
|
|
diningPhilosophers.h
|
|
|
|
|
|
|
|
Five hungry philosophers testing locks and threads
|
|
|
|
This program loops indefinitely.
|
|
|
|
|
|
|
|
Copyright (C) 1996 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Author: Scott Christley <scottc@net-community.com>
|
|
|
|
Date: 1996
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
This file is part of the GNUstep Application Kit Library.
|
|
|
|
|
2008-06-08 10:38:33 +00:00
|
|
|
This file is free software; you can redistribute it and/or
|
2005-07-15 22:51:23 +00:00
|
|
|
modify it under the terms of the GNU General Public
|
1996-02-13 15:40:05 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2007-09-14 11:36:11 +00:00
|
|
|
version 3 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2008-06-08 10:38:33 +00:00
|
|
|
This file is distributed in the hope that it will be useful,
|
1996-02-13 15:40:05 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2005-07-15 22:51:23 +00:00
|
|
|
General Public License for more details.
|
1996-02-13 15:40:05 +00:00
|
|
|
|
2005-07-15 22:51:23 +00:00
|
|
|
You should have received a copy of the GNU General Public
|
2008-06-08 10:38:33 +00:00
|
|
|
License along with this file; if not, write to the Free
|
2005-05-22 03:32:16 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
2005-02-22 11:22:44 +00:00
|
|
|
*/
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
#include <Foundation/NSLock.h>
|
|
|
|
#include <Foundation/NSThread.h>
|
1998-10-03 05:11:05 +00:00
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
1999-07-14 19:12:16 +00:00
|
|
|
#include <Foundation/NSValue.h>
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
// Conditions
|
|
|
|
#define NO_FOOD 1
|
|
|
|
#define FOOD_SERVED 2
|
|
|
|
|
|
|
|
// NSLocks ... umm I mean forks
|
|
|
|
id forks[5];
|
2003-06-24 03:46:13 +00:00
|
|
|
id fork_lock;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// A class of hungry philosophers
|
|
|
|
//
|
|
|
|
@interface Philosopher : NSObject
|
|
|
|
|
|
|
|
{
|
|
|
|
int chair;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instance methods
|
1999-07-14 19:12:16 +00:00
|
|
|
- (void)sitAtChair:(NSNumber*)position;
|
1996-02-13 15:40:05 +00:00
|
|
|
- (int)chair;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Philosopher
|
|
|
|
|
|
|
|
// Instance methods
|
1999-07-14 19:12:16 +00:00
|
|
|
- (void)sitAtChair:(NSNumber*)position
|
1996-02-13 15:40:05 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// Sit down
|
1999-07-14 19:12:16 +00:00
|
|
|
chair = [position intValue];
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
// Its a constant battle to feed yourself
|
|
|
|
while (1)
|
|
|
|
{
|
2003-06-24 03:46:13 +00:00
|
|
|
|
|
|
|
[fork_lock lockWhenCondition:FOOD_SERVED];
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
// Get the fork to our left
|
|
|
|
[forks[chair] lockWhenCondition:FOOD_SERVED];
|
|
|
|
|
|
|
|
// Get the fork to our right
|
|
|
|
[forks[(chair + 1) % 5] lockWhenCondition:FOOD_SERVED];
|
|
|
|
|
2003-06-24 03:46:13 +00:00
|
|
|
[fork_lock unlock];
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
// Start eating!
|
2003-06-24 03:46:13 +00:00
|
|
|
printf("Philosopher %d can start eating.\n", chair); fflush(stdout);
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
for (i = 0;i < 100000; ++i)
|
|
|
|
{
|
|
|
|
if ((i % 10000) == 0)
|
2003-06-24 03:46:13 +00:00
|
|
|
printf("Philosopher %d is eating.\n", chair); fflush(stdout);
|
1996-02-13 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done eating
|
2003-06-24 03:46:13 +00:00
|
|
|
printf("Philosopher %d is done eating.\n", chair); fflush(stdout);
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
|
|
// Drop the fork to our left
|
|
|
|
[forks[chair] unlock];
|
|
|
|
|
|
|
|
// Drop the fork to our right
|
|
|
|
[forks[(chair + 1) % 5] unlock];
|
|
|
|
|
|
|
|
// Wait until we are hungry again
|
|
|
|
for (i = 0;i < 1000000 * (chair + 1); ++i) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We never get here, but this is what we should do
|
|
|
|
[NSThread exit];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)chair
|
|
|
|
{
|
|
|
|
return chair;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
//
|
|
|
|
// my main for the test app
|
|
|
|
//
|
|
|
|
int main()
|
|
|
|
{
|
2002-02-25 15:04:06 +00:00
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
|
|
|
int i;
|
|
|
|
id p[5];
|
|
|
|
|
|
|
|
// Create the locks
|
|
|
|
for (i = 0;i < 5; ++i)
|
|
|
|
{
|
2005-02-22 11:22:44 +00:00
|
|
|
forks[i] = [[NSConditionLock alloc]
|
2002-02-25 15:04:06 +00:00
|
|
|
initWithCondition:NO_FOOD];
|
|
|
|
[forks[i] lock];
|
|
|
|
}
|
|
|
|
|
2003-06-24 03:46:13 +00:00
|
|
|
fork_lock = [[NSConditionLock alloc]
|
|
|
|
initWithCondition:NO_FOOD];
|
|
|
|
[fork_lock lock];
|
|
|
|
|
2002-02-25 15:04:06 +00:00
|
|
|
// Create the philosophers
|
|
|
|
for (i = 0;i < 5; ++i)
|
|
|
|
p[i] = [[Philosopher alloc] init];
|
|
|
|
|
|
|
|
// Have them sit at the table
|
|
|
|
for (i = 0;i < 5; ++i)
|
|
|
|
[NSThread detachNewThreadSelector:@selector(sitAtChair:)
|
|
|
|
toTarget:p[i] withObject: [NSNumber numberWithInt: i]];
|
|
|
|
|
|
|
|
// Now let them all eat
|
2003-06-24 03:46:13 +00:00
|
|
|
[fork_lock unlockWithCondition:FOOD_SERVED];
|
2002-02-25 15:04:06 +00:00
|
|
|
for (i = 0;i < 5; ++i)
|
|
|
|
[forks[i] unlockWithCondition:FOOD_SERVED];
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2002-02-25 15:04:06 +00:00
|
|
|
while (1);
|
|
|
|
[arp release];
|
1996-02-13 15:40:05 +00:00
|
|
|
}
|
|
|
|
|