KEMBAR78
OS Assignment | PDF | Software | Computer Science
0% found this document useful (0 votes)
37 views29 pages

OS Assignment

operating system questions answers

Uploaded by

Darkroom
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)
37 views29 pages

OS Assignment

operating system questions answers

Uploaded by

Darkroom
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/ 29

1.

Write a program (using fork() and/or exec() commands) where parent and child execute:
a. same program, same code.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t pid,p;
p=fork();
pid=getpid();
if(p<0)
{
fprintf(stderr,"Fork Failed");
return 1;
}
printf("Output of Fork ID ::\t%d\n",p);
printf("Process ID is ::\t%d\n",pid);
return 0;
}

OUTPUT:
b. same program, different code.

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
int pid;
pid=fork();
if(pid<0)
{
printf("\n Error...\n");
exit(1);
}
else if (pid==0)
{
printf("Hello... I am Child Process...\n");
printf("My pid is ::\t%d\n",getpid());
exit(0);
}
else
{
printf("Hello... I am Parent Process...\n");
printf("My actual pid is ::\t%d\n",getpid());
exit(1);
}
}

OUTPUT:
c. before terminating, the parent waits for the child to finish its task.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>

int main()
{
int pid;
pid=fork();
if(pid<0)
{
printf("\nError....\n");
exit(1);
}
else if(pid==0)
{
printf("Hello... I am Child Process...\n");
printf("My pid is ::\t%d\n",getpid());
exit(0);
}
else
{
wait(NULL);
printf("Hello... I am Parent Process...\n");
printf("My pid is ::\t%d\n",getpid());
exit(0);
}
}

OUTPUT:
2. Write a program to report behaviour of Linux kernel including kernel version, CPU type and
model. (CPU information)

#include<stdio.h>
#include<stdlib.h>

int main()
{
printf("The Kernel Version is::\n");
system("cat /proc/sys/kernel/osrelease");
printf("\nThe CPU space::\n");
system("cat /proc/cpuinfo |awk 'NR==3, NR==4{print} \n");
printf("Amount of CPU time since system was last booted is ::\n");
system("cat /proc/uptime\n");
return 0;
}

OUTPUT:
3. Write a program to report behaviour of Linux kernel including information on configured
memory, amount of free and used memory. (memory information)

#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("The configured memory is::\n");
system("cat /proc/meminfo | awk 'NR == 1{print $2}'\n");
printf("Amount of free memory::\n");
system("cat /proc/meminfo | awk 'NR == 2{print $2}'\n");
printf("Amount of used memory::\n");
system("cat /proc/meminfo | awk '{if (NR==1) a=$2; if (NR==2) b=$2} END {print a-b}'\n");
return 0;
}

OUTPUT:
4. Write a program to print file details including owner access permissions, file access time,
where file name is given as argument.

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int i;
struct stat s;
if (argc < 2)
{
cout<<"\n Enter a filename as command line argument"<<endl;
exit(0);
}
for(i=1;i<argc; i++)
{
cout<<"File : "<<argv[i]<<"\n";
if(stat(argv[i],&s)<0)
cout<<"error in obtaining stats In";
else
{
cout<<"owner UID : "; cout<<s.st_uid<<endl;
cout<<"group ID :"; cout<<s.st_gid<<endl;
cout<<"Acess permissions : "<<s.st_mode<<endl;
cout<<"Acess Time : :"<<s.st_atime<<endl;
cout<<"File Size : "<<s.st_size<<endl;
cout<<"File Size(in blocks) : "<<s.st_blksize<<endl;
}
}
return 0;
}

