0% found this document useful (0 votes)
44 views

OS Lab Programs Exercises

The document contains C++ program code examples for implementing process scheduling algorithms in an OS laboratory program exercises. The first example creates a process that launches the Windows calculator application for 10 seconds then terminates it, demonstrating process creation. The second and third examples implement the First-Come First-Served and Shortest-Job-First CPU scheduling algorithms, sorting processes by arrival time or burst time respectively and calculating waiting time, completion time and turnaround time metrics.

Uploaded by

ljjb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

OS Lab Programs Exercises

The document contains C++ program code examples for implementing process scheduling algorithms in an OS laboratory program exercises. The first example creates a process that launches the Windows calculator application for 10 seconds then terminates it, demonstrating process creation. The second and third examples implement the First-Come First-Served and Shortest-Job-First CPU scheduling algorithms, sorting processes by arrival time or burst time respectively and calculating waiting time, completion time and turnaround time metrics.

Uploaded by

ljjb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

OS Laboratory

Programs Exercises

1) Process Management - “Process Creation”


EX: Write a C++ program to create a process that starts a
windows application such as the calculator, allows user to use it
for 10 seconds then terminates this application.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;

void main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

if ( !CreateProcess(NULL, "C:\\Windows\\System32\\calc.exe" ,
NULL, NULL,FALSE,0,NULL,NULL,&si,&pi) )
cout<<"Creating Process Failed"<<endl;

WaitForSingleObject(pi.hProcess, 10000);
cout<<"Child Complete"<<endl;

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

_getch();
}
1|Page
2.1) CPU Scheduling - “First-Come, First-Served”
EX: Write a program to implement FCFS CPU Scheduling
algorithm with arrival time.

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <conio.h>

using namespace std;

float totalwait = 0;
float totalcomp = 0;
float totalturnaround = 0;
float n;
class process
{
char name;
float arrival, burst;
float wait, completion, turnaround;

public:
process()
{
cout<<"Enter Process Name: "<<endl;
cin>>name;
cout<<"Enter Process Arrival Time: "<<endl;
cin>>arrival;
cout<<"Enter Process Burst time: "<<endl;
cin>>burst;
}
process(char t) {name = 't';}
friend void sort(process p[],float);
friend void display(process p[],float);
friend void calculate(process p[], float);
};
void sort(process p[],float n)
{ process temp('t');
for (int i=0; i< n-1; i++)
for (int j=i+1 ; j<n; j++)
if (p[i].arrival > p[j].arrival)
{ temp= p[i];
p[i]= p[j];
p[j]= temp; }

2|Page
}

void calculate(process p[],float n)


{ p[0].wait = 0;
for (int i=1; i< n; i++)
p[i].wait = p[i-1].wait + p[i-1].burst ;

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


{
p[i].completion = p[i].wait + p[i].burst ;
p[i].turnaround = p[i].completion - p[i].arrival ;
totalwait = totalwait + p[i].wait ;
totalcomp = totalcomp + p[i].completion ;
totalturnaround = totalturnaround + p[i].turnaround ;
}
}

void display(process p[], float n)


{
for (int i=0; i< n; i++)
{ cout<<"Process Name: "<<p[i].name<<endl;
cout<<"Process Arrival Time: "<<p[i].arrival<<endl;
cout<<"Process Burst time: "<<p[i].burst<<endl;
cout<<"Process Wait Time: "<<p[i].wait<<endl;
cout<<"Process Completion Time: "<<p[i].completion<<endl;
cout<<"Process Turnaround time: "<<p[i].turnaround<<endl<<endl;
}
cout<<endl<<"Average waiting time: "<<totalwait/n<<endl;
cout<<"Average completion time: "<<totalcomp/n<<endl;
cout<<"Average turnaround time: "<<totalturnaround/n<<endl;
}

void main()
{
int num;
cout<<"Enter Number of Processes"<<endl;
cin>>num;
process *pr;
pr = new process [num];
sort(pr,num);
cout<<endl<<"Processes after sorting"<<endl;
calculate(pr,num);
display(pr,num);

_getch();
}

3|Page
2.2) CPU Scheduling - “Shortest-Job-First”
EX: Write a program to implement SJF CPU Scheduling
algorithm.

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <conio.h>

using namespace std;

float totalwait = 0;
float totalcomp = 0;
float totalturnaround = 0;
float n;
class process
{
char name;
float arrival, burst;
float wait, completion, turnaround;

public:
process()
{
cout<<"Enter Process Name: "<<endl;
cin>>name;
cout<<"Enter Process Arrival Time: "<<endl;
cin>>arrival;
cout<<"Enter Process Burst time: "<<endl;
cin>>burst;
}
process(char t) {name = 't';}
friend void sort(process p[],float);
friend void display(process p[],float);
friend void calculate(process p[], float);
};
void sort(process p[],float n)
{ process temp('t');
for (int i=0; i< n-1; i++)
for (int j=i+1 ; j<n; j++)
if (p[i].burst > p[j].burst)
{ temp= p[i];
p[i]= p[j];
p[j]= temp; }
}

4|Page
void calculate(process p[],float n)
{ p[0].wait = 0;
for (int i=1; i< n; i++)
p[i].wait = p[i-1].wait + p[i-1].burst ;

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


{
p[i].completion = p[i].wait + p[i].burst ;
p[i].turnaround = p[i].completion - p[i].arrival ;
totalwait = totalwait + p[i].wait ;
totalcomp = totalcomp + p[i].completion ;
totalturnaround = totalturnaround + p[i].turnaround ;
}
}

void display(process p[], float n)


{
for (int i=0; i< n; i++)
{ cout<<"Process Name: "<<p[i].name<<endl;
cout<<"Process Arrival Time: "<<p[i].arrival<<endl;
cout<<"Process Burst time: "<<p[i].burst<<endl;
cout<<"Process Wait Time: "<<p[i].wait<<endl;
cout<<"Process Completion Time: "<<p[i].completion<<endl;
cout<<"Process Turnaround time: "<<p[i].turnaround<<endl<<endl;
}
cout<<endl<<"Average waiting time: "<<totalwait/n<<endl;
cout<<"Average completion time: "<<totalcomp/n<<endl;
cout<<"Average turnaround time: "<<totalturnaround/n<<endl;
}

void main()
{
int num;
cout<<"Enter Number of Processes"<<endl;
cin>>num;
process *pr;
pr = new process [num];
sort(pr,num);
cout<<endl<<"Processes after sorting"<<endl;
calculate(pr,num);
display(pr,num);

_getch();
}

5|Page

You might also like