Sie sind auf Seite 1von 5

Operating Systems CSCI03I04 Lab 5

Deadlock:
Is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does. In an operating system, a deadlock is a situation which occurs when a process or thread enters a waiting state because a resource requested is being held by another waiting process, which is waiting for another resource.

If a process is unable to change its state because the resources requested by it are being used by another waiting process, then the system is said to be in a deadlock. Deadlock is a common problem in multiprocessing systems, parallel computing and distributed systems, where software and hardware locks are used to handle shared resources and implement process synchronization.

Starvation
Is a problem encountered in multitasking where a process needs for necessary resources. Without those resources, the program can never finish its task. Starvation is usually caused by an overly simplistic scheduling algorithm. For example, if a multi-tasking system always switches between the first two tasks while a third never gets to run, then the third task is being starved of CPU time It occurs when a scheduler process (i.e. the operating system) refuses to give a particular thread any quantity of a particular resource (generally CPU). If there are too many high-priority threads, a lower priority thread may be starved.

Five silent philosophers sit at a table around a bowl of spaghetti. A fork is placed between each pair of adjacent philosophers. Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when he has both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it's not being used by another philosopher. After he finishes eating, he needs to put down both forks so they become available to others. A philosopher can grab the fork on his right or the one on his left as they become available, but can't start eating before getting both of them. The algorithm to solve it is to:

think until the left fork is available; when it is, pick it up; think until the right fork is available; when it is, pick it up; when both forks are held, eat for a fixed amount of time; then, put the right fork down; then, put the left fork down; Repeat from the beginning.

The Solution:
#include <iostream> #include <process.h> #include <windows.h> #include <string> #include <time.h> /*There are 5 philosophers each an instance of the class Philosopher and upon the completion of eating they can either put back the fork or decide not too causing a deadlock randomly */ #define AMOUNT 5 using namespace std; class Philosopher { private: /* some sort of pointer, handle can be anything from an integer index to a pointer to a resource in kernel space */ HANDLE *left_fork, *right_fork; string name; bool has_eaten;

public: Philosopher(string phil_name, HANDLE* left_fork_handle, HANDLE* right_fork_handle) : name(phil_name), left_fork(left_fork_handle), right_fork(right_fork_handle) { has_eaten = false; } void eat() { if (!has_eaten) { cout << name << " is attempting to eat\n"; if (WaitForSingleObject(*left_fork, 5000) == WAIT_TIMEOUT || WaitForSingleObject(*right_fork, 5000) == WAIT_TIMEOUT) { cout << name << " can't eat at this point in time. " << name << " decides to think\n"; think(); } else { cout << name << " is now eating\n"; Sleep(9000); //Initialize random number generator(seed) srand(time(0)); if (rand() % 2) { //arising starvation cout << name << " decides that he/she is not going to put back the fork\n"; SuspendThread(GetCurrentThread());

else { ReleaseMutex(*left_fork); ReleaseMutex(*right_fork); } cout << name << " has finished eating <<<<<<----------\n"; has_eaten = true; } } else cout << name << " has already eaten :D \n"; } void think() { cout << name << " is now thinking\n"; Sleep(9000); eat(); } };

//once initiated he wants to eat unsigned int _stdcall PhiloInit(void * phil) { Philosopher * whichever = static_cast<Philosopher *>(phil); whichever->eat(); return 0; } void main() { HANDLE *forks; HANDLE *philo_guys = new HANDLE[AMOUNT]; forks = new HANDLE[AMOUNT]; for (int i = 0; i < AMOUNT; i++) { forks[i] = CreateMutex(0, 0, 0); } //_beginthreadex is a function creates a thread Philosopher Noha("Noha", &forks[0], &forks[1]); philo_guys[0] = (HANDLE)_beginthreadex(0, 0, &PhiloInit, &Noha, 0, 0); Philosopher Walid("Walid", &forks[1], &forks[2]); philo_guys[1] = (HANDLE)_beginthreadex(0, 0, &PhiloInit, &Walid, 0, 0); Philosopher Mostafa("Mostafa", &forks[2], &forks[3]); philo_guys[2] = (HANDLE)_beginthreadex(0, 0, &PhiloInit, &Mostafa, 0, 0); Philosopher Samiha("Samiha", &forks[3], &forks[4]); philo_guys[3] = (HANDLE)_beginthreadex(0, 0, &PhiloInit, &Samiha, 0, 0); Philosopher Karim("Karim", &forks[4], &forks[0]); philo_guys[4] = (HANDLE)_beginthreadex(0, 0, &PhiloInit, &Karim, 0, 0);

/*Waits until one or all of the specified objects are in the finish state or the time-out interval elapses.*/ WaitForMultipleObjects(AMOUNT, philo_guys, true, INFINITE); for (int i = 0; i < AMOUNT; i++) { CloseHandle(forks[i]); } }

Das könnte Ihnen auch gefallen