Question: Concurrency code: please write me a code for the follwoing instructions: --->> Ticket buying (browsing grabs for 2 minutes, auto-returning to pool after 2 minutes,

Concurrency code: please write me a code for the follwoing instructions:

--->> Ticket buying (browsing grabs for 2 minutes, auto-returning to pool after 2 minutes, buying takes ticket out of inventory, 20 random ticket returns of 1 ticket), big event of 500 tickets, 3 sellers, 1000 buyers of 1, 2, 3 tickets wanted

Below one of the example about Concurrency but i want you to write a code about this -->>> ticket buying (browsing grabs for 2 minutes, auto-returning to pool after 2 minutes, buying takes ticket out of inventory, 20 random ticket returns of 1 ticket), big event of 500 tickets, 3 sellers, 1000 buyers of 1, 2, 3 tickets wanted

Dining Philosophers

Another classic concurrency problem concerns a group of philosophers seated about a round table eating spaghetti. There are the same total number of forks as there are philosophers and one fork is placed between every two philosophersthat puts a single fork to the left and right of each philosopher. The philosophers run the traditional think-eat loop. After he sits quietly thinking for a while, a philosopher gets hungry.

To eat, a philosopher grabs the fork to the left, grabs the fork to the right, eats for a time using both forks, and then replaces the forks and goes back to thinking. (There's a related problem, the Dining Programmers, where you have group of programmers sitting around a round table eating sushi with one chopstick between every two programmers.)

There is a possible deadlock condition if the philosophers are allowed to grab forks freely. The deadlock occurs if no one is eating, and then all the philosophers grab the fork to their left, and then look over and wait for the right fork.

Solution

One simple and safe solution is to restrict the number of philosophers allowed to even try to eat at once. If you only allow (n-1) of the philosophers to try to eat, then you can show that the deadlock situation cannot occur. This correctness comes at the cost of allowing slightly less spaghetti throughput than without the restriction. The restrict- competition-to-avoid-the-deadlock can be a staple technique for avoiding deadlock.

What is another way to avoid the deadlock? Hint: don't have all the philosophers follow the exact same program.

/**

dining.c

* --------

The classic Dining Philosophers example. Allow a group of dining

philosophers to eat at a round sharing the forks between each person.

Each person needs both forks to eat.

*/

#include "thread_107.h"

#include

#define NUM_DINERS 5

#define EAT_TIMES 3

/* Macros to conveniently refer to forks to left and right of each person */

#define LEFT(philNum) (philNum)

#define RIGHT(philNum) (((philNum)+1) % NUM_DINERS)

/*

Our main is creates a semaphore for every fork in an unlocked state

(one philosopher can immediately acquire each fork) and sets up the

numEating semaphore to only allow N-1 philosophers to try and grab

their forks. Each philosopher runs its own thread. They should

finish after getting their fill of spaghetti. By running with the

-v flag, it will include the trace output from the thread library.

*/

void main(int argc, char **argv)

{

int i;

char name[32];

bool verbose = (argc == 2 && (strcmp(argv[1], "-v") == 0));

Semaphore fork[NUM_DINERS]; // semaphore to control access per fork Semaphore numEating; // to restrict contention for forks

InitThreadPackage(verbose);

for (i = 0; i < NUM_DINERS; i++) { // Create all fork semaphores sprintf(name, "Fork %d", i);

fork[i] = SemaphoreNew(name, 1); // all forks start available

}

numEating = SemaphoreNew(Num Eating, NUM_DINERS - 1);

for (i = 0; i < NUM_DINERS; i++) { // Create all philosopher threads sprintf(name, "Philosopher %d", i);

ThreadNew(name, Philosopher, 3, numEating, fork[LEFT(i)], fork[RIGHT(i)]);

}

RunAllThreads(); printf("All done! ");

SemaphoreFree(numEating);

for (i = 0; i < NUM_DINERS; i++) SemaphoreFree(fork[i]);

}

/**

Philosopher

* -----------

This is the routine run in each of the philosopher threads. Each runs in

ean at-think loop where pondering for a while builds up a big hunger.

*/

static void Philosopher(Semaphore numEating, Semaphore leftFork,

Semaphore rightFork)

{

int i;

for (i = 0; i < EAT_TIMES; i++) {

Think();

Eat(numEating, leftFork, rightFork);

}

}

static void Think(void)

{

printf("%s thinking! ", ThreadName()); RandomDelay(10000,50000); // "think" for random time

}

/**

We first wait on the availability of an opportunity to eat, and

only then do we attempt to grab our left & right forks and chow

some spaghetti. Notice that we let go of the locks in the reverse

order that we acquire them, so that we don't signal another

philosopher to eat until after we have put down both our forks.

*/

static void Eat(Semaphore numEating, Semaphore leftFork, Semaphore rightFork)

{

SemaphoreWait(numEating); // wait until can try to get forks SemaphoreWait(leftFork); // get left

SemaphoreWait(rightFork); // get right

printf("%s eating! ", ThreadName()); RandomDelay(10000,50000); // "eat" for random time

SemaphoreSignal(leftFork); // let go SemaphoreSignal(rightFork); SemaphoreSignal(numEating);

}

Output

elaine24:/usr/class/cs107/other/thread_examples> dining Philosopher 3 thinking!

Philosopher 4 thinking!

Philosopher 0 thinking!

Philosopher 1 thinking!

Philosopher 2 thinking!

Philosopher 2 eating!

Philosopher 0 eating!

Philosopher 0 thinking!

Philosopher 4 eating!

Philosopher 2 thinking!

Philosopher 4 thinking!

Philosopher 0 eating!

Philosopher 2 eating!

Philosopher 2 thinking!

Philosopher 0 thinking!

Philosopher 3 eating!

Philosopher 0 eating!

Philosopher 3 thinking!

Philosopher 2 eating!

Philosopher 4 eating!

Philosopher 4 thinking!

Philosopher 1 eating!

Philosopher 3 eating!

Philosopher 3 thinking!

Philosopher 1 thinking!

Philosopher 4 eating!

Philosopher 1 eating!

Philosopher 3 eating!

Philosopher 1 thinking!

Philosopher 1 eating! All done!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!