C program for SJF CPU Scheduling Algorithm

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

eee 1 0
ccc 2 1
bbb 3 3
aaa 4 6
ddd 5 10
total waiting time=20
avg waiting time=4.00

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
char p[10][5],temp[5];
int tot=0,wt[10],pt[10],i,j,n,temp1;
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]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
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

eee 1 0
ccc 2 1
bbb 3 3
aaa 4 6
ddd 5 10
total waiting time=20
avg waiting time=4.00

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

48 thoughts on “C program for SJF CPU Scheduling Algorithm

  1. it is not working , it is not showing and taking the ‘time value’ for different process .pls sort out the problem and send the code to my id

  2. hi for all
    this is the code after correct it
    so thanks man you are great (Sorry for bag English)
    #include
    #include
    #include

    void main()
    {

    char p[10][5],temp[5];
    int tot=0,wt[10],pt[10],i,j,n,temp1;
    float avg=0;

    cout<>n;
    cout<<endl;

    for(i=0;i<n;i++)
    {
    cout<<"Enter Process Name "<>p[i];
    //cout<<endl;
    cout<<"Enter Barst Time time for Process "<<p[i]<>pt[i];
    cout<<endl;

    }

    for(i=0;i<n-1;i++)
    {
    for(j=i+1;jpt[j])
    {
    temp1=pt[i];
    pt[i]=pt[j];
    pt[j]=temp1;
    strcpy(temp,p[i]);
    strcpy(p[i],p[j]);
    strcpy(p[j],temp);
    }
    }
    }

    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("p_name\t P_time\t w_time\n");

    for(i=0;i<n;i++)
    printf("%s\t%d\t%d\n",p[i],pt[i],wt[i]);
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);

    }

  3. #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();
    }

  4. #include
    #include
    #include
    void main()
    {
    char p[10][5],temp[5];
    int tot=0,wt[10],pt[10],i,j,n,temp1;
    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]);
    }
    for(i=0;i<n-1;i++)
    {
    for(j=i+1;jpt[j])
    {
    temp1=pt[i];
    pt[i]=pt[j];
    pt[j]=temp1;
    strcpy(temp,p[i]);
    strcpy(p[i],p[j]);
    strcpy(p[j],temp);
    }
    }
    }
    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();
    }

  5. HI THIS IS JAGADHEESWAR.VERY VERY THANKS FOR PUBLISHING THE C PROGRAMS IN THIS WEBSITE. I HAVE JAVA LAB PROGRAMS FOR JNTU BTECH STUDENTS WHICH HAS BEEN EXECUTED. (34 PROGRAMS). IF YOU WANT THAT I WILL SEND IT AS A MAIL TO YOU. VERY THANKS FOR PUBLISHING OS SCHEDULING ALGORITHMS . THANKYOU.

  6. could you please send those programs to me. because i need some
    references on my os class..specially the shortest job first program.
    thanks alot. any program will be highly appreciated.

  7. // A program of sjf algorithm with no preemption

    #include
    #include
    main()
    {
    int p[10],wt[10],bt[10],tat[10],i,j,twt=0,ttat=0,temp,n;
    float awt,atat;
    clrscr();
    printf(“enter the no. of process=>\n”);
    scanf(“%d”,&n);
    printf(“enter process=>\n”);
    for(i=0;i”);
    for(i=0;i<n;i++)
    {
    scanf("%d",&bt[i]);
    }
    for(i=0;i<n;i++)
    {
    for(j=0;jbt[j+1])
    {
    temp=p[j];
    p[j]=p[j+1];
    p[j+1]=temp;

    temp=bt[j];
    bt[j]=bt[j+1];
    bt[j+1]=temp;
    }
    }
    }

    wt[0]=0;
    for(i=1;i<n;i++)
    {
    wt[i]=wt[i-1]+bt[i-1]; //waiting time
    twt=twt+wt[i]; //total waiting time
    }
    for(i=0;i<n;i++)
    {
    tat[i]=wt[i]+bt[i]; //turn around time
    ttat=ttat+tat[i]; // total turned around time
    }
    awt=(float)twt/n; //avg waiting time
    atat=(float)ttat/n; //avg turn around time
    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();
    }

  8. This is compiled program which is error free….
    #include
    #include
    #include
    void main()
    {
    char p[10][5];
    int tot=0,wt[10],pt[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]+pt[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],pt[i],wt[i]);
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
    getch();
    }

  9. #include
    #include
    #include
    void main()
    {
    char p[20][10];
    int tot=0,wt[10],i,n,pt[25];
    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]+pt[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],pt[i],wt[i]);
    printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
    getch();
    }

  10. plz help me in making programs using simple multilevel cpu scheduling
    and feedback multilevel cpu scheduling algorithms in c++ or c.
    i searched all net 4 this bt did not find any thing.

  11. #include
    #include
    #include
    void main()
    {
    char p[10][5],temp[5];
    int tot=0,wt[10],pt[10],i,j,n,temp1;
    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]);
    }
    for(i=0;i<n-1;i++)
    {
    for(j=i+1;jpt[j])
    {
    temp1=pt[i];
    pt[i]=pt[j];
    pt[j]=temp1;
    strcpy(temp,p[i]);
    strcpy(p[i],p[j]);
    strcpy(p[j],temp);
    }
    }
    }
    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();
    }

  12. @Adolf…..^^

    shut ur mouth…. u have no fucking rights to comment on Indian engineering students….. Dont you kw that Indian engg students are d 1 who serve max at through out the word….

  13. #include
    struct a
    {
    int pno,bt,at,st,wt,ft,tat;
    };
    typedef struct a a1;

    void acc(a1 z[],int n)
    {
    int i;
    for(i=0;i<n;i++)
    {
    z[i].pno=0;
    z[i].bt=0;
    z[i].at=0;
    z[i].st=0;
    z[i].wt=0;
    z[i].ft=0;
    z[i].tat=0;
    }

    for(i=0;i<n;i++)
    {
    printf("pno=\n");
    scanf("%d",&z[i].pno);
    printf("bt=\n");
    scanf("%d",&z[i].bt);
    printf("at=\n");
    scanf("%d",&z[i].at);

    }

    }
    void dis(a1 z[],int n)
    {
    int i;
    for(i=0;i<n;i++)
    {
    printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n",z[i].pno,z[i].bt,z[i].at,z[i].st,z[i].wt,z[i].ft,z[i].tat);
    }

    }
    void swap(a1 z1[])
    {
    int i,j,g[3];
    a1 z2;
    for(i=0;i<3;i++)
    {for(j=0;j<3;j++)
    {
    if(z1[i].at<z1[j].at)
    {
    z2=z1[i];
    z1[i]=z1[j];
    z1[j]=z2;
    }
    }
    }
    for(i=0;i0)
    {
    g[i]=g[i-1]+z1[i].bt;
    z1[i].st=g[i-1];
    }
    z1[i].ft=g[i];
    printf(“g=%d\t”,g[i]);
    z1[i].wt=z1[i].st-z1[i].at;
    z1[i].tat=z1[i].ft-z1[i].at;
    }

    }
    void swap1(a1 z1[],int n)
    {
    int i,j;
    a1 z2;
    for(i=0;i<n;i++)
    {for(j=0;j<n;j++)
    {
    if(z1[i].pno<z1[j].pno)
    {
    z2=z1[i];
    z1[i]=z1[j];
    z1[j]=z2;
    }
    }
    }
    }
    void cal(a1 z[],int n)
    {
    int i;
    int s=0;
    float aw,atat;
    for(i=0;i<n;i++)
    {
    s+=z[i].wt;
    }
    aw=(float)s/n;
    s=0;
    for(i=0;i<n;i++)
    {
    s+=z[i].tat;
    }
    atat=(float)s/n;
    printf("average wt=%f\n",aw);
    printf("average tat=%f\n",atat);

    }
    main()
    {
    a1 z1[10];
    int n;
    scanf("%d",&n);
    acc(z1,n);
    dis(z1,n);
    swap(z1);
    printf("\n");
    // dis(z1,n);
    swap1(z1,n);
    printf("\n");
    dis(z1,n);
    cal(z1,n);
    return 0;
    }

  14. sir please give me preepitive sjf scheduling program in simple coding in which arrival time is also included.

  15. Yeah…Where’s the “et” variable dude??? If u again post this program with no such errors then I guess it will be a little helpful.

  16. TAG CLOUD

    linked list microprocessor c graphics ASM program Data structure C/C++ download lab programs C/C++ Programms Java C Programs c program frequently asked interview questions top interview questions it interview questions questions for an interview interview questions and answers c interview question engineering questions Source Codes

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in