C program for SJF CPU Scheduling Algorithm

Sunday, September 20th, 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

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

Author Name :
Ranjith

Total : 82 Comments


82 Responses to “C program for SJF CPU Scheduling Algorithm”

  1. skipliza says:

    Hello sir,
    I have seen the code but I dont have a clue how I can run or where do I save it?please tell me how to save and run this program..

  2. bharti says:

    can any one tell me plz wht is tis jpt[j] in the above program.i will b highly grateful!!!!!!!!!!!!!!!

  3. chaitanya says:

    thanks for u
    for giving
    this type of programms
    bt this are static arrival times of proceeossrs
    if some processor comes dynamically with some arrival time
    ?????

  4. preetam says:

    Can any1 help me wid c code for multilevel round roubin scheduling pls???

  5. Ivan says:

    Fucking syntax errors . don’t know how to fix it .

  6. murali krishna says:

    i want all cpu scheduling algorithms with programmes
    tell me any site
    but remeber one thing i want small programmes

  7. sri says:

    plz do try 2 post codes for preemptive scheduling also

  8. amey says:

    please upload pre-emptive algorithms pgm-fcfs,srtf,rr,priority

  9. Tamie Caffarelli says:

    Business, that’s easily defined – it’s other people’s money.
    Individuals will buy anything that is ‘one to a customer.’

  10. Ravi Kumar says:

    b) SJF:
    AIM: A program to simulate the SJF CPU scheduling algorithm
    PROGRAM:
    #include
    #include
    #include
    void main()
    {
    int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
    int totwt=0,totta=0;
    float awt,ata;
    char pn[10][10],t[10];
    clrscr();
    printf(“Enter the number of process:”);
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {
    printf("Enter process name, arrival time & execution time:");
    flushall();
    scanf("%s%d%d",pn[i],&at[i],&et[i]);
    }
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    {
    if(et[i]<et[j])
    {
    temp=at[i];
    at[i]=at[j];
    at[j]=temp;
    temp=et[i];
    et[i]=et[j];
    et[j]=temp;
    strcpy(t,pn[i]);
    strcpy(pn[i],pn[j]);
    strcpy(pn[j],t);
    }
    }
    for(i=0;i<n;i++)
    {
    if(i==0)
    st[i]=at[i];
    else
    6
    st[i]=ft[i-1];
    OS Lab Manual K. Ravi Chythanya
    wt[i]=st[i]-at[i];
    ft[i]=st[i]+et[i];
    ta[i]=ft[i]-at[i];
    totwt+=wt[i];
    totta+=ta[i];
    }
    awt=(float)totwt/n;
    ata=(float)totta/n;
    printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
    for(i=0;i<n;i++)
    printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
    printf("\nAverage waiting time is:%f",awt);
    printf("\nAverage turnaroundtime is:%f",ata);
    getch();
    }
    OUTPUT:
    Input:
    Enter the number of processes: 3
    Enter the Process Name, Arrival Time & Burst Time: 1 4 6
    Enter the Process Name, Arrival Time & Burst Time: 2 5 15
    Enter the Process Name, Arrival Time & Burst Time: 3 6 11
    Output:
    Pname arrivaltime executiontime waitingtime tatime
    1 4 6 0 6
    3 6 11 4 15
    2 5 15 16 31
    Average Waiting Time: 6.6667
    Average Turn Around Time: 17.3333

  11. msgd says:

    import java.awt.*;
    import java.applet.*;
    public class UserIn extends Applet
    {
    TextField text1, text2;
    public void init()
    {
    Label lob1=new Label();
    Label lob2=new Label();
    Label lob3=new Label();
    Label lob4=new Label();
    Label lob5=new Label();
    Label lob6=new Label();
    Label lob7=new Label();
    Label lob8=new Label();
    Label lob9=new Label();
    lob1.setText(“BE”);
    lob2.setText(“ES”);
    lob3.setText(“DMS”);
    lob4.setText(“DS”);
    lob5.setText(“PSQT”);
    lob6.setText(“FDL”);
    lob7.setText(“DS LAB”);
    lob8.setText(“BE LAB”);
    lob9.setText(“ECS LAB”);
    text1=new TextField(8);
    text2=new TextField(8);
    add(lob1);
    add (text1);
    add(lob2);
    add (text2);
    text1.setText (“0″);
    text2.setText (“0″);
    }
    public void paint(Graphics g)
    {
    int x=0, y=0, z=0;
    String s1=null, s2=null, s=null;
    g.drawString(“Input a number in each box”,10,45);
    try
    {
    s1=text1.getText();
    x= Integer.parseInt(s1);
    s2=text2.getText();
    y= Integer.parseInt(s2);
    }
    catch (Exception ex) { System.out.println(ex);}
    z= x + y;
    s=String.valueOf(z);
    g.drawString (“The Sum is: “+s,10,75);
    }
    public boolean action (Event event, Object object)
    {
    repaint ();
    return true;
    }
    }

  12. kj says:

    import java.io.*;
    import java.awt.*;
    import java.applet.*;

    public class Scorebar extends Applet
    {
    int sri[]={3,3,1,4,6,2,9,1,0,3,8,7,8,3,5,1,10,2,3,0,5,8,2,5,6,11,3,7,7,4,5,6,4,5,11,4,3,6,3,2,2,1,2,4,3,9,4,9,12,4};
    int ind[]={2,4,7,6,4,1,2,8,8,2,3,2,7,5,7,5,6,0,3,1,10,1,3,5,1,6,5,8,4,9,2,3,3,4,8,2,5,7,4,5,0,2,2,4,5,13,5,4,15,4};

    public void init()
    {

    }
    public void paint(Graphics g)
    {
    try
    {
    int j=-5;
    for(int i=0;i<50;i++)
    {
    g.setColor(Color.red);
    g.fillRect(j+5,200,5,sri[i]*10);
    g.setColor(Color.blue);
    j=j+5;
    g.fillRect(j+5,200,5,ind[i]*10);
    j=j+5;
    }
    }catch(Exception e){}
    }
    }

  13. Jyoti says:

    Thank u very much for ur programs. It helped me a lot in my OS subject….
    Thank u………..

  14. Code says:

    Very horrible coding design.

  15. muntasir says:

    very very thanx sir…………………….

  16. muntasir says:

    very very thanx sir…………………….
    nice program coding………………..

  17. vibhu says:

    initilise the array of et[10] to remove the error and the et with pt in no 37 line

  18. Surya Thakur says:

    Arrival time should also be considered in the mentioned code then the program will be versatile and useful

  19. Adolf says:

    All of you Indian Engineering kids.. you’re all noobs! If you can’t even run a spoonfed program, which company in hell will be ready to offer you a decent job? Ofcourse, the education system is to blame. But if an IT/Comp student can’t write such a simple piece of code, then may he die in hell or may God save his sorry arse!

  20. ankit says:

    thanks sir,,,,,,,,,bt dis [rogram not give the corruct output,,,,,,

  21. Sush says:

    @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….

  22. Ram Yewale says:

    Thank you………!

  23. arun says:

    thanks…..

  24. f says:

    thanxxxxxxxxxxxxx………..for os programs

  25. nilofar says:

    thanx

  26. yosiyajersson says:

    thank you sir…………………

  27. S.B says:

    Is this a pre-emptive sjf program?

  28. DHANANJAY says:

    #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;
    }

  29. Mohini says:

    Hello sir here et[ ] not declared in main….

  30. rajat says:

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

  31. atharva says:

    will u plz add the total turn around time and average turn around time ?

  32. shivani says:

    it’s very easy code to read & run this prog.

Leave a Reply

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

Free email signup

Email: