Practical 1
Practical 1
PRACTICAL-1
a) Study of Unix/Linux general purpose utility command list: man,who,cat, cd, cp, ps, ls, mv,
rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown, finger, pwd, cal,
logout, shutdown.
1. MAN: man command which is short for manual, provides in depth information about the
requested command (or) allows users to search for commands related to a particular keyword.
Options:
Options Description
One may not be able to remember the sections in which a command is present. So this
-f option gives the section in which the given command is present.Display a short
description from the manual page
BAIT, SURAT 1
OS [1010206404] 2107020603014
Example:
2. WHO: who command can list the names of users currently logged in, their terminal, the time
they have been logged in, and the name of the host from which they have logged in.?
Options:
Options Description
am i Print the username of the invoking user, The 'am' and 'i' must be space
separated.
-b Prints time of last system boot.
-d print dead processes.
-H Print column headings above the output.
-m Same as who am i.
-q Prints only the usernames and the user count/total no of users logged
in.
Example:
BAIT, SURAT 2
OS [1010206404] 2107020603014
Options:
Options Description
-b Omits line numbers for blank space in the output.
-e A $ character will be printed at the end of each line prior to a new line.
-E Displays a $ (dollar sign) at the end of each line.
-n Line numbers for all the output lines.
-s If the output has multiple empty lines it replaces it with one empty line.
-T Displays the tab characters in the output.
-v Non-printing characters (with the exception of tabs, new-lines and form-feeds)
are printed visibly.
Example:
4. CD: To change directory - change the current working directory to a specific Folder.
Options:
Options Description
Example:
BAIT, SURAT 3
OS [1010206404] 2107020603014
Options:
Options Description
-a archive files
-n no file overwrite
Example:
BAIT, SURAT 4
OS [1010206404] 2107020603014
Options:
Options Description
e e option it displays running Daemons.
f with the F option it displays processes with full options.
l with the L option it would list more information on those processes.
ef we were to combine e and F together that would display all processes and
Daemons on all TTYS.
a a would list all processes that run on terminals.
x X would list all processes that do not run on terminals.
aux we were to combine a U and X that would list all processes running on or off
terminals and it would format the results with additional information similar to a
long listing.
Example:
BAIT, SURAT 5
OS [1010206404] 2107020603014
Options:
Options Description
-S sort the list by size, with the largest at the top.
-g list the files in long format but without the owner name.
-a list all files including hidden files. These are files that start with “.”.
-A list all files including hidden files except for “.” and “..” – these refer to the entries
for the current directory, and for the parent directory.
-R list all files recursively, descending down the directory tree from the given path.
-t sort the list by time of modification, with the newest at the top.
-o list the files in long format but without the group name.
-l list the files in long format i.e. with an index number, owner name, group name,
size, and permissions.
Example:
BAIT, SURAT 6
OS [1010206404] 2107020603014
8. MV: mv is used to move one or more files or directories from one place to another in a file
system like UNIX.
Options:
Options Description
Example:
BAIT, SURAT 7
OS [1010206404] 2107020603014
Option:
Options Description
-f, --force ignore nonexistent files, never prompt.
-i prompt before every removal.
-I prompt once before removing more than three files, or when removing
recursively. Less intrusive than -i, while still giving protection against most
mistakes.
--interactive prompt according to WHEN: never, once (-I), or always (-i). Without
WHEN, prompt always.
--one-file-system when removing a hierarchy recursively, skip any directory that is on a file
system different from that of the corresponding command line argument.
--no-preserve-root do not treat '/' specially.
--preserve-root do not remove '/' (default).
-r remove directories and their contents recursively.
-v explain what is being done.
--help display this help and exit.
--version output version information and exit.
Example:
10. MKDIR: mkdir command in Linux allows the user to create directories.
Option:
Options Description
-p Add directory including its sub directory.
-v Print a message for each created directory.
-m Set access privilege.
BAIT, SURAT 8
OS [1010206404] 2107020603014
Example:
11. RMDIR: rmdir command is used remove empty directories from the filesystem in Linux.
Options:
Options Description
-p Each directory argument is treated as a pathname of which all
components will be removed, if they are empty, starting with the last
component.
-v Display verbose information for every directory processed.
--ignore-fail-on- Do not report a failure which occurs solely because a directory is non-
non-empty empty. Normally, when rmdir is instructed to remove a non-empty
directory, it reports an error. This option suppresses those error
messages.
--help Display a help message, and exit.
--version Output version information, and exit.
Example:
BAIT, SURAT 9
OS [1010206404] 2107020603014
12. ECHO: echo command in linux is used to display line of text/string that are passed as an
argument.
Options:
Options Description
\\ Shows the backslash character.
\a Alert (BEL)
\b Displays a backspace character.
\c Suppresses any further output.
\e Displays an escape character.
\f Displays a form feed character.
\n Adds a new line to the document.
\r Represents a carriage return.
\t Makes a horizontal tab appear.
\v Displays a vertical tab.
Example:
13. MORE: more command is used to view the text files in the command prompt, displaying one
screen at a time in case the file is large.
BAIT, SURAT 10
OS [1010206404] 2107020603014
Options:
Options Description
-num Limits the line displayed per page.
-d Displays user message at right corner.
-s Squeeze blank lines.
-string name It helps to find the string.
-num Used to display the content from a specific line.
Example:
14. DATE: date command is used to display the system date and time.
Options:
Options Description
-d Display time described by string STRING, instead of the default, which is 'now'.
-f Like --date, but processed once for each line of file DATEFILE.
-I Output date/time in ISO 8601 format. For values of TIMESPEC, use 'date' for date
only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time to the
indicated precision.
-r Display the last modification time of file FILE.
-R Output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -
0600
-s Set time described by string STRING.
-u Print or set Coordinated Universal Time.
BAIT, SURAT 11
OS [1010206404] 2107020603014
Example:
15. TIME: time command in Linux is used to execute a command and prints a summary of real-
time, user CPU time and system CPU time spent by executing a command when it terminates.
Example:
16. KILL: kill command in Linux (located in /bin/kill), is a built-in command which is used to
terminate processes manually.
Options:
Option Description
-s The name, abbreviated name, or number of the signal to be sent, preceded by a dash.
pid A numeric process ID. If you're not sure what the PID is of a process, use
the ps command to list it.
-l List available signal names.
-L List available signal names and numbers in a table.
BAIT, SURAT 12
OS [1010206404] 2107020603014
Example:
17. HISTORY: Linux history command is used to display the history of the commands executed
by the user.
Syntax: $history
Options:
Option Description
-c It will clear the history list. It will delete all the entries of the “.bash_history” file.
-d It will delete the history entry at the position offset.
-a It will help to append the new history lines to the “.bash_history” file.
-n It will read the history lines but not already read from the “.bash_history” file into
the current history list.
-r It is helping to read the contents of the “.bash_history” file and use it as the current
history of the terminal.
-w It will write the current history to the “.bash_history” file and help to overwrite the
current history file contents.
-s It will store the arguments in the “.bash_history” file as a single entry.
Example:
BAIT, SURAT 13
OS [1010206404] 2107020603014
18. CHMOD: chmod command is used to change the access mode of a file.
Options:
Option Description
-c It is similar to the verbose option, but the difference is that it is reported if a change has
been made.
-f It is used to suppress the error messages.
-v It is used to display a diagnostic for every processed file.
-R It is used to change files and directories recursively.
Example:
19. CHOWN: chown command is used to change the file Owner or group.
Options:
Options Description
-c Verbosely describe the action for each File whose ownership actually changes.
--dereference Do not act on symbolic links themselves but rather on what they point to.
-f Do not print error messages about files whose ownership cannot be changed.
--reference Use the user and group of the reference FILE instead of an explicit NewOwner
value.
BAIT, SURAT 14
OS [1010206404] 2107020603014
Example:
20. FINGER: Finger command is a user information lookup command which gives details of all
the users logged in.
Syntax: $finger
Options:
Options Description
-s Displays the user's login name, real name, terminal name and write status (as a "*" after
the terminal name if write permission is denied), idle time, login time, office location and
office phone number
-l Produces a multi-line format displaying all of the information described for the -s option
and the user's home directory, home phone number, login shell, mail status, and the
contents of the files ".plan", ".project", ".pgpkey" and ".forward" from the user's home
directory.
-p Prevents the -l option of finger from displaying the contents of the ".plan", ".project" and
".pgpkey" files.
-m Prevent matching of usernames. The user is usually a login name; however, matching is
also done on the users' real names, unless the -m option is supplied. All name matching
performed by finger is case insensitive.
Example:
BAIT, SURAT 15
OS [1010206404] 2107020603014
21. PWD: pwd stands for Print Working Directory. It prints the path of the working directory,
starting from the root.
Syntax: $pwd
Example:
22. CAL: cal command is a calendar command in Linux which is used to see the calendar of a
specific month or a whole year.
Options:
Option Description
-h Don't highlight today's date.
-m Specify a month to display.
-y Specify a year to display. For example, -y 1970 displays the entire calendar of
the year 1970.
-3 Display last month, this month, and next month.
-1 Display only this month. This is the default.
-A num Display num months occurring after any months already specified.
-B num Display num months occurring before any months already specified.
-d YYYY-MM Operate as if the current month is number MM of year YYYY.
BAIT, SURAT 16
OS [1010206404] 2107020603014
Example:
23. LOGOUT: logout command allows you to programmatically logout from your session.
Syntax: session-logout[options]
Example:
24. SHUTDOWN: The shutdown command in Linux is used to shut down the system in a safe
way.
BAIT, SURAT 17
OS [1010206404] 2107020603014
Options:
Option Description
-r Requests that the system be rebooted after it has been brought down.
-H Requests that the system be halted after it has been brought down.
-P Requests that the system be powered off after it has been brought down.
-c Cancels a running shutdown. TIME is not specified with this option, the first
argument is MESSAGE.
-k Only send out the warning messages and disable logins, do not actually bring
the system down.
-h Requests that the system be either halted or powered off after it has been
brought down, with the choice as to which left up to the system.
Example:
BAIT, SURAT 18
OS [1010206404] 2107020603014
b) Study of vi editor.
The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words, it
is available in all Linux distros. It is user-friendly and works same on different distros and
platforms. It is a very powerful application. An improved version of vi editor is vim. By default,
the vi editor starts in command mode. To enter text, you have to be in insert mode, just type 'i' and
you'll be in insert mode. Although, after typing i nothing will appear on the screen but you'll be in
insert mode. Now you can type anything. To exit from insert mode press Esc key, you'll be directed
to command mode.
The vi editor has two modes:
1) Command Mode
2) Insert Mode
1) Command Mode: In command mode, actions are taken on the file. The vi editor starts in
command mode. Here, the typed words will act as commands in vi editor. To pass a command,
you need to be in command mode.
Syntax: vi <fileName>
Example:
BAIT, SURAT 19
OS [1010206404] 2107020603014
2) Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will take
you to the command mode from insert mode. To move to the insert mode press i. Although,
there are other commands also to move to insert mode which we'll study in next page.
Syntax: vi
Example:
Commands Action
:wq Save and quit
:w Save
:q Quit
:w fname Save as fname
ZZ Save and quit
:q! Quit discarding changes made
:w! Save (and write to non-writable file)
BAIT, SURAT 20
OS [1010206404] 2107020603014
Commands Action
i Start typing before the current character
I Start typing at the start of current line
a Start typing after the current character
A Start typing at the end of current line
o Start typing on a new line after the current line
O Start typing on a new line before the current line
BAIT, SURAT 21
OS [1010206404] 2107020603014
c) Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system.
SHELL- It is a program which provides the interface between the user and an operating
system. When the user logs in OS starts a shell for user.
• Types of Shells in Linux -
1) Bourne shell
2) Bash shell
3) C shell
1) Bourne shell-
The Bourne shell, called "sh," is one of the original shells, developed for Unix computers by
Stephen Bourne at AT&T's Bell Labs in 1977. Its long history of use means many software
developers are familiar with it. It offers features such as PROGRAM and output redirection,
shell scripting with string and integer variables, and condition testing and looping.
2) Bash shell-
The popularity of sh motivated programmers to develop a shell that was compatible with it,
but with several enhancements. Linux systems still offer the sh shell, but "bash" -- the "Bourne-
again Shell," based on sh -- has become the new default standard. One attractive feature of
bash is its ability to run sh shell scripts unchanged. Shell scripts are complex sets of commands
that automate programming and maintenance chores; being able to reuse these scripts saves
programmer’s time. Conveniences not present with the original Bourne shell include command
completion and a command history.
3) C shell-
Developers have written large parts of the Linux operating system in the C and C++ languages.
Using C syntax as a model, Bill Joy at Berkeley University developed the "C-shell," csh, in
1978. Ken Greer, working at Carnegie-Mellon University, took csh concepts a step forward
with a new shell, tcsh, which Linux systems now offer. Tcsh fixed problems in csh and added
command completion, in which the shell makes educated "guesses" as you type, based on your
system's directory structure and files. Tcsh does not run bash scripts, as the two have substantial
differences.
BAIT, SURAT 22
OS [1010206404] 2107020603014
A UNIX filesystem is a collection of files and directories that has the following properties-
• It has a root directory (/) that contains other files and directories.
• Each file or directory is uniquely identified by its name, the directory in which it resides, and
a unique identifier, typically called an inode.
• By convention, the root directory has an inode number of 2 and the lost+found directory has
an inode number of 3. Inode numbers 0 and 1 are not used. File inode numbers can be seen by
specifying the -i option to ls command.
• It is self-contained. There are no dependencies between one filesystem and any other.
• The directories have specific purposes and generally hold the same types of information for
easily locating files.
BAIT, SURAT 23
OS [1010206404] 2107020603014
• Following are the directories that exist on the major versions of Unix -
Directory Description
/ This is the root directory which should contain only the directories needed
at the top level of the file structure
/bin This is where the executable files are located. These files are available to all users
/etc Supervisor directory commands, configuration files, disk configuration files, valid user lists,
groups, ethernet, hosts, where to send critical messages
/lib Contains shared library files and sometimes other kernel-related files
/home Contains the home directory for users and other accounts
/mnt Used to mount other temporary file systems, such as cdrom and floppy for the CD-ROM drive
and floppy diskette drive, respectively
/proc Contains all processes marked as a file by process number or other information that is dynamic
to the system
/usr Used for miscellaneous purposes, and can be used by many users. Includes administrative
commands, shared files, library files, and others
/var Typically contains variable-length files such as log and print files and any other type of file that
may contain a variable amount of data
/sbin Contains binary (executable) files, usually for system administration. For
example, fdisk and ifconfig utlities
BAIT, SURAT 24
OS [1010206404] 2107020603014
The /etc/bashrc is executed for both interactive and non-interactive shells. /etc/bashrc or
/etc/bash.bashrc is the systemwide bash per-interactive-shell startup file. Is is used system wide
functions and aliases. However, Environmental stuff goes in /etc/profile file. The /etc/profile is
executed only for interactive shells .bashrc is a shell script that Bash runs whenever it is started
interactively. It initializes an interactive shell session. Bashrc runs on every interactive shell
launch.
BAIT, SURAT 25
OS [1010206404] 2107020603014
• Environment variables-
Environment variables or ENVs basically define the behavior of the environment. They can affect
the processes ongoing or the programs that are executed in the environment.
Syntax: $NAME
BAIT, SURAT 26
OS [1010206404] 2107020603014
BAIT, SURAT 27
OS [1010206404] 2107020603014
PRACTICAL-2
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *mythread1(void *vargp)
{
int i;
printf("Thread1\n");
for(i=1;i<=10;i++)
printf("i=%d\n",i);
printf("exit from thread1\n");
return NULL;
}
void *mythread2(void *vargp)
{
int j;
printf("Thread2\n");
for(j=1;j<=10;j++)
printf("j=%d\n",j);
printf("exit from thread2\n");
return NULL;
}
int main()
{
pthread_t tid;
BAIT, SURAT 28
OS [1010206404] 2107020603014
printf("Before Thread\n");
pthread_create(&tid,NULL,mythread1,NULL);
pthread_create(&tid,NULL,mythread2,NULL);
pthread_join(tid,NULL);
pthread_join(tid,NULL);
exit(0);
}
OUTPUT:
BAIT, SURAT 29
OS [1010206404] 2107020603014
PRACTICAL-3
AIM: Write a C program to creates a thread using pthreads library and let it
run its functions.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *mythread1(void *vargp)
{
int i;
printf("thread1\n");
for(i=1;i<=10;i++)
printf("i=%d\n",i);
printf("Exit from Thread1\n");
return NULL;
}
void *mythread2(void *vargp)
{
int j;
printf("thread2 \n");
for(j=1;j<=10;j++)
printf("j=%d\n",j);
printf("Exit from Thread2\n");
return NULL;
}
int main()
{
pthread_t tid;
BAIT, SURAT 30
OS [1010206404] 2107020603014
printf("Before Thread\n");
pthread_create(&tid,NULL,mythread1,NULL);
pthread_create(&tid,NULL,mythread2,NULL);
pthread_join(tid,NULL);
pthread_join(tid,NULL);
exit(0);
}
OUTPUT:
BAIT, SURAT 31
OS [1010206404] 2107020603014
PRACTICAL-4
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int pid;
pid=fork();
if(pid<0)
{
printf("\n Fork failed\n");
exit(-1);
}
else if(pid==0)
{
execlp("/bin/ls","ls","-l",NULL);
}
else
{
wait(NULL);
printf("\n child complete\n");
exit(0);
}
}
BAIT, SURAT 32
OS [1010206404] 2107020603014
OUTPUT:
BAIT, SURAT 33
OS [1010206404] 2107020603014
PRACTICAL-5
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int pfds[2];
char buf[30];
if(pipe(pfds)==-1)
{
perror("Pipe Failed");
exit(1);
}
if(!fork())
{
close(1);
dup(pfds[1]);
system("ls -l");
}
else
{
printf("Parent reading from Pipe \n");
while(read(pfds[0],buf,80))
printf("%s\n",buf);
BAIT, SURAT 34
OS [1010206404] 2107020603014
}
}
OUTPUT:
BAIT, SURAT 35
OS [1010206404] 2107020603014
PRACTICAL-6
AIM: Simulate the following CPU scheduling algorithms: (a) Round Robin (b)
SJF (c) FCFS (d) Priority
A) ROUND ROBIN-
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
printf("Enter the no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d:",i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice:");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
BAIT, SURAT 36
OS [1010206404] 2107020603014
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
printf("\nThe Average Turnaround time is:%f\n",att/n);
printf("\nThe Average Waiting time is:%f\n",awt/n);
return 0;
}
BAIT, SURAT 37
OS [1010206404] 2107020603014
OUTPUT:
B) FCFS-
PROGRAM:
#include<stdio.h>
int main()
{
int p[10],at[10],bt[10],ct[10],tat[10],wt[10],i,j,temp=0,n;
float awt=0,atat=0;
printf("enter no of proccess you want:");
scanf("%d",&n);
printf("enter %d process:",n);
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
printf("enter %d arrival time:",n);
BAIT, SURAT 38
OS [1010206404] 2107020603014
for(i=0;i<n;i++)
{
scanf("%d",&at[i]);
}
printf("enter %d burst time:",n);
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<(n-i);j++)
{
if(at[j]>at[j+1])
{
temp=p[j+1];
p[j+1]=p[j];
p[j]=temp;
temp=at[j+1];
at[j+1]=at[j];
at[j]=temp;
temp=bt[j+1];
bt[j+1]=bt[j];
bt[j]=temp;
}
}
}
ct[0]=at[0]+bt[0];
for(i=1;i<n;i++)
{
BAIT, SURAT 39
OS [1010206404] 2107020603014
temp=0;
if(ct[i-1]<at[i])
{
temp=at[i]-ct[i-1];
}
ct[i]=ct[i-1]+bt[i]+temp;
}
printf("\np\t A.T\t B.T\t C.T\t TAT\t WT");
for(i=0;i<n;i++)
{
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
atat+=tat[i];
awt+=wt[i];
}
atat=atat/n;
awt=awt/n;
for(i=0;i<n;i++)
{
printf("\nP%d\t %d\t %d\t %d \t %d \t %d",p[i],at[i],bt[i],ct[i],tat[i],wt[i]);
}
printf("\naverage turnaround time is %f\n",atat);
printf("\naverage wating timme is %f\n",awt);
return 0;
}
BAIT, SURAT 40
OS [1010206404] 2107020603014
OUTPUT:
C) PRIORITY-
PROGRAM:
#include<stdio.h>
#define max 10
int main()
{
int i,j,n,bt[max],p[max],wt[max],tat[max],pr[max],total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority: ");
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time and Priority For Process %d: ",i+1);
scanf("%d",&bt[i]);
scanf("%d",&pr[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
BAIT, SURAT 41
OS [1010206404] 2107020603014
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n;
total=0;
printf("\n\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
BAIT, SURAT 42
OS [1010206404] 2107020603014
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\n P%d\t\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n;
printf("\n\nAverage Waiting Time:%.2f",avg_wt);
printf("\nAvgerage Turnaround Time: %.2f\n",avg_tat);
return 0;
}
OUTPUT:
D) SJF-
PROGRAM:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
BAIT, SURAT 43
OS [1010206404] 2107020603014
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time For Process %d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
BAIT, SURAT 44
OS [1010206404] 2107020603014
}
avg_wt=(float)total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t\t%d\t\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n;
printf("\n\nAverage Waiting Time:%f",avg_wt);
printf("\nAverage Turnaround Time:%f\n",avg_tat);
return 0;
}
OUTPUT:
BAIT, SURAT 45
OS [1010206404] 2107020603014
PRACTICAL-7
1. FORK():
Fork system call is used for creating a new process, which is called child process, which runs
concurrently with the process that makes the fork() call (parent process). After a new child process
is created, both processes will execute the next instruction following the fork() system call. A child
process uses the same pc(program counter), same CPU registers, same open files which use in the
parent process.
PROGRAM:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
if (fork() == 0)
printf("HEllo from child..\n");
else
printf("Hello from parent..\n");
}
int main()
{
forkexample();
return 0;
}
BAIT, SURAT 46
OS [1010206404] 2107020603014
OUTPUT:
2. WAIT():
A call to wait() blocks the calling process until one of its child processes exits or a signal is
received. After child process terminates, parent continues its execution after wait system call
instruction.
⎯ Child process may terminate due to any of these:
• It calls exit();
• It returns (an int) from main
• It receives a signal (from the OS or another process) whose default action is to terminate.
PROGRAM:
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t cpid;
if (fork() == 0)
exit(0);
else
cpid = wait(NULL);
printf("parent pid = %d\n",getpid());
printf("child pid = %d\n",cpid);
return 0;
}
BAIT, SURAT 47
OS [1010206404] 2107020603014
OUTPUT:
3. EXEC():
This system call runs when an executable file in the context of an already running process that
replaces the older executable file. However, the original process identifier remains as a new
process is not built, but stack, data, head, data, etc. are replaced by the new process.
PROGRAM:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(int argc,char *argv_list[])
{
pid_t pid;
int ret = 1;
int status;
pid = fork();
if (pid == -1)
{
printf("Program can't fork, Error has been Occured!!!\n");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
printf("Child Process, pid: %u\n",getpid());
execv("ls", argv_list);
exit(0);
BAIT, SURAT 48
OS [1010206404] 2107020603014
}
else
{
printf("parent Process, pid:%u\n",getppid());
if (waitpid(pid, &status, 0) > 0){
if (WIFEXITED(status) && !WEXITSTATUS(status))
printf("Program Run Succesfully!!!\n");
else if (WIFEXITED(status) && WEXITSTATUS(status))
{
if (WEXITSTATUS(status) == 127)
{
printf("execv failed\n");
}
else
printf("program terminated normally," " but returned a non-zero status\n");
}
else
printf("program didn't terminate normally\n");
}
else
{
printf("waitpid() failed\n");
}
exit(0);
}
return 0;
}
BAIT, SURAT 49
OS [1010206404] 2107020603014
OUTPUT:
4. EXIT():
The exit() is a system call that is used to end program execution. This call indicates that the thread
execution is complete, which is especially useful in multi-threaded Environmentals. The operating
system reclaims resources spent by the process following the use of the exit() system function.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("START\n");
exit(0);
printf("END\n");
}
OUTPUT:
BAIT, SURAT 50
OS [1010206404] 2107020603014
PRACTICAL-8
BAIT, SURAT 51
OS [1010206404] 2107020603014
printf("\n %d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\t\tNO\t\t:");
else
{
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemory is Full, Remaining Processes cannot be accomodated");
printf("\n\nTotal Internal Fragmentation is:%d\n",tif);
printf("\nTotal External Fragmentation is:%d\n",ef);
return 0;
}
OUTPUT:
BAIT, SURAT 52
OS [1010206404] 2107020603014
PROGRAM:
#include<stdio.h>
void main()
{
int ms,mp[10],i,temp,n=0;
char ch = 'y';
printf("\n Enter the total memory available(In Bytes):");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d(In Bytes):",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for process %d",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full!!!");
break;
}
printf("\nDo you want to Continue?(Y/N):");
scanf(" %c",&ch);
}
printf("\nTotal Memory Available:%d\n", ms);
printf("\n\n\tProcess\t\t Memory Allocated");
BAIT, SURAT 53
OS [1010206404] 2107020603014
for(i=0;i<n;i++)
{
printf("\n\t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is: %d",ms-temp);
printf("\n\nTotal External Fragementation is: %d\n",temp);
}
}
OUTPUT:
BAIT, SURAT 54
OS [1010206404] 2107020603014
PRACTICAL-9
PROGRAM:
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void PROGRAM();
void show();
void cal();
int main()
{
int i,j;
PROGRAM();
show();
cal();
return 0;
return 0;
}
void PROGRAM()
{
int i,j;
printf("Enter the no of Processes:");
scanf("%d",&n);
printf("\nEnter the no of resources instances:");
scanf("%d",&r);
BAIT, SURAT 55
OS [1010206404] 2107020603014
BAIT, SURAT 56
OS [1010206404] 2107020603014
{
printf("%d\t",alloc[i][j]);
}
for(j=0;j<r;j++)
{
printf("%d\t",max[i][j]);
}
if(i==0)
{
for(j=0;j<r;j++)
printf("%d\t",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
BAIT, SURAT 57
OS [1010206404] 2107020603014
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
BAIT, SURAT 58
OS [1010206404] 2107020603014
for(i=0;i<n;i++)
{
if(finish[i]==1)
{
c1++;
}
else
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state..\n");
}
else
{
printf("\n Process are in dead lock..\n");
printf("\n System is in unsafe state..\n");
}
}
BAIT, SURAT 59
OS [1010206404] 2107020603014
OUTPUT:
BAIT, SURAT 60
OS [1010206404] 2107020603014
PRACTICAL-10
scanf("%d",&n);
printf("\nEnter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\nEnter no. of frames:");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("\nThe Page Replacement Process is:\n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
}if(k==f)
{
}
if(m[k]==rs[i])
break;
BAIT, SURAT 61
OS [1010206404] 2107020603014
m[count++]=rs[i];
pf++;
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\nThe number of Page Faults using FIFO are:%d\n",pf);
}
OUTPUT:
BAIT, SURAT 62
OS [1010206404] 2107020603014
B) LRU
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,k,min,rs[25],m[10],count[10],flag[25],n,f,pf=0,next=1;
printf("Enter the length of reference string:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]); flag[i]=0;
}
printf("Enter the number of frames:");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0; m[i]=-1;
}
printf("\nThe Page Replacement process is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
BAIT, SURAT 63
OS [1010206404] 2107020603014
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min]>count[j])min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No.: %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are: %d\n",pf);
return 0;
}
BAIT, SURAT 64
OS [1010206404] 2107020603014
OUTPUT:
C) LFU
PROGRAM:
#include<stdio.h>
int main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
printf("\nEnter number length of refrence string:");
scanf("%d",&m);
printf("\nEnter the reference string:");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the number of frames:");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
BAIT, SURAT 65
OS [1010206404] 2107020603014
a[i]=-1;
}
printf("\nThe Page Replacement Process is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
printf("\tPF No.:%d",pf);
}
printf("\n\n Total number of page faults using LFU are:%d\n",pf);
return 0;
}
BAIT, SURAT 66
OS [1010206404] 2107020603014
OUTPUT:
BAIT, SURAT 67