Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #include <math.h>
- #define MIN(a, b)(a < b ? a : b)
- #define TRUE 1
- #define FULL_QUEUE 1
- #define SUCCESS 0
- #define VECTOR_LEN 100
- #define EPS 10E-4
- //(0 + rand() % (10 - 0));
- typedef struct
- {
- double *p_in;
- double *p_out;
- double *low_border;
- double *high_border;
- int is_full;
- int is_working;
- } vector_descriptor_t;
- // REWORK: PLUSS ALL ELEMENTS, EXCEPT P_OUT; CHECK IF THEY IN MACHINE LATER
- void elements_wait(vector_descriptor_t vector_descriptor_a,
- vector_descriptor_t vector_descriptor_b, double time) // INCORRECT WORK
- {
- for(double *ptr = vector_descriptor_a.low_border; ptr <= vector_descriptor_a.high_border; ptr++)
- {
- if (ptr != vector_descriptor_a.p_out &&
- ptr !=vector_descriptor_b.p_out)
- (*ptr) += time;
- }
- /*
- if (vector_descriptor.is_full)
- for(double *ptr = vector_descriptor.low_border; ptr <= vector_descriptor.high_border; ptr++)
- *ptr += time;
- else
- {
- if (vector_descriptor.p_in < vector_descriptor.p_out)
- for(double *ptr = vector_descriptor.p_in; ptr != vector_descriptor.p_out; ptr++)
- {
- *ptr += time;
- if (ptr == vector_descriptor.high_border)
- ptr = vector_descriptor.low_border - 1;
- }
- else
- for(double *ptr = vector_descriptor.p_out; ptr != vector_descriptor.p_in; ptr++)
- {
- *ptr += time;
- if (ptr == vector_descriptor.high_border)
- ptr = vector_descriptor.low_border - 1;
- }
- }
- */
- return;
- }
- double* vector_pop(vector_descriptor_t *vector_descriptor)
- {
- double* out = vector_descriptor -> p_out;
- if (vector_descriptor -> p_out == vector_descriptor -> high_border)
- vector_descriptor -> p_out = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_out) += 1;
- if (vector_descriptor -> is_full)
- (vector_descriptor -> is_full) = 0;
- return out;
- }
- void vector_push(vector_descriptor_t *vector_descriptor, double* element)
- {
- if (!(vector_descriptor -> is_full))
- {
- (vector_descriptor -> p_in) = element;
- if ((vector_descriptor -> p_in) == (vector_descriptor -> high_border))
- vector_descriptor -> p_in = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_in)++;
- }
- if ((vector_descriptor -> p_in) == (vector_descriptor -> p_out))
- (vector_descriptor -> is_full) = 1;
- return;
- }
- long int queue_len(vector_descriptor_t vector_descriptor)
- {
- if (vector_descriptor.is_full)
- return (vector_descriptor.high_border - vector_descriptor.low_border) + 1;
- if (vector_descriptor.p_in == vector_descriptor.p_out)
- return 0;
- long int cnt = 0;
- for(double *ptr = vector_descriptor.p_out; ptr != vector_descriptor.p_in; ptr++)
- {
- cnt++;
- if (ptr == vector_descriptor.high_border)
- ptr = vector_descriptor.low_border - 1;
- }
- return cnt;
- }
- void vector_imitate_queue(vector_descriptor_t vector_descriptor_a, vector_descriptor_t vector_descriptor_b,
- double min_process_a, double max_process_a,
- double min_process_b, double max_process_b,
- double *time_a, double *time_b, double *time_b_waiting,
- size_t *cnt_a_work, double p)
- {
- int cnt_b_out = 0;
- double time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- //(*cnt_a_work)++;
- double time_b_work = 0;
- vector_descriptor_a.is_working = 1;
- while(cnt_b_out != 1000)
- {
- if (vector_descriptor_a.is_working) // A working
- {
- if (vector_descriptor_b.is_working) // Both apparats are working right now
- {
- double time_min = MIN(time_a_work, time_b_work);
- time_a_work -= time_min;
- time_b_work -= time_min;
- elements_wait(vector_descriptor_a, vector_descriptor_b, time_min);
- (*time_a) += time_min;
- (*time_b) += time_min;
- if (time_a_work < EPS)
- {
- vector_descriptor_a.is_working = 0;
- if(((rand() % 100) / 100.0) > p)
- vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
- else
- vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
- }
- if (time_b_work < EPS)
- {
- cnt_b_out++;
- vector_descriptor_b.is_working = 0;
- vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
- }
- if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
- {
- (*cnt_a_work)++;
- vector_descriptor_a.is_working = 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
- {
- vector_descriptor_b.is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else // B is NOT working right now
- {
- (*time_a) += time_a_work;
- (*time_b_waiting) += time_a_work;
- elements_wait(vector_descriptor_a, vector_descriptor_b, time_a_work);
- if (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full)
- (*vector_descriptor_b.p_out) += time_a_work;
- time_a_work = 0;
- if (time_a_work < EPS)
- {
- vector_descriptor_a.is_working = 0;
- if(((rand() % 100) / 100.0) > p)
- vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
- else
- vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
- }
- if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
- {
- (*cnt_a_work)++;
- vector_descriptor_a.is_working = 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
- {
- vector_descriptor_b.is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- }
- else // A don`t work
- {
- if (vector_descriptor_b.is_working) // ONLY B works right now
- {
- (*time_b) += time_b_work;
- elements_wait(vector_descriptor_a, vector_descriptor_b, time_b_work);
- if (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full)
- (*vector_descriptor_a.p_out) += time_b_work;
- time_b_work = 0;
- if (time_b_work < EPS)
- {
- cnt_b_out++;
- vector_descriptor_b.is_working = 0;
- vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
- }
- if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
- {
- (*cnt_a_work)++;
- vector_descriptor_a.is_working = 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
- {
- vector_descriptor_b.is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else // NOTHING is working at the moment
- {
- if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
- {
- (*cnt_a_work)++;
- vector_descriptor_a.is_working = 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
- {
- vector_descriptor_b.is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- }
- if (cnt_b_out == 1)
- {
- printf("Current length of queue A: %li\n", queue_len(vector_descriptor_a));
- printf("Current length of queue B: %li\n", queue_len(vector_descriptor_b));
- }
- if (!(cnt_b_out % 100) && cnt_b_out != 0)
- {
- printf("Current length of queue A: %li\n", queue_len(vector_descriptor_a));
- printf("Current length of queue B: %li\n", queue_len(vector_descriptor_b));
- }
- }
- return;
- }
- int main(void)
- {
- srand(time(NULL));
- //int choice;
- float min_process_a = 0;
- float max_process_a = 6;
- float min_process_b = 1;
- float max_process_b = 8;
- long int queue_size = 100;
- double p = 0.7;
- char s[16];
- s[0] = '\0';
- while (TRUE)
- {
- printf("\n");
- printf(" _ __ ___ ___ _ __ _ _ \n");
- printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
- printf("| | | | | | __/ | | | |_| |\n");
- printf("|_| |_| |_|\\___|_| |_|\\____|\n");
- printf("\n1: Imitate queue on vector");
- printf("\n2: Imitate queue on list");
- printf("\n0: EXIT\n");
- printf("Choice: ");
- fgets(s, 15, stdin);
- if (strcmp(s, "0\n") == 0)
- break;
- if (strcmp(s, "1\n") == 0)
- {
- double *waiting_time;
- waiting_time = calloc(queue_size, sizeof(double));
- vector_descriptor_t queue_a;
- queue_a.p_in = waiting_time;
- queue_a.p_out = waiting_time;
- queue_a.low_border = waiting_time;
- queue_a.high_border = &(waiting_time[queue_size - 1]);
- queue_a.is_full = 1;
- queue_a.is_working = 0;
- vector_descriptor_t queue_b;
- queue_b.p_in = waiting_time;
- queue_b.p_out = waiting_time;
- queue_b.low_border = waiting_time;
- queue_b.high_border = &(waiting_time[queue_size - 1]);
- queue_b.is_full = 0;
- queue_b.is_working = 0;
- double time_a = 0;
- double time_b = 0;
- double time_b_waiting = 0;
- size_t cnt_a_work = 0;
- vector_imitate_queue(queue_a, queue_b,
- min_process_a, max_process_a,
- min_process_b, max_process_b,
- &time_a, &time_b, &time_b_waiting, &cnt_a_work, p);
- double sum_time_wait = 0;
- for (int i = 0; i < queue_size; i++)
- sum_time_wait += waiting_time[i];
- printf("Apparat a worked for %lf (seconds)\n", time_a);
- printf("Apparat b worked for %lf (seconds)\n", time_b);
- printf("Apparat b waited for %lf (seconds)\n", time_b_waiting);
- printf("Apparat a worked %li times\n", (long int) cnt_a_work);
- printf("Average waiting time %lf", sum_time_wait / queue_size);
- free(waiting_time);
- waiting_time = NULL;
- }
- }
- return 0;
- }
- /*
- void vector_push(vector_descriptor_t *vector_descriptor)
- {
- if (vector_descriptor -> p_in == vector_descriptor -> high_border)
- vector_descriptor -> p_in = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_in)++;
- }
- int vector_pop(vector_descriptor_t *vector_descriptor)
- {
- if (vector_descriptor -> p_in == vector_descriptor -> p_out)
- return FULL_QUEUE;
- if (vector_descriptor -> p_out == vector_descriptor -> high_border)
- vector_descriptor -> p_out = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_out)++;
- return SUCCESS;
- }
- void vector_imitate_queue(vector_descriptor_t vector_descriptor_a,
- vector_descriptor_t vector_descriptor_b,
- float min_process_a, float max_process_a,
- float min_process_b, float max_process_b,
- double *time_a, double *time_b, double *time_b_waiting)
- {
- min_process_a =+ 0.001; // if min_proces == 0 we will have troubles
- int cnt_b_out = 0;
- double time_a_work = 0;
- double time_b_work = 0;
- time_a_work += (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- while (cnt_b_out != 100)
- {
- //printf("%lf ", (double) ((rand() % 100) / 100.0));
- if (fabs(time_a_work) > EPS)
- {
- if (fabs(time_b_work) > EPS)
- {
- double time_min = MIN(time_a_work, time_b_work);
- (*time_a) += time_min;
- (*time_b) += time_min;
- time_a_work -= time_min;
- time_b_work -= time_min;
- if (fabs(time_a_work) < EPS)
- {
- if(((rand() % 100)) > 70)
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_b);
- }
- else
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_a);
- }
- // if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
- // time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if (fabs(time_b_work) < EPS)
- {
- cnt_b_out++;
- if (!vector_pop(&vector_descriptor_b))
- vector_push(&vector_descriptor_a);
- }
- if (fabs(time_a_work) < EPS)
- if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- if (fabs(time_b_work) < EPS)
- if (vector_descriptor_b.p_in != vector_descriptor_b.p_out)
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- else
- {
- *time_b_waiting += time_a_work;
- *time_a += time_a_work;
- time_a_work = 0;
- if(((rand() % 100)) > 70)
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_b);
- }
- else
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_a);
- }
- if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- if (vector_descriptor_b.p_in != vector_descriptor_b.p_out)
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else
- {
- if (fabs(time_b_work) > EPS)
- {
- if (!vector_pop(&vector_descriptor_b))
- {
- cnt_b_out++;
- *time_b += time_b_work;
- time_b_work = 0;
- vector_push(&vector_descriptor_a);
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else
- {
- ;
- }
- }
- }
- return;
- }
- int main(void)
- {
- srand(time(NULL));
- //int choice;
- float min_process_a = 0;
- float max_process_a = 6;
- float min_process_b = 1;
- float max_process_b = 8;
- char s[16];
- s[0] = '\0';
- while (TRUE)
- {
- printf("\n");
- printf(" _ __ ___ ___ _ __ _ _ \n");
- printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
- printf("| | | | | | __/ | | | |_| |\n");
- printf("|_| |_| |_|\\___|_| |_|\\____|\n");
- printf("\n1: Imitate queue on vecotor");
- printf("\n2: Imitate queue on list");
- printf("\n0: EXIT\n");
- printf("Choice: ");
- fgets(s, 15, stdin);
- if (strcmp(s, "0\n") == 0)
- break;
- if (strcmp(s, "1\n") == 0)
- {
- double time_sum_a = 0;
- double time_sum_b = 0;
- double time_sum_diff = 0;
- for(int i = 0; i < 100; i++)
- {
- int queue_a [VECTOR_LEN];
- //random_vector(queue_a, VECTOR_LEN);
- // tbh this is not necessary, but why not?
- vector_descriptor_t vector_descriptor_a;
- vector_descriptor_a.p_out = queue_a;
- vector_descriptor_a.p_in = &(queue_a[99]);
- vector_descriptor_a.low_border = queue_a;
- vector_descriptor_a.high_border = &(queue_a[99]);
- int queue_b [VECTOR_LEN];
- vector_descriptor_t vector_descriptor_b;
- vector_descriptor_b.p_out = queue_b;
- vector_descriptor_b.p_in = queue_b;
- vector_descriptor_b.low_border = queue_b;
- vector_descriptor_b.high_border = &(queue_b[99]);
- double time_a = 0;
- double time_b = 0;
- double time_b_waiting = 0;
- vector_imitate_queue(vector_descriptor_a, vector_descriptor_b,
- min_process_a, max_process_a,
- min_process_b, max_process_b,
- &time_a, &time_b, &time_b_waiting);
- printf("time a = %lf\n", time_a);
- printf("time b = %lf\n", time_b);
- printf("\apparat b waiting time = %lf\n", time_b_waiting);
- time_sum_a += time_a;
- time_sum_b += time_b;
- time_sum_diff += time_b_waiting;
- }
- printf("time a average = %lf\n", time_sum_a / 100.0);
- printf("time b average = %lf\n", time_sum_b / 100.0);
- printf("\apparat b waiting time average = %lf\n", time_sum_diff / 100.0);
- }
- else if (strcmp(s, "2\n") == 0)
- {
- printf("LIST QUEUE");
- }
- else
- printf("\nWrong choice!");
- }
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement