22cdl44 Os Lab Manual - Os - Final
22cdl44 Os Lab Manual - Os - Final
Dr. Suganya. R
Dr. Suganya. R
& Dr.B.Swathi
T.Sasikala
NEW HORIZON COLLEGE OF ENGINEERING
VISION
MISSION
QUALITY POLICY
To provide educational services of the highest quality both curricular and co-
curricular to enable students integrate skills and serve the industry and society
equally well at global level.
VALUES
• Academic Freedom
• Integrity Inclusiveness
• Innovation
• Professionalism
• Social Responsibility
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING (DATA SCIENCE)
PO2 Problem Analysis: Identify, formulate, review research literature and analyze complex
Computer Science and Data Science engineering problems reaching substantiated conclusions
using first principles of mathematics, natural sciences and engineering sciences.
PO3 Design / Development of Solutions: Design solutions for complex Computer Science
and Data Science engineering problems and design system components or processes that meet
specified needs with appropriate consideration for public health and safety, cultural, societal
and environmental considerations.
PO5 Modern tool usage: Create, select and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex Computer
Science and Data Science engineering activities with an understanding of the limitations.
PO6 The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice in Computer Science and Data Science
Engineering.
PO7 Environment and sustainability: Understand the impact of the professional engineering
solutions in Computer Science and Data Science engineering in societal and environmental
contexts, and demonstrate the knowledge of, and need for sustainable development.
PO8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
PO9 Individual and Team Work: Function effectively as an individual and as a member or
leader to diverse teams, and in multidisciplinary settings.
PO10 Communication: Communicate effectively on complex Computer Science and Data
Science engineering activities with the engineering community and with society at large, such
as, being able to comprehend and write effective report and design documentation, make
effective presentations, and give and receive clear instructions.
PO11 Project Management and Finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO12 Life-Long Learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.
PSO1
Apply data analysis techniques, algorithmic expertise, and advanced modelling to
effectively solve complex problems across various domains demonstrating their
capacity to derive insights and propose innovative solutions in the realm of data-driven
technologies.
PSO2
Collaborate proficiently with experts from diverse fields and actively engage in
continuous professional growth in the domain of Computer Science and Engineering,
specializing in the field of Data Science.
OPERATING SYSTEMS LAB
Course Code 22CDL44 CIE Marks 50
L:T:P:S 0:0:1:0 SEE Marks 50
Hrs / Week 2 Total Marks 100
Credits 01 Exam Hours 03
Course outcomes:
At the end of the course, the student will be able to:
22CDL44.1 Demonstrate the basic knowledge of Linux commands and file handling utilities by using Linux
shell environment.
22CDL44.2 Examine the various process scheduling algorithms
22CDL44.3 Implement various operations on deadlock
22CDL44.4 Implement various File Organization, File Allocation Strategies and Disk Scheduling Algorithms.
Mapping of Course Outcomes to Program Outcomes and Program Specific Outcomes:
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
22CDL44.1 3 2 2 2 3 - - - - - - 1 3 3
22CDL44.2 3 2 2 2 3 - - - - - - 1 3 3
22CDL44.3 3 2 2 2 3 - - - - - - 1 3 3
22CDL44.4 3 2 2 2 3 - - - - - - 1 3 3
1. echo command: echo command in linux is used to display line of text/string that
are passed as an argument . This is a built in command that is mostly used in shell
scripts and batch files to output status text to the screen or a file.
echo [string]
2. ls( listing files) is used to show list of files on your UNIX or Linux system.
16.egrep is a pattern searching command which belongs to the family of grep functions. It
works the same way as grep -E does. It treats the pattern as an extended regular expression
and prints out the lines that match the pattern. If there are several files with the matching
pattern, it also displays the file names for each line.
Syntax:
egrep [ options ] 'PATTERN' files
17.fgrep filter is used to search for the fixed-character strings in a file. There can be multiple
files also to be searched. This command is useful when you need to search for strings which
contain lots of regular expression metacharacters, such as “^”, “$”, etc.
Syntax:
fgrep [options] [ -e pattern_list] [pattern] [file]
Options with Description:
• -c : It is used to print only a count of the lines which contain the pattern.
• -h : Used to display the matched lines.
• -i : During comparisons, it will ignore upper/lower case distinction.
• -l : Used to print the names of files with matching lines once, separated by new-
lines. It will not repeat the names of files when the pattern is found more than
once.
• -n : It is used precede each line by its line number in the file (first line is 1).
• -s : It will only display the error messages.
• -v : Print all lines except those contain the pattern.
• -x : Print only lines matched entirely.
• -e pattern_list : Search for a string in pattern-list (useful when the string begins
with a “-“).
• -f pattern-file : Take the list of patterns from pattern-file.
• pattern : Specify a pattern to be used during the search for input.
• file : A path name of a file to be searched for the patterns. If no file operands are
specified, the standard input will be used.
2. Working with files & directories.
● Know the categories of files.
● Directory related Commands – pwd, mkdir, rmdir, cd, ls
● Manipulating Absolute paths and Relative paths using cd command.
● File related Commands – cat, cp, mv, rm, comm, cmp, diff, tar, umask, wc
● Listing seven attributes of a file : ls and its options
● File Permissions: Absolute and Relative permissions
● Manipulating File permissions using chmod command
● Manipulating File Ownership using chown command
● Manipulating Hardlink and Softlink using ln command
In Linux, everything is considered as a file. In UNIX, seven standard file types are regular,
directory, symbolic link, FIFO special, block special, character special, and socket. In
Linux/UNIX, we have to deal with different file types to manage them efficiently.
In Linux/UNIX, Files are mainly categorized into 3 parts:
1. Regular Files ,Directory Files
2. Special Files
The easiest way to find out file type in any operating system is by looking at its extension
such as .txt, .sh, .py, etc. If the file doesn’t have an extension then in Linux we can
use file utility. In this article, we will demonstrate file command examples to determine a
file type in Linux.
2. mkdir command in Linux allows the user to create directories (also referred to as
folders in some operating systems ). This command can create multiple directories
at once as well as set the permissions for the directories.
3. rmdir command is used remove empty directories from the filesystem in Linux.
The rmdir command removes each and every directory specified in the command
line only if these directories are empty.
6.cat(concatenate) command is very frequently used in Linux. It reads data from the
file and gives their content as output. It helps us to create, view, concatenate files.
7.cp: copy the contents of one file to another
8.rm: remove a particular file.
9.comm
Syntax :
$comm [OPTION]... FILE1 FILE2
• As using comm, we are trying to compare two files therefore the syntax of comm
command needs two filenames as arguments.
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps
you to find out whether the two files are identical or not.
Syntax:
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
diff stands for difference. This command is used to display the differences in the files
by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us
which lines in one file have is to be changed to make the two files identical.
tar stands for tape archive, is used to create Archive and extract the Archive files. tar
command in Linux is one of the important command which provides archiving functionality
in Linux. We can use Linux tar command to create compressed or uncompressed Archive
files and also maintain and modify them.
The umask command in Linux is used to set default permissions for files or directories the
user creates.
• The umask command specifies the permissions that the user does not want to be
given out to the newly created file or directory.
• umask works by doing a Bitwise AND with the bitwise complement (where the
bits are inverted, i.e. 1 becomes 0 and 0 becomes 1) of the umask.
• The bits which are set in the umask value, refer to the permissions, which are not
assigned by default, as these values are subtracted from the maximum permission
for files/directories.
$umask
wc stands for word count. As the name implies, it is mainly used for counting purpose.
• It is used to find out number of lines, word count, byte and characters count in
the files specified in the file arguments.
• By default it displays four-columnar output.
• First column shows number of lines present in a file specified, second column
shows number of words present in the file, third column shows number of
characters present in file and fourth column itself is the file name which are given
as argument.
$ wc filename
Syntax:- ls -l
$ ls -l /etc/hosts
-rw-r--r-- 1 root root 337 Oct 4 11:31 /etc/hosts
Hard Links
• Each hard linked file is assigned the same Inode value as the original, therefore
they reference the same physical file location. Hard links more flexible and remain
linked even if the original or linked files are moved throughout the file system,
although hard links are unable to cross different file systems.
• ls -l command shows all the links with the link column shows number of links.
• Links have actual file contents
if (pid < 0)
{
perror("fork() failure\n");
return 1;
}
// Child process
if (pid == 0)
{
printf("This is child process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
}
else
{ // Parent process
sleep(2);
printf("This is parent process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
printf("Newly created process id or child pid is %d\n", pid);
}
return 0;
}
Before fork: Process id is 166629
This is child process
Process id is 166630 and PPID is 166629
Before fork: Process id is 166629
This is parent process
Process id is 166629 and PPID is 166628
Newly created process id or child pid is 166630
4. Design, Develop and Implementation of CPU scheduling by using
a. FCFS
b. Priority
DESCRIPTION
FCFS CPU SCHEDULING ALGORITHM: For FCFS scheduling algorithm, read the
number of processes/jobs in the system, their CPU burst times. The scheduling is performed
on the basis of arrival time of the processes irrespective of their other parameters. Each process
will be executed according to its arrival time. Calculate the waiting time and turnaround time
of each of the processes accordingly.
Program:
FCFS Scheduling:
#include<stdio.h>
#include<conio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
Output:
PRIORITY CPU SCHEDULING ALGORITHM: For priority scheduling algorithm, read
the number of processes/jobs in the system, their CPU burst times, and the priorities. Arrange
all the jobs in order with respect to their priorities. There may be two jobs in queue with the
same priority, and then FCFS approach is to be performed. Each process will be executed
according to its priority. Calculate the waiting time and turnaround time of each of the processes
accordingly.
PRIORITY CPU SCHEDULING
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
int prior;
}
p[10],temp;
int main()
{
int i,j,n,totwt,tottt,arg1,arg2;
clrscr();
printf("enter the number of process");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("enter the burst time");
scanf("%d",&p[i].bt);
printf("\n enter the priority");
scanf("%d",&p[i].prior);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].prior>p[j].prior)
{
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp.bt;
temp.prior=p[i].prior;
p[i].prior=p[j].prior;
p[j].prior=temp.prior;
}}
}
p[i].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\n process to \t bt \t wt \t tt");
while(i<=n)
{
printf("\n%d\t %d\t %d\t %d\t",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
arg1=totwt/n;
arg2=tottt/n;
printf("\n arg1=%d \t arg2=%d\t",arg1,arg2);
getch();
return 0;
}
Output:
5. Design, Develop and Implementation of CPU scheduling by
a. SJF
b. Round Robin
DESCRIPTION
SJF CPU SCHEDULING ALGORITHM: For SJF scheduling algorithm, read the number
of processes/jobs in the system, their CPU burst times. Arrange all the jobs in order with respect
to their burst times. There may be two jobs in queue with the same execution time, and then
FCFS approach is to be performed. Each process will be executed according to the length of
its burst time. Then calculate the waiting time and turnaround time of each of the processes
accordingly.
SJF CPU SCHEDULING:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
}p[10],temp;
int main()
{
int i,j,n,totwt,tottt;
float avg1,avg2;
clrscr();
printf("\nEnter the number of process:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("\nEnter the burst time:\t");
scanf("%d",&p[i].bt);
}
for(i=1;i<n;i++){
for(j=i+1;j<=n;j++)
{
if(p[i].bt>p[j].bt)
{
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;p[i].bt=p[j].bt;
p[j].bt=temp.bt;
}}}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n){
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\nProcess id \tbt \twt \ttt");
while(i<=n){
printf("\n\t%d \t%d \t%d t%d\n",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
} avg1=totwt/n;
avg2=tottt/n;
printf("\nAVG1=%f\t AVG2=%f",avg1,avg2);
getch();
return 0; }
Output:
Program:
Output:
6. Design, Develop and Implement Threading and synchronized applications
Program:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
pthread_t tid[2];
int counter;
return NULL;
}
int main(void)
{
int i = 0;
int error;
while (i < 2) {
error = pthread_create(&(tid[i]), NULL, &trythis, NULL);
if (error != 0)
printf("\nThread can't be created : [%s]", strerror(error));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
Output :
Job 1 has started
Job 2 has started
Job 2 has finished
Job 2 has finished
PART -B
7. Design, Develop and Implement an Algorithm for Dead Lock Detection
#include <stdio.h>
#define MAX_PROCESSES 20
#define MAX_RESOURCES 10
int main()
{
int alloc[MAX_PROCESSES][MAX_RESOURCES],
request[MAX_PROCESSES][MAX_RESOURCES];
int avail[MAX_RESOURCES], r[MAX_RESOURCES], w[MAX_RESOURCES];
printf("\nEnter the number of processes: ");
scanf("%d", &np);
printf("\nEnter the number of resources: ");
scanf("%d", &nr);
if(deadlock)
printf("\nDeadlock detected\n");
else
printf("\nNo Deadlock possible\n");
return 0;
}
Example Input:
1. Number of processes: 3
2. Number of resources: 3
• Resource R1: 10
• Resource R2: 5
• Resource R3: 7
Request matrix:
322
010
211
Allocation matrix:
100
211
314
No Deadlock possible
8. Deadlock Avoidance- Bankers Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
} }
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
} }
} }
int flag = 1;
for(int i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("The following system is not safe");
break;
}
}
if(flag==1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
}
student@localhost ~]$ cc bank.c -std=c99
[student@localhost ~]$ ./a.out
Following is the SAFE Sequence
P1 -> P3 -> P4 -> P0 -> P2[student@localhost ~]$ cc bank.c
[student@localhost ~]$ ./a.out
Following is the SAFE Sequence
P1 -> P3 -> P4 -> P0 -> P2[student@localhost ~]$
Output
Following is the SAFE Sequence
P1 -> P3 -> P4 -> P0 -> P2
9. Page Replacement program for FIFO
#include <stdio.h>
#include <stdbool.h>
bool isPageInFrame(int frame[], int fsize, int page) {
for (int i = 0; i < fsize; i++) {
if (frame[i] == page) {
return true;
} }
return false;}
int main() {
int n, f, i;
printf("Enter the number of pages: ");
scanf("%d", &n);
int pages[n];
printf("Enter the pages: ");
for(i=0; i<n; i++)
{ scanf("%d", &pages[i]); }
printf("Enter the frame size: ");
scanf("%d", &f);
int frame[f];
int hits = 0, faults = 0, index = 0;
for (int i = 0; i < f; i++)
{ frame[i] = -1; }
for (int i = 0; i < n; i++)
{
int pg = pages[i];
if (isPageInFrame(frame, f, pg))
{
hits++;
}
else
{
faults++;
frame[index] = pg;
index = (index + 1) % f;
}
printf("Frame: ");
for (int j = 0; j < f; j++) {
if (frame[j] != -1) {
printf("%d ", frame[j]);
}
else {
printf(". "); } }
printf("\n");
}
printf("Total hits: %d\n", hits);
printf("Total faults: %d\n", faults);
return 0;
}
Output:
Incoming Frame 1 Frame 2 Frame 3
4 4 - -
1 4 1 -
2 4 1 2
4 4 1 2
5 5 1 2
Total Page Faults: 4
10. Program to implement file organization techniques
#include<stdio.h>
#include<string.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break; } }
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{ if(strcmp(f, dir.fname[i])==0) {
printf("File %s is found ", f);
break;
}}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]); }
break;
default: exit(0); } }
getch();
}
OUTPUT:
Enter name of directory -- CSE
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- A
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- B
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- C
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 4
The Files are -- A B C
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 3
Enter the name of the file – ABC
OUTPUT:
[student@localhost CD049]$ cc sfa.c
[student@localhost CD049]$ ./a.out
Enter no of files :3