Tuesday, 29 November 2022

OS

 


---------------------------------------UNIX FILE SYSTEM-----------------------------------

    

   

#!/usr/bin/bash
while :
do
echo "*Welcome to Menu**"
echo "1. List files & directory"
echo "2. Create directory"
echo "3. Copy to others file"
echo "4. Delete a Directory"
echo "5. change a Directory"
echo "6. Create a File"
echo "7. Rename a file"
echo "8. DElete a file"
echo "9. Showing current directory"
echo "10. Display the file content"
echo -n "Enter ur choiche : " 
read ch
case $ch in 
1) ls
 ;;
2) echo "Enter the name of directory"
 read name 
 mkdir $name
 ;;
3) echo "Enetr the name of copy file"
 read cpy
 echo "Enetr the name of copy file"
 read cpy1
 cp $cpy $cpy1
 ;;
4) echo "Delete a directory"
 echo "Enter a name of directory"
 read ddir
 rm -r $ddir
 ;;
5) echo "CHange a Directory"
 echo "Enter a name of directory"
 read cdir
 cd $cdir
 ;;
6) echo "Create a file"
 echo "Enter a name of file:"
 read cfl
 touch $cfl
 ;;
7) echo "Delete a file"
 echo "Enter a name of file:"
 read dfl
 rm $dfl
 ;;
8) echo "REname a file"
 echo "Enter a name of file:"
 read rfl
 mv $rfl
 ;;
9) echo "showing current directoy a file"
 read sdr
 pwd $sfl
 ;;
10)echo "Display the file content"
 echo "Enter a name of file:"
 read dfc
 cat $dfc
 ;;
*)echo "Invalid choice"
esac
done




---------------------------------------Zero deleted file-----------------------------------



 #!/usr/bin/bash
filename="/home/waqar/student/abc.c"
if [ -e $filename ]
then
echo "File exists"
else
echo " File does not exists "
fi
if [ -s $filename ]
then
echo "File is not empty"
else
echo "File is empty"
rm $filename
echo "File Deleted"
fi



  ---------------------------------------sort----------------------------------

   
#!/usr/bin/bash
echo "Enter the file name"
read filename
cat -n $filename | sort -k2 | uniq -f1 | sort -k1 | cut -f2




---------------------------------------Fage replacement -----------------------------------




#include <stdio.h>

#include <iostream>

//#include <Algorithm>

#include <bits/stdc++.h>

using namespace std;

int findLRU(int time[], int n)

{

 int i, minimum = time[0], pos = 0;

for(i = 1; i < n; ++i)

{

 if(time[i] < minimum)

{

 minimum = time[i];

 pos = i;

}

}

return pos;

}

void fifo()

{

 cout<<"\n-------PAGE REPLACEMENT ALGORITHM [FIFO]-------

:\n\n";

 int n, a[50], frame[10], no, k, avail, count = 0;

cout<<"\n ENTER THE NUMBER OF PAGES :\n";

cin>>n;

cout<<"\n ENTER THE VALUE FOR PAGE NUMBER \n";

for (int i = 1; i <= n; i++)

{

cout<<"Page no a[" << i << "] ";

cin>>a[i];

}

cout<<"\nENTER THE NUMBER OF FRAMES\n";

cin>>no;

for (int i = 0; i < no; i++)

{

frame[i] = -1;

}

int j = 0;

cout << "\tRef Pg Number:\t Page frames:\n";

for (int i = 1; i <= n; i++)

{

cout << "\t\t" << a[i];

avail = 0;

for (k = 0; k < no; k++)

{

if (frame[k] == a[i])

avail = 1;

}

if (avail == 0)

{

frame[j] = a[i];

j = (j + 1) % no;

count++;

for (k = 0; k < no; k++)

{

cout << "\t" << frame[k];

}

}

cout << endl;

}

cout << "\nNUMBER OF PAGE FAULTS : " << count<<endl;

cout<<"----------------------------------------------------------------------

"<<endl;

}

void LRU()

{

cout<<"\n-------PAGE REPLACEMENT ALGORITHM [LRU]------- :\n\n";

int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, 

time[10], flag1, flag2, i, j, pos, faults = 0;

 cout<<"\nENTER THE NUMBER OF FRAMES: ";

 cin>>no_of_frames;

cout<<"\nENTER THE NUMBER OF PAGES: ";

cin>>no_of_pages;

cout<<"\ENTER THE REFERENCE STRING: ";

for(i = 0; i < no_of_pages; ++i)

{

cin>>pages[i];

}

for(i = 0; i < no_of_frames; ++i)

{

frames[i] = -1;

}

for(i = 0; i < no_of_pages; ++i)

 {

flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j)

{

if(frames[j] == pages[i])

 {

counter++;

time[j] = counter;

flag1 = flag2 = 1;

break;

}

 }

if(flag1 == 0)

{

for(j = 0; j < no_of_frames; ++j)

{

if(frames[j] == -1)

{

counter++;

faults++;

frames[j] = pages[i];

time[j] = counter;

flag2 = 1;

break;

 }

 } 

}

if(flag2 == 0)

{

pos = findLRU(time, no_of_frames);

counter++;

faults++;

frames[pos] = pages[i];

time[pos] = counter;

}

cout<<"\n";

for(j = 0; j < no_of_frames; ++j)

{

cout<<"\t"<<frames[j];

}

}

cout<<"\n\nTOTAL FAGE FAULTS: "<<faults<<endl;

 cout<<"----------------------------------------------------------------------

"<<endl;

}

void Optimal()

{

 cout<<"\n-------PAGE REPLACEMENT ALGORITHM [OPTIMAL]-------

:\n\n";

int l;

cout<<"\nENTER THE LENGTH OF REFERENCE STRING :";

cin>>l;

int 

arr[l],pf=0,ph=0,flag1=0,flag2=0,flag3=0,count1=0,count2=0,count3=0;

cout<<"\nENTER THE REFERENCE STRING:\n";

for(int i=0;i<l;i++)

{

cout<<"-> ";

cin>>arr[i];

 }

cout<<endl;

int a=-1,b=-1,c=-1,d=0,i=0,f1=0,f2=1,f3=1;

while(i<l)

{

d=arr[i];

if(a==-1)

{

a=d;

pf++;

cout<<a<<endl;

 }

else if(b==-1)

{

b=d;

pf++;

cout<<a<<"\t"<<b<<endl;

}

else if(c==-1)

{

c=d;

pf++;

cout<<a<<"\t"<<b<<"\t"<<c<<endl;

 } 

if(i>2)

{

if(d==a || d==b || d==c)

{

ph++;

cout<<endl;

 }

else

{

flag1=0,flag3=0,flag2=0;

count1=0,count2=0,count3=0;

 for(int k=i; k<l; k++)

 {

if(arr[k]==a && flag1==0)

{

flag1=1;

 } 

else if(arr[k]==b && flag2==0)

{

flag2=1;

}

else if(arr[k]==c && flag3==0)

{

flag3=1;

}

if(flag1==0)

{

count1++;

}

if(flag2==0)

{

count2++;

}

if(flag3==0)

{

count3++;

}

if(flag1==1 && flag2==1 && flag3==1)

{

break;

}

}

if(count1>count2 && count1>count3)

{

a=d;

pf++;

}

else if(count2>count1 && count2>count3)

{

b=d;

pf++;

}

else if(count3>count2 && count3>count1)

{

c=d;

pf++;

}

else{

if(flag1==0 ){

a=d;

}

else if(flag2==0){

b=d;

}

else{

c=d;

}

pf++;

}

cout<<a<<"\t"<<b<<"\t"<<c<<endl; 

}

}

i++;

}

cout<<"\nPAGE FAULTS:"<<pf;

cout<<"\nPAGE HITS:"<<ph<<endl;

};

int main()

{

cout << "\n\t*****----------PAGE REPLACEMENT 

ALGORITHM----------*****\n";

int ch;

//int exit=0;

while (1)

{

cout << "\n 1. FIFO \n 2. LRU \n 3. OPTIMAL \n 4. EXIT 

\n";

cout << "\n ENETR YOUR CHOICE: ";

cin >> ch;

switch (ch)

 {

case 1:

{

fifo();

break;

}

case 2:

{

LRU();

break;

}

case 3:

{

Optimal();

break;

 }

case 4:

{

exit(1);

break;

 }

 }

}

 }



 --------------------------------------✌

---------------------------------------FIFO------------------------------------------


FIFO

#include<iostream>


using namespace std;


int main()

{

    // cout<<"\nPage Replacement Algorithm (FIFO) :\n\n";

    cout<<"ENTER THE LENGTH OF REFRENCE STRING:"<<endl;

    int l;

    cin>>l;

    

    //cout<<"\nEnter the Length of the reference string :";

    //cin>>l;

    int pf=0,ph=0,flag1=0,flag2=0,flag3=0,count1=0,count2=0,count3=0;

    int arr[l];

   // cin>>arr[l];

    cout<<"\nEnter the Reference String:";

     for(int i=0;i<l;i++)

     {

         //cout<<"-> ";

         cin>>arr[i];

     }

    cout<<endl;

    int a=-1,b=-1,c=-1,d=0,i=0,f1=0,f2=1,f3=1;

    while(i<l)

    {

        d=arr[i];

        if(a==-1)

        {

            a=d;

            pf++;

            cout<<a<<endl;

        }

        else if(b==-1)

        {

            b=d;

            pf++;

            cout<<a<<"\t"<<b<<endl;

        }

        else if(c==-1)

        {

            c=d;

            pf++;

            cout<<a<<"\t"<<b<<"\t"<<c<<endl;

        }

        if(i>2)

        {

            if(d==a || d==b || d==c)

            {

                ph++;

                cout<<endl;

            }

            else

            {

                flag1=0,flag3=0,flag2=0;

                count1=0,count2=0,count3=0;

                for(int k=i; k<l; k++)

                {

                    if(arr[k]==a && flag1==0)

                    {

                        flag1=1;

                    }

                    else if(arr[k]==b && flag2==0)

                    {

                        flag2=1;

                    }

                    else if(arr[k]==c && flag3==0)

                    {

                        flag3=1;

                    }


                    if(flag1==0)

                    {

                        count1++;

                    }

                    if(flag2==0)

                    {

                        count2++;

                    }

                    if(flag3==0)

                    {

                        count3++;

                    }


                    if(flag1==1 && flag2==1 && flag3==1)

                    {

                        break;

                    }



                }

                if(count1>count2 && count1>count3)

                {

                    a=d;

                    pf++;

                }

                else if(count2>count1 && count2>count3)

                {

                    b=d;

                    pf++;

                }

                else if(count3>count2 && count3>count1)

                {

                    c=d;

                    pf++;

                }

                else{

                if(flag1==0 ){

                a=d;

}

else if(flag2==0){

b=d;

}

else{

c=d;

}

pf++;

}

               cout<<a<<"\t"<<b<<"\t"<<c<<endl; 

            }

        }


        i++;


    }

    cout<<"\nPage Faults :"<<pf;

    cout<<"\nPage Hits :"<<ph<<endl;


return 0;


}









-------------------------------------✌✌✌✌✌✌✌✌✌

---------------------------------------LRU -----------------------------------


#include<iostream>


using namespace std;


int main()

{

int n;

cout<<"\nenter the no of pages=";

cin>>n;

int arr[n];

int nq;

bool ter;

for(int i=0;i<n;i++)

{

cout<<"\nEnter page no=";

cin>>arr[i];

}

cout<<"\n enter the size of page=";

cin>>nq;

int que[nq];

int page_hit=0;

int page_fault=0;

int count=0;

int qp=0;

for(int i=0;i<n;i++)

{ter=false;

if(i<nq)

{

que[count]=arr[i];

count++;

page_fault++;

}

else

{

for(int j=0;j<nq;j++)

{

if(que[j]==arr[i])

{

page_hit++;

ter=true;

break;

}

}

if(ter==false)

{

int temp[nq]={0,0,0};

int count2=0;

while(count2<nq)

{

for(int l=i-1;l>=0;l--)

{

if(arr[l]==que[count2])

{

temp[count2]=l;

break;

}

}

count2++;

    }

int min,min_pos;

min=temp[0];

min_pos=0;

for(int k=1;k<nq;k++)

{

if(min>temp[k])

{

min=temp[k];

min_pos=k;

}

}

que[min_pos]=arr[i];

page_fault++;

}

}

cout<<"\n temp=";

for(int i=0;i<nq;i++)

{

cout<<que[i]<<" ";

}

}

cout<<"\n page hit="<<page_hit;

cout<<"\n page fault="<<page_fault;

cout<<"\n quee=";

for(int i=0;i<nq;i++)

{

cout<<que[i]<<" ";

}


}

// 7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1



-----------------------------------✌✌✌✌✌✌✌✌✌

--------------------------------------OPTIMAL -----------------------------------


#include<iostream>


using namespace std;


int main()

{

int n;

cout<<"\nenter the no of pages=";

cin>>n;

int arr[n];

int nq;

bool ter;

for(int i=0;i<n;i++)

{

cout<<"\nEnter page no=";

cin>>arr[i];

}

cout<<"\n enter the size of page=";

cin>>nq;

int que[nq];

int page_hit=0;

int page_fault=0;

int count=0;

int qp=0;

for(int i=0;i<n;i++)

{ter=false;

if(i<nq)

{

que[count]=arr[i];

count++;

page_fault++;

}

else

{

for(int j=0;j<nq;j++)

{

if(que[j]==arr[i])

{

page_hit++;

ter=true;

break;

}

}

if(ter==false)

{

int temp[nq]={0};

int count2=0;

while(count2<nq)

{

for(int l=i+1;l<n;l++)

{

if(arr[l]==que[count2])

{

temp[count2]=l;

break;

}

}

count2++;

    }

int max,max_pos;

max=temp[0];

max_pos=0;

for(int k=1;k<nq;k++)

{

if(temp[k]==0)

{

max=temp[k];

max_pos=k;

}

if(max<temp[k])

{

max=temp[k];

max_pos=k;

}

}

que[max_pos]=arr[i];

page_fault++;

}

}

cout<<"\n temp=";

for(int i=0;i<nq;i++)

{

cout<<que[i]<<" ";

}

}

cout<<"\n page hit="<<page_hit;

cout<<"\n page fault="<<page_fault;

cout<<"\n quee=";

for(int i=0;i<nq;i++)

{

cout<<que[i]<<" ";

}


}

// 7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1



---------------------------------------Bankers✌✌✌✌✌----------------------------------



#include<iostream>

using namespace std;


int alloc[10][10],Max[10][10],need[10][10],avail[1][10];

int N,R,finish[10];


void calculateNeed(){

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

for(int j=0;j<R;j++){

need[i][j]=0;

need[i][j]=Max[i][j]-alloc[i][j];

}

}

}


void sequence(){

int cp=0,cur=0;

cout<<"\n The safe sequence is : ";

while(cp<N){

if(finish[cur]==0){

bool flag=false;

for(int j=0;j<R;j++){

if(need[cur][j]>avail[0][j]){

flag=false;

break;

}

else

flag=true;

}

if(flag){

cout<<"\t P"<<cur;

cp++;

finish[cur]=1;

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

avail[0][i]+=alloc[cur][i];

}

}

}

cur=(cur+1)%N;

}

}


int main(){

cout<<"\n Enter the no. of process : ";

cin>>N;

cout<<"\n Enter the no. of Resources : ";

cin>>R;

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

cout<<"\n";

finish[i]=0;

for(int j=0;j<R;j++){

cout<<" Enter the no of instances of resource "<<j<<" alloated to P "<<i<<" : ";

cin>>alloc[i][j];

}

}


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

cout<<"\n";

for(int j=0;j<R;j++){

cout<<" Enter the max no of instances of resource "<<j<<" required to P "<<i<<" : ";

cin>>Max[i][j];

}

}


cout<<"\n";

for(int j=0;j<R;j++){

cout<<" Enter the available instances of resource "<<j<<" : ";

cin>>avail[0][j];

}


calculateNeed();

cout<<"\n Allocaation :-";

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

cout<<"\n";

for(int j=0;j<R;j++){

cout<<alloc[i][j]<<"\t";

}

}

cout<<"\n Max:- ";

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

cout<<"\n";

for(int j=0;j<R;j++){

cout<<Max[i][j]<<"\t";

}

}

cout<<"\n Need :- ";

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

cout<<"\n";

for(int j=0;j<R;j++){

cout<<need[i][j]<<"\t";

}

}

  sequence();

 

return 0;

}





