OS PRactical Prep
OS PRactical Prep
#include<stdio.h>
int main()
{
int AT[10],BT[10],WT[10],TT[10],n;
int burst=0,cmpl_T;
float Avg_WT,Avg_TT,Total=0;
printf("Enter number of the process\n");
scanf("%d",&n);
printf("Enter Arrival time and Burst time of the process\n");
printf("AT\tBT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&AT[i],&BT[i]);
}
// printing of outputs
#include<stdio.h>
struct process
{
int id,WT,AT,BT,TAT;
};
struct process a[10];
//Driver function
int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of process \n");
scanf("%d",&n);
printf("Enter the Arrival time and Burst time of the process\n");
printf("AT BT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].AT,&a[i].BT);
a[i].id=i+1;
// here we are checking that arrival time
// of the process are same or different
if(i==0)
check_ar=a[i].AT;
if(check_ar!=a[i].AT )
check_ar=1;
}
// if process are arrived at the different time
// then sort the process on the basis of AT
if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
}
}
}
}
}
}
}
a[i].WT=Cmp_time-a[i].AT;
}
}
Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;
#include<stdio.h>
struct process
{
int WT,AT,BT,TAT;
};
int main()
{
int n,temp[10];
int count=0,t=0,short_P;
float total_WT=0, total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n);
printf("Enter the arrival time and burst time of the process\n");
printf("AT WT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].AT,&a[i].BT);
a[short_P].BT=a[short_P].BT-1;
// total calculation
total_WT=total_WT+a[short_P].WT;
total_TAT=total_TAT+a[short_P].TAT;
}
}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
#include<stdio.h>
struct process
{
int id,WT,AT,BT,TAT,PR;
};
struct process a[10];
//Driver function
int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of process \n");
scanf("%d",&n);
printf("Enter the Arrival time , Burst time and priority of the process\n");
printf("AT BT PR\n");
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PR);
a[i].id=i+1;
// here we are checking that arrival time
// of the process are same or different
if(i==0)
check_ar=a[i].AT;
if(check_ar!=a[i].AT )
check_ar=1;
}
// if process are arrived at the different time
// then sort the process on the basis of AT
if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
swap(&a[j].PR,&a[j+1].PR);
}
}
}
}
}
a[i].WT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;
}
}
}
a[i].WT=Cmp_time-a[i].AT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;
Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;
#include<stdio.h>
struct process
{
int WT,AT,BT,TAT,PT;
};
int main()
{
int n,temp[10],t,count=0,short_p;
float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n);
printf("Enter the arrival time , burst time and priority of the process\n");
printf("AT BT PT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);
for(t=0;count!=n;t++)
{
short_p=9;
for(int i=0;i<n;i++)
{
if(a[short_p].PT>a[i].PT && a[i].AT<=t && a[i].BT>0)
{
short_p=i;
}
}
a[short_p].BT=a[short_p].BT-1;
// total calculation
total_WT=total_WT+a[short_p].WT;
total_TAT=total_TAT+a[short_p].TAT;
}
}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y
// Use for loop to enter the details of the process like Arrival time and the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}
fork() system call code:-
#include <bits/stdc++.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
int main() {
int n;
cout<<"Enter number of elements in array: ";
cin>>n;
vector<int> arr(n); //Used for creating an array
cout<<"Enter array elements: ";
for(int i = 0;i < n;i++) cin>>arr[i];
int p = fork();
if(p > 0) {
//asc code
for(int i = 0;i < n;i++) {
for(int j = i+1;j < n;j++) {
if(arr[i] > arr[j])
swap(arr[i],arr[j]);
}
}
for(int i = 0;i < n;i++) {
cout<<arr[i]<<" Printed by pid: "<<getpid()<<" with ppid: "<<getppid()<<endl;
sleep(1);
}
sleep(100); // for making child process a zombie process
wait(NULL); // For waiting until child terminates
cout<<"Parent Terminating"<<endl;
}
else if(p == 0) {
//desc code
for(int i = 0;i < n;i++) {
for(int j = i+1;j < n;j++) {
if(arr[i] < arr[j])
swap(arr[i],arr[j]);
}
}
for(int i = 0;i < n;i++) {
cout<<"\t\t"<<arr[i]<<" Printed by pid: "<<getpid()<<" with ppid: "<<getppid()<<endl;
sleep(2);
}
cout<<"Child Terminating"<<endl;
}
}