Home > Back-end >  Describe a meal on all five philosophers into algorithm
Describe a meal on all five philosophers into algorithm

Time:10-20

Programming to complete the following process scheduling problem and the realization of synchronous simulation, (using the C language implementation in the Windows or Linux)
Requirements are as follows:
1) define a number of concurrent execution process, processes can define your own process (at least 2)
2) process scheduling can choose a scheduling algorithm: such as RR FCFS
Pray god give a code, I am a small white==

CodePudding user response:

The landlord to change the professional, this major is not for you,

CodePudding user response:

You want to change in the early, didn't get changed, just want to graduate,

CodePudding user response:

This is not simple, every philosopher, waiting for the other one chopsticks was starved to death

CodePudding user response:

Search code. Look at the example of others

CodePudding user response:

Operating system concurrency and mutually exclusive: philosophers dining and barber problems

1. Philosophers dining problem:
(1) under what circumstances may a philosopher all meals?
Consider two implementation ways, as follows:
A.
Algorithm description:
Void philosopher (int I)/* I: philosophers Numbers, from 0 to 4 */
{
While (TRUE) {
Think ();/* philosopher is thinking about */
Take_fork (I);/* */pick up on the left side of the chopsticks
Take_fork ((I + 1) % N);/* take the left side of the chopsticks. % of the modulus computing */
Eat (); Eat/* */
Put_fork (I);/* * put on the left side of the chopsticks back table/
Put_fork ((I + 1) % N);/* * put the chopsticks on the right side back table/
}
}
Analysis: if all the philosopher picked up on the left side of the chopsticks at the same time, see the chopsticks on the right side is not available, and were put down on the left side of the chopsticks,
Wait for a while, and picked up on the left side of the chopsticks at the same time, so, never repeated, in this case, that is, all programs are in
To run indefinitely, but were unable to make any progress, namely hunger, all philosophers have proper meal,
B.
Algorithm description:
Regulation, after get the chopsticks on the left of the check is on the right of chopsticks is available, if not available, are put on the left side of the chopsticks,
Wait for a period of time to repeat the entire process,
Analysis: when the following situations, at a certain moment, all philosophers launch this algorithm at the same time, picked up the chopsticks on the left of the
The son, and see the chopsticks on the right side is not available, and were put down on the left side of the chopsticks, wait for a while, and picked up on the left side of the chopsticks at the same time... So
Such repeated forever, in this case, all programs are running, but failed to make progress, namely hunger,
All the philosophers have proper meal,
(2) describe a no one starved to death (never take chopsticks) algorithm,
Considering the way four implementation (A, B, C, D) :
A. Principle: only allow up to four philosophers dining at the same time, to ensure that at least one philosopher to dine, eventually release
Release what he used two chopsticks, which can make more philosophers dining, the following room as a semaphore, allow only
Xu four philosopher into the restaurant at the same time, so you can ensure that at least one philosopher can dine, and applying to
Philosophers of the restaurant into the room waiting queue, based on the principle of FIFO, always entered the restaurant, and will therefore not be
Starve to death and the phenomenon of deadlock,
Pseudo code:
Semaphore chopstick [5]=,1,1,1,1 {1};
Semaphore room=4;
Void philosopher (int I)
{
While (true)
{
Think ();
Wait (room);//request into the dining room
Wait (chopstick [I]);//request to the left-hand side of the chopsticks
Wait (chopstick [5] (I + 1) %).//request right hand chopsticks
Eat ();
Signal (chopstick [5] (I + 1) %).//release the chopsticks on the right-hand side of the
Signal (chopstick [I]);//release the left-hand side of the chopsticks
Signal (room);//out of room to release semaphore room
}
}
B. Principle: only when philosophers about the two chopsticks are available, to allow him to pick up the chopsticks dinner,
Method 1: using type AND semaphore mechanism: according to the course, in a primitive, will also need a piece of code
To multiple critical resources, either all assigned to it, either a don't allocate, so not a deadlock situation, when
Some resource is not a blocking call process; But because of the waiting queue, make requests for resources to meet the requirements of the FIFO,
So there will not be hungry,
Pseudo code:
Semaphore chopstick [5]=,1,1,1,1 {1};
Void philosopher (int I)
{
While (true)
{
Think ();
Swait (chopstick/(I + 1) % 5, chopstick [I]);
Eat ();
Ssignal (chopstick/(I + 1) % 5, chopstick [I]);
}
}
Method 2: use the protection mechanism of the semaphore implementation, through the semaphore mutex to eat () take the left and the right side of the chopsticks before
The operations of protection, as an atomic operation, so that we can prevent the occurrence of a deadlock,
Pseudo code:
Semaphore mutex=1;
Semaphore chopstick [5]=,1,1,1,1 {1};
Void philosopher (int I)
{
While (true)
{
Think ();
Wait (mutex);
Wait (chopstick/(I + 1) % 5);
Wait (chopstick [I]);
Signal (mutex);
Eat ();
Signal (chopstick/(I + 1) % 5);
Signal (chopstick [I]);
}
}
C. Principle: provisions of odd number philosopher picked up his first on the left side of the chopsticks, chopsticks and then to pick up his right hand; And the even number
Philosopher, by contrast, according to the regulations, will be the no. 1, 2, a philosopher chopsticks competition 1, 3, 4, philosopher competition 3 chopsticks. Namely,
Five philosophers have an odd number of chopsticks competition, after get, again to compete the even number of chopsticks, finally there will always be a philosopher to
Two chopsticks while eating, and apply for less than the philosopher into the block waiting queue, root FIFO principle, application of zhe first
Scientists will more can have dinner first, so there will not be starved to death philosophers,
Pseudo code:
Semaphore chopstick [5]=,1,1,1,1 {1};
Void philosopher (int I)
{
While (true)
{
Think ();
If (I % 2==0)//even philosophers, right rear left first,
{
Wait (chopstick mod [I + 1] 5);
Wait (chopstick [I]);
Eat ();
Signal (chopstick mod [I + 1] 5);
Signal (chopstick [I]);
}
The Else//odd philosophers, right after the left first,
{
Wait (chopstick [I]);
Wait (chopstick mod [I + 1] 5);
Eat ();
Signal (chopstick [I]);
Signal (chopstick mod [I + 1] 5);
}
}
D. Using monitor system (finally the implementation is failure, see the following analysis) :
Principle: not on every set semaphore only chopsticks, but each philosopher set the semaphore, the test () function has the following a
Use:
A. if the current treatment of hungry and on both sides of the philosophers is not eat state, is currently a philosopher by
The test () function is trying to enter a state of eating,
B. if passed the test () the state of eating is not successful, then the current philosophers have blocked on the semaphore waiting for, until the
Other philosophers process through the test () sets the state of the philosopher to EATING,
C. when a philosopher process called put_forks () put down the chopsticks, will pass the test () to test its neighbors,
If hungry neighbors and the neighbors neighbors eat not state, then the neighbors eat into the state,
By as a result, the algorithm is not a deadlock, because a philosopher only two neighbors are not where, only allow
Xu conversion to dine,
The algorithm of a philosopher optimum end cannot eat would happen, when the philosophers about the two philosophers alternating
In state when you eat, the philosopher is unable to enter the state of the eating, so don't meet the requirements of the title,
But this algorithm can achieve for arbitrary many philosophers can achieve maximum parallelism, therefore has the important
The meaning of
Pseudo code:
# define N 5/* number of philosophers */
# define LEFT (I - 1 + N) % N/* I LEFT neighbor number */
# define RIGHT (I + 1) % N/* I RIGHT neighbor number */
Typedef enum {THINKING, HUNGRY, EATING} phil_state;/* philosopher state */
The monitor dp/* */passes
{
Phil_state state [N].
Semaphore mutex=1;
Semaphore s [N]./* every philosopher a semaphore, the initial value of 0 */
Void test (int I)
{
If (state [I]==HUNGRY & amp; & The state [LEFT (I)]!=EATING & amp; &
(I) the state [RIGHT]!=EATING)
{
The state [I]=EATING;
[I] V (s);
}
}
Void get_forks (int I)
{
P (mutex);
The state [I]=HUNGRY;
The test (I);/* */trying to get the two chopsticks
V (mutex);
[I] P (s);/* not chopsticks are blocking */
}
Void put_forks (int I)
{
P (mutex);
The state [I]=THINKING;
The test (LEFT (I));/* if the neighbours dinner */
The test (I) (RIGHT);/* whether right adjacent dining */
V (mutex);
}
}
Philosopher process is as follows:
Void philosopher (int process)
{
While (true)
{
Think ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related