OUTPUT:
5. Write a program to copy files using system calls.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
int main(int argc, char* argv[])
{

int fd1 , fd2;


fd1 = open(argv[1] , O_RDONLY);
fd2 = open(argv[2] , O_WRONLY|O_CREAT , 0777);
int count_fd1 , count_fd2;
char buffer[100];

if(argc!=3)
{
cout<<"Error"<<endl;
exit(100);
}

if(fd1<0)
{
cout<<"!!!!Error in opening file "<<endl;
}

else if(fd2<0)
{
cout<<"!!!Error in creating file "<<endl;
}

else
{
while(count_fd1 = read(fd1,buffer,sizeof(buffer)))
{
count_fd2 = write(fd2,buffer,count_fd1);

if(count_fd1!=count_fd2)
{
cout<<"!!!Error"<<endl;
}
}
}

close(fd1);
close(fd2);

OUTPUT:
6. Write program to implement FCFS scheduling algorithm.
#include<iostream>
using namespace std;
struct Process{
public:
int pid;
int arrival_T;
int burst_Time;
int response_Time;
int waiting_Time;
int turnaround_Time;
int complition_Time;
};
class FCFS
{
int n ;
float averageturnaroundtime , averagewaitingtime;
Process *P;
public:
void input()
{
cout<<"Enter the number of process : ";
cin>>n;
cout<<"Enter Burst Time for each process : "<<endl;
P = new Process[n];
for(int i=0;i<n;i++)
{
cin>>P[i].burst_Time;
}
cout<<endl;
cout<<"Enter arrival time for "<<n<< " process "<<endl;
for(int i=0;i<n;i++)
{
cin>>P[i].arrival_T;
}
cout<<"__________________________________"<<endl;
cout<<" GANTT CHART "<<endl;
cout<<"__________________________________"<<endl<<endl;
for(int i=1;i<=n;i++)
{
cout<<"[P"<<i<<"] ";
}
cout<<endl<<"________________________________________________________"<<endl;
int k=0;
for(int i=0;i<=n;i++)
{
cout<<k<<"\t ";
k += P[i].burst_Time;
}
for(int i=0;i<n;i++)
{
if(i==0){
P[i].complition_Time = P[i].burst_Time;
}
else
P[i].complition_Time = P[i-1].complition_Time + P[i].burst_Time;
P[i].turnaround_Time = P[i].complition_Time - P[i].arrival_T;
P[i].waiting_Time = P[i].turnaround_Time - P[i].burst_Time;
}
P[0].pid;
int l=100;
for(int i=0;i<n;i++)
{
P[i].pid = l;
l++;
}
for(int i=0;i<n;i++)
{
P[i].response_Time = P[i].waiting_Time;
}
cout<<endl<<endl;
cout<<"______________________________________________________________________________
_________________________"<<endl;
cout<<"Processes "<<" PID "<<" Burst time " <<" Waiting time "<<" Turn around time "<<"
Response time\n";
cout<<"______________________________________________________________________________
_________________________"<<endl<<endl;

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


{
averagewaitingtime += P[i].waiting_Time;
averageturnaroundtime += P[i].turnaround_Time;
cout<<" "<<i+1;
cout<<"\t "<<P[i].pid;
cout<<"\t\t "<<P[i].burst_Time;
cout<<"\t\t "<<P[i].waiting_Time;
cout<<"\t\t "<<P[i].turnaround_Time;
cout<<"\t\t "<<P[i].response_Time;
cout<<endl;
}
cout<<endl<<endl<<"Average Waiting Time : "<<averagewaitingtime<<endl;
cout<<endl<<"Average TurnAroundTime Time : "<<averageturnaroundtime<<endl;
}
};
int main()
{
FCFS obj;
obj.input();
return 0;
}
OUTPUT:
7. Write program to implement Round Robin scheduling algorithm.
#include<stdio.h>
int main()
{
int cnt,j,n,t,remain,flag=0,tq;
int wt=0,tat=0,at[10],bt[10],rt[10];
printf("Enter No. of Processes:\t ");
scanf("%d",&n);
remain=n;
for(cnt=0;cnt<n;cnt++)
{
printf("Enter Arrival Time and Burst Time for Process P[%d] :",cnt+1);
scanf("%d",&at[cnt]);
scanf("%d",&bt[cnt]);
rt[cnt]=bt[cnt];
}
printf("Enter Time Quantum:\t");
scanf("%d",&tq);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(t=0,cnt=0;remain!=0;)
{
if(rt[cnt]<=tq && rt[cnt]>0)
{
t+=rt[cnt];
rt[cnt]=0;
flag=1;
}
else if(rt[cnt]>0)
{
rt[cnt]-=tq;
t+=tq;
}
if(rt[cnt]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",cnt+1,t-at[cnt],t-at[cnt]-bt[cnt]);
wt+=t-at[cnt]-bt[cnt];
tat+=t-at[cnt];
flag=0;
}
if(cnt==n-1)
cnt=0;
else if(at[cnt+1]<=t)
cnt++;
else
cnt=0;
}
printf("\nAverage Waiting Time= %f\n",wt*1.0/n);
printf("Avg Turnaround Time = %f\n",tat*1.0/n);
return 0;
}
OUTPUT:
8. Write program to implement SJF scheduling algorithm.

#include <stdio.h>
int main()
{
int A[100][4]; // Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
// User Input Burst Time and alloting Process Id.
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
// Sorting process according to their Burst Time.
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;

temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
// Calculation of Turn Around Time and printing the
// data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
A[i][1], A[i][2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f\n", avg_tat);
}

OUTPUT:
9. Write program to implement non-preemptive priority based scheduling algorithm.

#include <iostream>
using namespace std;

int main()
{
int n = 5; //Number of Processes
int CPU = 0; //CPU Current time
int allTime = 0; // Time needed to finish all processes

int arrivaltime[n] = {0, 5, 12, 2, 9};


int bursttime[n] = {11, 28, 2, 10, 16};
int priority[n] = {2, 0, 3, 1, 4};
int ATt[n];
int NoP = n; //number of Processes
int PPt[n];
int waitingTime[n];
int turnaroundTime[n];
int i = 0;

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


{
PPt[i] = priority[i];
ATt[i] = arrivaltime[i];
}

int LAT = 0; //LastArrivalTime


for (i = 0; i < n; i++)
if (arrivaltime[i] > LAT)
LAT = arrivaltime[i];

int MAX_P = 0; //Max Priority


for (i = 0; i < n; i++)
if (PPt[i] > MAX_P)
MAX_P = PPt[i];

int ATi = 0; //Pointing to Arrival Time indix


int P1 = PPt[0]; //Pointing to 1st priority Value
int P2 = PPt[0]; //Pointing to 2nd priority Value

//finding the First Arrival Time and Highest priority Process


int j = -1;
while (NoP > 0 && CPU <= 1000)
{
for (i = 0; i < n; i++)
{
if ((ATt[i] <= CPU) && (ATt[i] != (LAT + 10)))
{
if (PPt[i] != (MAX_P + 1))
{
P2 = PPt[i];
j = 1;

if (P2 < P1)


{
j = 1;
ATi = i;
P1 = PPt[i];
P2 = PPt[i];
}
}
}
}

if (j == -1)
{
CPU = CPU + 1;
continue;
}
else
{
waitingTime[ATi] = CPU - ATt[ATi];
CPU = CPU + bursttime[ATi];
turnaroundTime[ATi] = CPU - ATt[ATi];
ATt[ATi] = LAT + 10;
j = -1;
PPt[ATi] = MAX_P + 1;
ATi = 0; //Pointing to Arrival Time index
P1 = MAX_P + 1; //Pointing to 1st priority Value
P2 = MAX_P + 1; //Pointing to 2nd priority Value
NoP = NoP - 1;
}
}
cout <<
"\nProcess_Number\tBurst_Time\tPriority\tArrival_Time\tWaiting_Time\tTurnaround_Time\n\n";
for (i = 0; i < n; i++)
{
cout << "P" << i + 1 << " \t\t" << bursttime[i] << " \t\t" << priority[i] << " \t\t" << arrivaltime[i]
<< " \t\t" << waitingTime[i] << " \t\t" << turnaroundTime[i] << endl;
}
float AvgWT = 0; //Average waiting time
float AVGTaT = 0; // Average Turn around time
for (i = 0; i < n; i++)
{
AvgWT = waitingTime[i] + AvgWT;
AVGTaT = turnaroundTime[i] + AVGTaT;
}

cout << "Average waiting time = " << AvgWT / n << endl;
cout << "Average turnaround time = " << AVGTaT / n << endl;
}

OUTPUT:
10. Write program to implement preemptive priority based scheduling algorithm.

// CPP program to implement preemptive priority scheduling


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

struct Process {
int processID;
int burstTime;
int tempburstTime;
int responsetime;
int arrivalTime;
int priority;
int outtime;
int intime;
};

// It is used to include all the valid and eligible


// processes in the heap for execution. heapsize defines
// the number of processes in execution depending on
// the current time currentTime keeps a record of
// the current CPU time.
void insert(Process Heap[], Process value, int* heapsize,
int* currentTime)
{
int start = *heapsize, i;
Heap[*heapsize] = value;
if (Heap[*heapsize].intime == -1)
Heap[*heapsize].intime = *currentTime;
++(*heapsize);

// Ordering the Heap


while (start != 0
&& Heap[(start - 1) / 2].priority
> Heap[start].priority) {
Process temp = Heap[(start - 1) / 2];
Heap[(start - 1) / 2] = Heap[start];
Heap[start] = temp;
start = (start - 1) / 2;
}
}

// It is used to reorder the heap according to


// priority if the processes after insertion
// of new process.
void order(Process Heap[], int* heapsize, int start)
{
int smallest = start;
int left = 2 * start + 1;
int right = 2 * start + 2;
if (left < *heapsize
&& Heap[left].priority
< Heap[smallest].priority)
smallest = left;
if (right < *heapsize
&& Heap[right].priority
< Heap[smallest].priority)
smallest = right;

// Ordering the Heap


if (smallest != start) {
Process temp = Heap[smallest];
Heap[smallest] = Heap[start];
Heap[start] = temp;
order(Heap, heapsize, smallest);
}
}

// This function is used to find the process with


// highest priority from the heap. It also reorders
// the heap after extracting the highest priority process.
Process extractminimum(Process Heap[], int* heapsize,
int* currentTime)
{
Process min = Heap[0];
if (min.responsetime == -1)
min.responsetime
= *currentTime - min.arrivalTime;
--(*heapsize);
if (*heapsize >= 1) {
Heap[0] = Heap[*heapsize];
order(Heap, heapsize, 0);
}
return min;
}
// Compares two intervals
// according to starting times.
bool compare(Process p1, Process p2)
{
return (p1.arrivalTime < p2.arrivalTime);
}
// This function is responsible for executing
// the highest priority extracted from Heap[].
void scheduling(Process Heap[], Process array[], int n,
int* heapsize, int* currentTime)
{
if (heapsize == 0)
return;

Process min = extractminimum(


Heap, heapsize, currentTime);
min.outtime = *currentTime + 1;
--min.burstTime;
printf("process id = %d current time = %d\n",
min.processID, *currentTime);

// If the process is not yet finished


// insert it back into the Heap
if (min.burstTime > 0) {
insert(Heap, min, heapsize, currentTime);
return;
}

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


if (array[i].processID == min.processID) {
array[i] = min;
break;
}
}
// This function is responsible for
// managing the entire execution of the
// processes as they arrive in the CPU
// according to their arrival time.
void priority(Process array[], int n)
{
sort(array, array + n, compare);

int totalwaitingtime = 0, totalbursttime = 0,


totalturnaroundtime = 0, i, insertedprocess = 0,
heapsize = 0, currentTime = array[0].arrivalTime,
totalresponsetime = 0;

Process Heap[4 * n];

// Calculating the total burst time


// of the processes
for (int i = 0; i < n; i++) {
totalbursttime += array[i].burstTime;
array[i].tempburstTime = array[i].burstTime;
}

// Inserting the processes in Heap


// according to arrival time
do {
if (insertedprocess != n) {
for (i = 0; i < n; i++) {
if (array[i].arrivalTime == currentTime) {
++insertedprocess;
array[i].intime = -1;
array[i].responsetime = -1;
insert(Heap, array[i],
&heapsize, &currentTime);
}
}
}
scheduling(Heap, array, n,
&heapsize, &currentTime);
++currentTime;
if (heapsize == 0
&& insertedprocess == n)
break;
} while (1);

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


totalresponsetime
+= array[i].responsetime;
totalwaitingtime
+= (array[i].outtime
- array[i].intime
- array[i].tempburstTime);
totalbursttime += array[i].burstTime;
}
printf("Average waiting time = %f\n",
((float)totalwaitingtime / (float)n));
printf("Average response time =%f\n",
((float)totalresponsetime / (float)n));
printf("Average turn around time = %f\n",
((float)(totalwaitingtime
+ totalbursttime)
/ (float)n));
}
// Driver code
int main()
{
int n, i;
Process a[5];
a[0].processID = 1;
a[0].arrivalTime = 4;
a[0].priority = 2;
a[0].burstTime = 6;
a[1].processID = 4;
a[1].arrivalTime = 5;
a[1].priority = 1;
a[1].burstTime = 3;
a[2].processID = 2;
a[2].arrivalTime = 5;
a[2].priority = 3;
a[2].burstTime = 1;
a[3].processID = 3;
a[3].arrivalTime = 1;
a[3].priority = 4;
a[3].burstTime = 2;
a[4].processID = 5;
a[4].arrivalTime = 3;
a[4].priority = 5;
a[4].burstTime = 4;
priority(a, 5);
return 0;
}

OUTPUT:
11. Write program to implement SRTF scheduling algorithm.

// C++ program to implement Shortest Remaining Time First


// Shortest Remaining Time First (SRTF)

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

struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};

