0% found this document useful (0 votes)
5 views32 pages

23SE02IE043-Final OS

The document contains practical assignments for a Computer Organization course, detailing various programming tasks and Linux commands. It includes scripts for calculating factorials, generating Fibonacci series, and performing basic arithmetic operations, as well as explanations of CPU scheduling algorithms and memory allocation techniques. Additionally, it covers practical exercises related to operating systems, such as simulating page replacement algorithms and resource allocation strategies.

Uploaded by

rishimalaviya05
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)
5 views32 pages

23SE02IE043-Final OS

The document contains practical assignments for a Computer Organization course, detailing various programming tasks and Linux commands. It includes scripts for calculating factorials, generating Fibonacci series, and performing basic arithmetic operations, as well as explanations of CPU scheduling algorithms and memory allocation techniques. Additionally, it covers practical exercises related to operating systems, such as simulating page replacement algorithms and resource allocation strategies.

Uploaded by

rishimalaviya05
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/ 32

Desai Krunal 23SE02IE043

Computer Organization SEIT2230

Practical -1
Desai Krunal 23SE02IE043
Computer Organization SEIT2230

Practical -1

2. Factorial of a number
Input:-

Output:-
Desai Krunal 23SE02IE043
Computer Organization SEIT2230

Practical -1

3. Check Prime number


Input:-

Output:-
Operating System 23SE02IE043 SEIT2230
Krunal Desai

Practical 1
Basic linux commands

1. lS command : This command use for all the list show.

2. mkdir : this command is use for a make directory.

3. CD : this command is use for a enter the make directory.

4. rmdir : This command is use for a delete the any directory or


File.
5. touch : this command is use for a make file.

6. cat : this command is use for a enter the file or directory.

7. echo : this command is use for a print the any massage.

8. cp : this command is use for copy any file.

9. mv : this command is use for a cut any file and past.

10. pwd : this command is use for a you are where working.
Operating System 23SE02IE043 SEIT2230
Krunal Desai
Practical:-2
Krunal Desai 23SE02IE043
Opreting system SEIT2230

touch : this command use for make file.

echo : this command use for print any message.

cat : this command use for enter the any file.


cat log.txt
cat > log.txt
cat >> log.txt

grep: find any error from the file.

Find : The find command in Linux is a dynamic utility designed for


comprehensive file and directory searches within a hierarchical
structure

Man : to display the user manual of any command that we can


run on the terminal

Wc: to find out number of lines, word count, byte and


characters count in the files specified in the file arguments.
Practical:-2
Krunal Desai 23SE02IE043
Opreting system SEIT2230

Zip: cambain file

chmod: modifies the read, write, and execute permissions of


specified files and the search permissions of specified directories
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-3
1.Addition of two numbers:
echo "Enter the first number : "
read num1 echo "Enter the
second number : " read num2
sum=$((num1 + num2)) echo
"sum of two value is $sum"

subtraction of two numbers:

echo "Enter the first number : "


read num1 echo "Enter the
second number : " read num2
sum=$((num1 - num2)) echo
"sum of two value is $sum"
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-3

AVARAGE OF TWO NUMbERS:


echo "Enter the first number : "
read num1
echo "Enter the second number : "
read num2 sum=$((num1 - num2
/ 2)) echo "sum of average is
$sum"

Parsentage of two numbers:


echo "Enter the first number : "
read num1
echo "Enter the second number : "
read num2
percentage=$(echo "scale=2; $num1 / $num2 * 100") echo
"the percentage of $num1 out of $sum2 is $percentage%"
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-3

2.Swapping two number:

a=20 b=$a echo


"swapping $b"

3.even and odd number:


echo "Enter the Number"
read n r=`expr $n % 2`
if [ $r -eq 0 ] then
echo "$n is Even number"
else
echo "$n is Odd number"
fi
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-3

4.finding greatest of three numbers:

echo "Enter Num1"


read num1 echo
"Enter Num2" read
num2 echo "Enter
Num3" read num3

if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]


then echo $num1
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then echo $num2 else echo $num3
fi

5. two numbers choices calculator


add() {
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-3
echo "Result: $(($1 + $2))"
}

subtract() { echo "Result:


$(($1 - $2))"
}

multiply() { echo "Result:


$(($1 * $2))"
}

divide() { if [ $2 -
eq 0 ]; then
echo "Error: Division by zero is not allowed!"
else
echo "Result: $(($1 / $2))"
fi
}

echo "Select an operation:"


echo "1. Add" echo "2.
Subtract" echo "3.
Multiply" echo "4. Divide"
echo "5. Exit"
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-3

read -p "Enter your choice (1-5): " choice

if [ "$choice" -ge 1 ] && [ "$choice" -le 4 ]; then


read -p "Enter first number: " num1 read -p
"Enter second number: " num2 fi

case $choice in
1) add $num1 $num2 ;;
2) subtract $num1 $num2 ;;
3) multiply $num1 $num2 ;;
4) divide $num1 $num2 ;;
5) echo "Exiting..."; exit 0 ;;
*) echo "Invalid choice, please select between 1 to 5." ;; esac
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical - 5

1.Write a shell script to calculate the factorial of a number.

#!/bin/bash

# Function to calculate factorial


factorial() {
local num=$1
if [ $num -eq 0 ] || [ $num -eq 1 ]; then
echo 1
else
echo $(( num * $(factorial $((num - 1))) ))
fi
}

# Read input from the user


read -p "Enter a number: " number

# Validate the input


if [[ $number =~ ^[0-9]+$ ]]; then
result=$(factorial $number)
echo "The factorial of $number is $result"
else
echo "Please enter a valid non-negative integer."
fi
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical - 5

2.Write a shell script to generate the Fibonacci series.

#!/bin/bash

# Script to generate the Fibonacci series

# Prompt the user for the number of terms


read -p "Enter the number of terms in the Fibonacci series: " n

# Check if the input is a valid positive integer


if ! [[ "$n" =~ ^[0-9]+$ ]] || [ "$n" -le 0 ]; then
echo "Please enter a valid positive integer."
exit 1
fi

# Initialize the first two terms


a=0
b=1
echo "Fibonacci series up to $n terms:"

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


do
echo -n "$a "
# Calculate the next term
temp=$((a + b))
a=$b
b=$temp
done
echo
23SE02IT005 Operating System

Practical 4.1

1. Simulate the First-Come-First-Serve (FCFS) CPU scheduling


algorithm, calculate waiting time and turnaround time.
Input:
23SE02IT005 Operating System

Output :
23SE02IE043 SEIT2230 OPERATING SYSTEM

Simulate Non-Preemptive SJF CPU Scheduling Algorithm.


23SE02IE043 SEIT2230 OPERATING SYSTEM

Output:-
23SE02IE043 Desai Krunal
Practical 5.1

Simulate Worst-Fit Algorithm for Contiguous Memory Allocation


Conceptual Understanding of Worst Fit Memory Allocation
Key Characteristics

 Contiguous Allocation: Processes are allocated memory in contiguous blocks.


 Worst Fit: The process is allocated the largest free partition that is sufficient to
accommodate it.
 Fragmentation Handling: Leaves large leftover memory chunks, potentially reducing
fragmentation.

Terminologies
 Memory Block: A fixed-size contiguous region of memory available for allocation.
 Process Size: The amount of memory required by a process.
 Allocation: Assigning a memory block to a process.
 Fragmentation: Wasted memory due to inefficient allocation.
 Deallocation: Releasing memory when a process completes execution.
 Splitting: If a memory block is larger than required, it is split into allocated and free
sections.
 Compaction: Merging scattered free memory to form larger blocks.

Advantages

 Helps in reducing external fragmentation by keeping larger free blocks available.


 Works well for processes that may grow dynamically.
 Ensures large memory chunks remain available for future allocations.

Disadvantages
 High memory wastage if a large block is allocated to a small process.
 Higher processing time due to searching for the largest block.
 Leads to inefficient space utilization over time.
23SE02IE043 Desai Krunal
Practical 5.1
Implementation Steps
Step 1: Input the Number of Memory Blocks and Their Sizes
 Take input for total available memory.
 Store memory block sizes in an array.
Step 2: Input the Number of Processes and Their Memory Requirements
 Take input for the number of processes.
 Store process memory requirements in an array.
Step 3: Worst Fit Allocation Logic
 Sort free blocks in descending order.
 For each process:
o Find the largest available block that can accommodate it.
o If found:
Allocate memory.
If extra space remains, split the block.
o If not found:
Mark process as unallocated.
Step 4: Deallocate Memory and Handle Fragmentation
 When a process finishes, mark its memory as free.
 Merge adjacent free blocks if needed.
Step 5: Display Results
 Print a table showing:
o Process ID
o Required Memory
o Allocated Block Size
 Show the updated memory block status.
Step 6: Calculate and Display Statistics
 Total memory used.
 Total memory wasted (fragmentation).
 Number of unallocated processes.
23SE02IE043 Desai Krunal
Practical 5.1
Sample Input

Memory Blocks (in KB): [100, 500, 200, 300, 600]


Processes (in KB): [212, 417, 112, 426]

Step-by-Step Allocation Process


Initial Memory Blocks
Block No. Block Size (KB) Status

1 100 Free

2 500 Free

3 200 Free

4 300 Free

5 600 Free

Sorting Blocks in Descending Order for Worst Fit


Sorted blocks: [600, 500, 300, 200, 100]
Process Allocation (Using Worst-Fit)
Process ID Process Size (KB) Allocated Block Remaining Block Size
1 212 600 KB 388 KB Left
2 417 500 KB 83 KB Left
3 112 388 KB 276 KB Left
4 426 Not Allocated -

Final Memory Block Status

Block No. Block Size (KB) Status


1 100 Free
2 500 417 Allocated, 83 Free
3 200 Free
4 300 Free
5 600 212 Allocated, 388 Left (112 Allocated, 276 Left)
Unallocated Process: Process 4 (426 KB)
23SE02IE043 Desai Krunal
Practical 5.1
Final Output
Memory Block Status After Worst-Fit Allocation:
Block 1: 100 KB -> Free
Block 2: 500 KB -> 417 KB Allocated, 83 KB Free
Block 3: 200 KB -> Free
Block 4: 300 KB -> Free
Block 5: 600 KB -> 212 KB Allocated, 388 KB Left (112 KB Allocated, 276 KB Free)

Process Allocation:
Process 1 (212 KB) -> Block 5 (600 KB) [388 KB Left]
Process 2 (417 KB) -> Block 2 (500 KB) [83 KB Left]
Process 3 (112 KB) -> Block 5 (388 KB) [276 KB Left]
Process 4 (426 KB) -> Not Allocated

Total Memory Wasted (Fragmentation): 100 + 83 + 200 + 300 + 276 = 959 KB


23SE02IE043 SEIT2230 OPERATING SYSTEM

Practical 6

#CODE

#include<stdio.h>

int main() {

int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5];

printf("Enter the number of processes and resources: ");

scanf("%d %d", &p, &c);

printf("Enter the allocation matrix:\n");

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

for (j = 0; j < c; j++) scanf("%d", &alc[i][j]);

printf("Enter the maximum matrix:\n");

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

for (j = 0; j < c; j++) scanf("%d", &max[i][j]);

printf("Enter available resources: ");

for (i = 0; i < c; i++) scanf("%d", &available[i]);

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

for (j = 0; j < c; j++) need[i][j] = max[i][j] - alc[i][j];

for (i = 0; i < p; i++) done[i] = 0;

do {

int processExecuted = 0;

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

if (!done[i]) {

int canExecute = 1;

for (j = 0; j < c; j++) if (need[i][j] > available[j]) canExecute = 0;

if (canExecute) {

safe[count++] = i;

done[i] = 1;

for (j = 0; j < c; j++) available[j] += alc[i][j];


23SE02IE043 SEIT2230 OPERATING SYSTEM

processExecuted = 1;

if (!processExecuted) {

printf("Safe sequence does not exist (deadlock detected)\n");

break;

} while (count < p);

if (count == p) {

printf("\nAvailable resources after completion:\n");

for (i = 0; i < c; i++) printf("%d\t", available[i]);

printf("\nSafe sequence:\n");

for (i = 0; i < p; i++) printf("p%d\t", safe[i]);

printf("\n");

return 0;

}
23SE02IE043 SEIT2230 OPERATING SYSTEM

Type your text


23SE02IE043 SEIT2230 OPERATING SYSTEM

OUTPUT:-
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical – 7.1

❖ Simulate FIFO Page Replacement Algorithm

#include<stdio.h> int main()


{
int i,j,n,a[50],frame[10],no,k,avail,count=0; printf("\n ENTER THE
NUMBER OF PAGES:\n"); scanf("%d",&n); printf("\n ENTER THE
PAGE NUMBER :\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no); for(i=0;i<no;i++) frame[i]= -1; j=0;
printf("\tref string\t page frames\n"); for(i=1;i<=n;i++)
{ printf("%d\t\t",a[i]);
avail=0; for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1; if
(avail==0)
{ frame[j]=a[i];
j=(j+1)%no; count++;
for(k=0;k<no;k++) printf("%d\t",frame[k]);
} printf("\n");
}
printf("Page Fault Is %d",count); return 0;
}
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical – 7.1
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-7.2

• Simulate the Least Recently Used (LRU)/Optimal


page replacement algorithm with a page reference
string.

#include<stdio.h> int findLRU(int


time[], int n){ int i, minimum =
time[0], pos = 0; for(i = 1; i < n;
++i){ if(time[i] < minimum){
minimum = time[i]; pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0,
time[10], flag1, flag2, i, j, pos, faults = 0; printf("Enter number of
frames: "); scanf("%d", &no_of_frames); printf("Enter number of
pages: "); scanf("%d", &no_of_pages); printf("Enter reference
string: "); for(i = 0; i < no_of_pages; ++i){ scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0; for(j = 0; j <
no_of_frames; ++j){ if(frames[j]
== pages[i]){ counter++;
time[j] = counter; flag1 = flag2 =
1; break;
}
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-7.2
}
if(flag1 == 0){ for(j = 0; j <
no_of_frames; ++j){ if(frames[j]
== -1){ counter++; faults++;
frames[j] = pages[i]; time[j] =
counter; flag2 = 1; break;
}
}
}
if(flag2 == 0){ pos = findLRU(time,
no_of_frames); counter++;
faults++; frames[pos] = pages[i];
time[pos] = counter;
} printf("\n"); for(j = 0; j <
no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults); return
0;
}
Desai Krunal 23SE02IE043
Operating System SEIT2230

Practical-7.2

You might also like