Geetika Operating Systems

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Practical File

of
Operating System
submitted in partial fulfillment of the requirement for the award of the degree of
Bachelor of Technology (B.Tech)
in
Computer Engineering With Data Science
by
Geetika Dua
20001016017
Under the guidance of
Ms. Deepika

Department of Computer Engineering


J. C. BOSE UNIVERSITY OF SCIENCE & TECHNOLOGY, YMCA
SECTOR-6 FARIDABAD HARYANA-121006
INDEX
Sr. No. List

1. WAP to implement Binary Search

2. WAP to implement selection sort

3. WAP to implement bubble sort

4. WAP to implement quick sort


WAP to implement First Come, First Serve Scheduling
5.
Algorithm
6. WAP to implement Shortest Job First Scheduling Algorithm
WAP to implement Shortest Remaining First CPU Scheduling
7.
Algorithm
8. WAP to implement Priority Scheduling Algorithm
9. WAP to implement Round Robin Scheduling Algorithm
10. WAP to implement FIFO Page Replacement Algorithm.
11. WAP to implement First Come First Serve disk scheduling.
12. WAP to implement Shortest Seek Time first disk scheduling
13. WAP to implement SCAN disk scheduling
14. WAP to implement C-SCAN disk scheduling
15. WAP to implement LOOK disk scheduling
16. WAP to implement C-LOOK disk scheduling
WAP to implement Binary Search
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,x;
    cin>>n;
    int a[n];
    cout<<"Enter the array ";
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cout<<"Enter the number you want to find ";
    cin>>x;
    int s=0,e=n-1,mid;
    int flag=0;
    while(s<=e){
        mid=(s+e)/2;
        if(x==a[mid]){
            cout<<"Found at "<<mid;
            flag=1;
            break;
        }
        else if(x>a[mid]){
            s=mid+1;
        }
        else if(x<a[mid]){
            e=mid-1;
        }
     
    }
    if(flag==0){
        cout<<"Not Found";
    }
}
WAP to implement Bubble Sort
#include<bits/stdc++.h>
using namespace std;
void bubbleSort(vector<int>& arr, int n)
{  
    for(int i = 1; i<n; i++) {
        bool swapped = false;
       
        for(int j=0; j<n-i; j++) {
            if(arr[j] > arr[j+1]) {
                swap(arr[j], arr[j+1]);
                swapped = true;
            }
        }
        if(swapped == false) {
            break;
        }
    }
}

int main(){
    int n;
    cout<<endl<<"Enter number of elements ";
    cin>>n;
    vector<int>v(n);
    cout<<endl<<"Enter the elements ";
    for (int i=0;i<n;i++)
    {
        cin>>v[i];
    }
    bubbleSort(v,n);
    for(int i=0;i<n;i++){
        cout<<v[i]<<" ";
    }
    return 0;
}
WAP to implement Selection Sort
#include<bits/stdc++.h>
using namespace std;

void selectionSort(vector<int>& arr, int n)


{  
    for(int i = 0; i < n-1; i++ ) {
        int minIndex = i;
        for(int j = i+1; j<n; j++) {
            if(arr[j] < arr[minIndex]) minIndex = j;
        }
        swap(arr[minIndex], arr[i]);
    }
}