--------------------------------------Barber ✌✌✌✌✌✌✌✌✌-----------------------------------


#include<stdio.h>

#include<stdlib.h>

#include<semaphore.h>

#include<pthread.h>


using namespace std;


sem_t empty;

pthread_mutex_t mutex;

int val;

void *none;


void * cutting(void *cno){

printf("\nCustomer %d is arrived.",*((int*)cno));

sem_getvalue(&empty,&val);

if(val==5){

sem_wait(&empty);

pthread_mutex_lock(&mutex);

printf("\n Barber is sleeping ...");

printf("\n Customer %d wake ups barber",*((int*)cno));

printf("\n Barber cuts hair of customer %d",*((int*)cno));

printf("\n Hair cutting of customer %d is completed",*((int*)cno));

pthread_mutex_unlock(&mutex);

sem_post(&empty);

}

else if(val>0){

sem_wait(&empty);

pthread_mutex_lock(&mutex);

printf("\n Customer %d is waiting ....",*((int*)cno));

printf("\n Barber cuts hair of customer %d",*((int*)cno));

printf("\nHair cutting of customer %d is completed",*((int*)cno));

pthread_mutex_unlock(&mutex);

sem_post(&empty);

}

else{

printf("\n no empty chairs are there so, Customer %d is leaving.",*((int*)cno));

}

return none;

}


int main(){

int a[8]={1,2,3,4,5,6,7,8};

pthread_t customer[8];

sem_init(&empty,0,5);

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

pthread_create(&customer[i],NULL,&cutting,(void *)&a[i]);

}

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

pthread_join(customer[i],NULL);

}

return 0;

}




✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌==-----PIPE-------------------------



#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

#include<sys/wait.h>


void processA(int writefd){

char buff[80];

int n=9;

printf("you are in the process A \n Enter the string : ");

scanf("%s",&buff);

write(writefd,buff,n);

}


void processB(int readfd){

char buff[80];

int n;

n=read(readfd,buff,80);

printf("you are in the process_B \n reading from a process_A : %s %d \n",buff,n);

printf("The reversed string is : \t");

for(int i=n;i>=0;i--){

printf("%c",buff[i]);

}

}



int main(){

int fd[2];

pid_t pid;

pipe(fd);

pid=fork();

if(pid>0){

processA(fd[1]);

}

else{

processB(fd[0]);

}

return 0;

}




✌✌✌✌✌✌✌✌✌ FCFS-------------------------



#include<iostream>

 

using namespace std;

 

int main()

{

    int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;

    cout<<"Enter total number of processes:";

    cin>>n;

 

    cout<<"\nEnter Process Burst Time\n";

    for(i=0;i<n;i++)

    {

        cout<<"P["<<i+1<<"]:";

        cin>>bt[i];

    }

 

    wt[0]=0;    //waiting time for first process is 0

 

    //calculating waiting time

    for(i=1;i<n;i++)

    {

        wt[i]=0;

        for(j=0;j<i;j++)

            wt[i]+=bt[j];

    }

 

    cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";

 

    //calculating turnaround time

    for(i=0;i<n;i++)

    {

        tat[i]=bt[i]+wt[i];

        avwt+=wt[i];

        avtat+=tat[i];

        cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];

    }

 

    avwt/=i;

    avtat/=i;

    cout<<"\n\nAverage Waiting Time:"<<avwt;

    cout<<"\nAverage Turnaround Time:"<<avtat;

 

    return 0;

}



------------------------------------------SJF-NON PREMP  ✌✌✌✌



#include<iostream>

using namespace std;

