C program for Round Robin CPU Scheduling Algorithm

Sunday, September 20th, 2009

INPUT :
enter the process name : aaa
enter the processing time : 4
enter the process name : bbb
enter the processing time : 3
enter the process name : ccc
enter the processing time : 2
enter the process name : ddd
enter the processing time : 5
enter the process name : eee
enter the processing time : 1

OUTPUT :

p_name p_time w_time
aaa 4 9
bbb 3 3
ccc 2 6
ddd 5 10
eee 1 11

total waiting time : 39
average waiting time : 7.8000

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char p[10][5];
int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m;
float avgwt;
clrscr();
for(i=0;i<n;i++)
{
printf("enter the process name : ");
scanf("%s",&p[i]);
printf("enter the processing time : ");
scanf("%d",&pt[i]);
}
m=n;
wt[0]=0;
i=0;
do
{
if(pt[i]>timer)
{
rt=pt[i]-timer;
strcpy(p[n],p[i]);
pt[n]=rt;
et[i]=timer;
n++;
}
else
{
et[i]=pt[i];
}
i++;
wt[i]=wt[i-1]+et[i-1];
}while(i<n);
 
count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count++;
found=j;
}
}
	if(found!=0)
	{
 
	wt[i]=wt[found]-(count*timer);
	count=0;
	found=0;
	}
}
for(i=0;i<m;i++)
{
totwt+=wt[i];
}
avgwt=(float)totwt/m;
for(i=0;i<m;i++)
{
printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]);
}
printf("\ntotal waiting time %d\n",totwt);
printf("total avgtime %f",avgwt);
}

INPUT :
enter the process name : aaa
enter the processing time : 4
enter the process name : bbb
enter the processing time : 3
enter the process name : ccc
enter the processing time : 2
enter the process name : ddd
enter the processing time : 5
enter the process name : eee
enter the processing time : 1

OUTPUT :

p_name p_time w_time
aaa 4 9
bbb 3 3
ccc 2 6
ddd 5 10
eee 1 11

total waiting time : 39
average waiting time : 7.8000

Author Name :
Ranjith

Total : 49 Comments


