Os Lab Manual
Os Lab Manual
COLLEGE OF ENGINEERING
(AUTONOMOUS)
N.RAJUPALEM, NELLORE (DIST)
OPERATING SYSTEM
LAB MANUAL
(R-20 REGULATION)
II BTECH CSE –I SEM
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
NAME OF THE STUDENT
ROLL.NO
YEAR
BRANCH
SREE VENKATESWARA COLLEGE OF ENGINEERING
NAAC ‘A’ Grade Accredited Institution
An ISO 9001: 2015 Certified Institution
(Approved by AICTE, New Delhi and Affiliated to JNTU, Anantapur)
Northrajupalem (Vi), Kodavaluru (M) , S.P.S.R Nellore (Dt)-524316
PEO-3: To train the graduates to have basic interpersonal skills and a sense of
social responsibility that covers them a way to become good team members and
leaders.
SREE VENKATESWARA COLLEGE OF ENGINEERING
NAAC ‘A’ Grade Accredited Institution
An ISO 9001:: 2015 Certified Institution
(Approved by AICTE, New Delhi and Affiliated to JNTU, Anantapur)
Northrajupalem (Vi), Kodavaluru (M) , S.P.S.R Nellore (Dt)-524316
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.
b) Study of vi editor
c) Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system
d) Study of Unix/Linux file system (tree structure)
e) Study of .bashrc, /etc/bashrc and Environment variables.
2) Write a C program that makes a copy of a file using standard I/O, and system calls
3) Write a C program to emulate the UNIX ls –l command.
4) Write a C program that illustrates how to execute two commands concurrently with a command pipe.
Ex: - ls –l | sort
5) Simulate the following CPU scheduling algorithms:
(a) Round Robin (b) SJF (c) FCFS (d) Priority
6) Multiprogramming-Memory management-Implementation of fork (), wait (), exec() and exit (), System calls
7) Simulate the following:
a) Multiprogramming with a fixed number of tasks (MFT)
b) Multiprogramming with a variable number of tasks (MVT)
8) Simulate Bankers Algorithm for Dead Lock Avoidance
9) Simulate Bankers Algorithm for Dead Lock Prevention.
10) Simulate the following page replacement algorithms:
a) FIFO b) LRU c) LFU
11) Simulate the following File allocation strategies
(a) Sequenced (b) Indexed (c) Linked
12) Write a C program that illustrates two processes communicating using shared memory
13) Write a C program to simulate producer and consumer problem usingsemaphores
14) Write C program to create a thread using pthreads library and let it run its function.
15) Write a C program to illustrate concurrent execution of threads using pthreads library.
Additional exercises
1. Study of Unix/Linux Basic Filters command list : grep , sed , awk
2. Study of Unix/Linux Basic Text processing utility command list : head, tail ,paste ,cmp, diff ,comm,
join
UNIX OPERATING SYSTEM LAB MANUAL
Example:
$cat > newfile
Hi
Hell
Welcome
$
The input operation is terminated by using <ctrl+d> command
5) The cd command:
cd - change directories
It is a command-line OS shell command used to change the current working
directory
Example:
$cd user1
user1]$
6) The cp command:
cp - copy files and directories
This command is used to create a duplicate of a file, a set of files or a directory
The cp command copies both text and binary files
Syntax:
$cp source-file/directory destination-file/directory
Examples:
i) $cp file1 file2
ii) $cp dir1/file1 dir1/file2
7) The ps command:
ps - report a snapshot of the current processes.
The ps command is used to display the attributes of processes that are running
currently.
Example:
$ps
PID TTY TIME CMD
476 tty03 00:00:01 login
659 tty03 00:00:01 sh
684 tty03 00:00:00 ps
$
8) The ls command:
ls - list directory contents
This command lists the contents in a directory.
Syntax:
$ls
Example:
$ls
9) The mv command:
mv - move (rename) files
This command is used to move either an individual file, a list of files or a directory
Syntax:
$mv source-file/directory destination-file/directory
Example:
$mv dir1/file1 dir2
The mv command is also used to rename a file or directory
Syntax:
$mv old-file-name new-file-name
Example:
$mv file1 file7
Syntax:
$rm filename
Example:
$rm file1
Using this command, the user can display the current date along with the time
nearest to the second
Example:
$date
Sat Jan 10 11:58:00 IST 2004
$
4 history
Exp.No: 1(b)
Study of vi editor.
Vi Editor:
- An editor allows the users to see a portion of a file on the screen and to modify
characters and lines by simply typing at the cursor position.
- The vi editor represents,
- Vi stands for visual
- It is a full screen editor and allows the user to view and edit the entire
document at the same time.
- Vi is case sensitive.
- It has powerful undo features.
Modes of Vi editor:
i) Command mode:
- In this mode all the keys pressed by the user are interpreted to be editor
commands.
- No text is displayed on the screen even if corresponding keys is pressed on the
keyboard.
ii) Insert mode:
- This mode permits to insert a new text, editing and replacement of existing text.
- When vi editor is in insert mode the letters typed at the keyboard are echoed on
the screen.
iii) Escape mode:
- Commands typed at the command line.
Starting with vi editor:
Syntax:
$vi filename
Saving text:
:w – save the file and remains in edit mode
:wq – save the file and quits from edit mode
:q – quit without changes from edit mode
Quitting vi:
Press zz or ‘:wq’ in command mode.
Exp.No: 1(c)
Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system.
Types of Shells:
- The Shells are 4 types
a) The Bourne Shell (sh)
b) The C Shell (csh)
c) The Korn Shell (ksh)
d) The Bourne-Again Shell (bash)
a) The Bourne Shell (sh):
- This is the most common shell available on Unix Systems.
- The first major shell developed by Stephen Bourne at AT&T Bell labs.
- This shell is widely used.
- This shell is distributed as the standard shell on almost all Unix Systems.
b) The C Shell (csh):
- It is developed by Bill Joy at UCB as part of the BSD release.
- Its syntax and usage is very similar to the C programming language.
- This shell is not available on all machines.
- Shell scripts written in the C shell are not compatible with the Bourne Shell.
- A version of it called tcsh is available free of cost under Linux.
c) The Korn Shell (ksh):
- This shell was developed by David Korn at AT&T Bell labs.
- Basically it is built on the Bourne Shell.
- It also incorporates certain features of the C shell.
- At present it is one of the widely used shells.
- It can run Bourne shell scripts without any modification.
- One of its versions, the Public Domain Korn Shell (pdksh), comes with Linux
free of cost.
d) The Bourne-Again Shell:
- It was developed by B Fox and C Ramey at Free Software Foundation.
- Certain Linux operating system variants come with this shell as its default shell.
- This is clearly a free ware shell.
Exp.No: 1(d)
Study of Unix/Linux file system (tree structure).
Unix/Linux file system (tree structure):
- It is also called as UNIX File System, because it is used to organize UNIX files.
- The different directories in the Directory Hierarchy are,
Directory Meaning
root (/) Root of the file system
/bin Essential programs in executable form (binaries)
/tmp Temporary files; cleaned when system is restarted
/dev Device files
/etc System miscellany
/sbin System binaries
/usr User file system
/var Variables
/usr/lib Libraries for C, FORTRAN. Etc
/usr/bin User binaries
Exp.No: 1(e)
Study of .bashrc, /etc/bashrc and Environment variables.
.bashrc
.bashrc file is automatically executed when new terminal(shell) is opened.
Purpose of bashrc file:
• You can export environment variables(So there is no need to export
environment variable every time)
• You can define aliases
• You can provide the path for cross compiler
• You can add your own script which can start automaticallywhenever
new shell is opened.
• You can change the history length
/etc/bashrc
• Like .bash_profile you will also commonly see a .bashrc file in your home
directory. This file is meant for setting command aliases and functions used
by bash shell users.
• Just like the /etc/profile is the system wide version of .bash_profile. The
/etc/bashrc for Red Hat and /etc/bash.bashrc in Ubuntu is the system wide
version of .bashrc.
Interestingly enough in the Red Hat implementation the /etc/bashrc also executes
the shell scripts within /etc/profile.d but only if the users shell is a Interactive Shell
(aka Login Shell)
Environment Variables:
variable purpose
PATH The PATH variable holds a list of directories in a certain order. In this
list colon (:) separate different directories
HOME When a user logs in, he or she will be automatically placed in the home
directory. This directory is decided by the system administrator at the
time of opening an account for a user.This directory is stored in the file
/etc/passwd
IFS This variable holds tokens used by the shell commands to parse a string
into substrings such as a word or a record into its individual fields. The
default tokens are the three white space tokens Space, Tab, New line
MAIL This variable holds the absolute pathname of the file where user’s mail
is kept. Usually the name of this file is the user’s login name
SHELL This variable contains the name of the users shell program in the form of
absolute pathname. System administrator sets the default shell If
required, user can change it
TERM This variable holds the information regarding the type of the terminal
being used. If TERM is not set properly, utilities like vi editor will not
work
2. Aim:
Write a C Program that makes a copy of a file using standard I/O and system
calls.
Source Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
void typefile (char *filename)
{
int fd, nread;
char buf[1024];
fd = open (filename, O_RDONLY);
if (fd == -1) {
perror (filename);
return;
}
while ((nread = read (fd, buf, sizeof (buf))) > 0)
write (1, buf, nread);
close (fd);
}
int main (int argc, char **argv)
{
int argno;
for (argno = 1; argno<argc; argno++)
typefile (argv[argno]);
}
Output:
3. Aim:
Write a C program to emulate the Unix ls -l command.
Source Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int pid; //process id
pid = fork(); //create another process
if ( pid< 0 )
{ //fail
printf(“\nFork failed\n”);
exit (-1);
}
else if ( pid == 0 )
{ //child
execlp ( “/bin/ls”, “ls”, “-l”, NULL ); //execute ls
}
else
{ //parent
wait (NULL); //wait for child
printf(“\nchild complete\n”);
}
Output:
4. Aim:
Write a program that illustrates how to execute two commands concurrently with
a command pipe.
Source Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.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);
}
}
Output:
5. Aim:
Write a C program to implement the various process scheduling mechanisms such
as
A)Round Robin
Scheduling. Source
Code: #include<stdio.h>
#include<conio.h>
main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no \t Burst time \t Wait time \t Turn around time\n");
for(i=0;i<n;i++)
printf("%d \t%d \t%d \t%d\n",i+1,bt[i],wt[i],tat[i]);
printf("Avg wait time is %f \n Avg turn around time is %f",awt,atat);
getch();
}
Output:
(b)
Aim:
Write a C program to implement the various process scheduling mechanisms such
as
SJF Scheduling.
Source Code:
#include<conio.h>
#include<stdio.h>
void main()
{
int i, j, n, process[10], total=0, wtime[10], ptime[10], temp, ptemp;
float avg=0;
clrscr();
printf("\nEnter number of Processes:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Process %d ID:",i+1);
scanf("%d", &process[i]);
printf("\nEnter Process %d Time:",i+1);
scanf("%d",&ptime[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(ptime[i]>ptime[j])
{
temp = ptime[i];
ptime[i] = ptime[j];
ptime[j] = temp;
ptemp = process[i];
process[i] = process[j];
process[j] = ptemp;
}
}
}
wtime[0]=0;
for(i=1;i<n;i++)
{
wtime[i]=wtime[i-1]+ptime[i-1];
total=total+wtime[i];
}
avg=(float)total/n;
printf("\nP_ID\t P_TIME\t W_TIME\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",process[i],ptime[i],wtime[i]);
printf("\nTotal Waiting Time: %d \nAverage Waiting Time: %f", total, avg);
getch();
}
Output:
(c)
Aim:
Write a C program to implement the various process scheduling mechanisms.
Source Code:
#include<stdio.h>
void main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
clrscr();
printf("Enter number of processors:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of the process %d",i+1);
scanf("\n %d",&t[i]);
}
printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
printf("1 \t\t 0 \t\t %d \n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n Average Waiting Time %4.2f",awt);
Output:
(d)
Aim:
Write a C program to implement the various process scheduling mechanisms such
as
Priority Scheduling.
Source Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
float awt,atat;
clrscr();
printf("\n-----------PRIORITY SCHEDULING ------------- \n");
printf("Enter the No of Process: ");
scanf("%d", &n);
for (i=0;i<n;i++)
{
pid[i] = i;
printf("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);
}
for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if (pr[i] > pr[j] )
{
t = pr[i];
pr[i] = pr[j];
pr[j] = t;
t = bt[i];
bt[i] = bt[j];
bt[j] = t;
t = pid[i];
pid[i] = pid[j];
pid[j] = t;
}
}
tat[0] = bt[0];
wt[0] = 0;
for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}
printf("\n \n");
printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n");
printf("\n -\n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat+tat[i];
twt = twt + wt[i];
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time: %f\n",awt,atat);
getch();
}
Output:
6. Aim:
Write a program for Multiprogramming-Memory management- Implementation of
fork (), wait (), exec() and exit (), System calls
Source Code:
#include<stdio.h>
#include<unistd.h>
main( )
{
int i, pid;
pid=fork( );
if(pid== -1)
{
perror(“fork failed”);
exit(0);
}
else if(pid==0)
{
printf(“\n Childprocess starts‟);
for(i=0; i<5; i++)
{
printf(“\n Child process %d is called”, i);
}
printf(“\n Child process ends”);
}
else
{
wait(0);
printf(“\n Parent process ends”);
}
exit(0);
}
Output:
7. Aim:
aWrite a program to implement Dynamic allocation of memories in MVT.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int i,m,n,tot,s[20];
clrscr();
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter no. of pages:");
scanf("%d",&n);
printf("Enter memory for OS:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("Enter size of page%d:",i+1);
scanf("%d",&s[i]);
}
tot=tot-m;
for(i=0;i<n;i++)
{
if(tot>=s[i])
{
printf("Allocate page %d\n",i+1);
tot=tot-s[i];
}
else
printf("process p%d is blocked\n",i+1);
}
printf("External Fragmentation is=%d",tot);
getch();
}
Output:
(b)
Aim:
Write a program to implement Dynamic allocation of memories in MVT.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int ms,i,ps[20],n,size,p[20],s,intr=0;
clrscr();
printf("Enter size of memory:");
scanf("%d",&ms);
printf("Enter memory for OS:");
scanf("%d",&s);
ms-=s;
printf("Enter no.of partitions to be divided:");
scanf("%d",&n);
size=ms/n;
for(i=0;i<n;i++)
{
printf("Enter process and process size");
scanf("%d%d",&p[i],&ps[i]);
if(ps[i]<=size)
{
intr=intr+size-ps[i];
printf("process%d is allocated\n",p[i]);
}
else
printf("process%d is blocked",p[i]);
}
printf("total fragmentation is %d",intr);
getch();
}
Output:
8&9Aim:
To implement deadlock avoidance & Prevention by using Banker’s Algorithm.
Deadlock avoidance & Dead Lock Prevention
Source Code:
#include<stdio.h>
#include<conio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{
int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;
clrscr();
printf("\n ENTER THE NO. OF PROCESSES:");
scanf("%d",&n);
printf("\n ENTER THE NO. OF RESOURCES:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("PROCESS %d \n",i+1);
for(j=0;j<r;j++)
{
printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCE %d:",j+1);
scanf("%d",&p[i].a1[j]);
p[i].need[j]=p[i].max[j]-p[i].a1[j];
}
}
for(i=0;i<r;i++)
{
printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);
scanf("%d",&tot[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
temp=temp+p[j].a1[i];
av[i]=tot[i]-temp;
temp=0;
}
printf("\n\t RESOURCES ALLOCATED NEEDED TOTAL AVAIL");
for(i=0;i<n;i++)
{
printf("\n P%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].a1[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].need[j]);
printf("\t");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" ");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",av[j]);
}
}
printf("\n\n\t AVAIL BEFORE\T AVAIL AFTER ");
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j] >av[j])
cn++;
if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
printf("\n P %d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;
}
else
{
cn=0;cz=0;
}
}
}
if(c==n)
printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");
else
printf("\n DEADLOCK OCCURED");
getch();
}
Output:
10Aim:
To implement page replacement algorithms a)FIFO (First In First Out)
Source code:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
clrscr();
printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of frames ... ");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\t No.of pages faults...%d",pf);
getch();
}
Output:
(b)
Aim:
To implement page replacement algorithm LRU (Least Recently Used)
Source code:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames ... ");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\n\t The given reference string:");
printf("\n ..................................................");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
SREE VENKATESHWARA COLLEGE OF ENGINEERING Page42
UNIX OPERATING SYSTEM LAB MANUAL
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
Output:
(c)
Aim:
To implement page replacement algorithms like LFU (Least Frequently Used)
Source Code:
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String\n");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
printf("\n");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{
Output:
11. Aim:
Write a C Program to implement
a)Sequential File Allocation method.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
Output:
(b)
Aim:
Write a C Program to implement Indexed File Allocation method.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
Output:
(c)
Aim:
Write a C Program to implement Linked File Allocation method.
Source Code:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
Output:
12. Aim:
Write a C program that illustrates two processes communicating using shared
memory.
Source Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
Struct country
{
char name[30];
char capital_city [30];
char currency[30];
int population;
};
int main(intargc,char*argv[])
{
int shm_id;
char*shm_addr;
int*countries_num;
struct country*countries;
structshmid_dsshm_desc;
shm_id=shmget(100,2048,IPC_CREAT|IPC_EXCL\0600);
if(shm_id==-1){
perror(“main:shmget:”);
exit(1);
}
shm_addr=shmat(shm_id,NULL,0);
if(!shm_addr){
ierror(“main:shmat:”);
exit(1);
}
SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 56
UNIX OPERATING SYSTEM LAB MANUAL
countries_num=(int*)shm_addr;
*countries_num=0;
countries=(struct country*)((void*)shm_addrsizeof(int));
strcpy(countries[0],name,”U.S.A”);
strcpy(countries[0],capital_city,”WASHINGTON”);
strcpy(countries[0],currency,”U.S.DOLLAR”);
countries[0].population=250000000;
( countries_num) ;
strcpy(countries[1].name,”israel”);
strcpy(countries[1].capital_city,”jerushalem”);
strcpy(countries[1].currency,”NEW ISRAEL SHEKED”);
countries[1].population=6000000;
(*countries_num) ;
strcpy(countries[2].name,”France”);
strcpy(countries[2].capital_city,”paris”);
strcpy(countries[2].currency,”Frank”);
countries[2].population=60000000;
(*countries_num) ;
for(i=0;i<(*countries_num);i )
{
printf(“country%d:\n”,i 1);
printf(“name:%d:\n”,i 1);
printf(“currency:%s:\n”,countries[i].currency);
printf(“population:%d:\n”,countries[i].population);
}
if(shmdt(shm_addr)==-1){
perror(“main:shmdt:”);
}
if(shmctl(shm_id,IPC_RMID,&SHM_DESC)==-1)
{
perror(“main:shmctl:”);
}
return 0;
SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 57
UNIX OPERATING SYSTEM LAB MANUAL
Output:
13. Aim:
Write a C program to simulate producer and consumer problem using semaphores
Source Code:
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1)
{
printf("\nENTER YOUR CHOICE\n");
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;
}
SREE VENKATESHWARA COLLEGE OF ENGINEERING Page 59
UNIX OPERATING SYSTEM LAB MANUAL
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);
}
Output:
14. Aim:
Write C program to create a thread using pthreads library and let it run its function
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *mythread(void *vargp)
{
sleep(1);
printf("welcome to PThreads Library\n");
return NULL;
}
int main()
{
pthread_tid;
printf("before thread\n");
pthread_create(&tid,NULL,mythread,NULL);
pthread_join(tid,NULL);
exit(0);
}
Output:
15. Aim:
Write a C program to illustrate concurrent execution of threads using pthreads
Library
Source Code:
#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_tid;
printf("before thread\n");
pthread_create(&tid,NULL,mythread1,NULL);
pthread_create(&tid,NULL,mythread2,NULL);
pthread_join(tid,NULL);
pthread_join(tid,NULL)
}