본문 바로가기
C++/0x06-algorithm

Round Robin Scheduling Algorithm

by SpeeDr00t 2016. 7. 14.
반응형

Round Robin Scheduling Algorithm


1. 소스1

#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>

using namespace std;

struct process
{
    int no;
    int at,et,wt,tt;
    int tet;
    int t;
};

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}


int main( int argc , char ** argv )
{
    process p[99];
    int i,j,k;

    cout<<"\n Enter No of Processes:";
    int np;
    cin>>np;


    for (i=0;i<np;i++)
    {
        cout<<"\n Enter Execution time of process"<<i+1<<":";
        cin>>p[i].et;
        p[i].tet=p[i].et;
        p[i].at=p[i].t=p[i].tt=p[i].wt=0;
        p[i].no=i+1;
    }
    cout<<"\n Enter Time Quantum:";
    int q;
    cin>>q;

    cout<<"\n Entered Data";
    cout<<"\n Process\tET";

    for(i=0;i<np;i++)
    {
        cout<<"\n "<<p[i].no<<"\t"<<p[i].et;
    }

    int totaltime=0;
    for(i=0;i<np;i++)
    {
        totaltime+=p[i].et;
    }

    i=0;
    k=0;

    int rrg[99];
    for(j=0;j<totaltime;j++)
    {
        if((k==0)&&(p[i].et!=0))
        {

            p[i].wt=j;

            if((p[i].t!=0))
            {
                p[i].wt-=q*p[i].t;
            }
        }

        if((p[i].et!=0)&&(k!=q))
        {
            rrg[j]=p[i].no;
            p[i].et-=1;
            k++;
        }
        else
        {
            if((k==q)&&(p[i].et!=0))
            {
                p[i].t+=1;
            }
            i=i+1;

          if(i==np)
            {
                i=0;
            }

            k=0;
            j=j-1;
        }
    }



    int twt=0;
    int ttt=0;
    cout<<"\n Result Of Round Robin";
    cout<<"\n PNo\tET\tWT\tTT";
    for(i=0;i<np;i++)
    {
        p[i].tt=p[i].wt+p[i].tet;
        ttt+=p[i].tt;
        twt+=p[i].wt;
        cout<<"\n "<<p[i].no<<"\t"<<"\t"<<p[i].tet<<"\t"<<p[i].wt<<"\t"<<p[i].tt;
    }

    cout<<"\n Average Waiting Time:"<<(float)twt/np;
    cout<<"\n Average Turn Around Time:"<<(float)ttt/np;

   getch();


return 0;
}


결과

hacker@HACKER:~/cpp$ g++ -o r2 r2.cpp

hacker@HACKER:~/cpp$ ./r2

 Enter No of Processes:5

 Enter Execution time of process1:10

 Enter Execution time of process2:2

 Enter Execution time of process3:1

 Enter Execution time of process4:6

 Enter Execution time of process5:3

 Enter Time Quantum:10

 Entered Data
 Process        ET
 1      10
 2      2
 3      1
 4      6
 5      3
 Result Of Round Robin
 PNo    ET      WT      TT
 1              10      0       10
 2              2       10      12
 3              1       12      13
 4              6       13      19
 5              3       19      22
 Average Waiting Time:10.8


2. 소스2

#include <iostream>
	
using namespace std;
 
int main()
{
	int k,j,q,i,n,ts,temp;
     int aw;                      float awt;
     int bt[10],wt[10],te[10],rt[10],at[10];j=0; //te array stores the number of times a process comes to CPU before completion

	 //bt is my burst time store array
	 //wt is my waiting time array
	 //te keeps a count of the number of times a process enters the CPU before completion
	 //at is the arrival time

 
	cout<<"Enter number of processes"<<endl;
	 cin>>n;
 
	 
 
	 for(i=0;i<n;i++)
	 {
		 
	 cout<<"Enter burst time"<<endl; 
 
		 cin>>bt[i];
 
		 cout<<"Enter arrival times"<<endl;
		 cin>>at[i];
 
		 te[i] = 0; wt[i] = 0;
	 }
 
	 for(i=0;i<n;i++)
	 {
		 for(j = i+1;j<n;j++)
		 {
			 if(at[j]<at[i])
			 {
				 temp = at[i];
			     at[i] = at[j];
				 at[j] = temp;
             if(at[j] ==at[i])
				 temp = bt[i];
			     bt[i] = bt[j];
				 bt[j] = temp;
			 }
		 }
	 }
 

	 cout<<"Enter time slice"<<endl;
	 cin>>ts;
 
	 cout<<"process:"<<endl;
 
	 
	    for(i=0;i<n;i++)
	 {
		 cout<<"\t"<<i+1<<endl;
	 } 
 
	 
	 
	cout<<"Burst time:"<<endl;
	for(i=0;i<n;i++)
	{
		cout<<"  "<<bt[i]<<endl;
		rt[i] = bt[i];
	}
 
	cout<<"arrival time:"<<endl;
	for(i=0;i<n;i++)
	{
		cout<<"  "<<at[i]<<endl;
	}
 
	cout<<"Gaint chart"<<endl;
 
	while (j<=n)
	{
		
		j++;
 
		for(i = 0;i<n;i++)
		{
			if(rt[i] ==0) continue;
			if(rt[i]>=ts)
			{
				cout<<"\t"<<q<<i+1<<endl;
				q = q + ts;
				rt[i] = rt[i] - ts;
				te[i] = te[i] + 1;
			}
 
			else
			{
				cout<<"  "<<q<<i+1<<endl;
				wt[i] = q-te[i]*ts;
				q = q +rt[i];
				rt[i] = rt[i] - rt[i];
			}
		}
	}
 
	awt = 0;
 

	cout<<"Process Waiting Time"<<endl;
	for(i =0;i<n;i++)
	{
		wt[i] = wt[i] - at[i];
		cout<<"  "<<i+1<<endl;
			cout<<wt[i]<<endl;
		awt = awt + wt[i];
	}
	aw = awt;
	cout<<"Total waiting time"<<aw<<endl;
	cout<<"Average waiting time "<<awt/n<<endl;
	
	return 0;
 

}


결과
hacker@HACKER:~/cpp$ g++ -o r3 r3.cpp
hacker@HACKER:~/cpp$ ./r3
Enter number of processes
2
Enter burst time
10
Enter arrival times
2
Enter burst time
20
Enter arrival times
3
Enter time slice
10
process:
        1
        2
Burst time:
  10
  20
arrival time:
  2
  3
Gaint chart
        327671
        327772
Process Waiting Time
  1
-2
  2
-3
Total waiting time-5
Average waiting time -2.5


반응형

'C++ > 0x06-algorithm' 카테고리의 다른 글

pattern matching  (0) 2016.08.03
stack  (0) 2016.08.03