Os Record
Os Record
AIM-1:
Use the cat commands tp create a file containing the following data. Call it Mytable.txt
use
tabs to separate the fields.
Mytable.txt:
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
A) Use the cat command to display the file. Mytable.txt?
Output:
Command :sec-h@vignan:~$ cat Mytable.txt
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
B) Use the Vicomm and to correct any errors in the file.Mytable.txt?
Output:
Command: vi mytable.txt
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
C) Use the sort command to sort the file Mytable.txt according to the first field?
Output:
Command :sec-h@vignan:~$ sort Mytable.txt
1425 ravi 15.65
1450 raju 21.86
4320 ramu 26.27
6830 sita 36.15
D) Call the sorted file output.txt (different name)
Output:
Command :
sec-h@vignan:~$ sort Mytable.txt > output.txt
sec-h@vignan:~$ sort -o output.txt Mytable.txt
sec-h@vignan:~$ cat output.txt
1425 ravi 15.65
1450 raju 21.86
4320 ramu 26.27
6830 sita 36.15
E) Print the file Mytable.txt?
Output:
Command :sec-H@vignan:~$ ip Mytable.txt
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
F) Use the cut & paste commands to swap fields 2 nd and 3 rd in Mytable.txt. Call it
Mytable.txt?
Output:
Command “ignan@vignan:~$ cut -f1,3 mlq.txt > cat
vignan@vignan:~$ cut -f2 mlq.txt | paste cat -> result
vignan@vignan:~$ cat result
1425 15.65 ravi
4320 26.27 ramu
6830 36.15 sita
1450 21.86 raju
AIM-2.
Write a shell script that displays a list of all the files in the current directory to which the
user has read, write and execute permissions.
Source Code:
#!/bin/bash
echo "List of files in current directory having read,write and execute permissions:"
for file in *
doif [ -f $file ]
then
if [ -r $file -a -w $file -a -x $file ]
then
ls -l $file
fi
fi
done
Output:
List of files in current directory having read,write and execute permissions:
-rwxrw-r-- 1 vignan vignan 37 Feb 3 08:40 a
-rwxrw-r-- 1 vignan vignan 111 Feb 3 08:43 d
-rwxrwxrwx 1 vignan vignan 139 Feb 3 08:59 permission.sh
-rwxrwxrwx 1 vignan vignan 114 Feb 3 08:57 permission.sh~
AIM-3.
Write a shell script that computes the total and average marks of a student according to
the
following;
● Ifaveragemarks≥69thenresultis-
● Ifaveragemarks≥59and≤
● Ifaveragemarks≥49and≤
● Note that any subject marks ≤ 40then result is-Fail .
‖
Accept student name and six subject marks through the keyboard
Source code:
echo "Enter Student name"
read name
echo "Enter Student Registered Number"
read reg
echo "Enter the marks of five subjects : "
read m1 m2 m3 m4 m5
let total=$m1+$m2+$m3+$m4+$m5
let per=(total/5)
echo "Name :$name"
echo "Registered number :$reg"
echo "student total marks:$total"
echo "Student percentage :$per"
if [ $per -ge 69 ]
then
echo "grade :Distinction"
elif [ $per -ge 59-a $per -lt 69 ]
then
echo "Grade :First class"
elif [ $per -ge 49-a $per -lt 59 ]
then
echo "Grade :Pass"
else
echo "Grade :Fail"
fi
Output:
vignan@vignan:~/os$ ./hi.sh
Enter Student name
abc
Enter Student Registered Number
4200
Enter the marks of five subjects :
95 98 78 96 99
Name :abc
Registered number :4200
student total marks:466
Student percentage :93
grade :Distinction
AIM-4.
Simulate the following non-pre-emptive CPU scheduling algorithms to find turnaround
time and waiting time. a. FCFS b. SJF c. Round Robin (pre-emptive).
A. FCFS:
echo -n "Enter process number: "
read n1
BurstTime=()
WaitingTime=()
TurnAroundTime=()
for i in $(seq 1 1 $n1)
do
echo -n "Enter Burst Time for process:"
read bt
BurstTime+=($bt)
done
WaitingTime[0]=0
TurnAroundTime+=${BurstTime[0]
TotalWaitingTime=0
TotalTurnAroundTime=${
for i in $(seq 1 1 $((n1-1)))
do
WaitingTime[$i]=$((${
TurnAroundTime[$i]=$((${
TotalWaitingTime=$(($
TotalTurnAroundTime=$(($
doneAvgWaitingTime=$(($
AvgTurnAroundTime=$(($
echo "PROCESS BURSTTIME WAITINGTIME TURNAROUND TIME"
for(( i=0; i<$n1; i++))
do
echo "p:${i} ${BurstTime[$i]} ${WaitingTime[$i]} $
{TurnAroundTime[$i]}"
done
echo Average Waiting Time : $AvgWaitingTime
echo Average Turn Around Time : $AvgTurnAroundTime
OUTPUT:
vignan@vignan:~/os$ bash fcfs.sh
Enter process number: 3
Enter Burst Time for process:9
Enter Burst Time for process:6
Enter Burst Time for process:8
PROCESS BURSTTIME WAITINGTIME TURNAROUND TIME
p:0 9 0 9
p:1 6 9 15
p:2 8 15 23
Average Waiting Time : 8
Average Turn Around Time : 15
B. SJF:
readarray fileDat < $1
quantum=${fileDat[${#fileDat[@
unset fileDat[${#fileDat[@]}-1]
processCount=${#fileDat[@]}
if [ $quantum -lt 3 ] || [ $quantum -gt 10 ] ; then
echo "ERROR: Quantum must be between 3 to 10"
exit
fi
function printProcess {
process=(${fileDat[$1]})
if [ -z $process ] ; then
return
fi
processName=${process[0]}
arrival=${process[1]}
burst=${process[2]}priority=${process[3]}
echo Process Name: $processName
echo Arrival Time: $arrival
echo Burst Time: $burst
echo Priority: $priority
echo
}
count=0
let end=processCount-1
until [ $count -gt $end ]; do
printProcess $count
let count=count+1
done
echo Quantum: $quantum
echo
echo "~~~ Shortest Job First (SJF) Scheduling ~~~"
echo
sjfDat=("${fileDat[@]}")
shortestBurstIdx=0
currentTime=0
totalTurnaroundTime=0
waitingTime=0
echo "Grantt Chart: "
echo -n $currentTime' '
while [ ${#sjfDat[@]} -gt 0 ] ; do
shortestBurst=99999
count=0
until [ $count -gt $processCount ]; do
process=(${sjfDat[$count]})
if [ -z $process ] ; then
let count=count+1
continue
fi
burst=${process[2]}
if [ $burst -lt $shortestBurst ]; then
shortestBurst=$burst
shortestBurstIdx=$count
filet count=count+1
done
chosenProcess=(${sjfDat[$
processName=${chosenProcess[0]
arrival=${process[1]}
burst=${chosenProcess[2]}
let currentTime=currentTime+burst
echo -n [$processName] $currentTime' '
let turnaroundTime=currentTimelet waitingTime=waitingTime+
let totalTurnaroundTime=
unset sjfDat[$shortestBurstIdx]
done
let avgWaitingTime=waitingTime/
let avgTurnAroundTime=
echo "Total Turnaround Time :" $totalTurnaroundTime
echo "Average Turnaround Time :" $avgTurnAroundTime
echo "Total Waiting Time :" $waitingTime
echo "Average Waiting Time :" $avgWaitingTime
input.txt:
P1 2 6 7
P2 1 8 1
P3 18 4 2
P4 2 2 5
4
vignan@vignan:~/os$ ./sjf.sh input.txt
Process Name: P1
Arrival Time: 2
Burst Time: 6
Priority: 7
Process Name: P2
Arrival Time: 1
Burst Time: 8
Priority: 1
Process Name: P3
Arrival Time: 18
Burst Time: 4
Priority: 2Process Name: P4
Arrival Time: 2
Burst Time: 2
Priority: 5
Quantum: 4
Shortest Job First (SJF) Scheduling
Grantt Chart:
0 [P4] 2 [P3] 6 [P1] 12 [P2] 20 Total Turnaround Time : 40
Average Turnaround Time : 10
Total Waiting Time : 20
Average Waiting Time : 5
C. Round Robin:
echo Enter number of process:
read n
echo Enter quantum time:
read qt
echo Enter the burst time for each process:
for i in $(seq 1 1 $n)
do
echo -n Process $i : burst time:
read bt[i]
rbt[i]=${bt[i]}
done
p=$n
pt=0
while [[ $p>0 ]]
do
for i in $(seq 1 1 $n)
do
if [[ ${rbt[i]} -gt 0 ]]
then
if [[ ${rbt[i]} -le $qt ]]
then
pt=$((pt+rbt[i]))
rbt[i]=0
tat[i]=$pt
wt[i]=$((pt-bt[i]))
p=$((p-1))else
rbt[i]=$((rbt[i]-qt))
pt=$((pt+qt))
fi
fi
done
done
for i in $(seq 1 1 $n)
do
echo process $i :waiting time ${wt[i]} turnaround time ${tat[i]}
done
OUTPUT:
vignan@vignan:~/os$ bash rr.sh
Enter number of process:
3
Enter quantum time:
2
Enter the burst time for each process:
Process 1 : burst time:10
Process 2 : burst time:6
Process 3 : burst time:3
process 1 :waiting time 9 turnaround time 19
process 2 :waiting time 9 turnaround time 15
process 3 :waiting time 8 turnaround time 11
AIM-5:
Write a shell script that accepts a file name starting and ending line numbers as
arguments and displays all the lines between the given line numbers.
SOURCE CODE:
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: $0 filename1 (filenames...)"
exit 1
fi
case $choice in
# Copy file
1)
read -p "enter the source file name:" src
if [ ! -f "$src" ]; then
echo "enter: file '$src' does not exist"
continue
fi
read -p "enter the destination file name:" dest
cp "$src" "$dest"
echo "file copied from '$src' to '$dest'"
;;
# Remove file
2)
read -p "enter the file name to remove:" file
if [ ! -f "$file" ]; then
echo "error: file '$file' does not exist"
continue
fi
rm "$file"
echo "file '$file' has been removed"
;;
# Rename file
3)
read -p "enter the current file name:" oldname
if [ ! -f "$oldname" ]; then
echo "error: file '$oldname' does not exist"
continue
fi
read -p "enter the new file name:" newname
mv "$oldname" "$newname"
echo "file renamed from '$oldname' to '$newname'"
;;
# Symbolic link
4)
read -p "enter the source file name:" src
if [ ! -f "$src" ]; then
echo "error: file '$src' does not exist"
continue
fi
read -p "enter symbolic linkname:" linkname
ln -s "$src" "$linkname"
echo "symbolic link '$linkname' created for '$src'"
;;
# Exit
5)
echo "exiting the Program"
exit 0
;;
*)
echo "invalid choice, please enter a number between 1-5"
;;
esac
done
OUTPUT:
Choose an operation:
1)copy a file
2)remove a file
3)rename a file
4)create a symbolic link
5)exit
Enter your choice(1-5):1
Enter the source file name:backup.txt
Enter the destination file name:backup.txt
File copied from “example.txt” to “backup.txt”
AIM-7:
Write a shell script, which receives two file names as arguments. It should check whether
the two file contents are same or not. If they are same then second file should be deleted.
SOURCE CODE:
if [ $# -ne 2 ]; then
echo "Usage: $0 <file1> <file2>"
exit 1
fi
file1="$1"
file2="$2"
filename="$1"
start_line="$2"
end_line="$3"
if [ ! -f "$filename" ]; then
echo "Error: file '$filename' not found"
exit 1
fi
word="$1"
shift
AIM-10:
Implementation of new process creation and its communications.
SOURCE CODE:
Scc process-creation.c-o process-creation
./process_creation
OUTPUT:
Parent process(pid):12345,
childprocess:12346
AIM-11:
Implement of thread creation and deletion.
SOURCE CODE:
Gcc –pthread thread_creation. C-o thread_creation
./thread_creation
OUTPUT:
Hello from thread
AIM-12:
Implementation of producer consumer problem.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces"
"item %d",
x);
++mutex;
}void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
for (i = 1; i > 0; i++) {
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)
&& (empty != 0)) {
producer();
}
else {
printf("Buffer is full!");
}
break;
case 2:
if ((mutex == 1)
&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!");}
break;
case 3:
exit(0);
break;
}
}
}
OUTPUT:
vignan@vignan:~$ ./a.out
1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:2
Buffer is empty!
Enter your choice:1
Producer producesitem 1
Enter your choice:1
Producer producesitem 2
Enter your choice:1
Producer producesitem 3
Enter your choice:2
Consumer consumes item 3
Enter your choice:2
Consumer consumes item 2
Enter your choice:2
Consumer consumes item 1
Enter your choice:2
Buffer is empty!
Enter your choice:2
Buffer is empty!
Enter your choice
AIM-13:
Implementation of Banker’s algorithm for Dead lock avoidance.
SOURCE CODE:
#include <stdio.h>
int m, n, i, j, al[10][10], max[10][10], av[10], need[10][10], temp, z, y, p, k;
void main() {
printf("\n Enter no of processes : ");
scanf("%d", &m);
printf("\n Enter no of resources : ");
scanf("%d", &n);
p = 1;
y = 0;
while (p != 0) {
for (i = 0; i < m; i++) {
z = 0;
for (j = 0; j < n; j++) {
if (need[i][j] <= av[j] && (need[i][0] != -1)) {
z++;
}
}
if (z == n) {
for (k = 0; k < n; k++) {
av[k] += al[i][k];
}
printf("\n SS process %d", i);
need[i][0] = -1;
y++;
}
}
if (y == m) {
p = 0;
}}
printf("\n");
}
OUTPUT:
vignan@vignan:~$ ./a.out
Enter no of processes : 3
Enter no of resources : 2
Enter instances for al[0][0] = 1
Enter instances for al[0][1] = 2
Enter instances for al[1][0] = 1
Enter instances for al[1][1] = 2
Enter instances for al[2][0] = 1
Enter instances for al[2][1] = 2
Enter instances for max[0][0] = 1
Enter instances for max[0][1] = 2
Enter instances for max[1][0] = 1
Enter instances for max[1][1] = 2
Enter instances for max[2][0] = 1
Enter instances for max[2][1] = 2
Available Resource for av[0] = 2
Available Resource for av[1] = 2
Alocation Values : 1 2 1 2 1 2
Max Values : 1 2 1 2 1 2
Need Values : 0 0 0 0 0 0 SS
process 0 SS
process 1 SS
process 2
MODULE-2
1.Aim :- scan
Code :-
#include <stdio.h>
#include <stdlib.h>
void sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void scan(int requests[], int n, int head, int direction) {
int seek_sequence[100], index = 0, distance, total_head_movement = 0;
sort(requests, n);
seek_sequence[index++] = head;
if (direction == 1) {
for (int i = 0; i < n; i++) {
if (requests[i] >= head) {
seek_sequence[index++] = requests[i];
}
}
seek_sequence[index++] = 199;
for (int i = n - 1; i >= 0; i--) {
if (requests[i] < head) {
seek_sequence[index++] = requests[i];
}
}
} else {
for (int i = n - 1; i >= 0; i--) {
if (requests[i] <= head) {
seek_sequence[index++] = requests[i];
}
}
seek_sequence[index++] = 0;
for (int i = 0; i < n; i++) {
if (requests[i] > head) {
seek_sequence[index++] = requests[i];
}
}
}
for (int i = 0; i < index - 1; i++) {
distance = abs(seek_sequence[i + 1] - seek_sequence[i]);
total_head_movement += distance;
}
printf("Seek Sequence: ");
for (int i = 0; i < index; i++) {
printf("%d ", seek_sequence[i]);
}
printf("\nTotal Head Movement: %d\n", total_head_movement);
}
int main() {
int requests[] = { 98, 183, 37, 122, 14, 124, 65, 67 };
int n = sizeof(requests) / sizeof(requests[0]);
int head = 53;
int direction = 1;
scan(requests, n, head, direction);
return 0;
}
Output:-
2
Aim :- c-scan
Code :-
#include <stdio.h>
#include <stdlib.h>
void sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void cscan(int requests[], int n, int head, int direction) {
int seek_sequence[100], index = 0, distance, total_head_movement = 0;
sort(requests, n);
seek_sequence[index++] = head;
if (direction == 1) {
for (int i = 0; i < n; i++) {
if (requests[i] >= head) {
seek_sequence[index++] = requests[i];
}
}
seek_sequence[index++] = 199;
seek_sequence[index++] = 0;
for (int i = 0; i < n; i++) {
if (requests[i] < head) {
seek_sequence[index++] = requests[i];
}
}
} else {
for (int i = n - 1; i >= 0; i--) {
if (requests[i] <= head) {
seek_sequence[index++] = requests[i];
}
}
seek_sequence[index++] = 0;
seek_sequence[index++] = 199;
for (int i = n - 1; i >= 0; i--) {
if (requests[i] > head) {
seek_sequence[index++] = requests[i];
}
}
}
4.
Aim :- c-look
Code :-
#include <stdio.h>
#include <stdlib.h>
void sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void clook(int requests[], int n, int head, int direction) {
int seek_sequence[100], index = 0, distance, total_head_movement = 0;
sort(requests, n);
seek_sequence[index++] = head;
if (direction == 1) {
for (int i = 0; i < n; i++) {
if (requests[i] >= head) {
seek_sequence[index++] = requests[i];
}
}
if (index == 1) {
seek_sequence[index++] = requests[0];
} else {
seek_sequence[index++] = requests[n - 1];
for (int i = 0; i < n; i++) {
if (requests[i] < head) {
seek_sequence[index++] = requests[i];
}
}
}
} else {
for (int i = n - 1; i >= 0; i--) {
if (requests[i] <= head) {
seek_sequence[index++] = requests[i];
}
}
if (index == 1) {
seek_sequence[index++] = requests[n - 1];
} else {
seek_sequence[index++] = requests[0];
for (int i = n - 1; i >= 0; i--) {
if (requests[i] > head) {
seek_sequence[index++] = requests[i];
}
}
}
}
for (int i = 0; i < index - 1; i++) {
distance = abs(seek_sequence[i + 1] - seek_sequence[i]);
total_head_movement += distance;
}
printf("Seek Sequence: ");
for (int i = 0; i < index; i++) {
printf("%d ", seek_sequence[i]);
}
printf("\nTotal Head Movement: %d\n", total_head_movement);
}
int main() {
int requests[] = { 98, 183, 37, 122, 14, 124, 65, 67 };
int n = sizeof(requests) / sizeof(requests[0]);
int head = 53;
int direction = 1;
clook(requests, n, head, direction);
return 0;
}
Output:-
5.
Aim :- FCFS
Code :-
#include <stdio.h>
#include <stdlib.h>
void fcfs(int requests[], int n, int head) {
int seek_sequence[100], index = 0, distance, total_head_movement = 0;
seek_sequence[index++] = head;
for (int i = 0; i < n; i++) {
seek_sequence[index++] = requests[i];
}
for (int i = 0; i < index - 1; i++) {
distance = abs(seek_sequence[i + 1] - seek_sequence[i]);
total_head_movement += distance;
}
printf("Seek Sequence: ");
for (int i = 0; i < index; i++) {
printf("%d ", seek_sequence[i]);
}
printf("\nTotal Head Movement: %d\n", total_head_movement);
}
int main() {
int requests[] = { 98, 183, 37, 122, 14, 124, 65, 67 };
int n = sizeof(requests) / sizeof(requests[0]);
int head = 53;
fcfs(requests, n, head);
return 0;
}
Output:-
6.
Aim :- sstf
Code :-
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void sstf(int requests[], int n, int head) {
bool visited[100] = {false};
int seek_sequence[100], index = 0, total_head_movement = 0;
int current_position = head;
seek_sequence[index++] = current_position;
PAGE REPLACEMENTS
7.
Aim :- FIFO
Code :-
#include <stdio.h>
int isPageInFrames(int frames[], int capacity, int page) {
for (int i = 0; i < capacity; i++) {
if (frames[i] == page) return 1;
}
return 0;
}
int fifoPageReplacement(int pages[], int n, int capacity) {
int frames[capacity];
int front = 0;
int page_faults = 0;
for (int i = 0; i < capacity; i++) {
frames[i] = -1;
}
for (int i = 0; i < n; i++) {
if (!isPageInFrames(frames, capacity, pages[i])) {
frames[front] = pages[i];
front = (front + 1) % capacity;
page_faults++;
}
printf("\nStep %d: ", i + 1);
for (int j = 0; j < capacity; j++) {
if (frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
}
return page_faults;
}
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3, 5, 1, 3, 6, 3};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3;
printf(" FIFO Page Replacement Simulation\n");
printf("===================================\n");
int page_faults = fifoPageReplacement(pages, n, capacity);
printf("\n\nTotal Page Faults: %d\n", page_faults);
return 0;
}
Output:-
8.
Aim :- optimal page replacement
Code :-
#include <stdio.h>
#define MAX_FRAMES 10
#define MAX_PAGES 50
int isPageInFrame(int frames[], int num_frames, int page) {
for (int i = 0; i < num_frames; i++) {
if (frames[i] == page) {
return 1;
}
}
return 0;
}
int findOptimalReplacement(int frames[], int num_frames, int pages[], int num_pages, int
current_index) {
int farthest = -1, replace_index = -1;
9.
Aim :- LRU
Code :- #include <stdio.h>
int isPageInFrames(int frames[], int capacity, int page) {
for (int i = 0; i < capacity; i++) {
if (frames[i] == page) return i;
}
return -1;
}
int findLRU(int time[], int capacity) {
int min = time[0], pos = 0;
for (int i = 1; i < capacity; i++) {
if (time[i] < min) {
min = time[i];
pos = i;
}
}
return pos;
}
int lruPageReplacement(int pages[], int n, int capacity) {
int frames[capacity], time[capacity];
int page_faults = 0, count = 0;
10.
Aim :- second chance algorithm
Code :-
#include <stdio.h>
#define MAX_FRAMES 3
struct Page {
int pageNumber;
int referenceBit;
};
struct Page frames[MAX_FRAMES];
int findPage(int page, int frameCount) {
for (int i = 0; i < frameCount; i++) {
if (frames[i].pageNumber == page) {
frames[i].referenceBit = 1;
return 1;
}
}
return 0;
}
void secondChance(int pages[], int n) {
int frameCount = 0, pointer = 0, pageFaults = 0;
for (int i = 0; i < n; i++) {
if (!findPage(pages[i], frameCount)) {
pageFaults++;