Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.9 kB
4
Indexable
Never
#include<bits/stdc++.h>
#define FastIO      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define pb          push_back
#define mp          make_pair
#define mem(x,i)    memset(x,i,sizeof(x))
#define ff          first
#define ss          second
#define all(x)      x.begin(),x.end()
#define fileout     freopen("output.txt","w",stdout)
#define filein      freopen("input.txt","r",stdin)
#define w(x)        cout << (#x) << " is " << x << "\n" ;
#define mod         1000000007
#define INF         1000000000
#define INF18       1e18
#define for1(i,n)   for( int i = 1 ; i<=n ; i++ )
#define for0(i,n)   for( int i = 0 ; i<n ; i++ )
typedef long long   ll;
const char nl = '\n';
 
using namespace std;


int main()
{
    FastIO ;

    int maxTime = 1000;
    int done = 0;

    // // example1
    // int n = 5;
	// int burst[] = {6,2,8,3,4};
	// int rem[] = {6,2,8,3,4};    
    // int arr[] = {0,0,0,0,0};
    // int wait[] = {0,0,0,0,0};

    //example2
    int n = 4;
    int burst[] = {6,8,7,3};
	int rem[] = {6,8,7,3};    
    int arr[] = {0,0,0,0};
    int wait[] = {0,0,0,0};


    for( int timer = 1 ; timer <= maxTime ; timer++ )
    {
		int pid = -1;
		int MinBurst = 100000;

        //finding the lowest remaining time process whose arrival time < current time
		for( int j = 0 ; j < n ; j++ )
    	{
    		if( arr[j] < timer )
    		{
    			if( burst[j] < MinBurst && rem[j] != 0 )
    			{
                    // cout<<MinBurst<<" Min "<<nl;
    				MinBurst = burst[j];
                    pid = j;
    			} 
    		}
    	}

        //Upadting the waiting time of unfinished process whose arrival time < current time
        for( int j = 0 ; j < n ; j++ )
        {
            if( j != pid )
            {
                if( rem[j] != 0 && arr[j] < timer )
                {
                    wait[j]++;
                }
            }
        }

        if( pid != -1 )
        {
            cout<<timer<<" P"<<pid+1<<nl;
            rem[pid]--;
            
            if( rem[pid] == 0 )
            {
                done++;
                if(done == n )
                {
                    break;
                }
            } 
        }
        else
        {
            cout<<timer<<" NO"<<nl;
        }
    }


    int avg_waiting = 0;
    int turn_time = 0;

    cout<<"Pro"<<" "<<"Burst"<<" "<<"Arr"<<" "<<"Wait"<<" "<<"Turn_T"<<nl;
    for( int i = 0 ; i < n ; i++ )
    {
        avg_waiting += wait[i];
        turn_time += burst[i]+wait[i];
    	cout<<i+1<<"    "<<burst[i]<<"    "<<arr[i]<<"    "<<wait[i]<<"    "<<burst[i]+wait[i]<<nl;
    } 


    cout<<"Average Waiting time : "<<(double)avg_waiting/n<<nl;
    cout<<"Average Turn Around time : "<<(double)turn_time/n<<nl;

    return 0;
}