OperatingSystem Lab - File Final
OperatingSystem Lab - File Final
OperatingSystem Lab - File Final
Serial
Main Content Date
Number
Write a shell script that accepts a file name starting and ending line
4.
numbers as argumentsand display all the lines between given line
no.
5. Write a shell script that deletes all lines containing a specified word.
6. Write a shell script that displays a list of all the files in the current
directory
7. Simulation of Unix commands using C.
Command Description
cd Change directory
cp Copy file
mv Move file
rm Remove file
touch file1.txt
List Files
To list the files in the current directory, use the ls command. For example, to list the files in the current
directory, type:
ls
To view the content of a file, use the cat command. For example, to view the content of a file named
file1.txt , type:
cat file1.txt
Copy a File
To copy a file, use the cp command. For example, to copy a file named file1.txt to a file named file2.txt
, type:
cp file1.txt file2.txt
Move a File
To move a file, use the mv command. For example, to move a file named file1.txt to a file named
file2.txt , type:
mv file1.txt file2.txt
Remove a File
To remove a file, use the rm command. For example, to remove a file named file1.txt , type:
rm file1.txt
Create a Directory
To create a directory, use the mkdir command. For example, to create a directory named dir1 , type:
mkdir dir1
List Directories
To list the directories in the current directory, use the ls command. For example, to list the directories in the
current directory, type:
ls -d */
Change Directory
To change the current directory, use the cd command. For example, to change the current directory to dir1 ,
type:
cd dir1
Remove a Directory
To remove a directory, use the rmdir command. For example, to remove a directory named dir1 , type:
rmdir dir1
Print Working Directory
To print the current directory, use the pwd command. For example, to print the current directory, type:
Search for Files
To search for files, use the find command. For example, to search for files named file1.txt in the current
directory, type:
Printing
echo "Hello World"
i Insert mode
:wq! Write (save) and quit, even if file has been changed
Program 1:
find . -inum [inode-number] - searches for a file with a specific inode number in the current
directory and its subdirectories.
df -i - displays information about inode usage and availability for file systems.
>> - redirects standard output to a file, appending the output to the end of the file.
top - displays information about the processes running on the system in real-time.
displays the current email queue on the system. sendmail - sends email messages from the
command line. mutt - a text-based email client for sending and receiving emails from the
command line.
Shell Programming
Write a shell script that asks the user to enter a number and then calculates its square
using arithmetic.
#!/bin/bash
read -p "Enter a number: " num
result=$((num * num))
echo "The square of $num is $result"
Write a shell script that takes two numbers as command-line arguments and prints
the sum of the two numbers if they are both positive.
#!/bin/bash
Write a shell script that uses a while loop to read numbers from a file until the sum of
the numbers is greater than 100.
#!/bin/bash
sum=0
Write a shell script that prompts the user to enter their age, and then uses an if-then-
else statement to check if they are old enough to vote.
#!/bin/bash
Write a shell script that uses a for loop to print the numbers 1 to 10.
#!/bin/bash
do
echo $i
done
Program 2:
Objective: Write a shell script that accepts a file
name starting and ending line numbers as
arguments anddisplay all the lines between given
line no.
#!/bin/bash
# check if the start and end line numbers are valid if ! [[ "$2" =~ ^[0-
9]+$ ]] || ! [[ "$3" =~ ^[0-9]+$ ]] || [ "$2" -gt "$3"
]; then echo "Error: invalid start or end
line numbers" exit 1 fi
Program 3
echo "All lines containing '$2' have been deleted from '$1'"
Program 4:
Objective: Write a shell script that displays a list of all the
files in the current directory
#!/bin/bash
ls -al
Objective: Simulation of Unix commands using C
ls command
#include <stdio.h>
#include <dirent.h>
if (argc < 2) {
dir = opendir(".");
} else { dir =
opendir(argv[1]);
}
closedir(dir);
return 0;
}
cat command
#include <stdio.h>
return 1; }
while ((ch = fgetc(fp)) != EOF)
{ putchar(ch); }
fclose(fp);
return 0;
}
rm command
#include <stdio.h>
if (remove(argv[1]) == 0) { printf("File %s
deleted successfully\n", argv[1]); return 0;
} else { printf("Cannot delete file %s\n",
argv[1]); return 1;
}
}
mkdir command
#include <stdio.h>
#include <sys/stat.h>
if (mkdir(argv[1], 0777) == 0) {
printf("Directory %s created successfully\n", argv[1]);
return 0; } else { printf("Cannot
create directory %s\n", argv[1]); return 1;
}
}
Program 5:
Obejctive: Implement the following CPU
Scheduling Algorithms. (i) First Come First
Serve(FCFS), (ii)Shortest Job First(SJF)
printf("Enter the arrival time and burst time for each process:\n");
wait_time[0] = 0;
turnaround_time[0] = burst_time[0];
avg_wait_time += wait_time[i];
avg_turnaround_time += turnaround_time[i];
}
avg_wait_time /= num_processes;
avg_turnaround_time /= num_processes;
return 0;
}
(ii) Shortest Job First(SJF)
#include <stdio.h>
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for (time = 0; ; ) {
smallest = 9;
for (i = 0; i < num_processes; i++) { if
(burst_time[i] <= burst_time[smallest] && burst_time[i] > 0)
{ smallest =
i;
}
}
if (smallest == 9) {
break;
}
burst_time[smallest]--;
if (burst_time[smallest] == 0) {
turnaround_time[smallest] = time + 1;
wait_time[smallest] = turnaround_time[smallest] -
temp[smallest];
avg_wait_time += wait_time[smallest];
avg_turnaround_time += turnaround_time[smallest];
}
time++;
}
avg_wait_time /= num_processes;
avg_turnaround_time /= num_processes;
return 0;
}
Program 6
Objective: Implement the following CPU
Scheduling Algorithms. (i) Round Robin, (ii)
Priority Based
typedef struct {
int pid; int
arrival_time; int
burst_time; int
remaining_time; int
completion_time;
int turnaround_time;
int waiting_time;
} Process;
int main() {
Process processes[MAX]; int n, quantum_time,
i, j, time = 0, completed = 0;
printf("Enter the arrival time and burst time for each process:\n");
processes[i].remaining_time = processes[i].burst_time;
processes[i].completion_time = 0;
processes[i].turnaround_time = 0;
processes[i].waiting_time = 0;
}
if (processes[i].remaining_time == 0) {
processes[i].completion_time = time;
processes[i].turnaround_time = processes[i].completion_time -
processes[i].arrival_time; processes[i].waiting_time =
processes[i].turnaround_time
- processes[i].burst_time; total_turnaround_time +=
processes[i].turnaround_time; total_waiting_time +=
processes[i].waiting_time;
}
}
}
}
typedef struct {
int pid; int
burst_time; int
priority; int
waiting_time; int
turnaround_time;
} Process;
int main() {
Process processes[MAX]; int n, i, j; float
total_waiting_time = 0, total_turnaround_time = 0;
sort(processes, n);
processes[0].waiting_time = 0;
return 0;
}