Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ECE 254 LAB 5
- Student 1: Simon Chun Man Chan (ID: 20467509)
- Student 2: Abdullah Hashmi (ID: 20485365)
- Date: November 6, 2014
- */
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <mqueue.h>
- #include <sys/stat.h>
- #include <time.h>
- #include "common.h"
- #include "point.h"
- #include <wait.h>
- #include <semaphore.h>
- int num_int;
- int producers;
- int consumers;
- int queue_size;
- sem_t accessible;
- sem_t blocked;
- pthread_mutex_t mutex;
- struct timeval tv;
- double t1;
- double t2;
- int *global_buffer;
- int production_count;
- int consumption_count;
- void *production(void *id){
- int counter = 0;
- int producer_id = (int)(intptr_t)id;
- for(counter = id; counter<queue_size; counter+=producers)
- {
- sem_wait(&blocked);
- pthread_mutex_lock(&mutex);//once getting access, lock the array
- global_buffer[production_count] = counter; //send data
- production_count = (production_count + 1) % queue_size; //increment so it is ready next time we send data.
- pthread_mutex_unlock(&mutex); //unlock for other processes
- sem_post(&accessible); //inform consumer that data is in array
- }
- }
- void *consumption(void *id){
- int counter = 0;
- int consumer_id = (int)(intptr_t)id;
- int consumed_integer;
- //LOOP: start at id, and send jumps of p to the buffer (for i%P effect)
- while(1){
- if (sem_trywait(&total) == -1){
- break;
- }
- sem_wait(&accessible);
- pthread_mutex_lock(&mutex);//once getting access, lock the array
- consumed_integer = global_buffer[consumption_count]; //send data
- consumption_count = (consumption_count + 1) % queue_size; //increment so it is ready next time we send data.
- if (sqrt(consumed_integer) % 1.0 == 0.0)
- {
- printf("%d %d %d\n", consumer_id, consumed_integer,(int)sqrt(consumed_integer));
- }
- pthread_mutex_unlock(&mutex); //unlock for other processes
- sem_post(&blocked); //inform consumer that data is in array
- }
- }
- int main(int argc, char* argv[]){
- //if exactly 5 arguments are passed
- if (argc == 5) {
- num_int = atoi(argv[1]);
- queue_size = atoi(argv[2]);
- producers = atoi(argv[3]);
- consumers = atoi(argv[4]);
- if ((num_int && queue_size && producers && consumers) != 0){ //if the two parameters are integers
- pthread_t P_buffer[producers];
- pthread_t C_buffer[consumers];
- sem_init (&accessible, 0, 0);
- sem_init (&blocked, 0, queue_size);
- sem_init (&total, 0, num_int);
- global buffer = (int *)malloc(sizeof(int)*queue_size);
- gettimeofday(&tv, NULL);
- t1 = tv.tv_sec + tv.tv_usec/1000000.0;
- int i, b, m b;
- for (i=0; i < producers; i++){
- pthread_create (&P_buffer[i],NULL,&production,(void *)(intptr_t)i);
- }
- for(b = 0; b < consumers; b++){
- pthread_create (&C_buffer[b], NULL,&consumption,(void *)(intptr_t)b);
- }
- for (m = 0; m < producers; k++){
- pthread_join(P_buffer[m],NULL);
- }
- for (n = 0; n < consumers; l++){
- pthread_join(C_buffer[n],NULL);
- }
- gettimeofday(&tv, NULL);
- t2 = tv.tv_sec + tv.tv_usec/1000000.0;
- printf("System Execution time: %f\n",t2-t1);
- return 0;
- }
- }
- else{ //if the incorrect number of parameters are passed, exit
- perror("Incorrect number of parameters. Only provide 4 integers.");
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement