C Program for FCFS CPU scheduling algorithm

Saturday, September 19th, 2009

OUTPUT:
enter no of processes: 5

enter process1 name: aaa
enter process time: 4
enter process2 name: bbb
enter process time: 3
enter process3 name: ccc
enter process time: 2
enter process4 name: ddd
enter process time: 5
enter process5 name: eee
enter process time: 1

p_name  P_time  w_time
aaa    4    0
bbb    3    4
ccc    2    7
ddd    5    9
eee    1    14
total waiting time=34
avg waiting time=6.80

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
char p[10][5];
int tot=0,wt[10],i,n;
float avg=0;
clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:\n",i+1);
scanf("%s",&p[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}
 
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+et[i-1];
tot=tot+wt[i];
}
avg=(float)tot/n;
printf("p_name\t P_time\t w_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
getch();
}

OUTPUT:
enter no of processes: 5

enter process1 name: aaa
enter process time: 4
enter process2 name: bbb
enter process time: 3
enter process3 name: ccc
enter process time: 2
enter process4 name: ddd
enter process time: 5
enter process5 name: eee
enter process time: 1

p_name  P_time  w_time
aaa    4    0
bbb    3    4
ccc    2    7
ddd    5    9
eee    1    14
total waiting time=34
avg waiting time=6.80

Avatar Image

Author Name :
Ranjith

Total : 51 Comments


51 Responses to C Program for FCFS CPU scheduling algorithm

  1. vaibhav

    error in d prog
    u hav not defined et[i]

  2. sumit tripathi

    #include
    #include
    #include
    void main()
    {
    char p[10][5];
    int tot=0,wt[10],i,n;
    float avg=0;
    clrscr();
    printf(“enter no of processes:”);
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {
    printf("enter process%d name:\n",i+1);
    scanf("%s",&p[i]);
    printf("enter process time");
    scanf("%d",&pt[i]);
    }

    wt[0]=0;
    for(i=1;i<n;i++)
    {
    wt[i]=wt[i-1]+et[i-1];
    tot=tot+wt[i];
    }
    avg=(float)tot/n;
    printf("p_name\t P_time\t w_time\n");
    for(i=0;i<n;i++)
    printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
    getch();
    }

  3. Nandhini

    Thanx for this site

  4. PiterJankovich

    My name is Piter Jankovich. oOnly want to tell, that your blog is really cool
    And want to ask you: is this blog your hobby?
    P.S. Sorry for my bad english

  5. Subhani6945

    Very good,i

  6. Swathi S G

    program is nice……….
    but et[] should be replaced by pt[] in the line 23 and 29.

  7. mahya

    Hi friends :
    I chaneged this program a little,
    It’s correct :

    include

    void main()

    {

    char p[10][5];

    int tot=0,wt[10],pt[10],z,n,i;

    float avg=0;

    printf(“\nenter number of process : “);

    scanf(“%d”,&n);

    for(i=0;i<n;i++)

    {

    printf("\nenter process %d name : ",i+1);

    scanf("%s",&p[i]);

    printf("enter process time : ");

    scanf("%d",&pt[i]);

    }

    wt[0]=0;

    for (i=1;i<n;i++)

    {

    wt[i]=wt[i-1]+pt[i-1];

    tot=tot+wt[i];

    }

    avg=(float)tot/n;

    printf("\np-name\tp-time\tw-time \n");

    for (i=0;i<n;i++)

    printf("%s\t%d\t%d\n",p[i],pt[i],wt[i]);

    printf("\n total waiting time : %d \n avg wating time : %f \n\n ",tot ,avg);

    }

  8. Priyanka Aggarwal

    //A program to FCFS

    #include
    #include
    main()
    {
    int p[10],wt[10],bt[10],tat[10],ttat=0,twt=0,i,j,temp,n;
    float awt,atat;
    clrscr();
    printf(“enter the no. of process=>”);
    scanf(“%d”,&n);
    printf(“enter process=>”);
    for(i=0;i”);
    for(i=0;i<n;i++)
    {
    scanf("%d",&bt[i]);
    }
    wt[0]=0;
    for(i=1;i<n;i++)
    {
    wt[i]=wt[i-1]+bt[i-1];
    twt=twt+wt[i];
    }
    for(i=0;i<n;i++)
    {
    tat[i]=wt[i]+bt[i];
    ttat=ttat+tat[i];
    }
    awt=(float)twt/n;
    atat=(float)ttat/n;
    printf("p_number\t P_bt\t p_wt\t p_tat\n");
    for(i=0;i<n;i++)
    printf("%d \t\t %d\t %d\t %d\n",p[i],bt[i],wt[i],tat[i]);
    printf("total waiting time=%d\n avg waiting time=%f",twt,awt);
    printf("\ntotal turnedaroundtime=%d\n avg turnedaround time=%f",ttat,atat);
    getch();
    }

  9. sanjeet

    ET[i] should be replaced by pt[i]. and pt[i] should be declared……….

  10. shrikantsharma

    et[i]should be replaced y pt[i],pt[i] should be declared
    ………..progm is nice

  11. abhi sharma

    et[i]should be replaced y pt[i],pt[i] should be declared
    ………..progm is nice

  12. monk

    #include
    using namespace std;
    int main()
    {
    int k,p,b,wt=0,total;
    cout<>p;
    for(k=1;k<=p;k++)
    {
    cout<>b;
    total=wt+b;
    cout<<"\nprocess="<<k;
    cout<<"\nburst time="<<b;
    cout<<"\nwaiting time="<<wt;
    cout<<"\nturn around time="<<total;
    wt=wt+b;
    total=wt+b;
    }
    return 0;
    }

  13. abaay

    #include
    #include
    #include
    void main()
    {
    char p[10][5];
    int tot=0,wt[10],i,n;
    float avg=0;
    clrscr();
    printf(“enter no of processes:”);
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {
    printf("enter process%d name:\n",i+1);
    scanf("%s",&p[i]);
    printf("enter process time");
    scanf("%d",&pt[i]);
    }

    wt[0]=0;
    for(i=1;i<n;i++)
    {
    wt[i]=wt[i-1]+et[i-1];
    tot=tot+wt[i];
    }
    avg=(float)tot/n;
    printf("p_name\t P_time\t w_time\n");
    for(i=0;i<n;i++)
    printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
    getch();
    }

  14. ff

    it
    works

  15. s parusuram

    program is good some errors are there

  16. Ruel

    Where is “process.h”?

    whew!

  17. Vidhyasagar

    hi friends i want c program for B.E. 4th sem os lab please help

  18. Indhumathi

    hi friends i want to c program for B.tech 4th sem os lab program

  19. shikha

    pls help me getting the program of fcfs (non primitive)

  20. swaminathan

    Really Nice one…Thanks friends

  21. haritha

    this is very useful…
    THANK U!!!!!!!!

  22. Ranjit

    very good

  23. surendra

    #include
    #include
    #include
    void main()
    {
    char p[10][5];
    int avg_wt=0,avg_tt=0,at[10],bt[10],tt[10],i,n,sp[10],ep[10];
    sp[0]=ep[0]=tt[0]=0;
    clrscr();
    printf(“enter no of processes:”);
    scanf(“%d”,&n);
    for(i=1;i<n+1;i++)
    {
    printf("\nenter process %d name:\t",i);
    scanf("%s",&p[i]);
    printf("enter process Arrival time:\t");
    scanf("%d",&at[i]);
    printf("enter process Burst Time:\t");
    scanf("%d",&bt[i]);

    }
    printf("\nProcss Name\t\tWaiting Time\t\tTurnaround Time\n");
    for(i=1;i<n+1;i++)
    {
    sp[i]=ep[i-1];
    ep[i]=sp[i]+bt[i];
    tt[i]=(sp[i]-at[i])+bt[i];
    avg_wt+=sp[i]-at[i];
    avg_tt+=tt[i];
    printf("\n%s\t\t\t%d\t\t\t%d\n",p[i],(sp[i]-at[i]),tt[i]);
    }
    printf("\nAverage Waiting Time=\t\t%f\n\nAverage Turnaround Time=\t%f",(float)avg_wt/n,(float)avg_tt/n);
    getch();
    }

  24. sumesh patrawal

    #include
    #include
    void main()
    {
    int n,c[20],wt[20],tat[20],i;
    float avwt=0.0,avtat=0.0; clrscr();
    printf(“Enter the no of process”);
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)
    {
    printf("\n\nEnter the Cpu Brust process for %d process\t",i);
    scanf("%d",&c[i]);
    }
    wt[i]=0;
    for(i=2;i<=n;i++)
    {
    wt[i]=wt[i-1]+c[i-1];
    }
    for(i=1;i<=n;i++)
    {
    avtat=avtat+tat[i];
    }
    for(i=1;i<=n;i++)
    {
    avwt=avwt+wt[i];
    avtat=avtat+tat[i];
    }
    avwt=avwt/n;
    avtat=avtat/n;
    printf("\n\nThe process are\n\n");
    printf("process");
    printf("\tWaiting time");
    printf("\tTurn Around Time");
    for(i=1;i<=n;i++)
    {
    printf("\n\np[%d]",i);
    printf("\t%d",wt[i]);
    printf("\t\t%d",tat[i]);
    }
    printf("\n\n\nAvg total time %f",avtat);
    printf("\nAvg waiting time %f",avwt);
    getche();
    }

  25. christian

    please help me..i need a code. round robin and non pre-emptive with a arrival time..
    email me…thanks.
    christiangilaquino@yahoo.com

  26. christian

    please help me..i need a code. round robin and non pre-emptive including the arrival time..
    email me…thanks.
    christiangilaquino@yahoo.com

  27. Phani

    Can we do the same for multiple processors.

  28. Hariom verma

    its Good……………..

  29. garima

    i dun think there is ny need of 2-d array ! “p[10][5]“

  30. janakiraman

    hi frnds ple corect the folowing program
    #include
    {
    int p1,p2,p3,p4;
    int p[1]=2.p[2]=10,p[3]=14,p[4]=16;
    printf(“the cpu burset time is =”,p[1],p[2],p[3],p[4]);
    scanf(“%d”,&p[1]&,p[2]&,p[3]&,p[4]);
    for(i=1;i<=4;i++)
    {
    int x[0],p[0]=0;
    waiting timex[i]=x[i-1]+p[i-1];
    }
    printf("waiting time of cpu is=",&x1);
    printf("waiting time of cpu is=",&x2);
    printf("waiting time of cpu is=",&x3);
    printf("waiting time of cpu is=",&x4);
    average time is a=x[4]/4;
    printf("average time is=",&a);
    }

  31. vck

    i like dis…..

  32. vck

    i like this

  33. vck

    if u have any doubts contact me
    i will help u ……
    All are worst fellows……..

  34. venki

    niyab denga dani ke puteru ra meeru.

  35. chaitu vjk

    hey you stupid fellow ranjit
    don’t u no how to write the lab programs

  36. gv

    main()
    {
    int a,k,sum=0,i,n;
    clrscr();
    printf(“enter the dick size);
    scanf(“%d”,&a);
    n=a;
    while(n!=0)
    {
    k=n%10;
    sum=sum+k;
    n=n/10;
    }
    printf(“\nvachi na madda gudandi ra);
    getch();
    }

  37. gv

    this is the correct program

  38. don2

    my name is billa
    b for billa
    bizidi billa
    merupe saynyamla vastadilla

  39. jeni

    #include
    #include
    int n,bu[20],ch=0;
    float twt,awt,wt[20],tat[20];
    void main()
    {
    void getdata();
    void fcfs();
    do
    {
    switch(ch)
    {
    case 0:
    printf(“\n0.MENU”);
    printf(“\n1.Getting BurstTime”);
    printf(“\n2.FirstComeFirstServed”);
    printf(“\n3.EXIT”);
    break;
    case 1:
    getdata();
    break;
    case 2:
    printf(“FIRST COME FIRST SERVED SCHEDULING”);
    fcfs();
    break;
    case 3:
    exit(0);
    break;
    }
    printf(“\nEnter your choice:”);
    scanf(“%d”,&ch);
    getch();
    }while(ch<4);
    }
    void fcfs()
    {
    int i,b[10];
    float sum=0.0;
    twt=0.0;
    for(i=1;i<=n;i++)
    {
    b[i]=bu[i];
    printf("\nBurst time for process p %d =",i);
    printf("%d",b[i]);
    }
    wt[1]=0;
    for(i=2;i<=n;i++)
    {
    wt[i]=b[i-1]+wt[i-1];
    }
    for(i=1;i<=n;i++)
    {
    twt=twt+wt[i];
    tat[i]=b[i]+wt[i];
    sum+=tat[i];
    }
    awt=twt/n;
    sum=sum/n;
    printf("\nTotal Waiting Time= %f",twt);
    printf("\nAverage Waiting Time= %f",awt);
    printf("\nAverage Turnaround time=%f",sum);
    }
    void getdata()
    {
    int i;
    printf("Enter the no of processes:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    printf("\nEnter The BurstTime for Process p %d =",i);
    scanf("%d",&bu[i]);
    }
    }

  40. jeni

    #include
    #include
    int n,bu[20],ch=0;
    float twt,awt,wt[20],tat[20];
    void main()
    {
    void getdata();
    void fcfs();
    void sjf();
    do
    {
    switch(ch)
    {
    case 0:
    printf(“\n0.MENU”);
    printf(“\n1.Getting BurstTime”);
    printf(“\n2.ShortestJobFirst”);
    printf(“\n3.EXIT”);
    break;
    case 1:
    getdata();
    break;
    case 2:
    printf(“SHORTEST JOB FIRST SCHEDULING”);
    sjf();
    break;
    case 3:
    exit(0);
    break;
    }
    printf(“\nEnter your choice:”);
    scanf(“%d”,&ch);
    getch();
    }while(ch<4);
    }
    void sjf()
    {
    int i,j,temp,b[10];
    float sum=0.0;
    twt=0.0;
    for(i=1;i=1;i–)
    {
    for(j=2;jb[j])
    {
    temp=b[j-1];
    b[j-1]=b[j];
    b[j]=temp;
    }
    }
    }
    wt[1]=0;
    for(i=2;i<=n;i++)
    {
    wt[i]=b[i-1]+wt[i-1];
    }
    for(i=1;i<=n;i++)
    {
    twt=twt+wt[i];
    tat[i]=b[i]+wt[i];
    sum+=tat[i];
    }
    awt=twt/n;
    sum=sum/n;
    printf("\nTotal Waiting Time=%f",twt);
    printf("\nAverage Waiting Time=%f",awt);
    printf("\nAverage turnaround time=%f",sum);
    }
    void getdata()
    {
    int i;
    printf("Enter the no of processes:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    printf("\nEnter The BurstTime for Process p %d =",i);
    scanf("%d",&bu[i]);
    }
    }

  41. am

    very good

  42. lakshman

    tnx 4 ths site

  43. lakshman

    tnx 4 ths syt

  44. lakshman

    can i have the same program with strucures?

  45. Ivan

    those programs are not correct. pt[] should be declared .

  46. Mehvish

    frnz plz i need the fcfs program and algorithm.help me

  47. Mehvish

    Can u also help me in the following plz it an urgent assignment
    (1)Critical-section(program and algorithm)(small program)
    (2)Dining philosopher(program and algorithm)(small program)
    (3)Bakers (program and algorithm)(small program)

  48. panja

    re puku JENI, niku pani madda ledu ra lamdi kodaka,ma srinath gadi madda naku vachi……………
    thks fort dis site ………………..

  49. dn0myar

    Please help me ,,give me example of LCFS(last come first serve)

  50. nidhya

    #include
    #include
    void main()
    {
    int n,c[20],wt[20],tat[20],i;
    float avwt=0.0,avtat=0.0; clrscr();
    printf(“Enter the no of process”);
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)
    {
    printf("\n\nEnter the Cpu Brust process for %d process\t",i);
    scanf("%d",&c[i]);
    }
    wt[i]=0;
    for(i=2;i<=n;i++)
    {
    wt[i]=wt[i-1]+c[i-1];
    }
    for(i=1;i<=n;i++)
    {
    avtat=avtat+tat[i];
    }
    for(i=1;i<=n;i++)
    {
    avwt=avwt+wt[i];
    avtat=avtat+tat[i];
    }
    avwt=avwt/n;
    avtat=avtat/n;
    printf("\n\nThe process are\n\n");
    printf("process");
    printf("\tWaiting time");
    printf("\tTurn Around Time");
    for(i=1;i<=n;i++)
    {
    printf("\n\np[%d]",i);
    printf("\t%d",wt[i]);
    printf("\t\t%d",tat[i]);
    }
    printf("\n\n\nAvg total time %f",avtat);
    printf("\nAvg waiting time %f",avwt);
    getche();
    }

  51. nidhya

    hi friends i dont know large programs pls give some examples to my mail its a humble request

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup