Os Practical File 1
Os Practical File 1
on
OPERATING SYSTEMS LAB
(PCS-506)
2022-23
MRS.HIMADR
I VAIDYA
NEHA
KUMARI
TABLE OF CONTENTS
PROGRAM – 1
OBJECTIVE - Implementation of basic commands such as i) ls and ii) cp
command in C program.
SOURCE CODE –
#include<sys/wait.h>
#include<unistd.h>
int main () {
int a[2];
pipe(a);
if (! fork()) {
close(1);
dup(a[1]);
close(a[0]);
else {
close(0);
dup(a[0]);
close(a[1]);
OUTPUT –
PROGRAM – 2
OBJECTIVE - Implementation of fork () command in different ways.
SOURCE CODE –
1. #include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main () {
fork();
printf(“Hello World!\n”);
return 0;
2. #include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
void forkexample(){
if(fork()==0)
else
int main () {
forkexample();
return 0;
OUTPUT –
PROGRAM – 3
SOURCE CODE –
rs=`expr $i % 2`
if [ $rs == 0 ]
then
# end of if loop
Fi
# incrementing i by one
((++i))
# end of while loop
done
((++i))
done
OUTPUT –
PROGRAM – 4
SOURCE CODE –
#include<stdio.h>
int main(){
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("nEnter Process Burst Time");
for(i=0;i<n;i++) {
printf("P[%d]:",i+1);
scanf("%d",&bt[i]); }
wt[0]=0;
for(i=1;i<n;i++) {
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j]; }
printf("\nProcess\t\tBurst Time\t\tWaiting Time\t\tTurnaround Time\n");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d\n",i+1,bt[i],wt[i],tat[i]); }
avwt/=i;
avtat/=i;
printf("nnAverage Waiting Time:%d\n",avwt);
printf("nAverage Turnaround Time:%d",avtat);
return 0; }
OUTPUT –
PROGRAM – 5
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("nEnter Burst Time:n");
for(i=0;i<n;i++) {
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; }
for(i=0;i<n;i++){
pos=i;
for(j=i+1;j<n;j++) {
if(bt[j]<bt[pos])
pos=j; }
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp; }
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i]; }
avg_wt=(float)total/n;
total=0;
printf("nProcesst BurstTime tWaitingTime tTurnaroundTime”);
for(i=0;i<n;i++) {
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt%dtt%dttt%d",p[i],bt[i],wt[i],tat[i]); }
avg_tat=(float)total/n;
printf("nAverage Waiting Time=%f",avg_wt);
printf("Avg Turnaround Time=%fn",avg_tat); }
OUTPUT –
PROGRAM – 6
OUTPUT –
PROGRAM - 7
OBJECTIVE - Implementation of Longest Job First Search (Non-Premptive)
Scheduling in C.
SOURCE CODE –
#include<stdio.h>
int main(){
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process\n:");
scanf("%d",&n);
printf("Enter Burst Time:\n");
for(i=0;i<n;i++) {
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; }
for(i=0;i<n;i++){
pos=i;
for(j=i+1;j<n;j++) {
if(bt[j]<bt[pos]) pos=j; }
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp; }
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i]; }
avg_wt=(float)*
total/n;
total=0;
printf("Process\t\tBurst Time\t\tWaiting Time\t\tTurnaround Time\n");
for(i=0;i<n;i++) {
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("p%d\t\t%d\t\t%d\t\t%d\n",p[i],bt[i],wt[i],tat[i]);}
avg_tat=(float)total/n;
printf("Average Waiting Time=%f\n",avg_wt);
printf("Average Turnaround Time=%f\n",avg_tat);}
OUTPUT –
PROGRAM - 8
OBJECTIVE - Implementation of Longest Remaining Time First (Premptive)
Scheduling in C.
SOURCE CODE –
#include <stdio.h>
int main () {
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("Enter the Total Number of Processes:");
scanf("%d", &limit);
printf("Enter Details of %d Processes", limit);
for(i = 0; i < limit; i++){
printf("Enter Arrival Time:");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i]; }
burst_time[9] = 0;
smallest = 0;
if(arrival_time[i]>=time&&burst_time[i]>burst_time[smallest]&&burst_time[i]>0){
smallest = i;}}
burst_time[smallest]--;
if(burst_time[smallest] == 0){
count++;
end = time + 1;
wait_time = wait_time - end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest]; } }
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("Average Waiting Time:%f\n", average_waiting_time);
printf("Average Turnaround Time:%f\n", average_turnaround_time);
return 0; }
OUTPUT –
PROGRAM – 9
OBJECTIVE - Implementation of Round Robin Scheduling in C.
SOURCE CODE –
#include <stdio.h>
int turnarroundtime(int processes[],int n,int bt[],int wt[],int tat[]) {
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];return 1;}
int waitingtime(int processes[],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; }
return 1; }
int findavgTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
waitingtime(processes, n, bt, wt, quantum);
turnarroundtime(processes, n, bt, wt, tat);
printf("Processes Burst Time Waiting Time turnaround time\n");
for (int i=0; i<n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("\t%d\t\t\t%d\t\t\t%d\t\t\t%d\n",i+1, bt[i], wt[i], tat[i]); }
printf("Average waiting time = %f", (float)total_wt / (float)n);
printf("\nAverage turnaround time = %f\n", (float)total_tat / (float)n);
return 1;}
int main() {
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {8, 2, 7};
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0; }
OUTPUT –
PROGRAM – 10
OBJECTIVE - Implementation of Priority Based (Premptive) Scheduling in C.
SOURCE CODE –
#include<stdio.h>
int main () {
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:\n");
scanf("%d",&n);
printf("Enter Burst Time and Priority\n");
for(i=0;i<n;i++){
printf("P[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; }
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;
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;
total=0;
printf("\nProcess\t\tBurst Time\t\tWaiting Time\t\tTurnaround Time");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\nP[%d]\t\t%d\t\t\t%d\t\t\t%d\n",p[i],bt[i],wt[i],tat[i]); }
avg_tat=total/n;
printf("Average Waiting Time=%d\n",avg_wt);
printf("Average Turnaround Time=%d",avg_tat);
return 0; }
OUTPUT –
PROGRAM – 11
OBJECTIVE - Implementation of Priority Based (Non- Premptive) Scheduling
in C.
SOURCE CODE –
#include <stdio.h>
*b=temp;}
int main(){
int n;
scanf("%d",&n);
int b[n],p[n],index[n];
for(int i=0;i<n;i++){
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&b[i],&p[i]);
index[i]=i+1; }
for(int i=0;i<n;i++){
int a=p[i],m=i;
for(int j=i;j<n;j++){
if(p[j] > a) {
a=p[j];
m=j;} }
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]); }
int t=0;
for(int i=0;i<n;i++){
t+=b[i]; }