49 Responses to “C program for Round Robin CPU Scheduling Algorithm”

  1. raju says:

    cpu scheduling

  2. Great post, my favorite app is Scheduly

  3. pooja says:

    hey that really helped me with my term work.I’m really thankful.

  4. harith says:

    the program has some unreachable control flow…
    but it will provide you the idea and lot of coding portion..
    if i am wrong send the solution only after running this code..
    thanx…(waiting)

  5. harith says:

    some portion is incorrect in this code…
    please help..

  6. Ashirbad says:

    The time slice concept hasnt been implemented. ie their is no input as time slice value.

  7. M.Qayyum says:

    @Ashirbad
    timer set to 3 which is used for time slicing…..

  8. santhoshkumar says:

    how to calculate slice time and as well as average waiting time..
    please help… and tell about how to run round robin program…

    thank you!!!

  9. Arun Pradhan says:

    THANKS SIR……………

  10. shriram kakade says:

    the program has some unreachable control flow
    but it will provide me the idea and lot of coding portion
    Thank you!!!

  11. don doper says:

    bhag bhurbhak,teri mayya ki gaand mein haathi ka lund

  12. monty says:

    aaaa sala! burbaak! wat shity program..hu eva wrote it is an asshole..it doesnt work for shit.

  13. don chinju says:

    yea seriously… wtf is dis… i cant understand a single word of this
    shit..

  14. don doper says:

    eh chingu!wats da program

  15. don chinju says:

    aahh.. i donn get dis shit…
    fck it man.. lets go for a smoke..
    wat abt d businesS????
    hws family eh don doper??

  16. queen latifa says:

    hahahahaaah…. i love dis shyt… haha.. reminds me of d shyt we had in
    lonavala

  17. SUNIL KUMAR SISTLA says:

    #include
    #include
    int main()
    {
    char p[5][5],q[5][5];
    int i,j,n,m,slice,tte=0,wt[5],et[5],pt[5],twt=0;
    float awt;

    printf(“\nENTER NO.OF PROCESSES AND TIME SLICE: “);
    scanf(“%d%d”,&n,&slice);
    for(i=0;i<n;i++)
    {
    printf("\nENTER %d PROCESS NAME AND TIME:",i+1);
    scanf("%s%d",&p[i],&pt[i]);
    }
    for(i=0;i0)
    {
    for(i=0;islice)
    {
    pt[i]-=slice;
    m=slice;
    printf(“%s “,&p[i]);
    }
    else
    {
    m=pt[i];
    pt[i]=0;
    printf(“%s “,&p[i]);
    }
    tte-=m;

    for(j=0;j<n;j++)
    if((pt[j]!=0)&&(i!=j))
    {
    wt[j]+=m;
    //printf("-%s%d%d",p[j],i,j);
    }
    }
    }
    }
    printf("\n\nPROCESS NAME \tPROCESS TIME \tWAIT TIME\n");
    for(i=0;i<n;i++)
    {
    twt+=wt[i];
    printf("\n%s\t\t %d \t\t%d",p[i],et[i],wt[i]);
    }
    printf("\n\nTOTAL WAIT TIME = %d\n",twt);
    system("PAUSE");
    }

    /*
    ********** OUTPUT **********
    ENTER NO.OF PROCESSES AND TIME SLICE: 3 6

    ENTER 1 PROCESS NAME AND TIME:a 30
    ENTER 2 PROCESS NAME AND TIME:b 45
    ENTER 3 PROCESS NAME AND TIME:c 28

    EXECUTION SEQUENCE:
    a b c a b c a b c a b c a b c b b b

    PROCESS NAME PROCESS TIME WAIT TIME
    a 30 48
    b 45 58
    c 28 60

    TOTAL WAIT TIME = 166
    Press any key to continue . . .
    */

  18. mafiaso says:

    heyy don doper.. hws business…
    whens d family meet??

  19. zubair says:

    not nice

  20. SHAHZAD (SH) says:

    #include
    #include
    void main()
    {
    clrscr();
    printf(“-***********ROUND ROBIN ALGORITHIM***************-”);
    int bp[10]={0};
    int n,total=0;
    long int wait[10]={0};
    int count=0;
    float avgwait=0;
    int q;
    printf(“\n NO OF PROCESSES= “);
    scanf(“%d”,&n);
    printf(“Quantum “);
    scanf(“%d”,&q);
    for(int k=0;k<n;k++)
    {
    printf("\nSet burst time for Process-%d ",k+1);
    scanf("%d",&bp[k]);
    }
    printf("\n\n ");
    for(k=0;k<n;k++){
    for(int a=0;a<=n-1;a++){
    if(bp[a]!=0){
    for(int i=0;i=q && bp[k]!=0){
    bp[k]=bp[k]-q;
    count=count+4;

    }
    else if(bp[k]=q && bp[a]!=0){
    bp[a]=bp[a]-q;
    count=count+4;

    }
    else if(bp[a]<q && bp[a]!=0){

    int num=bp[a];
    wait[a]=count;
    bp[a]=bp[a]-num;
    count=count+num;

    }
    }

    }
    total=total+wait[a];
    }

    }

    }

    avgwait=total/n;
    printf("\n AVERAGE Waiting Time is %f",avgwait);
    for(int a=0;a<n;a++){
    printf("\n\nWaiting time for Process-%d is %d ",a+1,wait[a]);
    }
    getch();
    }

  21. venky says:

    how to implement round robin in linux systems

  22. Vignesh says:

    thank u very much this helps me to understand easily

  23. zakir ughradar says:

    sir

    plz give our simple example

  24. Preethi says:

    hey this program is good…But i also want the code for the same alg with arrival time…pls

  25. owins says:

    it helps us

  26. Preeya says:

    what abt the contends in process.h???

  27. chaitanya vjk says:

    hey ranjith bro…the progam was awsome but they r some drawbacks i.e if
    we declare the no.. of processes as n, it is executing as more than n……..
    but understanding the program was simple n easy to understand it

  28. anvesh v says:

    hey

  29. ravi teja says:

    worst program seen in my life

  30. ravi teja cr says:

    WTF

  31. rose says:

    it was really good but can anybody help me with writing round robin with java?

  32. crazy boy says:

    hai rose! ikkada nenu vundaga nuvvu ranjith gadini adugutaventi
    i dont like this
    naku anni programs vachu neeku edi kavalante adi nerpista nuvvu nannu adugu naa daggaraku raa
    i can write programs
    i can read programs
    i can talk programs
    i can run programs
    i can play programs
    i can dick programs
    i can eat programs
    i can sleep programs
    i can load programs and download also

    inkokasari nuvvu vere vadini programs adigavo ninnu kadu vadini champesta

    crazy boy ko pakad ne keliye mushkil nayi….. naamun kiliye

  33. CoOl says:

    hey thanks for the post>>keep it up…:)

  34. Nandhu says:

    Sir can you gv code for avg turn around time and avg respönse time?

  35. mostafa says:

    Thanks a lot, i want simulate this algorithm with flash please help me

  36. elvis says:

    please help!where is the cpu time and can you giv code for around time and respounse time?

  37. unknown says:

    good noon sir, can i request a round robin cpu scheduling program in VB.NET?..thanks and godbless.

  38. farid says:

    it’s so great
    thank you

  39. Rupesh says:

    sir, can i have a program of multilevel feedback schedulling program in JAVA…..if yes then plz send….

  40. Rupesh says:

    the program has some unreachable control flow…
    but it will provide you the idea and lot of coding portion..
    if i am wrong send the solution only after running this code..
    thanx…(waiting)

  41. hmm bhatti says:

    tum bhosdika kuchg bhi dal de te ho!!!!!!!!!!!!!!
    tmhari ma ki choot
    tumhari ma ka bhosda
    behen k laude jindagi me tune pehli baar kch dala hai
    aur wo bhi galat
    ma k laude tere paida hone ka mujhe bahut afsos hai
    na jaane kis muth ki paidaish hai tu……….
    fuck you!!!!!!!!

  42. gajhni says:

    saalon thumari maa ka….
    sala 1 code likhne me thumari gaand phat jaati hai..???? jo yahan se copy kar rahe ho??

  43. Sonu Kumar says:

    #include
    #include
    void main()
    {
    clrscr();
    printf(“-***********ROUND ROBIN ALGORITHIM***************-”);
    int bp[10]={0};
    int n,total=0;
    long int wait[10]={0};
    int count=0;
    float avgwait=0;
    int q;
    printf(“\n NO OF PROCESSES= “);
    scanf(“%d”,&n);
    printf(“Quantum “);
    scanf(“%d”,&q);
    for(int k=0;k<n;k++)
    {
    printf("\nSet burst time for Process-%d ",k+1);
    scanf("%d",&bp[k]);
    }
    printf("\n\n ");
    for(k=0;k<n;k++){
    for(int a=0;a<=n-1;a++){
    if(bp[a]!=0){
    for(int i=0;i=q && bp[k]!=0){
    bp[k]=bp[k]-q;
    count=count+4;
    }
    else if(bp[k]=q && bp[a]!=0){
    bp[a]=bp[a]-q;
    count=count+4;
    }
    else if(bp[a]<q && bp[a]!=0){
    int num=bp[a];
    wait[a]=count;
    bp[a]=bp[a]-num;
    count=count+num;
    }
    }
    }
    total=total+wait[a];
    }
    }
    }
    avgwait=total/n;
    printf("\n AVERAGE Waiting Time is %f",avgwait);
    for(int a=0;a<n;a++){
    printf("\n\nWaiting time for Process-%d is %d ",a+1,wait[a]);
    }
    getch();
    }

  44. sekhar says:

    its n’t actual…..time slice should be allotted for each process…then we have to start the all the process…its n’t exact code……..

  45. Anto says:

    Check Out My Program……
    I have made it look good… ;)

    #include
    #include
    #include
    void main()
    {int a[10],b[3][10],c[11],t[10],z,i,j=1,n,q,m=0,max;
    float d,h;
    clrscr();

    cout< “;
    cin>>n;
    cout<<"\n";
    for(i=0;i<n;i++)
    {t[i]=0;
    c[i]=0;
    a[i]=i+1;
    cout<<"Enter Burst Time For Process – "<<i+1< “;
    cin>>b[0][i];
    b[1][i]=b[0][i];
    b[2][i]=b[0][i];
    }
    cout< “;
    cin>>q;
    for(i=0;ib[1][i+1] )
    {max=b[1][i];}
    else{max=b[1][i+1];}
    }
    m=max;
    cout<<"\nCalculating Waiting Time";
    cout<<".";
    for(i=0;i<n-1;i++)
    {sleep(1);
    cout<<".";}
    sleep(1);
    cout<<"\n";
    cout<<"_________________________________________________________________\n";
    cout<<"| Process No. | Burst Time | Waiting Time |TurnAround Time|\n";
    cout<<"|_______________|_______________|_______________|_______________|\n";
    int x=0;

    while(max!=0){

    for(i=0;i0){
    x=x+q;
    b[0][i]=b[0][i]-q;
    }

    if(b[0][i]==0)
    {t[i]=x;
    b[0][i]=-1000;
    }}max=max-q;}

    for(i=0;i<n;i++)
    {c[i]=t[i]-b[1][i];
    }

    for((i=0,d=0,h=0);i<n;i++)
    {cout<<"|\t"<<a[i]<<"\t|\t"<<b[1][i]<<"\t|\t"<<c[i]<<"\t|\t"<<t[i]<<"\t|\n";
    d=(d+c[i]);
    h=(h+t[i]);
    }
    cout<<"|_______________|_______________|_______________|_______________|\n";
    cout<<"\nAverage Waiting Time = "<<d/n<<"\n";
    cout<<"\nAverage Turn Around Time = "<<h/n<<"\n";
    getch();
    }

  46. Anto says:

    idk y the header files arnt appearing on this site…
    anyway use these header files-

    dos.h
    iostream.h
    conio.h

    Note that this is a C++ program…

  47. بهناز says:

    کد الگوریتم بهترین جا یا بست فیت رو در زبان c++می خوام خواهش می کنم کمکم کنید خیلی لازمش دارم اگه میشه توضیح هم بدید

  48. xyz says:

    thnqqq soo much really helpful

  49. god says:

    what is this et[] array stands for ?

Leave a Reply

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

Free email signup

Email: