Os Lab Manual
Os Lab Manual
Os Lab Manual
Example:
OS PROGRAMMING LAB
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:
OS PROGRAMMING LAB
$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
$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
OS PROGRAMMING LAB
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
Exp.No: 1(c)
Study of Bash shell, Bourne shell and C shell in Unix/Os 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 Os.
- 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 Os free
of cost.
Exp.No: 1(d)
Exp.No: 1(e)
.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 automatically whenever 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
OS PROGRAMMING LAB
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
OS PROGRAMMING LAB
Exp.No: 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]);
}
OS PROGRAMMING LAB
Output:
$cc ioscalls.c
$cat>ff
Hi hello
$./a.out
ff Hi
hello
OS PROGRAMMING LAB
Exp.No: 3
Output:
$cc lslonglist.c
$./a.out
-rw-r- - r- -file1
-rw-r- - r- -file2
-rw-r- - r- -file3
-rw-r- - r- -file4
Child completed
OS PROGRAMMING LAB
Exp.No: 4
Output :
[student@gcet ~]$ cc
pipe Total 24
-rwxrwxr-x l student student 5563Aug 3 10:39 a.out
-rw-rw-r—l
Student student 340 jul 27 10:45 pipe2.c
-rw-rw-r—l student student
Pipes2.c
-rw-rw-r—l student student 401 34127 10:27 pipe2.c
student
OS PROGRAMMING LAB
#include<stdio.h
>
#include<conio.h
>
#include<process
.h>
#include<string.h
> void main()
char p[10][5];
int
et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,fou
nd=0,m; float avgwt;
clrscr();
for(i=0;i<n;i+
+)
{
printf("enter the process
name : "); scanf("%s",&p[i]);
printf("enter the processing
time : "); scanf("%d",&pt[i]);
}
m=n;
wt[0]=
0; i=0;
do
{
if(pt[i]>timer)
{
rt=pt[i]-timer;
strcpy(p[n],p[i
]); pt[n]=rt;
et[i]=timer;
n++;
OS PROGRAMMING LAB
}
else
{
et[i]=pt[i];
} i+
+;
wt[i]=wt[i-1]+et[i-1];
}while(i<n);
count=0;
for(i=0;i<m;i+
+)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count+
+;
found=j
;
}
}
if(found!=0)
{
wt[i]=wt[found]-
(count*timer); count=0;
found=0;
}
OS PROGRAMMING LAB
Expected Input/Output
INPUT :
enter the process name:
aaa
enter the processing
time : 4
enter the process name
: bbb
enter the processing
time : 3
enter the process name
: ccc
enter the processing
time : 2
enter the process name
: ddd
enter the processing
time : 5
enter the process
name: eee
enter the processing
time : 1
OUTPUT :
Original input/output:
b)SJF
Program:
#include<stdio.
h>
#include<conio.
h>
#include<process
.h> void main()
{
char p[10][5],temp[5];
int
tot=0,wt[10],pt[10],i,j,n,tem
p1; float avg=0;
clrscr();
OS PROGRAMMING LAB
printf("enter no of
processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d
name:\n",i+1); scanf("%s",&p[i]);
printf("enter process
time");
scanf("%d",&pt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[
i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
wt[0]=
0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+et[i-
1]; tot=tot+wt[i];
}
avg=(float)tot/n;
printf("p_name\t P_time\t
w_time\n"); for(i=0;i<n;i++)
printf("%s\t%d\t
%d\n",p[i],et[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=
%f",tot,avg); getch();
}
OS PROGRAMMING LAB
Expected input/output:
enter no of processes: 5
enter process1
name: aaa enter
process time: 4 enter
process2 name: bbb
enter process time: 3
enter process3
name: ccc enter
process time: 2 enter
process4 name: ddd
enter process time: 5
enter process5
name: eee enter
process time: 1
p_name P_time w_time
eee 1 0
ccc 2 1
bbb 3 3
aaa 4 6
ddd 5 10
total waiting
time=20 avg
waiting time=4.00
Original input/output:
OS PROGRAMMING LAB
Experiment #1
Program:
#include<stdio.h
>
#include<conio.h
>
#include<process
.h> void main()
{
char p[10][5];
int tot=0,wt[10],i,n;
float
avg=0;
clrscr();
printf("enter no of
processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d
name:\n",i+1); scanf("%s",&p[i]);
printf("enter process
time");
scanf("%d",&pt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+et[i-
1]; tot=tot+wt[i];
}
}
OS PROGRAMMING LAB
avg=(float)tot/n;
printf("p_name\t P_time\t
w_time\n"); for(i=0;i<n;i++)
printf("%s\t%d\t
%d\n",p[i],et[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=
%f",tot,avg); getch();
Expected input/output:
enter no of processes: 5
enter process1
name: aaa
eee 1 14
total waiting
time=34 avg
waiting time=6.80
Original output:
d)priority
Program:
#include<stdio.
h>
#include<conio.
h> void main()
{
char p[10][5],temp[5];
int
i,j,pt[10],wt[10],totwt=0,pr[10],temp
1,n; float avgwt;
clrscr();
OS PROGRAMMING LAB
printf("enter no of
processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d
name:",i+1); scanf("%s",&p[i]);
printf("enter process
time:");
scanf("%d",&pt[i]);
printf("enter priority:");
scanf("%d",&pr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pr[i]>pr[j])
{
temp1=pr[i];
pr[i]=pr[j];
pr[j]=temp1;
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[
i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
wt[0]=
0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+et[i-
1];
totwt=totwt+wt[i]
;
OS PROGRAMMING LAB
}
avgwt=(float)totwt/n;
printf("p_name\t p_time\t priority\t
w_time\n"); for(i=0;i<n;i++)
{
printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]);
}
printf("total waiting time=%d\n avg waiting time=
%f",tot,avg); getch();
}
Expected input/output:
enter no of processes: 5
enter process1
name: aaa
enter process time: 4
enter priority:5
enter process2
name: bbb
enter process time: 3
enter priority:4
enter process3
name: ccc
enter process time:
2 enter priority:3
enter process4
name: ddd
enter process time: 5
enter priority:2
enter process5
name: eee
enter process time:
1 enter priority:1
OS PROGRAMMING LAB
total waiting
time=26 avg
waiting time=5.20
Original Output :
D
OS PROGRAMMING LAB
Program
#include
<stdio.h>
#include
<stdlib.h>
#include <unistd.h> /* for fork
*/ #include <sys/types.h> /* for
pid_t */ #include <sys/wait.h>
/* for wait */
Expected Input/Output:
Original Input/Output:
OS PROGRAMMING LAB
Program
#include<stdio.
h>
#include<conio.
h> main()
int
i,m,n,tot,s[20];
clrscr();
OS:"); scanf("%d",&m);
for(i=0;i<n;i++)
%d:",i+1); scanf("%d",&s[i]);
}
OS PROGRAMMING LAB
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
%d",tot); getch();
}
Expected Input/Output:
Enter total memory size : 50
Enter no. of pages:4
Enter memory for OS :10
Enter size of page:10
Enter size of page:9
Enter size of page:9
Enter size of page:10
External Fragmentation is = 2
OS PROGRAMMING LAB
Original output:
OS PROGRAMMING LAB
Program
#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);
divided:");
scanf("%d",&n);
size=ms/n;
OS PROGRAMMING LAB
for(i=0;i<n;i+
+)
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();
Expected input/output:
Internal Fragmentation is = 4
Original output
OS PROGRAMMING LAB
OS PROGRAMMING LAB
Program
#include
<stdio.h>
#include
<stdlib.h> int
main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10],
safeSequence[10]; int p, r, i, j, process, count;
count = 0;
printf("Enter the no of
processes : "); scanf("%d", &p);
printf("\n\nEnter the no of
resources : "); scanf("%d", &r);
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
printf("%d ",Max[i][j]);
printf("\t\t");
printf("\n");
}
OS PROGRAMMING LAB
process = -1;
if(process != -1)
{
printf("\nProcess %d runs to completion!",
process + 1); safeSequence[count] = process +
1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process]
[j]; alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
OS PROGRAMMING LAB
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe
state!!\n"); printf("Safe Sequence : <
");
for( i = 0; i < p; i++)
printf("%d ",
safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
Output:
Enter the no of
processes : 5 Enter the
no of resources : 3
For process 2 : 3
2
2
For process 3 : 7
0
2
For process 4 : 2
2
2
OS PROGRAMMING LAB
For process 5 : 4
3
3
For process 2 : 2
0
0
For process 3 : 3
0
2
For process 4 : 2
1
1
For process 5 : 0
0
2
Process 2 runs to
completion! Max matrix:
Allocation
matrix:
753 010
000 000
702 302
222 211
433 002
Process 3 runs to
completion! Max matrix:
Allocation
OS PROGRAMMING LAB
matrix:
753 010
000 000
000 000
222 211
433 002
Process 4 runs to
completion! Max matrix:
Allocation
matrix: 7 5 30 1 0
000 000
000 000
000 000
433 002
OS PROGRAMMING LAB
Process 1 runs to
completion! Max matrix:
Allocation
matrix:
000 000
000 000
000 000
000 000
433 002
Process 5 runs to
completion! The system
is in a safe state!! Safe
Sequence : < 2 3 4 1 5 >
OS PROGRAMMING LAB
Program
#include<
stdio.h>
#include<
conio.h> void
main()
{
int allocated[15][15],max[15][15],need[15]
[15],avail[15],tres[15],work[15],flag[15]; int
pno,rno,i,j,prc,count,t,total;
count=
0;
clrscr();
printf("\n available
resources:\n"); for(j=1;j<=
rno;j++)
{
avail[j]=
0;
total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-
total;
work[j]=avail[j];
printf(" %d \t",work[j]);
}
do
{
for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
}
OS PROGRAMMING LAB
prc=0;
for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0
;
brea
k;
}
}
}
if(prc!
=0)
break;
}
if(prc!=0)
{
printf("\n Process %d
completed",i); count++;
printf("\n Available
matrix:"); for(j=1;j<=
rno;j++)
{
work[j]
+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(" %d",work[j]);
}
}
OS PROGRAMMING LAB
}
while(count!=pno&&prc!
=0);
if(count==pno)
printf("\nThe system is in a safe
state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OS PROGRAMMING LAB
Output:
Enter number of
of resources:3
Process 4 completed
Available matrix: 7 4 3
Allocated matrix Max
need
0 1 0 7 5 3 7 4 3
| |
0 0 0 0 0 0 0 0 0
| |
3 0 2 9 0 2 6 0 0
| |
0 0 0 0 0 0 0 0 0
| |
0 0 2 4 3 3 4 3 1
| |
Process 1 completed
Available matrix: 7 5 3
Allocated matrix Max
need
0 0 0 0 0 0 0 0 0
| |
0 0 0 0 0 0 0 0 0
| |
3 0 2 9 0 2 6 0 0
| |
0 0 0 0 0 0 0 0 0
| |
0 0 2 4 3 3 4 3 1
| |
Process 3 completed
Available matrix: 10 5 5
Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed
Available matrix: 10 5 7
The system is in a safe state!!
OS PROGRAMMING LAB
Program
include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF
PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i
]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&n
o);
for(i=0;i<no;i+
+) frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
OS PROGRAMMING LAB
for(i=1;i<=n;i+
+)
{ printf("%d\t\t",a
[i]); avail=0;
for(k=0;k<no;k+
+)
if(frame[k]==a[
i])
avail=1
; if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k+
+)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is
%d",count); return 0;
}
OS PROGRAMMING LAB
Output
ENTER THE NUMBER OF PAGES: 20
ENTER THE PAGE NUMBER : 70120304230321201
7 0 1 ENTER THE NUMBER OF FRAMES :3
ref string page frames
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
0
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
OS PROGRAMMING LAB
3
2
1 0 1 3
2 0 1 2
0
1
7 7 1 2
0 7 0 2
1 7 0 1
Page Fault Is 15
OS PROGRAMMING LAB
Program
#include<stdio.
h> main()
{
int
q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[
20]; printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference
string:"); for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of
frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t
%d\n",q[k]); c++;
k++;
OS PROGRAMMING LAB
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!
=q[j]) c1+
+;
}
if(c1==f)
{
c++;
if(k<f
)
{ q[k]=p[i
]; k++;
for(j=0;j<k;j++)
printf("\t
%d",q[j]);
} printf("\n");
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!
=p[j]) c2[r]
++;
else
brea
k;
}
OS PROGRAMMING LAB
}
for(r=0;r<f;r+
+) b[r]=c2[r];
for(r=0;r<f;r+
+)
{
for(j=r;j<f;j++)
OS PROGRAMMING LAB
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j
]; b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t
%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
OS PROGRAMMING LAB
OUTPUT:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
OS PROGRAMMING LAB
1 6 2
Program
#include<stdio.
h>
#include<conio.
h> main()
{
int
fr[5],i,j,k,t[5],p=1,flag=0,page[25],psz,nf,t1,u
[5]; clrscr();
printf("enter the number of
frames:"); scanf("%d",&nf);
printf("\n enter the page
size"); scanf("%d",&psz);
fr[i]=-1;
for(i=1; i<=psz; i++)
{
if(full(fr,nf)==
1) break;
else
{
flag=0;
for(j=1; j<=nf; j++)
{
if(page[i]==fr[j])
{
flag=1;
printf(" \t
%d:\t",page[i]); break;
}
}
if(flag==0)
{
fr[p]=page[i];
printf(" \t
%d:\t",page[i]); p++;
}
}
p=0;
for(; i<=psz; i++)
{
flag=0;
for(j=1; j<=nf; j++)
{
OS PROGRAMMING LAB
if(page[i]==fr[j])
{
flag=1
;
break
;
}
}
if(flag==0)
{
p++;
for(j=1; j<=nf; j++)
{
for(k=i+1; k<=psz; k++)
{
if(fr[j]==page[k])
{
u[j]=k
;
break
;
}
else
u[j]=21;
}
}
for(j=1; j<=nf; j+
+) t[j]=u[j];
for(j=1; j<=nf; j++)
{
for(k=j+1; k<=nf; k++)
OS PROGRAMMING LAB
if(t[j]<t[k])
{
t1=t[j];
t[j]=t[k
];
t[k]=t1;
}
}
OS PROGRAMMING LAB
}
for(j=1; j<=nf; j++)
{
if(t[1]==u[j])
{
fr[j]=page[i
]; u[j]=i;
}
}
printf("page fault\t");
}
else
printf(" \t");
printf("%d:\t",page
[i]); for(j=1; j<=nf;
j++)
printf(" %d ",fr[j]);
printf("\n");
}
printf("\ntotal page faults: %d",p+3);
// getch();
}
int full(int a[],int n)
{
int k;
for(k=1; k<=n; k++)
{
if(a[k]==-
1)
return
0;
OS PROGRAMMING LAB
}
return 1;
}
OUTPUT:
enter the number of
frames:5
Program
#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++)
scanf("%d",&b[i]);
%d",i+1); scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
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
%d",b[x-1]);
printf("blocks
occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
c[i][j]=sb[i]++;
OS PROGRAMMING LAB
Expected Input/Output:
Enter no.of files: 2
file1 2
block length
OS PROGRAMMING LAB
1 2 4
2 5 10
is:12803
Orginal output:
OS PROGRAMMING LAB
Program
#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++)
%d:",i+1); scanf("%d%d",&sb[i],&s[i]);
scanf("%d",&m[i]);
%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);
%d\n",x); i=x-1;
printf("Index is:%d",sb[i]);
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
Expected Input/Ouput:
enter blocks of
file1:3 2 5 4 6 7
2 6 4 7
file2:5
OS PROGRAMMING LAB
1 2 10
2 3 5
Original Output
Program
#include<stdio.
h>
#include<conio.
{
OS PROGRAMMING LAB
char fname[10];
int start,size,block[10];
}f[10];
main()
int i,j,n;
clrscr();
scanf("%d",&
n);
for(i=0;i<n;i+
+)
scanf("%s",&f[i].fname);
printf("Enter starting
block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
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\tblo
ck\n"); for(i=0;i<n;i++)
{
OS PROGRAMMING LAB
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();
}
OS PROGRAMMING LAB
45
32
25
Enter file
name:rajesh Enter
starting block:12
Enter no.of
blocks:5 Enter
block numbers:6
venkat 20 6 4--->12--->15--->45--->32--->25
rajesh 12 5 6--->5--->4--->3--->2
OS PROGRAMMING LAB
Original output:
LINUX PROGRAMMING LAB
EXP-11
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);
}
LINUX PROGRAMMING LAB
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);
LINUX PROGRAMMING LAB
}
if(shmdt(shm_addr)==-1)
{
perror(“main:shmdt:”);
}
if(shmctl(shm_id,IPC_RMID,&SHM_DESC)==-1)
{
perror(“main:shmctl:”);
}
return 0;
}
Output:
$cc shared.c $./a.out
Exp.No: 13 Aim:
}
}
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);
}
LINUX PROGRAMMING LAB
Output:
$cc pipe.c
$./a.out
1.PRODUCER
2.CONSUMER
3.EXIT
Enter your choice 1
producer produces the
item1
Enter your choice 2
consumer consumes
item2
Enter your choice 2
consumer consumes
item1
Enter your choice
1
BUFFER IS EMPTY
Enter your choice
3
LINUX PROGRAMMING LAB
Exp.No: 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);
}
LINUX PROGRAMMING LAB
Output:
$cc pthread.c
$./a.out
welcome to PThreads Library
LINUX PROGRAMMING LAB
Exp.No: 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);
Output:
$cc pthib.c
$./a.out
thread 1
i=1
i=2
i=3
thread 2
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
i=4
i=5
i=6
i=7
i=8
i=9
i=10
Exit from thread1
J=9
J=10
Exit from thread2