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 995023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:39:27+00:00 2026-05-16T06:39:27+00:00

I wrote this code and compiled it, #include <windows.h> #include <stdio.h> #include <stdlib.h> #include

  • 0

I wrote this code and compiled it,

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Data Structures
typedef struct process{
    char    jobName;
    int     arrivalTime;
    int     execTime;
    struct process *next;
} P;

typedef struct result{
    char Name;
    struct result *next;
} result;
// End of Data Structures

int quantum;
int jobCounts=0;
int ResponseTime=0;
double timeEsp=0;

// Function Prototypes
int gettimeofday(struct timeval *tp, void * tzp);
void add_to_process_list(P **List,P *data);
void add_to_result_list(result **List,char name);
P *readData(char* fileName);
void writeData(result *res,char *out);
P *removeFromList(P **head,P **El);
int jobCount(P *head);
double get_time(void);
// End of Function Prototypes

int main(int argc,char *argv[]){
    P *pList=NULL,*tmpNode=NULL;
    result *RR_result=NULL,*JR=NULL;
    double start, stop;
    int counter=0;

    printf("Reading Data ... ");
    pList=readData(argv[1]);
    printf("Complete\n");
    jobCounts=jobCount(pList);

    start = get_time();

    printf("Algorithm Started ...\n");
    if(pList!=NULL){
        tmpNode=pList;

        while(tmpNode!=NULL){
            if(tmpNode->arrivalTime<=counter){
                JR=(result *)malloc(sizeof(result *));
                printf("Giving Quantum to %c\n",tmpNode->jobName);
                Sleep(quantum*1000); //simulate process time
                add_to_result_list(&RR_result,tmpNode->jobName);
                tmpNode->execTime-=quantum;
                if(tmpNode->execTime==0){
                    ResponseTime+=((get_time()-start)-tmpNode->arrivalTime);
                    tmpNode=removeFromList(&pList,&tmpNode);
                }else
                    tmpNode=tmpNode->next;
                counter++;
            }else
                tmpNode=tmpNode->next;
        }

        printf("Algorithm Finished.\n");
        stop= get_time();
        timeEsp=(stop-start);
        printf("Writing data ... ");
        writeData(RR_result,argv[2]);
        printf("Completed.\n");
        return 0;
    }else{
        return 1;
    }

}

void add_to_process_list(P **List,P *data){
    P *new=malloc(sizeof(P));
    new->arrivalTime=data->arrivalTime;
    new->execTime=data->execTime;
    new->jobName=data->jobName;

    if((*List)==NULL){
        new->next=new;
        *List=new;
    }else{
        P *tmp;
        tmp=*List;
        while(tmp->next!=(*List)) tmp=tmp->next;
        tmp->next=new;
        new->next=*List;
    }
}

void add_to_result_list(result **List,char name){
    result *new=malloc(sizeof(result));
    new->Name=name;
    new->next=NULL;

    if(*List==NULL){
        *List=new;
    }else{
        result *tmp;
        tmp=*List;
        while(tmp->next!=NULL) tmp=tmp->next;
        tmp->next=new;
    }
}

P *readData(char* fileName){
    P *head=NULL,*cur=NULL;
    char Name[20],tmp[255];
    int AT,ET;
    FILE *iF;

    if((iF=fopen(fileName,"r"))>0){
        fgets(tmp,255,iF);
        sscanf(tmp,"Interval:%d\n",&quantum);
        fgets(tmp,255,iF); //waste

        while(!feof(iF) &&  fgets(tmp,255,iF)){
            sscanf(tmp,"%20c %20d %20d",Name,&AT,&ET);
            cur=(P *)malloc(sizeof(P *));
            cur->jobName=Name[0];
            cur->arrivalTime=AT;
            cur->execTime=ET;
            add_to_process_list(&head,cur);
        }

        fclose(iF);
        return head;
    }else{
        printf("Fatal error:\n\tError openning input file.\n");
        return NULL;
    }
}

