Showing posts with label Process Management. Show all posts
Showing posts with label Process Management. Show all posts

Tuesday, April 11, 2017

C++ code for solving Producer-Consumer problem using sleep and wake up calls

This is the solution for Producer-Consumer problem using sleep and wake up calls
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <time.h>
#include <conio.h>
#define N 100
using namespace std;

int arr[N];
int arrLength = 0,counter = 0;

class doTrade
{
public:
    doTrade()
    {
        myFile.open("text.txt");
        conState = true;
        proState = true;
    }
    void consumer();
    void producer();
private:
    ofstream myFile;
    bool conState,proState;
};
void doTrade::producer()
{
    // Producer goes to sleep
     if(arrLength == N-1)
     {
         if(proState == true)
            myFile << "producer goes to sleep  [P S]" << endl;
         proState = false;
     }
     else
     {
          if(proState == true){
            myFile << "producer produces [P]" << endl;
            arr[arrLength] = 1;
            arrLength ++ ;
            counter++;
          }
        else
             myFile << "producer can't produce because it is asleep  [P C]" << endl;
     }
     //waking up consumer
     if(arrLength == 1)
     {
        if(conState == false){
            myFile << "producer wakes up [P W]" << endl;
            conState = true;
        }
     }
}
void doTrade::consumer()
{
    //consumer goes to sleep
    if(arrLength == 0)
    {
        if(conState == true){
        myFile << "COnsumer goes to sleep  [C S]"<<endl;
        conState = false;
        }
    }
    else
    {
        if(conState == true){
            myFile << "Consumer consumes [C ]" << endl;
            arr[arrLength] = 2;
            arrLength--;
            counter--;
        }
        else
            myFile << "consumer can't consume beacuse it is asleep.  [C C]" << endl;

    }
    //consumer cant wake up producer so once producer is asleep both will sleep forever
}
int main()
{
    srand(time(NULL));
    doTrade p1;
    double i = 0;
    int a;
    while(i <= 15000)
    {
        a = rand()%2;
        if(a)
            p1.consumer();
        else
            p1.producer();
        i++;
    }
    return 0;
}

C++ code for mutual exclusion(ME) with busy waiting using Lock variable

Solution for Mutual exclusion with busy waiting using locked variable in C++ Object Oriented Approach
Note:The output will be displayed in the same folder.
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <time.h>
using namespace std;
int lock = 0, check;
class process
{
public :
    process()
    {
        counter = 0;
        checkEntry = 0;
    }
    int print(){
        if(counter == 1)
        {
            lock = 0;
            counter = 0;
            return 1;
        }
        if(lock == 0){
            lock = 1;
            if(checkEntry!=1)
                check += 1;
            counter++;
            return 0;
        }


    }
private:
    int counter,checkEntry;

};

int main()
{
    srand(time(NULL));
    ofstream myfile;
    int i = 0,k = 0,a[5] = {0,1,2,3,4},arrLength = 5;
    myfile.open ("example.txt");
    int n;
    process p[5];
    while(check!=5)
    {
        n = rand()%5;

        if(p[n].print())
            myfile << "P" << n << " is in busy wait"<< endl;
        else
            myfile << "P" << n << " is in cr"<<endl;
        k++;

    }
}

C++ code for Round Robin scheduling

This is the solution for Round Robin scheduling algorithm using C++ Object Oriented Approach
#include <iostream>
#include <windows.h>
#include<conio.h>
using namespace std;

int time[20];
int quantum;
int timeLength;
int pt[20];
int ptLength;
int check;
class round
{
public :
    round()
    {
        lastTime = 0;
        state = true;
        first = true;

    }
    void askAC()
    {
        cout << "Enter Arrival : " ;
        cin >> arrivalTime ;
        cout <<"and Cpu Burst : ";
        cin >> cpuBurstTime;
        deburst = cpuBurstTime;
        WT = -arrivalTime;
    }


    int getArrival()
    {
        return arrivalTime;
    }


    int getCpu()
    {
        return cpuBurstTime;
    }

    void setLastTime(int n)
    {
        lastTime = n;
    }
    void setWt(int n,int a = 0 )
    {
        WT += n - lastTime - a;
    }

    int getWt()
    {
        return WT;
    }
    int deDeburst(int n)
    {
        int a = deburst;
        if(deburst>=quantum){
                deburst -= n;
        return quantum;
        }
        else if(deburst>0&&deburst<quantum)
        {
            deburst -= n;
            return a;
        }
        else
            return 0;
    }
    bool checkState()
    {
        return state;
    }

