Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Filosofi.cpp : Defines the entry point for the console application.
- //
- // Date: 12.06.2013
- // Time: 22:06
- // Semaphores example program
- #include "stdafx.h"
- #include "windows.h"
- #include "iostream"
- #include "process.h"
- #include "math.h"
- using namespace std;
- //global variables
- //hMutex used for blocking threads
- HANDLE hMutex;
- //used to block entrance to access to check if mutex is blocked
- int gCurrentAvailableSemaphores = 3;
- //obvious
- int gRequiredIterations=0;
- //program output - factorial of iteration count value
- long gOutput = 1;
- //global enum used to set colors of text and backgroind in console using names of colors
- enum ConsoleColor
- {
- Black = 0,
- Blue = 1,
- Green = 2,
- Cyan = 3,
- Red = 4,
- Magenta = 5,
- Brown = 6,
- LightGray = 7,
- DarkGray = 8,
- LightBlue = 9,
- LightGreen = 10,
- LightCyan = 11,
- LightRed = 12,
- LightMagenta = 13,
- Yellow = 14,
- White = 15
- };
- //sets the console output text color to extText and background to extBackground
- void SetColor(ConsoleColor extText, ConsoleColor extBackground)
- {
- HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hStdOut, (WORD)((extBackground << 4) | extText));
- }
- class semaphore {
- bool state;
- public:
- //constructor
- semaphore() {
- state = true;
- }
- void setState(bool ex_state) {
- state = ex_state;
- }
- bool getState() const{
- return state;
- }
- };
- class philosophist {
- int forkA, forkB, number;
- public:
- philosophist(int ex_number, int ex_forkAval, int ex_forkBval) {
- forkA = ex_forkAval;
- forkB = ex_forkBval;
- number = ex_number;
- }
- int getForkA () {return forkA;}
- int getForkB () {return forkB;}
- int getID() {return number;}
- };
- //all semaphores in one array
- semaphore *gSems=new semaphore[5];
- //static thread function __stdcall required for _beginthreadex
- static unsigned __stdcall philosophistFunction(void *params)
- {
- //getting id from thread params
- philosophist *ph = static_cast<philosophist*>(params);
- do {
- //if forks are available
- if (gSems[ph->getForkA()].getState() && gSems[ph->getForkB()].getState()) {
- //reducing available semaphores count
- gSems[ph->getForkA()].setState(false);
- gSems[ph->getForkB()].setState(false);
- Sleep(100);
- SetColor(Black,White);
- cout << "Philosophist #"<<ph->getID()<<" gets his spoons and started eating"<<endl;
- Sleep(1800);
- cout << "Philosophist #"<<ph->getID()<<" finished eating"<<endl;
- Sleep(100);
- gSems[ph->getForkA()].setState(true);
- gSems[ph->getForkB()].setState(true);
- }
- else {
- Sleep(250);
- cout << "Philosophist #"<<ph->getID()<<" is waiting for his spoons become available"<<endl;
- Sleep(250);
- }
- } while (true);
- return NULL;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- //creating threads
- philosophist *ph1 = new philosophist(1,0,1);
- _beginthreadex(NULL, 0, &philosophistFunction, &ph1, 0, 0);
- Sleep(50);
- philosophist *ph2 = new philosophist(2,1,2);
- _beginthreadex(NULL, 0, &philosophistFunction, &ph2, 0, 0);
- Sleep(50);
- philosophist *ph3 = new philosophist(3,2,3);
- _beginthreadex(NULL, 0, &philosophistFunction, &ph3, 0, 0);
- Sleep(50);
- philosophist *ph4 = new philosophist(4,3,4);
- _beginthreadex(NULL, 0, &philosophistFunction, &ph4, 0, 0);
- Sleep(50);
- philosophist *ph5 = new philosophist(5,4,5);
- _beginthreadex(NULL, 0, &philosophistFunction, &ph5, 0, 0);
- //loopin till the end
- cin;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement