Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6141071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:12:00+00:00 2026-05-23T18:12:00+00:00

I am trying to create a simulation of a stochastic queue system. The thing

  • 0

I am trying to create a simulation of a stochastic queue system. The thing is that when running the file, I get a segmentation fault as soon as it tries to call update(). I figured it’s a stack problem because the code in the function runs fine but since I am not very experienced with C I’d like your help too. Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <limits.h>

#define MAXCLIENTS 11
#define COUNTER_LIMIT 80000000
#define MEAN_DIFFERENCE 0.0000001

char servers_enabled;           
int state = 0;              
float p[MAXCLIENTS];            
float p_help_a[MAXCLIENTS];
float p_help_b[MAXCLIENTS];     
int arrivals = 0;               
int arrival_a[MAXCLIENTS];      
int arrival_b[MAXCLIENTS];
long int count = 0;             
float lambda[3]={0.5,1.0,1.5};  
float mi_a = 2,mi_b = 0.5;      
float gamma_a = 0,gamma_b = 0,gamma_ratio=0; 
float p_b_ab,p_a,p_b,p_a_ab;            
double E = 0,lastE = 0.1;                   

void arrive(char queue_enabled) {       
    ++arrivals;
    switch (queue_enabled) {
    case 'a' :
        ++arrival_a[state];
        break;
    case 'b' :
        ++arrival_b[state];
        break;
    }
    ++count;
    if (state<MAXCLIENTS)
        ++state;
}

void depart() {     
    ++count;
    --state;
}

float randomize() {
    float result;
    srand(time(NULL));
    result = (float) ((float)rand()/INT_MAX);
    return result;
}

void update(FILE *input,FILE *output, FILE *helper) {   
    int i;
    if (count%50000 == 0) {
        gamma_a=0;
        gamma_b=0;
        for (i=0;i<MAXCLIENTS;i++) {
            p_help_a[i] = (float) (arrival_a[i]/arrivals);
            p_help_b[i] = (float) (arrival_b[i]/arrivals);
            p[i] = p_help_a[i]+p_help_b[i];
            gamma_a += p[i]*mi_a;
            gamma_b += p[i]*(arrival_b[i]/arrivals);
            if (E != 0)
                lastE = E;
            E = 0;
            E += i*p[i];
        }
        gamma_a-=p_help_b[1]*mi_a-p_help_a[0];
        if (gamma_b != 0.0)
            gamma_ratio = gamma_a/gamma_b;
        fprintf(output,"throughput_a = %f   | throughput_b = %f     | throughput ratio = %8.6f",gamma_a,gamma_b,gamma_ratio);
        fprintf(output,"    | E = %f    | Pli8os Gegonotwn = %ld\n",E,count);
    }
    //count++;
    fprintf(input,"p_a[%2d] = %.10f p_b[%2d] = %.10f \n",i,p_help_a[i],i,p_help_b[i]);
    fprintf(input,"p = %f \n",p[i]);
    fprintf(helper,"gamma_a= %f    gamma_b= %f    gamma_ratio = %8.6f ",gamma_a,gamma_b,gamma_ratio);
    fprintf(helper,"    E= %f      count= %ld \n",E,(count-1));
}

int main() {

    int i,j,k;      
    FILE *input,*output,*helper;

    input=fopen("first.txt","w");
    output=fopen("second.txt","w");
    helper=fopen("third.txt","w");


    for (i=0;i<3;i++) { 
        fprintf(input,"Ruthmos Afiksewn (lambda) =   %1.1f\n\n",lambda[i]);
        fprintf(output,"Ruthmos Afiksewn (lambda) =   %1.1f\n\n",lambda[i]);
        fprintf(helper,"Ruthmos Afiksewn (lambda) =   %1.1f\n\n",lambda[i]);
        p_a=lambda[i]/(lambda[i]+mi_a);                      p_a_ab=lambda[i]/(lambda[i]+mi_a+mi_b); 
        p_b=lambda[i]/(lambda[i]+mi_b);     
        p_b_ab=(lambda[i]+mi_a)/(lambda[i]+mi_a+mi_b); // ????
        for (j=1;j<=10;j++) {       
            fprintf(input,"--------------------------\n\n");
            fprintf(input,"Katofli (threshold) =   %d\n\n",j);
            fprintf(input,"--------------------------\n\n");
            fprintf(output,"--------------------------\n\n");
            fprintf(output,"Katofli (threshold) =   %d\n\n",j);
            fprintf(output,"--------------------------\n\n");
            fprintf(helper,"--------------------------\n\n");
            fprintf(helper,"Katofli (threshold) =   %d\n\n",j);
            fprintf(helper,"--------------------------\n\n");

            for (k=0;k<(MAXCLIENTS-1);k++) {
                /******************/
            }
            servers_enabled = 'a';
            while(count<=COUNTER_LIMIT && fabs(E-lastE)>=MEAN_DIFFERENCE) {
                if (state == 0) {
                    arrive('a');
                    update(input,output,helper);
                } else
                switch (servers_enabled) {
                    case 'a' :
                        if ((state <= j) && randomize() <= p_a) {
                            arrive('a');
                            update(input,output,helper);
                        } else {
                            depart();
                            update(input,output,helper);
                        }
                        if (state > j)
                            servers_enabled = 'b';
                        break;
                    case 'b' :
                        if (state == 1) 
                            if (randomize() < p_b) {
                                arrive('b');
                                update(input,output,helper);
                            } else {
                                depart();
                                update(input,output,helper);
                            } else if (randomize() < p_a) {
                                arrive('b');
                                update(input,output,helper);
                            } else if (randomize() < p_b_ab) {
                                depart();
                                update(input,output,helper);
                            } else {
                                depart();
                                update(input,output,helper);
                                if (state <= j)
                                    servers_enabled = 'a';
                            }
                        break;
                }
            }
        }
    }
    fclose(input);
    fclose(output);
    fclose(helper);
    return 0;
}

Please don’t mind the rest of the errors. I am currently in the debugging stage, so any help would be greatly appreciated, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:12:01+00:00Added an answer on May 23, 2026 at 6:12 pm

    Your problem is in line:

    fprintf(input,"p_a[%2d] = %.10f p_b[%2d] = %.10f \n",i,p_help_a[i],i,p_help_b[i]);
    

    Here i is uninitialized and p_help_a[i] leads to access violation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Here's my situation: I'm trying to create a SSL certificate that will be installed
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
I am trying to create a simple app for the iPad that with play
I'm trying to create a simulation for our web Portal and need to add
I am trying to create an application on BlackBerry 9930 simulator that authenticate the
I am trying to create a JTable that has a row header that looks
I'm working on a simulation that uses multithreading extensively. The thing is that, until
I am trying to create some JUnit tests for a method that requires user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.