// Function to find the waiting time for all


// processes
void findWaitingTime(Process proc[], int n,
int wt[])
{
int rt[n];

// Copy the burst time into rt[]


for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;

int complete = 0, t = 0, minm = INT_MAX;


int shortest = 0, finish_time;
bool check = false;

// Process until all processes gets


// completed
while (complete != n) {

// Find process with minimum


// remaining time among the
// processes that arrives till the
// current time`
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) &&
(rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}

if (check == false) {
t++;
continue;
}

// Reduce remaining time by one


rt[shortest]--;

// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;

// If a process gets completely


// executed
if (rt[shortest] == 0) {

// Increment complete
complete++;
check = false;

// Find finish time of current


// process
finish_time = t + 1;

// Calculate waiting time


wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;

if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}
// Function to calculate turn around time
void findTurnAroundTime(Process proc[], int n,
int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}

// Function to calculate average time


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

// Function to find waiting time of all


// processes
findWaitingTime(proc, n, wt);

// Function to find turn around time for


// all processes
findTurnAroundTime(proc, n, wt, tat);

// Display processes along with all


// details
cout << " P\t\t"
<< "BT\t\t"
<< "WT\t\t"
<< "TAT\t\t\n";

// Calculate total waiting time and


// total turnaround time
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t\t " << wt[i]
<< "\t\t " << tat[i] << endl;
}
cout << "\nAverage waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n << endl;
}

// Driver code
int main()
{
Process proc[] = { { 1, 6, 2 }, { 2, 2, 5 },
{ 3, 8, 1 }, { 4, 3, 0}, {5, 4, 4} };
int n = sizeof(proc) / sizeof(proc[0]);

findavgTime(proc, n);
return 0;
}

OUTPUT:

You might also like