int main(){
    int n;
    cout<<endl<<"Enter number of elements ";
    cin>>n;
    vector<int>v(n);
    cout<<endl<<"Enter the elements ";
    for (int i=0;i<n;i++)
    {
        cin>>v[i];
    }
    cout<<"The sorted array: ";
    selectionSort(v,n);
    for(int i=0;i<n;i++){
        cout<<v[i]<<" ";
    }
    return 0;
}
WAP to implement Quick Sort
#include<bits/stdc++.h>
using namespace std;
int partition(vector<int>& arr, int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for(int j = low; j < high; j++) {
        if(arr[j] < pivot) {
            i++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i+1], arr[high]);
    return i+1;
}
void quicksort(vector<int>& arr, int low, int high) {
    if(low < high) {
        int pi = partition(arr, low, high);
        quicksort(arr, low, pi-1);
        quicksort(arr, pi+1, high);
    }
}
int main() {
    int n;
    cout<<endl<<"Enter number of elements ";
    cin>>n;
    vector<int>v(n);
    cout<<endl<<"Enter the elements ";
    for (int i=0;i<n;i++)
    {
        cin>>v[i];
    }
    cout<<"The sorted array: ";
    quicksort(v,0,n-1);
    for(int i=0;i<n;i++){
        cout<<v[i]<<" ";
    }
}
WAP to implement First Come, First Serve Scheduling Algorithm
include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cout<<"Enter the number of processes: ";
    cin>>n;
    int bt[n],wt[n],tat[n],average_wt=0,average_tat=0,i,j;
    cout<<"\nEnter the Burst Time of processes\n";
    for (i=0;i<n;i++) {
        cout<<"P["<<i+1<<"]:";
        cin>>bt[i];
    }
    wt[0]=0;
    for (i=1;i<n;i++) {
        wt[i]=0;
        for (j=0;j<i;j++)
                    wt[i]+=bt[j];
    }
    cout<<"\Process Burst Time\tWaiting Time\tTurnaround Time";

    for (i=0;i<n;i++) {
        tat[i]=bt[i]+wt[i];
        average_wt+=wt[i];
        average_tat+=tat[i];
        cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];
    }
    average_wt/=n;
    average_tat/=n;
    cout<<"\an\nAverage Waiting Time:"<<average_wt;
    cout<<"\nAverage Turnaround Time:"<<average_tat;
    return 0;
}
WAP to implement Shortest Job First Scheduling Algorithm
#include<bits/stdc++.h>
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 process"<<endl;
      cin>>n;
      int a[n],b[n],e[n],tat[n],wt[n];
      cout<<"enter arival time \n";  
      for(i=0;i<n;i++)
      {
             
            cin>>a[i];
      }
      for(i=0;i<n;i++)
      {
            cout<<"enter brust time ";      
            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


}
WAP to implement Priority Scheduling Algorithm
//priority scheduling
#include<iostream>
using namespace std;
int main()
{
    int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
    cout<<"Enter Total Number of Process:";
    cin>>n;
 
    cout<<"\nEnter Burst Time and Priority\n";
    for(i=0;i<n;i++)
    {
        cout<<"\nP["<<i+1<<"]\n";
        cout<<"Burst Time:";
        cin>>bt[i];
        cout<<"Priority:";
        cin>>pr[i];
        p[i]=i+1;           //contains process number
    }
 
    //sorting burst time, priority and process number in ascending order using selection sort
    for(i=0;i<n;i++)
    {
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(pr[j]<pr[pos])
                pos=j;
        }
 
        temp=pr[i];
        pr[i]=pr[pos];
        pr[pos]=temp;
 
        temp=bt[i];
        bt[i]=bt[pos];
        bt[pos]=temp;
 
        temp=p[i];
        p[i]=p[pos];
        p[pos]=temp;
    }
 
    wt[0]=0;            //waiting time for first process is zero
 
    //calculate waiting time
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];
 
        total+=wt[i];
    }
 
    avg_wt=total/n;      //average waiting time
    total=0;
 
    cout<<"\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time";
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];     //calculate turnaround time
        total+=tat[i];
        cout<<"\nP["<<p[i]<<"]\t\t  "<<bt[i]<<"\t\t    "<<wt[i]<<"\t\t\t"<<tat[i];
    }
 
    avg_tat=total/n;     //average turnaround time
    cout<<"\n\nAverage Waiting Time="<<avg_wt;
    cout<<"\nAverage Turnaround Time="<<avg_tat;
 
    return 0;
WAP to implement Round Robin Scheduling Algorithm
#include <iostream>
#include <queue>
using namespace std;

queue<int> q;

int minimumAT(int AT2[], int n, int t)


{
    int min2 = 0;
    for (int i = 0; i < n; i++)
    {
        if (AT2[i] < AT2[min2])
        {
            min2 = i;
        }
    }
    return min2;
}
void completion_time(int process[], int AT[], int BT[], int n, int CT[], int TQ)
{
    int t = 0;
    int AT2[10];
    int BT2[10];

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


    {
        AT2[i] = AT[i];
        BT2[i] = BT[i];
    }

    int minimum_AT = minimumAT(AT2, n, t);


    AT2[minimum_AT] = 9999;
    q.push(minimum_AT);
    for (int i = 0; q.size() != 0; i++)
    {
        int running = q.front();
        q.pop();
        if (BT2[running] > TQ)
        {
            CT[running] = t + TQ;
            t = CT[running];
            BT2[running] -= TQ;
            for (int i = 1; i <= TQ; i++)
            {
                int minimum_AT = minimumAT(AT2, n, t);
                if (AT[minimum_AT] <= t && AT2[minimum_AT] != 9999)
                {
                    q.push(minimum_AT);
                    AT2[minimum_AT] = 9999;
                }
            }
            q.push(running);
        }
        else
        {
            CT[running] = t + BT2[running];
            t = CT[running];
            for (int i = 1; i <= BT2[running]; i++)
            {
                int minimum_AT = minimumAT(AT2, n, t);
                if (AT[minimum_AT] <= t && AT2[minimum_AT] != 9999)
                {
                    q.push(minimum_AT);
                    AT2[minimum_AT] = 9999;
                }
            }
            BT2[running] = 0;
        }
    }
}
void turn_around_time(int process[], int AT[], int BT[], int n, int CT[], int TAT[])
{
    for (int i = 0; i < n; i++)
    {
        TAT[i] = CT[i] - AT[i];
    }
}
void waiting_time(int process[], int AT[], int BT[], int n, int CT[], int TAT[], int WT[])
{
    for (int i = 0; i < n; i++)
    {
        WT[i] = TAT[i] - BT[i];
    }
}
int main()
{
    int process[] = {1, 2, 3, 4, 5};
    int n = sizeof(process) / sizeof(process[0]);

    int AT[] = {0, 1, 2, 3, 4};


    int BT[] = {5, 3, 1, 2, 3};
    int TQ = 2;
    int CT[10];
    int TAT[10];
    int WT[10];

    completion_time(process, AT, BT, n, CT, TQ);


    turn_around_time(process, AT, BT, n, CT, TAT);
    waiting_time(process, AT, BT, n, CT, TAT, WT);

    float sumTAT;
    float sumWT;
    for (int i = 0; i < n; i++)
    {
        printf("%d %d %d %d %d %d\n", i + 1, AT[i], BT[i], CT[i], TAT[i], WT[i]);
        sumTAT += TAT[i];
        sumWT += WT[i];
    }
    printf("Average Turn Around Time: %f\n", sumTAT / n);
    printf("Average Waiting Time: %f", sumWT / n);
}
WAP to implement FIFO Page Replacement Algorithm.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int pageFaults(int pages[], int n, int page_slots)


{
    set<int> s;
    queue<int> slots;
    int fault = 0;
    for (int i = 0; i < n; i++)
    {
        if (s.size() < page_slots)
        {
            if (s.find(pages[i]) == s.end())
            {
                s.insert(pages[i]);
                slots.push(pages[i]);
                fault++;
            }
        }
        else
        {
            if (s.find(pages[i]) == s.end())
            {
                s.erase(slots.front());
                slots.pop();
                s.insert(pages[i]);
                slots.push(pages[i]);
                fault++;
            }
        }
    }
    return fault;
}

int main()
{
    int pages[] = {3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4};
    int n = sizeof(pages) / sizeof(pages[0]);
    int page_slots = 4;
    cout << "Page Faults: " << pageFaults(pages, n, page_slots);
}
WAP to implement FCFS Disk Scheduling
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[]={55,58,39,18,90,160,150,38,184};
    int seektime=45;
    float avg_seektime=0,n=9;
    for(int i=1;i<9;i++){
        seektime=seektime+abs(a[i]-a[i-1]);
    }
    avg_seektime=seektime/n;
    cout<<avg_seektime;
}

WAP to implement SSTF Disk Scheduling


#include <bits/stdc++.h>
using namespace std;
int seekCount(int request_sequence[], int n, int head)
{
    int seek_count = 0;
    int head_position = 0;
    vector<int> list;
    for (int i = 0; i < 8; i++)
    {
        list.push_back(request_sequence[i]);
    }
    list.push_back(head);

    sort(list.begin(), list.end());
    for (int i = 0; i < list.size(); i++)
    {
        if (list[i] == head)
        {
            head_position = i;
        }
    }

    while (list.size() != 1)
    {
        if (head_position == 0)
        {
            seek_count += abs(list[head_position] - list[head_position + 1]);
            list.erase(list.begin() + head_position);
        }
        else if (head_position == list.size() - 1)
        {
        }
        else if (abs(list[head_position] - list[head_position - 1]) < abs(list[head_position]
- list[head_position + 1]))
        {
            seek_count += abs(list[head_position] - list[head_position - 1]);

            list.erase(list.begin() + head_position);
            head_position = head_position - 1;
        }
        else if (abs(list[head_position] - list[head_position - 1]) >=
abs(list[head_position] - list[head_position + 1]))
        {
            seek_count += abs(list[head_position] - list[head_position + 1]);
            list.erase(list.begin() + head_position);
        }
    }
    return seek_count;
}

int main()
{
    int request_sequence[] = {55,58,39,18,90,160,150,38,184};
    int n = sizeof(request_sequence) / sizeof(request_sequence[0]);
    int head = 100;
    cout << "Seek Count: " << seekCount(request_sequence, n, head);
}

WAP to implement SCAN Disk Scheduling


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int seekCount(int request_sequence[], int n, int head)