int main()
{
    int bt[10],pt[10],wt[10],wt1=0,ta[10],ta1;
    char arr4[100];
    int a,b,c;

    cout<<"Enter the No of Processes :";
    cin>>a;

    for(int i=0;i<a;i++)
    {
        cout<<"\nEnter the Name of the Process :";
        cin>>arr4[i];
        cout<<"Enter the Burst Time :";
        cin>>bt[i];
    }

    cout<<"\nName\tB.T\n";
    for(int i=0;i<a;i++)
    {
        cout<<endl<<arr4[i]<<"\t"<<bt[i]<<endl;
    }
    for(int i=1;i<a;i++)
    {
        int j=i-1;
        int d=bt[i];
        char e=arr4[i];
        while(j>=0 && bt[j]>d)
        {
            bt[j+1]=bt[j];
            arr4[j+1]=arr4[j];
            j--;
        }
          bt[j+1]=d;
            arr4[j+1]=e;
    }
     cout<<"\n\nName\tB.T\n";
    for(int i=0;i<a;i++)
    {
        cout<<endl<<arr4[i]<<"\t"<<bt[i];
    }
    int i=0;
    while(i<a)
    {
        if(i==0){
            wt[i]=0;
            ta[i]=bt[i];
        }
        else
        {
            wt[i]=ta[i-1];
            ta[i]=wt[i]+bt[i];
        }
        i++;
    }
     cout<<"\n\nName\tB.T\tW.T\tT.A";
    for(int i=0;i<a;i++)
    {
        cout<<endl<<arr4[i]<<"\t"<<bt[i]<<"\t"<<wt[i]<<"\t"<<ta[i];
    }

}

✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌ SJF premp-----------------------

#include<iostream>

using namespace std;
int main()
{
      int n,temp,tt=0,min,d,i,j;
      float atat=0,awt=0,stat=0,swt=0;

      cout<<"enter no of processes"<<endl;
      cin>>n;
      int a[n],b[n],e[n],tat[n],wt[n];
  
      for(i=0;i<n;i++)
      {
            cout<<"enter arrival time ";       //input
            cin>>a[i];
      }
      for(i=0;i<n;i++)
      {
            cout<<"enter burst time ";      //input
            cin>>b[i];
      }
      for(i=0;i<n;i++)
      {
         for(j=i+1;j<n;j++)
          {
                if(b[i]>b[j])
                {
                      temp=a[i];
                      a[i]=a[j];
                      a[j]=temp;

                      temp=b[i];
                      b[i]=b[j];
                      b[j]=temp;
                }
          }
      }
      min=a[0];
      for(i=0;i<n;i++)
      {
            if(min>a[i])
            {
                  min=a[i];
                  d=i;
            }
      }
      tt=min;
      e[d]=tt+b[d];
      tt=e[d];

      for(i=0;i<n;i++)
      {
            if(a[i]!=min)
            {
                  e[i]=b[i]+tt;
                  tt=e[i];
            }
      }
      for(i=0;i<n;i++)
      {

            tat[i]=e[i]-a[i];
            stat=stat+tat[i];
            wt[i]=tat[i]-b[i];
            swt=swt+wt[i];
      }
      atat=stat/n;
      awt=swt/n;
      cout<<"Process  Arrival-time(s)  Burst-time(s)  Waiting-time(s)  Turnaround-time(s)\n";

    for(i=0;i<n;i++)
    {
    cout<<"P"<<i+1<<"              "<<a[i]<<"                "<<b[i]<<"                  "<<wt[i]<<"               "<<tat[i]<<endl;
    }

    cout<<"awt="<<awt<<" atat="<<atat;  //average waiting time and turn around time




}









✌✌✌✌✌✌✌✌✌✌✌👉 RounRobin-------------------------------




#include<iostream>
using namespace std;

void findWaitingTime( int n,int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];

int t = 0; 

while (1)
{
bool done = true;

for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false; 
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}

else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}

if (done == true)
break;
}
}

void findTurnAroundTime( int n,int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

void findavgTime( int n, int bt[], int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime( n, bt, wt, quantum);

findTurnAroundTime( n, bt, wt, tat);

// Display processes along with all details
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";

// Calculate total waiting time and total turn
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}

cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Driver code
int main()
{
// process id's
int n;
cout<<"Enter the number of processes : \n";
cin>>n;
int bt[n];
cout<<"Enter Burst Time of each process  \n";
for(int i=0;i<n;i++){
cout<<"Enter burst time for Process P "<<i<<" :";
cin>>bt[i];
}
int quantum;
cout<<"\nEnter Quantum time for Round Robin Algorithm : ";
cin>>quantum;

findavgTime( n, bt, quantum);
return 0;
}


















No comments:

Post a Comment

Accessing and Parsing OneNote Notebook Content from Azure Storage Containers

Accessing and Parsing OneNote Notebook Content from Azure Storage Containers OneNote is a powerful tool for digital note-taking and collabor...