void writeData(result * res ,char *out){
    result *tmp=res;
    FILE *oF;
    int tmpCounter=1;
    double throughput=jobCounts/timeEsp;
    double RR=ResponseTime/jobCounts;

    if((oF=fopen(out,"w"))>0){
        fprintf(oF,"%-15s %-15s\n","Job Name","Execution Time");
        while(tmp!=NULL){
            if(tmp->next!=NULL && tmp->Name==tmp->next->Name)
                tmpCounter++;
            else{
                fprintf(oF,"%-15c %-15d\n",tmp->Name,tmpCounter*quantum);
                tmpCounter=1;
            }
            tmp=tmp->next;
        }
        fprintf(oF,"Response Time: %0.2f\n",RR);
        fprintf(oF,"Throghput: %0.2f\n",throughput);

        fclose(oF);
    }else{
        printf("Fatal error:\n\tError openning output file.\n");
    }
}


P *removeFromList(P **head,P **El){
    P *tmp=*head;
    if((*El)->next==*El){
        free(*El);
        return NULL;
    }else{
        while(tmp->next!=NULL && tmp->next!=*El) tmp=tmp->next;
        tmp->next=tmp->next->next;
        free(*El);
        return tmp->next;
    }
}

int jobCount(P *head){
    P *tmp=head;
    int count=1;
    if(tmp->next==tmp){
        return 1;
    }

    while(tmp->next!=head) {
        tmp=tmp->next;
        count++;
    }
    return count;
}


double get_time(void){
    struct timeval stime;
    gettimeofday (&stime, (struct timezone*)0);
    return (stime.tv_sec+((double)stime.tv_usec)/1000000);
}

it worked correctly but when I chenged it to the code bellow (removing program arguments and use static file names) I can’t run program and to pauses on reading data ! (the strange thing is when I run second code with argument (I mean just pass argument in execution) it works!!

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Data Structures
typedef struct process{
    char    jobName;
    int     arrivalTime;
    int     execTime;
    struct process *next;
} P;

typedef struct result{
    char Name;
    struct result *next;
} result;
// End of Data Structures

int quantum;
int jobCounts=0;
int ResponseTime=0;
double timeEsp=0;

// Function Prototypes
int gettimeofday(struct timeval *tp, void * tzp);
void add_to_process_list(P **List,P *data);
void add_to_result_list(result **List,char name);
P *readData(char* fileName);
void writeData(result *res,char *out);
P *removeFromList(P **head,P **El);
int jobCount(P *head);
double get_time(void);
// End of Function Prototypes

int main(){
    P *pList=NULL,*tmpNode=NULL;
    result *RR_result=NULL,*JR=NULL;
    double start, stop;
    int counter=0;
    char *in="input.txt";
    char *out="output.txt";

    printf("Reading Data ... ");
    pList=readData(in);
    printf("Complete\n");
    jobCounts=jobCount(pList);

    start = get_time();

    printf("Algorithm Started ...\n");
    if(pList!=NULL){
        tmpNode=pList;

        while(tmpNode!=NULL){
            if(tmpNode->arrivalTime<=counter){
                JR=(result *)malloc(sizeof(result *));
                printf("Giving Quantum to %c\n",tmpNode->jobName);
                Sleep(quantum*1000); //simulate process time
                add_to_result_list(&RR_result,tmpNode->jobName);
                tmpNode->execTime-=quantum;
                if(tmpNode->execTime==0){
                    ResponseTime+=((get_time()-start)-tmpNode->arrivalTime);
                    tmpNode=removeFromList(&pList,&tmpNode);
                }else
                    tmpNode=tmpNode->next;
                counter++;
            }else
                tmpNode=tmpNode->next;
        }

        printf("Algorithm Finished.\n");
        stop= get_time();
        timeEsp=(stop-start);
        printf("Writing data ... ");
        writeData(RR_result,out);
        printf("Completed.\n");
        return 0;
    }else{
        return 1;
    }

}

void add_to_process_list(P **List,P *data){
    P *new=malloc(sizeof(P));
    new->arrivalTime=data->arrivalTime;
    new->execTime=data->execTime;
    new->jobName=data->jobName;

    if((*List)==NULL){
        new->next=new;
        *List=new;
    }else{
        P *tmp;
        tmp=*List;
        while(tmp->next!=(*List)) tmp=tmp->next;
        tmp->next=new;
        new->next=*List;
    }
}

void add_to_result_list(result **List,char name){
    result *new=malloc(sizeof(result));
    new->Name=name;
    new->next=NULL;

    if(*List==NULL){
        *List=new;
    }else{
        result *tmp;
        tmp=*List;
        while(tmp->next!=NULL) tmp=tmp->next;
        tmp->next=new;
    }
}

P *readData(char* fileName){
    P *head=NULL,*cur=NULL;
    char Name[20],tmp[255];
    int AT,ET;
    FILE *iF;

    if((iF=fopen(fileName,"r"))>0){
        fgets(tmp,255,iF);
        sscanf(tmp,"Interval:%d\n",&quantum);
        fgets(tmp,255,iF); //waste

        while(!feof(iF) &&  fgets(tmp,255,iF)){
            sscanf(tmp,"%20c %20d %20d",Name,&AT,&ET);
            cur=(P *)malloc(sizeof(P *));
            cur->jobName=Name[0];
            cur->arrivalTime=AT;
            cur->execTime=ET;
            add_to_process_list(&head,cur);
        }

        fclose(iF);
        return head;
    }else{
        printf("Fatal error:\n\tError openning input file.\n");
        return NULL;
    }
}

void writeData(result * res ,char *out){
    result *tmp=res;
    FILE *oF;
    int tmpCounter=1;
    double throughput=jobCounts/timeEsp;
    double RR=ResponseTime/jobCounts;

    if((oF=fopen(out,"w"))>0){
        fprintf(oF,"%-15s %-15s\n","Job Name","Execution Time");
        while(tmp!=NULL){
            if(tmp->next!=NULL && tmp->Name==tmp->next->Name)
                tmpCounter++;
            else{
                fprintf(oF,"%-15c %-15d\n",tmp->Name,tmpCounter*quantum);
                tmpCounter=1;
            }
            tmp=tmp->next;
        }
        fprintf(oF,"Response Time: %0.2f\n",RR);
        fprintf(oF,"Throghput: %0.2f\n",throughput);

        fclose(oF);
    }else{
        printf("Fatal error:\n\tError openning output file.\n");
    }
}


P *removeFromList(P **head,P **El){
    P *tmp=*head;
    if((*El)->next==*El){
        free(*El);
        return NULL;
    }else{
        while(tmp->next!=NULL && tmp->next!=*El) tmp=tmp->next;
        tmp->next=tmp->next->next;
        free(*El);
        return tmp->next;
    }
}

int jobCount(P *head){
    P *tmp=head;
    int count=1;
    if(tmp->next==tmp){
        return 1;
    }

    while(tmp->next!=head) {
        tmp=tmp->next;
        count++;
    }
    return count;
}


double get_time(void){
    struct timeval stime;
    gettimeofday (&stime, (struct timezone*)0);
    return (stime.tv_sec+((double)stime.tv_usec)/1000000);
}

What’s the problem? is it buffer overflow?!

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-16T06:39:27+00:00Added an answer on May 16, 2026 at 6:39 am

    There appears to be at least one overflow. The following allocation is only allocating 4 bytes (in a 32-bit system):

            cur=(P *)malloc(sizeof(P *));
    

    It should probably be:

            cur=(P *)malloc(sizeof(P));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code: #include stdafx.h #include stdio.h #include math.h #include stdlib.h #include time.h int main()
can any one help in this program? #include <stdio.h> #include <stdlib.h> #include <time.h> int
I wrote this simple C code and compiled it using Visual Studio 2010, with
I wrote this code that compiles on Solaris gcc it works fine too for
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I wrote this code for zoom in/out . it works but even with one
I wrote this code for zoom in / out and it suppesed to only
I wrote this code to create a custom annotation image - (MKAnnotationView *)mapView:(MKMapView *)mapView
I wrote this code in C# to encrypt a string with a key: private
I wrote this code. The constructor works normally, but in the destructor I get

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.