{
    int seek_count = 0;
    int head_position = 0;
    vector<int> list;
    for (int i = 0; i < n; i++)
    {
        list.push_back(request_sequence[i]);
    }
    list.push_back(head);
    list.push_back(199);
    sort(list.begin(), list.end());

    for (int i = 0; i < list.size(); i++)


    {
        if (list[i] == head)
        {
            head_position = i;
        }
    }

    while (list.size() != 1)
    {
        if (head_position != (list.size() - 1))
        {
            seek_count += abs(list[head_position] - list[head_position + 1]);
            list.erase(list.begin() + head_position);
        }
        else
        {
            seek_count += abs(list[head_position] - list[head_position - 1]);
            list.erase(list.begin() + head_position);
            head_position--;
        }
    }
    return seek_count;
}

int main()
{
    int request_sequence[] = {55,58,39,18,90,160,150,38,184};
    int n = sizeof(request_sequence) / sizeof(request_sequence[0]);
    int head = 100;
    cout << "Seek Count: " << seekCount(request_sequence, n, head);
}

WAP to implement CSCAN Disk Scheduling


#include <bits/stdc++.h>
using namespace std;

int seekCount(int request_sequence[], int n, int head)


{
    int seek_count = 0;
    int head_position = 0;
    vector<int> list;
    for (int i = 0; i < n; i++)
    {
        list.push_back(request_sequence[i]);
    }
    list.push_back(0);
    list.push_back(head);
    list.push_back(199);
    sort(list.begin(), list.end());

    for (int i = 0; i < list.size(); i++)


    {
        if (list[i] == head)
        {
            head_position = i;
        }
    }

    while (list.size() != 1)
    {
        if (head_position != (list.size() - 1))
        {
            seek_count += abs(list[head_position] - list[head_position + 1]);
            list.erase(list.begin() + head_position);
        }
        else
        {
            seek_count += abs(list[head_position] - list[0]);
            list.erase(list.begin() + head_position);
            head_position = 0;
        }
    }
    return seek_count;
}

int main()
{
    int request_sequence[] = {55,58,39,18,90,160,150,38,184};
    int n = sizeof(request_sequence) / sizeof(request_sequence[0]);
    int head = 100;
    cout << "Seek Count: " << seekCount(request_sequence, n, head);
}

WAP to implement LOOK Disk Scheduling

#include <bits/stdc++.h>
using namespace std;

int seekCount(int request_sequence[], int n, int head)


{
    int seek_count = 0;
    int head_position = 0;
    vector<int> list;
    for (int i = 0; i < n; i++)
    {
        list.push_back(request_sequence[i]);
    }
    list.push_back(head);
    sort(list.begin(), list.end());

    for (int i = 0; i < list.size(); i++)


    {
        if (list[i] == head)
        {
            head_position = i;
        }
    }

    while (list.size() != 1)
    {
        if (head_position != (list.size() - 1))
        {
            seek_count += abs(list[head_position] - list[head_position + 1]);
            list.erase(list.begin() + head_position);
        }
        else
        {
            seek_count += abs(list[head_position] - list[head_position - 1]);
            list.erase(list.begin() + head_position);
            head_position--;
        }
    }
    return seek_count;
}

int main()
{
    int request_sequence[] = {55,58,39,18,90,160,150,38,184};
    int n = sizeof(request_sequence) / sizeof(request_sequence[0]);
    int head = 100;
    cout << "Seek Count: " << seekCount(request_sequence, n, head);
}
WAP to implement C-LOOK Disk Scheduling
#include <bits/stdc++.h>
using namespace std;

int seekCount(int request_sequence[], int n, int head)


{
    int seek_count = 0;
    int head_position = 0;
    vector<int> list;
    for (int i = 0; i < n; i++)
    {
        list.push_back(request_sequence[i]);
    }
    list.push_back(head);
    sort(list.begin(), list.end());

    for (int i = 0; i < list.size(); i++)


    {
        if (list[i] == head)
        {
            head_position = i;
        }
    }

    while (list.size() != 1)
    {
        if (head_position != (list.size() - 1))
        {
            seek_count += abs(list[head_position] - list[head_position + 1]);
            list.erase(list.begin() + head_position);
        }
        else
        {
            seek_count += abs(list[head_position] - list[0]);
            list.erase(list.begin() + head_position);
            head_position = 0;
        }
    }
    return seek_count;
}

int main()
{
    int request_sequence[] = {55,58,39,18,90,160,150,38,184};
    int n = sizeof(request_sequence) / sizeof(request_sequence[0]);
    int head = 100;
    cout << "Seek Count: " << seekCount(request_sequence, n, head);
}

You might also like