    void disableState()
    {
        state = false;
    }
    void enableCheck()
    {
        check += 1;
    }
    int getTat()
    {
        TAT = WT + cpuBurstTime;
        return TAT;
    }
private:
    int TAT;
    int WT;
    bool state;
    int arrivalTime;
    int cpuBurstTime;
    int lastTime;
    bool first;
    int deburst;



};

int main()
{
    int noProcess ;
    round p[100];
    cout << "Enter Quantum: ";
    cin >> quantum;
    cout << "Enter number of Process: ";
    cin >> noProcess;

    for(int i = 0; i < noProcess; i++)
    {
        p[i].askAC();
    }

    time[timeLength++] = 0;
    while(check!=noProcess)
    {
            for(int i = 0; i < noProcess ; i++)
            {

                if(p[i].checkState())
                {

                    if(p[i].getArrival()>time[timeLength-1] ){
                        time[timeLength] = p[i].getArrival();
                        timeLength++;
                        pt[ptLength++] = 333;
                    }

                    int a;

                    a = p[i].deDeburst(quantum);

                    if(a!=0){
                        p[i].setWt(time[timeLength-1]);

                        time[timeLength] = time[timeLength-1] + a;
                        timeLength++;

                        pt[ptLength++] = i+1;

                        p[i].setLastTime(time[timeLength-1]);

                    }
                    else
                    {
                        p[i].disableState();
                        p[i].enableCheck();


                    }


                }


            }


    }
    for(int i = 0; i < timeLength; i++)
    {
        cout <<  time[i] <<"\t" ;
    }
    cout << endl;
    for(int i = 0; i < ptLength; i++)
    {
        if(pt[i] == 333)
            cout << "  idle\t";
        else
        cout <<  "   p[" << pt[i] << "]\t";
    }
    cout << endl<< endl;

    for(int i =0 ;i<5 ;i++)
    {
        cout <<  "waiting time p[" << i+1 << "] :" << p[i].getWt() << endl;
        cout <<  "TAT p[" << i+1 << "] :" << p[i].getTat() << endl;
    }
    return 0;
}

C++ code for First Come First Serve scheduling

This is solution for FCFS scheduling algorithm using C++ Object Oriented Approach
#include <iostream>
#include <conio.h>
using namespace std;
int time[20];
int timeLength;
int pt[20];
int ptLength;
class FCFS
{
public:

    void askAC()
    {
        cout << "Enter Arrival : " ;
        cin >> arrivalTime ;
        cout <<"and Cpu Burst : ";
        cin >> cpuBurstTime;

    }


    int getArrival()
    {
        return arrivalTime;
    }


    int getCpu()
    {
        return cpuBurstTime;
    }


    void setWt(int n)
    {
        WT = n;
    }

    int getWt()
    {
        return WT;
    }
    void setTat()
    {
        TAT = WT+cpuBurstTime;
    }

    int getTat()
    {
        return TAT;
    }


private:
    int arrivalTime;
    int cpuBurstTime;
    int WT;
    int TAT;

};
int main()
{
    FCFS p[5];
    int a;
    for(int i =0;i<5;i++)
    {
        cout << "For p[" << i << "]" << endl;
        p[i].askAC();
    }
    time[timeLength++] = 0;

    for(int i = 0; i < 5; i++)
    {


        a = time[timeLength-1]-p[i].getArrival();

        if(a<0){
            time[timeLength] = -a + time[timeLength-1];
        timeLength++;
        pt[ptLength++] = 333;
            a = 0;
        }
        p[i].setWt(a) ;
        p[i].setTat( );
        if(time[timeLength-1] < p[i].getArrival() ){

            time[timeLength] = p[i].getArrival() + p[i].getCpu();
            timeLength++;
            pt[ptLength++] = i;

        }
        else{
            time[timeLength] = time[timeLength-1]+p[i].getCpu();
            timeLength++;
            pt[ptLength++] = i;

        }


    }
    cout << endl;
    for(int i = 0; i < timeLength; i++)
    {
        cout <<  time[i] <<"\t" ;
    }
    cout << endl;
    for(int i = 0; i < ptLength; i++)
    {
        if(pt[i] == 333)
            cout << "idle\t";
        else
        cout <<  "  p[" << pt[i] << "]\t";
    }

    cout <<endl<< ".";
    cout << endl<< endl;

    for(int i =0 ;i<5 ;i++)
    {
        cout <<  "waiting time p[" << i << "] :" << p[i].getWt() << endl;
        cout <<  "TAT p[" << i << "] :" << p[i].getTat() << endl;
    }
    return 0;
}