0% found this document useful (0 votes)
15 views31 pages

Os 8 10

Os

Uploaded by

shashankkimarzi
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)
15 views31 pages

Os 8 10

Os

Uploaded by

shashankkimarzi
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/ 31

PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY

Operating System (203105214) B. Tech. 2nd Year

Set 2: Show the use of command line argument for finding the biggest number from two
numbers using if else.

Input:
if [ $# -ne 3 ]; then
echo "usage: $0 num1 num2 num3"
exit 1
fi
num1=$1
num2=$2
num3=$3
if [ $num1 -ge $num2 ]; then
if [ $num1 -ge $num3 ]; then
echo "gretest num is:$num1"
else

echo "gretest num is:$num3"


fi
else
if [ $num2 -ge $num3 ]; then
echo "gretest num is:$num2"
else
echo "gretest num is:$num3"
fi
fi

Output:

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Set 3: Show the use of command line argument for finding the biggest number among three
numbers using nested if else.

Input:
if [ $# -ne 3 ]; then
echo "usage: $0 num1 num2 num3"
exit 1
fi
num1=$1
num2=$2
num3=$3
if [ $num1 -ge $num2 ]; then
if [ $num1 -ge $num3 ]; then
echo "gretest num is:$num1"
else

echo "gretest num is:$num3"


fi
else
if [ $num2 -ge $num3 ]; then
echo "gretest num is:$num2"
else
echo "gretest num is:$num3"
fi
fi

Output:

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

PRACTICAL NO: 9

Definition: Print the pattern using for loop which is used to do the same thing again and
again untilsome condition is satisfied.

For loop is used to do the same thing until some condition is there.

Flowchart:

Set 1: Print following pattern using for loop.


*
**
***
****
*****

Sample Code:
for (( i=1;i<=5;i++ )); do

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

for (( j=1;j<=i;j++ )); do


echo -n "*"
done
echo ""
done

Output:

Set 2: Print following pattern using for loop.


1
23
456
7 8 9 10
Input:
for (( i=1;i<=4;i++ )); do
for (( j=1;j<=i;j++ )); do
echo -n "$num"
(( num++ ))
done
echo ""
done

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Output:

Set 3: Print following pattern using for loop.


1

AB
234
CDEF
56789

Input:
num=1
char=65
for (( i=1;i<=5;i++ )); do

for (( j=1;j<=i;j++ )); do


if (( i%2 == 1 )); then
echo -n "$num"
(( num++ ))
else
echo -n "$( printf \\$(printf '%03o' "$char" ))"

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

(( char++ ))
fi
done
echo ""
done

Output:

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

PRACTICAL NO: 10
Definition: Various commands are available in Linux to check whether the given
directory or fileare exist or not in the system. Several options are also available to
check special conditions like fileis empty or not. We can use these commands in
shell script as well.

Flowchart:

Set 1: Shell script to determine whether given directory, exist or not.


Sample Code:

Input:
read -p "enter the directory path:"dir
if [ -d "$dir" ]; then
echo "directory exists"
else

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

echo "directory does not exists"


fi

Output:

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Set 2: Write a shell script to check, whether the given file exist or not.
Input:
#!/bin/bash
# Prompt user to enter a file name
echo "Enter the name of the file to check:"
read file_name

# Check if file exists


if [ -f "$file_name" ]; then

echo "File '$file_name' exists."


else
echo "File '$file_name' does not exist."
Fi

Output:

Set 3: Write a shell script to check, whether the given file, which should not be empty, exist
or not.

Input:
#!/bin/bash
# Get the filename from the user
echo "Enter the filename:"

read filename

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

# Check if the file exists and is not empty


if [ -e "$filename" ]; then
if [ -s "$filename" ]; then
echo "The file '$filename' exists and is not empty."
else
echo "The file '$filename' exists but is empty."
fi
else

echo "The file '$filename' does not exist."


Fi

Output:

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

PRACTICAL NO: 11
Definition: First come, first served (FCFS) is an operating system process scheduling algorithm and
a network routing management mechanism that automatically executes queued requests and processes
by the order of their arrival.

Flowchart:

Set 1: How to take Arrival Time and Burst Time of the processes.

Sample Code:

#include<stdio.h> int main()

int n,count;

int at[10],bt[10];

printf("Enter Total Process:\t "); scanf("%d",&n); for(count=0;count<n;count++)

{
printf("Enter Arrival Time and Burst Time for Process Number %d :",count+1);
scanf("%d",&at[count]);
Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

scanf("%d",&bt[count]);
}
return 0;
}

Output:

Set 2: How to sort processes based on their Arrival Time for 5 processes.

Sample Code:

#include<stdio.h>

void main()

int i,temp,j,f,array[10]; printf("Enter 5 Processes");

//read array for(i=0;i<5;i++)

scanf("%d",&array[i]);

//bubble sort for(i=0;i<10;i++)

{ f=1;

for(j=0;j<9;j++)

if(array[j]>array[j+1])

if(f==1)

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

break;

printf("after swaping");

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

printf(" %d",array[i]);

Output:

Set 3: Calculate Turn Around Time and Waiting Time.

Input:

#include<stdio.h>

void main() {

int i, j, temp, f, n;

int array[10], bt[10], wt[10], tat[10];

printf("Enter the number of processes (up to 10): ");

scanf("%d", &n);

printf("Enter Burst Times for %d Processes: ", n);

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

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

scanf("%d", &array[i]);

bt[i] = array[i];

for(i = 0; i < n - 1; i++) {

f = 0;

for(j = 0; j < n - 1 - i; j++) {

if(array[j] > array[j + 1]) {

temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

f = 1;

if(f == 0) {

break;

wt[0] = 0;

for(i = 1; i < n; i++) {

wt[i] = bt[i - 1] + wt[i - 1];

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

tat[i] = bt[i] + wt[i];

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround

Time\n"); for(i = 0; i < n; i++) {

printf("P[%d]\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);

float avg_wt = 0.0, avg_tat

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

avg_wt +=

wt[i]; avg_tat

+= tat[i];

avg_wt /=

n; avg_tat

/= n;

printf("\nAverage Waiting Time: %.2f", avg_wt);

printf("\nAverage Turnaround Time: %.2f", avg_tat);

Output:

Enrollment:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

PRACTICAL NO: 12

Definition: Round robin (RR) scheduling is a job-scheduling algorithm that is


considered to be very fair, as it uses time slices that are assigned to each process in
the queue or line. Each process is then allowed to use the CPU for a given amount
of time, and if it does not finish within the allotted time,it is preempted and then
moved at the back of the line so that the next process in line is able to usethe CPU
for the same amount of time.

Flowchart:

Set 1:How to take Arrival Time, Burst Time and Time Quantum of the processes.

Sample Code:

#include <stdio.h>
Int main()
{

Enrollment:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

int n,count, tq; int at[10],bt[10];


printf("Enter Total Process:\t ");
scanf("%d",&n);
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
}
printf("Enter Time Quantum:\t ");
scanf("%d",&tq);
return 0;
}

Output:

Set 2: Calculate Turn Around Time.


Input:
#include <stdio.h>

int main() {

int n, tq, time = 0, remain, flag = 0;

int at[10], bt[10], rt[10];

int wt = 0, tat = 0;

printf("Enter Total Process: ");

scanf("%d", &n);

Enrollment:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

remain = n;

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

printf("Enter Arrival Time and Burst Time for Process Number %d: ", i + 1);

scanf("%d", &at[i]);

scanf("%d", &bt[i]);

rt[i] = bt[i];

printf("Enter Time Quantum: ");

scanf("%d", &tq);

printf("\nProcess | Turnaround Time | Waiting Time\n");

for(int count = 0; remain != 0;) {

if(rt[count] <= tq && rt[count] > 0) {

time += rt[count];

rt[count] = 0;
flag = 1;

} else if(rt[count] > 0) {

rt[count] -= tq;

time += tq;

if(rt[count] == 0 && flag == 1) {

remain--;

int tat_count = time - at[count];

int wt_count = tat_count - bt[count];

printf("P[%d]\t|\t%d\t|\t%d\n", count + 1, tat_count, wt_count);

wt += wt_count;

tat += tat_count;

flag = 0;

count = (count + 1) % n;

printf("\nAverage Waiting Time = %f\n", wt * 1.0 / n);

Enrollment:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

printf("Average Turnaround Time = %f\n", tat * 1.0 / n);

return 0;

Output:

Set 3: Calculate Waiting Time. Find out Average Turnaround Time and Waiting Time.
Input:
#include <stdio.h>
int main() {
int n, tq, time = 0, remain, flag = 0;
int at[10], bt[10], rt[10], wt = 0, tat = 0;
printf("Enter Total Process: ");
scanf("%d", &n);
remain = n;
for(int i = 0; i < n; i++) {
printf("Enter Arrival Time and Burst Time for Process Number %d: ", i + 1);
scanf("%d", &at[i]);
scanf("%d", &bt[i]);
rt[i] = bt[i];
}

Enrollment:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

printf("Enter Time Quantum: ");


scanf("%d", &tq);
printf("\nProcess | Turnaround Time | Waiting Time\n");
for(int count = 0; remain != 0;) {
if(rt[count] <= tq && rt[count] > 0) {
time += rt[count];

rt[count] = 0;
flag = 1;
} else if(rt[count] > 0) {
rt[count] -= tq;
time += tq;
}
if(rt[count] == 0 && flag == 1) {
remain--;
int tat_count = time - at[count];

int wt_count = tat_count - bt[count];


printf("P[%d]\t|\t%d\t|\t%d\n", count + 1, tat_count, wt_count);
wt += wt_count;
tat += tat_count;
flag = 0;
}
count = (count + 1) % n;
}
printf("\nAverage Waiting Time = %.2f\n", (float)wt / n);
printf("Average Turnaround Time = %.2f\n", (float)tat / n);
return 0;

Enrollment:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Output:

Enrollment:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

PRACTICAL NO: 13
Definition: The Banker's algorithm, sometimes referred to as the detection algorithm, is a
resource allocation and deadlock avoidance algorithm. It tests for safety by simulating the
allocation of predetermined maximum possible amounts of all resources. When a new
process enters a system, it must declare the maximum number of instances of each resource
type that it may ever claim; clearly, that number may not exceed the total number of
resources in the system. Also, when a process gets all its requested resources it must return
them in a finite amount of time.

Flow Chart:
Resource-Request Algorithm

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Set 1: How to take no. of processes, no. of resources, maximum resource matrix, allocated
resource matrix and available resources for each process.

Input:

#include <stdio.h> #include <stdlib.h> int main()


{

int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10]; int p, r, i, j, process,


count;
count = 0;
printf("Enter the no of processes : "); scanf("%d", &p);
for(i = 0; i< p; i++) completed[i] = 0;

printf("\n\nEnter the no of resources : "); scanf("%d", &r);


printf("\n\nEnter the Max Matrix for each process : "); for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1); for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : "); for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1); for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : "); for(i = 0; i < r; i++)
scanf("%d", &avail[i]);

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


{
for( j = 0; j < r; j++) printf("%d ", Max[i][j]); printf("\t\t");
for( j = 0; j < r; j++) printf("%d ", alloc[i][j]); printf("\n");

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

}
}

Output:

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Set 2: Find the Need matrix of each process from allocated resources and Maximum
resource matrix.

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

void calculateNeed(int p, int r, int Max[10][10], int alloc[10][10], int need[10][10]) {


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

for (int j = 0; j < r; j++) {


need[i][j] = Max[i][j] - alloc[i][j];
}
}
}

int main() {
int Max[10][10], need[10][10], alloc[10][10];
int p, r, i, j;

printf("Enter the no of processes: ");


scanf("%d", &p);

printf("Enter the no of resources: ");


scanf("%d", &r);

printf("Enter the Max Matrix for each process: ");


for(i = 0; i < p; i++) {
printf("\nFor process %d: ", i + 1);

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

for(j = 0; j < r; j++) {


scanf("%d", &Max[i][j]);
}
}

printf("Enter the allocation for each process: ");


for(i = 0; i < p; i++) {
printf("\nFor process %d: ", i + 1);
for(j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}

calculateNeed(p, r, Max, alloc, need);

printf("\nThe Need Matrix is: \n");


for (i = 0; i < p; i++) {

for (j = 0; j < r; j++) {


printf("%d ", need[i][j]);
}
printf("\n");
}

return 0;
}

Output:

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Set 3: Perform the Banker’s Algorithm and find out that whether the System is Safe or not
and also print the Safe Sequence.
Safety Algorithm:
2.Let Work and Finish be vectors of length m and n, respectively. Initially,
Work = Available

Finish[i] =false for i = 0, 1, ... , n - 1.


This means, initially, no process has finished and the number of available resources is
represented by the Available array.

2. Find an index i such that both

Finish[i] ==false Needi <= Work


If there is no such i present, then proceed to step 4.
It means, we need to find an unfinished process whose need can be satisfied by the available
resources. If no such process exists, just go to step 4.
3. Perform the following:

Work = Work + Allocation; Finish[i] = true;

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Go to step 2.
When an unfinished process is found, then the resources are allocated and the process is
marked finished. And then, the loop is repeated to check the same for all other processes.

4. If Finish[i] == true for all i, then the system is in a safe state.

That means if all processes are finished, then the system is in safe state.

Input:
#include <stdio.h>
#include <stdlib.h>
void calculateNeed(int p, int r, int Max[10][10], int alloc[10][10], int need[10][10]) {
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
need[i][j] = Max[i][j] - alloc[i][j];
}
}
}
int main() {
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSeq[10];
int p, r, i, j, process, count;

count = 0;
printf("Enter the no of processes: ");
scanf("%d", &p);
for(i = 0; i < p; i++)
completed[i] = 0;
printf("Enter the no of resources: ");
scanf("%d", &r);
printf("Enter the Max Matrix for each process: ");
for(i = 0; i < p; i++) {
printf("\nFor process %d: ", i + 1);

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

for(j = 0; j < r; j++)


scanf("%d", &Max[i][j]);
}
printf("Enter the allocation for each process: ");
for(i = 0; i < p; i++) {
printf("\nFor process %d: ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}

printf("Enter the Available Resources: ");


for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
calculateNeed(p, r, Max, alloc, need);
printf("\nThe Need Matrix is: \n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
int Work[r];
for(i = 0; i < r; i++)
Work[i] = avail[i];

int Finish[p];
for(i = 0; i < p; i++)
Finish[i] = 0;
count = 0;
while(count < p) {
int found = 0;

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

for(i = 0; i < p; i++) {


if(Finish[i] == 0) {

int flag = 1;
for(j = 0; j < r; j++) {
if(need[i][j] > Work[j]) {
flag = 0;
break;

}
}
if(flag == 1) {
for(j = 0; j < r; j++)
Work[j] += alloc[i][j];
safeSeq[count++] = i;
Finish[i] = 1;

found = 1;
}
}
}
if(found == 0) {
printf("\nThe system is in an unsafe state!");
return 0;
}
}
printf("\nThe system is in a safe state!\n");
printf("Safe Sequence: ");

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


printf("%d ", safeSeq[i] + 1);
return 0;

Enrollment No:2303031050558
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year

Output:

Enrollment No:2303031050558

You might also like