Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include<stdbool.h>
  8. #include <math.h>
  9.  
  10. int genRandClass()
  11. {
  12. int num = rand() % 3 + 1;
  13. return num;
  14. }
  15.  
  16. int genRandEngine()
  17. {
  18. int num = rand() % 2;
  19. if (num == 0)
  20. {
  21. return 2;
  22. }
  23. else
  24. {
  25. return 4;
  26. }
  27. }
  28.  
  29. typedef struct starShip
  30. {
  31. double starShipClass,
  32. numOfEngines;
  33. pthread_mutex_t lock;
  34. }starShipInstance;
  35.  
  36. void init(starShipInstance* starShip)
  37. {
  38. starShip->starShipClass = 0;
  39. starShip->numOfEngines = 0;
  40. pthread_mutex_init(&starShip->lock, NULL);
  41. }
  42.  
  43. double globalShipCount = 0;
  44. double globalTimeSpent = 0;
  45. starShipInstance starShip;
  46.  
  47. void spawnShip(starShipInstance* currShip)
  48. {
  49. pthread_mutex_lock(&currShip->lock);
  50. currShip->starShipClass = (double)genRandClass();
  51. currShip->numOfEngines = (double)genRandEngine();
  52. //printf("%f\n", currShip->starShipClass);
  53. //printf("%f\n", currShip->numOfEngines);
  54. pthread_mutex_unlock(&currShip->lock);
  55. }
  56.  
  57. /*double timeRefuelling(starShipInstance* currShip)
  58. {
  59. pthread_mutex_lock(&currShip->lock);
  60. double timeSpent;
  61. timeSpent = currShip->starShipClass * currShip->numOfEngines;
  62. pthread_mutex_unlock(&currShip->lock);
  63. return timeSpent;
  64. }*/
  65.  
  66. int getClass(starShipInstance* starShip)
  67. {
  68. pthread_mutex_lock(&starShip->lock);
  69. int shipClass = starShip->starShipClass;
  70. pthread_mutex_unlock(&starShip->lock);
  71. return shipClass;
  72. }
  73.  
  74. int getEngine(starShipInstance* starShip)
  75. {
  76. pthread_mutex_lock(&starShip->lock);
  77. int shipEngine = starShip->numOfEngines;
  78. pthread_mutex_unlock(&starShip->lock);
  79. return shipEngine;
  80. }
  81.  
  82. void* fuelShip(void* vargp)
  83. {
  84. srand(time(NULL));
  85. for (int i = 0; i <= 10000; i++)
  86. {
  87. spawnShip(&starShip);
  88. globalShipCount++;
  89. globalTimeSpent += (getClass(&starShip) * getEngine(&starShip));
  90. //globalTimeSpent += timeRefuelling(&starShip);
  91. }
  92. return NULL;
  93. }
  94.  
  95.  
  96.  
  97. int main()
  98. {
  99. pthread_t threadId[4];
  100.  
  101. for (int i = 0; i < 4; i++)
  102. pthread_create(&threadId[i], NULL, fuelShip, (void*)&threadId);
  103.  
  104. for (int i = 0; i < 4; i++)
  105. pthread_join(threadId[i], NULL);
  106.  
  107. printf("\nTotal refueling time: %f centuries", globalTimeSpent);
  108. printf("\nTotal starship count: %f starships", globalShipCount);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement