VIT-AP
B. Tech (CSE)
Operating System
Course-Code: CSE2008
FALL SEM-2025-26
Presenter
ODNALA SRINIVAS
SCOPE, VIT-AP University
Operating System
Lab Experiments:
Experiment-1:
a. Study of Basic Linux Commands Experiment-5:
b. Basic Shell Programs a. Implementation of Deadlock Prevention Using Banker’s
c. Implementation of System Calls Algorithm
Experiment-2: Implementation of Scheduling Algorithms b. Implementation of Memory Management Using Paging
A. FCFS Algorithm c. Implementation of Memory Management Using
B. CPU Scheduling Using SJF Segmentation
C. CPU Scheduling Using Priority Experiment-6: Implementation of Page Replacement Algorithms
D. CPU Scheduling Using Round Robin A. First Come First Serve (FCFS) Page Replacement
Experiment-3: Implementation of Multithreading B. Least Recently Used (LRU) Page Replacement Algorithm
A. Multithreading Using JAVA C. Optimal Page Replacement Algorithm
B. Multithreading Using PTHREAD
Experiment-4: Implementation of Interprocess
Communication
A. IPC Using Semaphore – Producer and Consumer
Problem
B. IPC Using Semaphore – Readers and Writers Problem
C. IPC Using Semaphore – Dining Philosopher Problem
D. IPC Using Pipes
1. LINUX COMMAND
• pwd: To know which directory you are in, you can use the “pwd” command.
• cd: Use the "cd" command to go to a directory.
• cd dir_1/dir_2/dir_3: This command is used to move inside a directory from a
directory.
• cd /: this command is used to change directory to the root directory.
• cd ~ : this command is used to change directory to the home directory.
• cd .. : this command is used to move to the parent directory of current directory, or
the directory one level up from the current directory.
LINUX COMMAND – File Listing
• ls: List the contents of the folder from which it runs.
• ls-a: To view hidden files, use the command. Hidden files name begins with “.”
• ls-l: List all attributes of all files in current directory.
• ls –R: Shows all the files not only in directories but also subdirectories.
• ls-t: Place files in order of their modification. The last modified will be placed first.
• ls-s: Biggest file in size will be placed first.
LINUX COMMAND – File Listing
LINUX COMMAND – File Listing
LINUX COMMAND – Directory/Files
• mkdir & rmdir — Use the mkdir command when you need to create a folder or a directory
• rmdir: Use rmdir to delete a directory. But rmdir can only be used to delete an empty
directory.
• rm: Use the rm command to delete files and directories. Use "rm -r" to delete just the
directory. It deletes both the folder and the files it contains when using only the rm command.
• touch — The touch command is used to create a file. It can be anything, from an empty txt
file to an empty zip file. For example, “touch new.txt”.
• cp — Use the cp command to copy files through the command line. It takes two arguments:
The first is the location of the file to be copied, the second is where to copy.
LINUX COMMAND – Directory/Files
• mv — Use the mv command to move files through the command line. We can also use
the mv command to rename a file. For example, if we want to rename the file “text” to “new”, we
can use “mv text new”. It takes the two arguments, just like the cp command.
• locate — The locate command is used to locate a file in a Linux system, just like the search
command in Windows. Using the -i argument with the command helps to ignore the case (it doesn't
matter if it is uppercase or lowercase). So, if you want a file that has the word “hello”, it gives the list
of all the files in your Linux system containing the word "hello" when you type in “locate -i hello”.
If you remember two words, you can separate them using an asterisk (*). For example, to locate a file
containing the words "hello" and "this", you can use the command “locate -i *hello*this”.
LINUX COMMAND
• man & --help — To know more about a command and how to use it, use
the man command. It shows the manual pages of the command. For example, “man cd”
shows the manual pages of the cd command. Typing in the command name and the
argument helps it show which ways the command can be used (e.g., cd –help).
LINUX COMMAND: Intermediate Commands
• echo — The "echo" command helps us move some data, usually text into a file. For
example, if you want to create a new text file or add to an already made text file, you
just need to type in, “echo hello, my name is xyz >> new.txt”.
• cat — Use the cat command to display the contents of a file. It is usually used to easily
view programs.
LINUX COMMAND – Creating & Viewing Files
• cat: The 'cat' command is used to display text files. It can also be used for copying,
combining and creating new text files. Only text files can be displayed and combined using
this command.
To create a new file, use the command:
1. cat > filename
2. Add content To copy file
3. Press 'ctrl + d' to return to command prompt. cat mytext.txt > newfile.txt
To view a file, use the command –
1. cat filename
To combine 2 files use Command –
1. cat file1 file2 > newfilename
LINUX COMMAND – Creating & Viewing Files
Append a text file's contents to another text file:
• cat mytext.txt >> another-text-file.txt
Will read the contents of mytext.txt, and write them at the end of another-text-file.txt.
If another-text-file.txt does not already exist, it will be created and the contents
of mytext.txt will be written to the new file.
LINUX COMMAND
• df — Use the df command to see the available disk space in each of the partitions in your system. If
you want it shown in megabytes, you can use the command “df -m”.
• du — Use du to know the disk usage of a file in your system.
• tar — Use tar to work with tarballs (or files compressed in a tarball archive) in the Linux command
line. It has a long list of uses. It can be used to compress and uncompress different types of tar
archives like .tar, .tar.gz, .tar.bz2,etc. It works on the basis of the arguments given to it. For example,
"tar -cvf" for creating a .tar archive, -xvf to untar a tar archive, -tvf to list the contents of the archive,
etc.
LINUX COMMAND
• zip, unzip: Use zip to compress files into a zip archive, and unzip to extract files from a zip archive.
• apt-get: Use apt to work with packages in the Linux command line. Use apt-get to install packages.
• sudo: A widely used command in the Linux command line, sudo stands for "SuperUser Do". So, if you want
any command to be done with administrative or root privileges, you can use the sudo command.
• ping: Use ping to check your connection to a server.
• cal: Displays the calendar of the current month
• clear: This command clears the screen.
• head: Displays the first few lines of a file. By default, the ‘head’ command displays the first 10 lines of a file.
But with -n option, the number of lines to be viewed can be specified.
• tail: Similar to ‘head’; the ‘tail’ command shows the last 10 lines by default, and -n option is available as well.
LINUX COMMAND
• chmod: change the permissions of file to octal, which can be found separately for user,
group, and world by adding:
4 – read (r)
2 – write (w)
1 – execute (x)
Examples:
chmod 777 – read, write, execute for all
chmod 755 – rwx for owner, rx for group and world.
For instance, for the owner to have full permissions, the group read and execute, and
no permissions for anyone else, you'd type the following:
chmod 750 test
LINUX COMMAND
Practice following Linux commands
1. who
SYNTAX: $who
DESCRIPTION: who displays all users that are currently logged into the system.
2. whoami
SYNTAX: $whoami
DESCRIPTION: It displays a single line of output pertaining the logic details of the particular user.
3. tty
SYNTAX: $tty[-option]
DESCRITION: tty returns the name of the current device where the current user is logged in i.e.,a terminal device.
Option: s->suppresses the priority of the path name of the device file.
4. date
SYNTAX: $date
DESCRIPTION: It shows the current date.
5. banner
SYNTAX: $banner<text>
DESCRIPTION: Shows the enlarged text on the screen.
LINUX COMMAND
Practice following Linux commands
6. lock.
SYNTAX: $lock [-t]
DESCRIPTION: It disappear $ after pressing the same password ,you can enter into UNIX until t seconds.
7. logname
SYNTAX: $logname
DESCRIPTION: It shows the login name of the user.
8. script
SYNTAX: $script
DESCRIPTION: All your keystrokes are stored in typescript which is places virtually.
9. wc
SYNTAX: wc [-option]<file name>
DESCRIPTION: This is used to count the number of lines ,words and characters in the file given.
Options: l-number of lines in the file.
w-number of words in the file.
c-number of characters in the file.
2. Shell Programming
1. Shell first programming 2. Shell programming for adding two
numbers 3. Shell programming using IF-Else
Desktop $ vi wel.sh #!/bin/bash
Desktop $ vi add.sh
echo “Welcome to shell Read a echo "Enter the first number:"
programming" read num1
Read b
echo "Enter the second number:"
c=$((a+b))
read num2
Save: Esc : wq echo “Result =$c”
echo "Enter the third number:"
Note: press ‘I’ for input read num3
into the bash file Save: Esc : wq if [ "$num1" -ge "$num2" ] && [
Execute: . / wel.sh Execute: ./ add.sh "$num1" -ge "$num3" ]; then
Permissions: chmod 777 add.sh echo "$num1 is the greatest number."
elif [ "$num2" -ge "$num1" ] && [
"$num2" -ge "$num3" ]; then
Note1: At terminal $ vi wel.sh echo "$num2 is the greatest number."
Note2: press ‘I’ for input data to the program else
Note3: use ‘Esc : wq’ for save the file echo "$num3 is the greatest number."
Note4: use ‘esc :q!’ quit without saving fi
Note5: chmod 777 wel.sh
Note6: Execusion: ./wel.sh
4. Shell programming using basic case
statement
#!/bin/bash
echo "Enter choice:“ 5. Shell programming using basic functions
echo "1. List of files"
echo "2. List of users“
myfunc()
echo "3. Delete a file" {
read -p "Your choice: " ch
case $ch in echo "i was called as $@"
1) x=2
echo "The list of files is:"
ls }
;; echo "script was called with the $@"
2)
echo "The users are:" x=1
who echo "x is $x"
;;
3) myfunc 1 2 3
read -p "Enter the file to be deleted: " filename
echo "x is $x"
rm "$filename"
;;
*)
echo "Invalid choice."
;;
esac
Experiment-2: Shell Programming
6. Shell programming using do-while loop
ch="y" 7. Shell programming
while [ "$ch" = "y" ] using for loop
do
echo "Enter a number"
read number sum=0
fact=1 for i in 1 2 3 4
while [ "$number" -gt "1" ] do
do sum=$((sum + i))
fact=`expr $fact \* $number`
done
number=`expr $number \- 1`
done echo "The sum is $sum"
echo "factorial is $fact"
echo "if you want to continue(y/n)?"
read ch
done
3. System Calls
1. FORK() SYSTEM CALL 2. STAT() SYSTEM CALL
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
#include<unistd.h> #include<unistd.h>
main()
int main() {
{
int status;
int pid;
// Run the stat command to get file information
pid=fork();
if(pid==0)
status = system("stat pro1.c");
{ if (status == -1) {
Printf(“\n Child process id is %d”, getpid()); perror("system call failed");
printf("\n I am a child,my parent process is return 1;
%d",getppid());
}
}
return 0;
else
}
printf("\n I am parent process,my id is %d",getpid());
}
Note1: At terminal $ vi pro1.c Note4: at terminal $ gcc pro1.c
Note2: Press ‘I’ for input data to the program Note5: $ ./a.out
Note3: use ‘Esc: wq’ for saving the file
System Calls
4. OPENDIR( ),READDIR( ) SYSTEM CALL
3. EXIT( ),WAIT( ),EXEC( ) SYSTEM CALLL #include<sys/types.h>
#include<stdio.h>
#include<stdio.h> #include<dirent.h>
#include<stdlib.h> // Include for exit()
#include<stdlib.h> int main (int argc,char *argv[])
{
main() DIR *dp;
struct dirent *dirp; // Corrected variable name from drip to dirp
{ if(argc!=2)
{
int status; printf("a single argument (the directory name) is required\n");
exit(1);
if(fork()<0) }
if(( dp= opendir ( argv[1] ))==NULL) // Use NULL instead of 0 for pointer
exit(0); comparison
{
else printf("can't open %s \n",argv[1]);
exit(1);
execl("/bin/date","date",0); }
while((dirp=readdir(dp))!=NULL) // Corrected variable name and used NULL
wait(&status); {
printf("%s\n",dirp->d_name);
} }
closedir(dp);
exit(0);
}
System Calls
5. OPEN( ),READ( ) AND WRITE( ) SYSTEM CALLL while (file_size > 0) {
// Read one character
#include <stdio.h> #include <fcntl.h>
if (read(fd, &buff, 1) == -1) {
#include <sys/types.h> #include <unistd.h> // Required for lseek, read, perror("Error reading from file");
write, close close(fd);
int main() { int fd; return 1;
}
char buff; // Single character buffer for reading/writing one byte at a
time // Write the character to standard output
if (write(STDOUT_FILENO, &buff, 1) == -1) {
off_t file_size; // To store the size of the file perror("Error writing to stdout");
printf("\n\nThis file is in reversed order:\n"); close(fd);
// Open the file in read-only mode return 1;
}
fd = open("wel.c", O_RDONLY);
if (fd == -1) { perror("Error opening file"); file_size--; // Decrement the counter
return 1; // Indicate an error } // Move the file pointer two positions back:
// Get the file size by seeking to the end // one for the character just read, and one to get to the previous character
file_size = lseek(fd, 0, SEEK_END); if (file_size > 0) { // Avoid seeking past the beginning
if (lseek(fd, -2, SEEK_CUR) == -1) {
if (file_size == -1) { perror("Error seeking backwards");
perror("Error seeking to end of file"); close(fd);
return 1;
close(fd); return 1; } }
if (lseek(fd, -1, SEEK_END) == -1) { }
perror("Error seeking before reading"); }
close(fd); close(fd); // Close the file
return 1; return 0; // Indicate success
} }
Experiment 1: (Ass-1: Linux Commands, Ass-2: Shell Programming, Ass-3: System Calls)
Assignment-1: Define and explain the syntax and purpose of the following Linux
Commands using examples.
1.pwd 2. mkdir 3. cd 4. ls 5. cp
6. mv 7. touch 8.echo 9.cat 10. df
11. du 12. head 13. tail 14. clear 15. tar
16. cal 17.date 18.chmod 19.wc 20.banner
OSLAB1234
LINUX1234 SHELL1234 SCALL1234
Note: Create a directory with your last four digits of your registration no (Ex: OSLABXXXX”,
then create 3 subdirectories: “LinuxXXXX”, “ShellXXXX”, “ScallXXXX” and save related files
of each.
Assignment-2: Execute the following shell programs
1. Write a shell program to find biggest of 3 numbers
2. Write a shell program to find the factorial of given number
3. Write a shell program to find sum of n natural numbers using for loop (Where n=12)
4. Write the shell programs to show following patterns
(using for loop) (using while loop)
54321 *
4321 * *
321 * * *
21 * * * *
1 * * * * *
5. Write a shell program to swap 2 numbers using functions
Assignment-3: Execute the following system calls
1. FORK() SYSTEM CALL
2. STAT() SYSTEM CALL
3. EXIT( ),WAIT( ),EXEC( ) SYSTEM CALLL