Operating systems
Posted: Fri Jan 17, 2003 12:00 am
Hi there,
I real need ideas on how to solve these problems
A certain number of philosophers sit around a circular table. They spend their lives alternating between thinking and eating. In the original version of the problem, there are as many chopsticks on the table as the number of philosophers. The philosophers thus had to synchronize access to the chopsticks, with their neighbors.
For the current exercise, you will assume instead that each philosopher is provided with a pair of chopsticks and can start eating at any point of time. Each philosopher will think for a random amount time between 0 and 6 seconds and eat for time between 0 and 3 seconds. All philosophers start out thinking. Your program, philos, will take as input the number of philosophers (N) and the number of times each philosopher will alternate between thinking and eating (M). The philosophers write to output their unique id (between 0 and (N-1)) and their current state at every transition. The program terminates after all philosophers have completed M transitions, printing the "Done." on a separate line.
You are to build a threaded program: Each "philosopher" must execute in a separate thread.
Sample invocation : ./philos 5 3
Sample output :
Philos 0 : Thinking.
Philos 1 : Thinking.
Philos 2 : Thinking.
Philos 3 : Thinking.
Philos 4 : Thinking.
Philos 0 : Eating.
Philos 1 : Eating.
Philos 1 : Thinking.
Philos 3 : Eating.
Philos 2 : Eating.
Philos 2 : Thinking.
Philos 4 : Eating.
Philos 1 : Eating.
Philos 2 : Eating.
Philos 0 : Thinking.
Philos 3 : Thinking.
Philos 0 : Eating.
Philos 4 : Thinking.
Philos 3 : Eating.
Philos 4 : Eating.
Done.
Hints: use the following man pages
pthread_create, pthread_join
random
sleep
make
gcc
gdb (issue an "add gnu" command to get access to the Gnu Debugger)
Implement a user-level non-preemptive threads library mythreads.a with the following methods:
mythread_create
mythread_yield
mythread_join
mythread_exit
These methods have the same signature as their corresponding counterparts in the previous exercise with pthread_ replaced by mythread_. You should implement simple FCFS (first come first serve) as a scheduling policy using a doubly linked queue of threads, including an idle thread. Also, think carefully about the semantics of the join operation and consider using a wrapper function to invoke the actual thread function for a newly create thread. (What is the relation between the wrapper and the join operation?)
You cannot use any of the pthread_XXX or thr_XXX routines, you have to exclusively use the _lwp_XXX routines below. The idea is to design and implement your own threading library.
Hints: use the following man pages
getcontext
_lwp_makecontext
_lwp_create, _lwp_exit
_lwp_suspend, _lwp_continue
Mathiew
I real need ideas on how to solve these problems
A certain number of philosophers sit around a circular table. They spend their lives alternating between thinking and eating. In the original version of the problem, there are as many chopsticks on the table as the number of philosophers. The philosophers thus had to synchronize access to the chopsticks, with their neighbors.
For the current exercise, you will assume instead that each philosopher is provided with a pair of chopsticks and can start eating at any point of time. Each philosopher will think for a random amount time between 0 and 6 seconds and eat for time between 0 and 3 seconds. All philosophers start out thinking. Your program, philos, will take as input the number of philosophers (N) and the number of times each philosopher will alternate between thinking and eating (M). The philosophers write to output their unique id (between 0 and (N-1)) and their current state at every transition. The program terminates after all philosophers have completed M transitions, printing the "Done." on a separate line.
You are to build a threaded program: Each "philosopher" must execute in a separate thread.
Sample invocation : ./philos 5 3
Sample output :
Philos 0 : Thinking.
Philos 1 : Thinking.
Philos 2 : Thinking.
Philos 3 : Thinking.
Philos 4 : Thinking.
Philos 0 : Eating.
Philos 1 : Eating.
Philos 1 : Thinking.
Philos 3 : Eating.
Philos 2 : Eating.
Philos 2 : Thinking.
Philos 4 : Eating.
Philos 1 : Eating.
Philos 2 : Eating.
Philos 0 : Thinking.
Philos 3 : Thinking.
Philos 0 : Eating.
Philos 4 : Thinking.
Philos 3 : Eating.
Philos 4 : Eating.
Done.
Hints: use the following man pages
pthread_create, pthread_join
random
sleep
make
gcc
gdb (issue an "add gnu" command to get access to the Gnu Debugger)
Implement a user-level non-preemptive threads library mythreads.a with the following methods:
mythread_create
mythread_yield
mythread_join
mythread_exit
These methods have the same signature as their corresponding counterparts in the previous exercise with pthread_ replaced by mythread_. You should implement simple FCFS (first come first serve) as a scheduling policy using a doubly linked queue of threads, including an idle thread. Also, think carefully about the semantics of the join operation and consider using a wrapper function to invoke the actual thread function for a newly create thread. (What is the relation between the wrapper and the join operation?)
You cannot use any of the pthread_XXX or thr_XXX routines, you have to exclusively use the _lwp_XXX routines below. The idea is to design and implement your own threading library.
Hints: use the following man pages
getcontext
_lwp_makecontext
_lwp_create, _lwp_exit
_lwp_suspend, _lwp_continue
Mathiew