Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <semaphore.h>
- #include <pthread.h>
- #include <iostream>
- #include <queue>
- #include <pthread.h>
- #include <time.h>
- #include <fstream>
- using namespace std;
- struct car {
- int CarID;
- char direction;
- string arrival_time;
- string start_time;
- string end_time;
- };
- //returns a string with Hours:minute:seconds
- string getTime()
- {
- time_t rawtime;
- struct tm *info;
- char buffer[80];
- time( &rawtime );
- info = localtime( &rawtime );
- strftime(buffer,80,"%I:%M:%S", info);
- string output(buffer);
- return output;
- }
- int pthread_sleep(int seconds)
- {
- pthread_mutex_t mutex;
- pthread_cond_t conditionvar;
- struct timespec timetoexpire;
- if (pthread_mutex_init(&mutex, NULL))
- {
- return -1;
- }
- if (pthread_cond_init(&conditionvar, NULL))
- {
- return -1;
- }
- timetoexpire.tv_sec = (unsigned int)time(NULL) + seconds + 1;
- timetoexpire.tv_nsec = 0;
- return pthread_cond_timedwait(&conditionvar, &mutex, &timetoexpire);
- }
- int carCounter = 0;
- queue<car> northCars, southCars;
- sem_t countSouth, countNorth;
- pthread_mutex_t road;
- bool flagPerson = false;
- ofstream carFile;
- ofstream flagFile;
- void printToCar(car& c)
- {
- carFile << c.CarID << "\t" << c.direction << "\t\t" << c.arrival_time << "\t" << c.start_time << "\t" << c.end_time << endl;
- }
- void printToFlag(string status)
- {
- flagFile << getTime() << "\t" << status << endl;
- }
- void* southGenerator(void* args) {
- while(true)
- {
- do {
- car c;
- c.CarID = carCounter++;
- c.direction = 'S';
- c.arrival_time = getTime();
- c.start_time = "";
- c.end_time = "";
- southCars.push(c);
- sem_post(&countSouth);
- } while (rand() % 100 + 1 <= 80);
- pthread_sleep(20);
- }
- return 0;
- }
- void* northGenerator(void* args) {
- while(true)
- {
- do {
- car c;
- c.CarID = carCounter++;
- c.direction = 'N';
- c.arrival_time = getTime();
- c.start_time = "";
- c.end_time = "";
- northCars.push(c);
- sem_post(&countNorth);
- } while (rand() % 100 + 1 <= 80);
- pthread_sleep(20);
- }
- return 0;
- }
- void* southHandler(void* args) {
- bool locked = true;
- while(true) {
- //wait for a car going south and the mutex lock
- sem_wait(&countSouth);
- if(locked)
- pthread_mutex_lock(&road);
- locked = false;
- if (!flagPerson) {
- flagPerson = true;
- printToFlag("woken-up");
- }
- car c = southCars.front();
- c.start_time = getTime();
- pthread_sleep(1);
- southCars.pop();
- c.end_time = getTime();
- printToCar(c);
- if (northCars.size() >= 10 || southCars.empty()) {
- pthread_mutex_unlock(&road);
- locked = true;
- if (southCars.empty() && northCars.empty()) {
- flagPerson = false;
- printToFlag("sleep");
- }
- }
- }
- return 0;
- }
- void* northHandler(void* args) {
- bool locked = true;
- while(true) {
- //wait for a car going north and the mutex lock
- sem_wait(&countNorth);
- if(locked)
- pthread_mutex_lock(&road);
- locked = false;
- if (!flagPerson) {
- flagPerson = true;
- printToFlag("woken-up");
- }
- car c = northCars.front();
- c.start_time = getTime();
- pthread_sleep(1);
- northCars.pop();
- c.end_time = getTime();
- printToCar(c);
- if (northCars.size() >= 10 || northCars.empty()) {
- pthread_mutex_unlock(&road);
- locked = true;
- if (southCars.empty() && northCars.empty()) {
- flagPerson = false;
- printToFlag("sleep");
- }
- }
- }
- return 0;
- }
- int main() {
- srand (time(NULL));
- sem_init(&countSouth, 0, 0);
- sem_init(&countNorth, 0, 0);
- pthread_t south, north, sHandler, nHandler;
- carFile.open("car.log");
- flagFile.open("flagperson.log");
- carFile << "carID\tdirection\tarrival-time\tstart-time\tend-time" << endl;
- flagFile << "time\t\tstate" << endl;
- if ( -1 == pthread_create(&south, NULL, southGenerator, NULL) ) {
- perror("pthread_create");
- return -1;
- }
- if ( -1 == pthread_create(&north, NULL, northGenerator, NULL) ) {
- perror("pthread_create");
- return -1;
- }
- if ( -1 == pthread_create(&sHandler, NULL, southHandler, NULL) ) {
- perror("pthread_create");
- return -1;
- }
- if ( -1 == pthread_create(&nHandler, NULL, northHandler, NULL) ) {
- perror("pthread_create");
- return -1;
- }
- cout << "Press enter to stop the simulation.";
- cin.ignore();
- carFile.close();
- flagFile.close();
- sem_destroy(&countSouth);
- sem_destroy(&countNorth);
- pthread_mutex_destroy(&road);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement