1

I'm trying to make a simulation of a waiting queue. The program includes the queue and the controller of the queue. It works perfectly for 1 queue at a single run time, but now I have to add multiple queues to it. I've made a new struct which includes the two structs mentioned above(queue struct and controller struct), then I allocate memory dynamically using malloc. The thing is that new customers should enter the queue that has the less customers in it each time. I have made a function called QueueCheck but it doesn't make the pointer I've assigned to it show to the queue with the less customers.

The queue functions are the basic functions to handle a queue. Please ignore the comments that are not in English, thank you. Here is the code:

int main(void)
{   Cont_Oura            *diaxeirish;                   /* struct array that includes a queue and it's Controller */
    float           arrival_possibility; /*pi8anothta afikshs*/
    unsigned int    simulation_time; /*synolikos xronos prosomoiwshs*/
    unsigned int    service_time;/*xronos e3yphrethshs enos pelath*/
    unsigned int minnum; /* used to randomly generate service time */
    unsigned int maxnum; /* used to randomly generate service time */
    unsigned int    time1;               /*roloi prosomoiwshs*/
    unsigned int    time_left;   /*time left for a customer to be serviced*/
    unsigned int    customer_sum;    /*customers that were serviced*/
    unsigned int    waiting_time;    /*synolikos xronos anamonhs*/
    unsigned int    left_cust; /* customers that were not serviced*/
    unsigned int max_cust; /*max number of customers entered the queue */
    TSOuras     customer;           /*The customer in the queue*/
    float           average;         /*average time of customers waiting in the queue*/
    float           randomArrival;
    int       controllerInactive=0;
    time_t          t;
    TController Controller;
    TOuras queue;
    int n, i;

    srand(time(&t));
    printf("Give number of queues that the simulation will have\n");
    scanf("%d", &n);
    diaxeirish = (Cont_Oura *)malloc(n * sizeof(Cont_Oura));
    if (diaxeirish == NULL){
      printf("Not enough space to allocate memory for diaxeirish");
      return -1;
    }

    printf("Give units of time for the simulation (0 <=), arrival possibility in unit of time (0,1), min and max number to calculate (randomly) the service time(same for every queue)\n");
    scanf("%u %f %u %u",&simulation_time,&arrival_possibility,&minnum, &maxnum);
    getchar();
    printf("The simulation will last %4u units of time.\n",simulation_time);
    printf("The arrival possibility for a customer in a unit of time is: %4.2f.\n",arrival_possibility);
    service_time = (rand() % (maxnum + 1 - minnum) + minnum);
    CustomerSetServiceTime(&customer, service_time);
    printf("The service time for every customer is %d units of time .\n",CustomerGetServiceTime(customer));
    for (i=0; i<=n-1; i++){
        QueueCreation(&(diaxeirish->queue)); /* initializes queue */
        ControllerCreation(&(diaxeirish->Controller)); /*initializes Controller */
    }
    QueueCreation(&queue);
    ControllerCreation(&Controller);
    time1 = 0;
    time_left = 0;
    waiting_time =0;
    i=0;
    while( time1 < simulation_time )
    {   /* Pelatis- Aytokinhto  */
        randomArrival = (float)rand()/(float)RAND_MAX;

        if ( randomArrival < arrival_possibility ){
            CustomerSetEntranceTime(&customer, time1);
            QueueCheck(diaxeirish, n, &queue);
            if (!QueueIns(&queue, customer)){
                  printf("Queue is small! The simulation will stop \n");
                  getchar();
                  return 0;
            };
        };

The Cont_Oura struct is this

typedef struct{ /*struct for controller and queue */
    TOuras queue;
    TController Controller; 
}

And the QueueCheck function is this one:

void QueueCheck(Cont_Oura *diaxeirish, int n, TOuras *queue){ /*the customers start entering from the queue number n, then n-1... */
    if (QueueGetSize((diaxeirish+n-1)->queue) == 0 || (QueueGetSize((diaxeirish+n-1)->queue) == QueueGetSize((diaxeirish)->queue))) /* if the last queue has 0 customer  or the first and last queue have the same number of customers */
        queue = &((diaxeirish+n-1)->queue); /* then add customer to the last queue */
    else if (QueueGetSize((diaxeirish+n-2)->queue) < QueueGetSize((diaxeirish+n-1)->queue))
        QueueCheck(diaxeirish, n-1, queue);
}
stefnto
  • 85
  • 3
  • 8
  • `queue = ...` in `QueueCheck` does not change the queue outside that function. Maybe you can return the queue from `QueueCheck` instead. – wimh Mar 23 '19 at 11:56
  • See also [How do I modify a pointer that has been passed into a function in C?](https://stackoverflow.com/q/766893/33499) – wimh Mar 23 '19 at 11:59
  • You mean changing the function to return TOuras (aka the struct type of queue) or a TOuras * ? – stefnto Mar 23 '19 at 12:17

0 Answers0