Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. /*
  2. ECE 254 LAB 5
  3. Student 1: Simon Chun Man Chan (ID: 20467509)
  4. Student 2: Abdullah Hashmi (ID: 20485365)
  5. Date: November 6, 2014
  6. */
  7.  
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <mqueue.h>
  12. #include <sys/stat.h>
  13. #include <time.h>
  14. #include "common.h"
  15. #include "point.h"
  16. #include <wait.h>
  17. #include <semaphore.h>
  18.  
  19. int num_int;
  20. int producers;
  21. int consumers;
  22. int queue_size;
  23. sem_t accessible;
  24. sem_t blocked;
  25. pthread_mutex_t mutex;
  26. struct timeval tv;
  27. double t1;
  28. double t2;
  29. int *global_buffer;
  30. int production_count;
  31. int consumption_count;
  32.  
  33.  
  34. void *production(void *id){
  35.  
  36. int counter = 0;
  37. int producer_id = (int)(intptr_t)id;
  38.  
  39.  
  40. for(counter = id; counter<queue_size; counter+=producers)
  41. {
  42.  
  43. sem_wait(&blocked);
  44. pthread_mutex_lock(&mutex);//once getting access, lock the array
  45. global_buffer[production_count] = counter; //send data
  46. production_count = (production_count + 1) % queue_size; //increment so it is ready next time we send data.
  47. pthread_mutex_unlock(&mutex); //unlock for other processes
  48. sem_post(&accessible); //inform consumer that data is in array
  49.  
  50. }
  51. }
  52.  
  53. void *consumption(void *id){
  54.  
  55. int counter = 0;
  56. int consumer_id = (int)(intptr_t)id;
  57. int consumed_integer;
  58.  
  59. //LOOP: start at id, and send jumps of p to the buffer (for i%P effect)
  60.  
  61. while(1){
  62. if (sem_trywait(&total) == -1){
  63. break;
  64. }
  65. sem_wait(&accessible);
  66. pthread_mutex_lock(&mutex);//once getting access, lock the array
  67. consumed_integer = global_buffer[consumption_count]; //send data
  68. consumption_count = (consumption_count + 1) % queue_size; //increment so it is ready next time we send data.
  69.  
  70. if (sqrt(consumed_integer) % 1.0 == 0.0)
  71. {
  72. printf("%d %d %d\n", consumer_id, consumed_integer,(int)sqrt(consumed_integer));
  73. }
  74.  
  75. pthread_mutex_unlock(&mutex); //unlock for other processes
  76. sem_post(&blocked); //inform consumer that data is in array
  77. }
  78. }
  79.  
  80.  
  81. int main(int argc, char* argv[]){
  82.  
  83. //if exactly 5 arguments are passed
  84. if (argc == 5) {
  85.  
  86. num_int = atoi(argv[1]);
  87. queue_size = atoi(argv[2]);
  88. producers = atoi(argv[3]);
  89. consumers = atoi(argv[4]);
  90.  
  91.  
  92. if ((num_int && queue_size && producers && consumers) != 0){ //if the two parameters are integers
  93.  
  94. pthread_t P_buffer[producers];
  95. pthread_t C_buffer[consumers];
  96.  
  97. sem_init (&accessible, 0, 0);
  98. sem_init (&blocked, 0, queue_size);
  99. sem_init (&total, 0, num_int);
  100.  
  101. global buffer = (int *)malloc(sizeof(int)*queue_size);
  102.  
  103. gettimeofday(&tv, NULL);
  104. t1 = tv.tv_sec + tv.tv_usec/1000000.0;
  105. int i, b, m b;
  106. for (i=0; i < producers; i++){
  107. pthread_create (&P_buffer[i],NULL,&production,(void *)(intptr_t)i);
  108. }
  109.  
  110. for(b = 0; b < consumers; b++){
  111. pthread_create (&C_buffer[b], NULL,&consumption,(void *)(intptr_t)b);
  112. }
  113.  
  114. for (m = 0; m < producers; k++){
  115. pthread_join(P_buffer[m],NULL);
  116. }
  117.  
  118. for (n = 0; n < consumers; l++){
  119. pthread_join(C_buffer[n],NULL);
  120. }
  121.  
  122. gettimeofday(&tv, NULL);
  123. t2 = tv.tv_sec + tv.tv_usec/1000000.0;
  124.  
  125. printf("System Execution time: %f\n",t2-t1);
  126. return 0;
  127. }
  128. }
  129.  
  130.  
  131. else{ //if the incorrect number of parameters are passed, exit
  132. perror("Incorrect number of parameters. Only provide 4 integers.");
  133. exit(1);
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement