Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include<stdio.h>
  2. #define HAVE_STRUCT_TIMESPEC
  3. #pragma comment(lib, "pthreadVC2.lib")
  4. #include <pthread.h>
  5. #include <semaphore.h>
  6. #include<stdlib.h>
  7. #include <time.h>
  8. #include <stdbool.h>
  9. #include <Windows.h>
  10.  
  11. #define vMax 50       // maximum visitor threads
  12. #define dMax 5        // maximum doctor threads
  13. #define roomMax 4     // maximum visitors in a room
  14. #define MAX 100
  15. #define pshared 0 // 0:threads   1+:process
  16. #define key 1 // 1+:unlocked   0:locked
  17.  
  18. #define BUFFER_SIZE 5 // 50 visitors + 3 doctors
  19. int buffer[BUFFER_SIZE];
  20. pthread_t visitorQueue[vMax];
  21. pthread_t doctorQueue[dMax];
  22. int visitorCursor, doctorCursor = 0;
  23. int visitorCount, doctorCount = 0;
  24. int peopleInside = 0;
  25. sem_t s;
  26. int i, number = 0;
  27. bool isDisplay = false; // Check if the display thread is created
  28.  
  29. // Critical section, a thread enters the room
  30. void* enterRoom() {
  31.     // enter room
  32.     // pause a random perid
  33.     // exit room
  34.     int wait = 42; // generateTime();
  35.     srand(time(0));
  36.     printf("Visitor %lu wishes to enter.\n", pthread_self());
  37.     printf("Visitor %lu entered the patient's room, waited for 42 ms.\n", GetThreadId());
  38.     Sleep(wait);
  39.     printf("Visitor %lu exited the patient's room, stayed for %d ms.\n", GetThreadId(), wait);
  40.     sem_post(&s);
  41.     exit(0);
  42. }
  43.  
  44.  
  45. //void* display() {
  46. //    printf("Display -> In the room: %d doctors [ %d ], and %d visitors [ %d, %d]\n");
  47. //    return NULL;
  48. //}
  49.  
  50. // Specifies the thread type
  51. void type(){
  52.     sem_wait(&s);
  53.     if (visitorCount < vMax) {
  54.         visitorCount++;
  55.         visitorQueue[visitorCursor] = pthread_self(); // Add the thread to the visitor queue;
  56.         visitorCursor = ++visitorCursor % vMax; // Increment circular cursor
  57.         enterRoom();
  58.     }
  59.     else if (doctorCount < dMax) { // < 4 or < 5 if there's a doctor in the room
  60.         doctorCount++;
  61.         doctorQueue[doctorCursor] = pthread_self(); // Add the thread to the visitor queue;
  62.         doctorCursor = ++doctorCursor % dMax; // Increment circular cursor
  63.     }
  64. }
  65.  
  66. int main() {
  67.     pthread_t TID[BUFFER_SIZE + 1]; // one for display
  68.     sem_init(&s, pshared, key);
  69.     //pthread_create(&TID[i], NULL, (void*)display, NULL); // create a display thread
  70.     for (i = 0; i < BUFFER_SIZE + 1; i++) {
  71.         // create thread
  72.         pthread_create(&TID[i], NULL, (void*)type, NULL);
  73.     }
  74.     for (i = 0; i < +1; i++) {
  75.         //printf("Contents of visitor queue: %lu\n", visitorQueue[i]);
  76.     }
  77.     for (i = 0; i < BUFFER_SIZE + 1; i++) {
  78.         // join thread (wait)
  79.         pthread_join(TID[i], NULL);
  80.     }
  81.     // destory thread
  82.     pthread_exit(0);
  83.  
  84.     // destroy semaphore
  85.     sem_destroy(&s);
  86.  
  87.     return 0;
  88. }
  89.  
  90. //
  91. //
  92. //int generateNumber(int max){
  93. //    int num = (rand() % (max) + 1);
  94. //    return num;
  95. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement