Check error in Arrival time of SJF scheduling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pooja8389
    New Member
    • Sep 2010
    • 22

    Check error in Arrival time of SJF scheduling

    In this program there is error in processing time of last process..plz check the error...
    the program is as
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float avgwt,avgtt;
    char pname[10][10],c[10][10];
    int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0;
    clrscr();
    printf("\n\n Enter the number of processes:");
    scanf("%d",&n);
    printf("\n\n Enter the NAME,BURSTTIME and ARRIVALTIME of the processes");
    for(i=1;i<=n;i++)
    {
    printf("\n\n NAME :");
    scanf("%s",&pname[i]);
    printf("\n\nBURST TIME :");
    scanf("%d",&bt[i]);
    printf("\n\n ARRIVAL TIME :");
    scanf("%d",&at[i]);
    }
    for(i=1;i<n;i++)
    for(j=i+1;j<n;j++)
    {
    if(at[i]==at[j])
    if(bt[i]>bt[j])
    {
    t=at[i];
    at[i]=at[j];
    at[j]=t;
    q=bt[i];
    bt[i]=bt[j];
    bt[j]=q;
    strcpy(c[i],pname[i]);
    strcpy(pname[i],pname[j]);
    strcpy(pname[j],c[i]);
    }
    if(at[i]!=at[j])
    if(bt[i]>bt[j])
    {
    t=at[i];
    at[i]=at[j];
    at[j]=t;
    q=bt[i];
    bt[i]=bt[j];
    bt[j]=q;
    strcpy(c[i],pname[i]);
    strcpy(pname[i],pname[j]);
    strcpy(pname[j],c[i]);
    }
    }
    wt[1]=0;
    for(i=1;i<=n-1;i++)
    {
    wt[i+1]=wt[i]+bt[i];
    sum=sum+(wt[i]-at[i]);
    sbt=sbt+(wt[i+1]-at[i]);
    tt[i]=wt[i]+bt[i];
    ss=ss+bt[i];
    }
    printf("\n\n GANTT CHART");
    printf("\n\n ------------------------------------------------------------------\n");
    for(i=1;i<=n;i++)
    {
    printf("|\t%s\t",pname[i]);
    sbt=sbt+wt[i+1];
    tt[i]=wt[i]+bt[i];
    ss=ss+bt[i];
    }
    printf("\n\nGANTT CHART");
    printf("\n--------------------------------------------------------------------\n");
    for(i=1;i<=n;i++)
    {
    printf("|\t%s\t",pname[i]);
    }
    printf("\n--------------------------------------------------------------------\n");
    for(i=1;i<=n;i++)
    {
    printf("%d\t\t",wt[i]);
    }
    printf("%d\n",ss);
    printf("\n--------------------------------------------------------------------\n");
    printf("\n\n Total WAITING TIME of the process=%d",sum);
    printf("\n\nTotal TURNAROUND TIME of the process=%d",sbt);
    avgwt=(float)sum/n;
    avgtt=(float)sbt/n;
    printf("\n\nAverage WAITING TIME of the process=%f",avgwt);
    printf("\n\nAverage TURNAROUND TIME of the process=%f",avgtt);
    getch();
    }
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    I checked the code.
    Its compiling as well as running properly, without any error.
    What error are you taking about?
    Is it logical error or what?

    Comment

    • pooja8389
      New Member
      • Sep 2010
      • 22

      #3
      yes,there is logical error in this.....

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        check line number 21

        Code:
        for(i=1;i<n;i++)
        Don't you think it has to be:

        Code:
        for(i=1;i<=n;i++)
        Otherwise it will skip the last record.

        Comment

        • pooja8389
          New Member
          • Sep 2010
          • 22

          #5
          i change it.but the error remain same....
          Code:
           Enter the NAME,BURSTTIME and ARRIVALTIME of the processes
          
           NAME :a
          
          
          BURST TIME :3
          
          
           ARRIVAL TIME :1
          
          
           NAME :b
          
          
          BURST TIME :2
          
          
           ARRIVAL TIME :1
          
          
           NAME :c
          
          
          BURST TIME :5
          
          
           ARRIVAL TIME :1
          
          
           GANTT CHART
          
           ------------------------------------------------------------------
          |       b       |       a       |       c
          
          GANTT CHART
          --------------------------------------------------------------------
          |       b       |       a       |       c
          --------------------------------------------------------------------
          0               2               5               15
          
          --------------------------------------------------------------------
          
          
           Total WAITING TIME of the process=0
          
          Total TURNAROUND TIME of the process=12
          
          Average WAITING TIME of the process=0.000000
          
          Average TURNAROUND TIME of the process=4.000000
          this is the output of one example which has same arrival time....in this output see in grant chart the process completion time of process c should be 10 but it shows 15..

          Comment

          • ashitpro
            Recognized Expert Contributor
            • Aug 2007
            • 542

            #6
            Code:
            # GANTT CHART
            # --------------------------------------------------------------------
            # |       b       |       a       |       c
            # --------------------------------------------------------------------
            # 0               2               5               15
            #  
            # --------------------------------------------------------------------
            According to your code, these figures are waiting times.
            0,2,5 are the waiting times for respective processes. Like you are saying, you need 10 after 5, which makes no sense. Because, there is no fourth process in queue to display its waiting time.

            15 is getting printed because of line 80. You are printing 'ss' variable.

            You can just comment that line, and everything will start making sense.

            If you want some other thing to be printed, like burst time or anything else, tell me.

            Comment

            • pooja8389
              New Member
              • Sep 2010
              • 22

              #7
              thanku for this....it shows waiting time zero which is incorect and trunaround time is also incorrect.
              Trunaround time=time terminated-time entered
              TAT of process a=5-1=4
              TAt of process b=2-1=1
              TAT of process c=10-1=9
              then tatal trunaround time should be4+1+9=14
              and avrage TAT should be 14/4
              bt it show 12
              plz check that waiting time and trunaround time..

              Comment

              • ashitpro
                Recognized Expert Contributor
                • Aug 2007
                • 542

                #8
                Sure Sir/Mam,

                Give me few minutes.

                Comment

                • pooja8389
                  New Member
                  • Sep 2010
                  • 22

                  #9
                  the problem of trunaround time has solved now there was mistake in the for loop....
                  wt[1]=0;
                  for(i=1;i<=n-1;i++)
                  {
                  wt[i+1]=wt[i]+bt[i];
                  sum=sum+(wt[i]-at[i]);
                  sbt=sbt+(wt[i+1]-at[i]);
                  tt[i]=wt[i]+bt[i];
                  ss=ss+bt[i];
                  in this loop the i is start from 1 thats way it skip the first process time...
                  Code:
                  wt[1]=0; 
                  for(i=1;i<=n-1;i++)
                  {
                  j=i-1;
                  wt[i+1]=wt[i]+bt[i];
                  sum=sum+(wt[i]-at[j]);
                  sbt=sbt+(wt[i+1]-at[j]);
                  tt[i]=wt[i]+bt[i]; 
                  ss=ss+bt[i]; 
                  }
                  plz check it now plz plz
                  and plz solve the error of waiting time.

                  Comment

                  • ashitpro
                    Recognized Expert Contributor
                    • Aug 2007
                    • 542

                    #10
                    Code:
                    #include<stdio.h>
                    #include<conio.h>
                    void main()
                    {
                    float avgwt,avgtt;
                    char pname[10][10],c[10][10];
                    int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0;
                    clrscr();
                    printf("\n\n Enter the number of processes:");
                    scanf("%d",&n);
                    printf("\n\n Enter the NAME,BURSTTIME and ARRIVALTIME of the processes");
                    for(i=1;i<=n;i++)
                    {
                    printf("\n\n NAME :");
                    scanf("%s",&pname[i]);
                    printf("\n\nBURST TIME :");
                    scanf("%d",&bt[i]);
                    printf("\n\n ARRIVAL TIME :");
                    scanf("%d",&at[i]);
                    }
                    for(i=1;i<=n;i++)
                    for(j=i+1;j<n;j++)
                    {
                    if(at[i]==at[j])
                    if(bt[i]>bt[j])
                    {
                    t=at[i];
                    at[i]=at[j];
                    at[j]=t;
                    q=bt[i];
                    bt[i]=bt[j];
                    bt[j]=q;
                    strcpy(c[i],pname[i]);
                    strcpy(pname[i],pname[j]);
                    strcpy(pname[j],c[i]);
                    }
                    if(at[i]!=at[j])
                    if(bt[i]>bt[j])
                    {
                    t=at[i];
                    at[i]=at[j];
                    at[j]=t;
                    q=bt[i];
                    bt[i]=bt[j];
                    bt[j]=q;
                    strcpy(c[i],pname[i]);
                    strcpy(pname[i],pname[j]);
                    strcpy(pname[j],c[i]);
                    }
                    }
                    wt[1]=0;
                    for(i=1;i<=n-1;i++)
                    {
                    wt[i+1]=wt[i]+bt[i];
                    sum=sum+(wt[i]-at[i]);
                    sbt=sbt+(wt[i+1]-at[i]);
                    tt[i]=wt[i]+bt[i];
                    ss=ss+bt[i];
                    }
                    sum = 0;
                    sbt = 0;
                    for(i=1;i<=n;i++)
                    {
                      sum=sum + wt[i];
                      sbt = sbt + (wt[i] + bt[i] - at[i]);
                    }
                    
                    printf("\n\n GANTT CHART");
                    printf("\n\n ------------------------------------------------------------------\n");
                    for(i=1;i<=n;i++)
                    {
                    printf("|\t%s\t",pname[i]);
                    //sbt=sbt+wt[i+1];
                    tt[i]=wt[i]+bt[i];
                    ss=ss+bt[i];
                    }
                    printf("\n\nGANTT CHART");
                    printf("\n--------------------------------------------------------------------\n");
                    for(i=1;i<=n;i++)
                    {
                    printf("|\t%s\t",pname[i]);
                    }
                    printf("\n--------------------------------------------------------------------\n");
                    for(i=1;i<=n;i++)
                    {
                    printf("%d\t\t",wt[i]);
                    }
                    printf("%d\n",ss);
                    printf("\n--------------------------------------------------------------------\n");
                    printf("\n\n Total WAITING TIME of the process=%d",sum);
                    printf("\n\nTotal TURNAROUND TIME of the process=%d",sbt);
                    avgwt=(float)sum/n;
                    avgtt=(float)sbt/n;
                    printf("\n\nAverage WAITING TIME of the process=%f",avgwt);
                    printf("\n\nAverage TURNAROUND TIME of the process=%f",avgtt);
                    getch();
                    }
                    Added code from 60 to 66.
                    Commented line 73.

                    Let me know if that helps you.

                    Comment

                    • pooja8389
                      New Member
                      • Sep 2010
                      • 22

                      #11
                      thanku thanku so much......yes it is working correctly...
                      really thanku so much..

                      Comment

                      Working...