OS Lab
OS Lab
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.
4) Write a C program that illustrates how to execute two commands concurrently with a
command pipe. Ex: - ls –l | sort.
13) Write a C program to simulate producer and consumer problem using semaphores.
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.
R20 II-I CSE UNIX PROGRAMMING LAB
EXPERIMENT - 1
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.
MAN Command:
man is the system's manual viewer; it can be used to display manual pages, scroll up
and down, search for occurrences of specific text, and other useful functions.
Syntax:
$ man cat
WHO Command:
Cat Command
cat filename
cat is used to display the contents of the file.
Cd Command.
R20 II-I CSE UNIX PROGRAMMING LAB
Cd directoryname
Cal command
cal command will print the calendar on current month by default. If you want to print
calendar of august of 1965. That's eighth month of 1965.
August 1965
S M Tu W Th F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Pwd command.
pwd command will print your home directory on screen, pwd means print working
directory.
/home/satish
Ls command
ls command is most widely used command and it displays the contents of directory.
options
• ls will list all the files in your home directory, this command has many options.
R20 II-I CSE UNIX PROGRAMMING LAB
• ls -l will list all the file names, permissions, group, etc in long format.
• ls -a will list all the files including hidden files that start with . .
• ls -lt will list all files names based on the time of creation, newer files bring first.
• ls -Rwill lists all the files and files in the all the directories, recursively.
• ls -R | more will list all the files and files in all the directories, one page at a time.
Mkdir command
mkdir aditya
will create new directory, i.e. here aditya directory is created.
Cd command
cd aditya
will change directory from current directory to aditya directory. Use pwd to check your
current directory and ls to see if aditya directory is there or not.
Chmod command
cal.txt.
initially when this file will be created the permissions for this file depends upon umask set
in your profile files. As you can see this file has
time16:14 and at the end there is name of this file. Learn to read these permissions in
binary, like this for example Decimal 644 which is 110 100 100 in binary meand rw-r--r--
or user can read,write this file, group can read only, everyone else can read only. Similarly,
if permissions are 755 or 111 101 101 that means rwxr-xr-x or user can read, write and
execute, group can read and execute, everyone else can read and execute. All directories
have d in front of permissions. So if you don't want anyone to see your files or to do
anything with it use chmod command and make permissions so that only you can read and
write to that file, i.e.
chmod 600 filename.
mkdir Command
rm Command
The rm command removes (deletes) files or directories.
rm filename Deletes filename
rmdir Command
Date command.
Date displays todays date, to use it type date at prompt.
Sun Dec 7 14:23:08 EST 1997
is similar to what you should see on screen.
The time command runs the specified program command with the given arguments.
When command finishes, time writes a message to standard error giving timing statistics
about this program run.
R20 II-I CSE UNIX PROGRAMMING LAB
PS command
ps command is probably the most useful command for systems administrators. It reports
information on active processes.
ps options
Options.
MV Command
Kill Command
The command kill sends the specified signal to the specified process or process group. If
no signal is specified, the TERM signal is sent.
More Command
Viewing file contents page by page.
R20 II-I CSE UNIX PROGRAMMING LAB
EXPERIMENT- 2
Aim : Write a C program that makes a copy of a file using standard I/O, and system
calls
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
if(argc != 3){
printf ("Usage: cp file1 file2");
return 1;
}
/* Copy process */
while((ret_in = read (input_fd, &buffer, BUF_SIZE)) > 0){
R20 II-I CSE UNIX PROGRAMMING LAB
return (EXIT_SUCCESS);
}
EXPERIMENT- 3
EXPERIMENT- 4
Write a C program that illustrates how to execute two commands concurrently with a
command pipe. Ex: - ls –l | sort
#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:
EXPERIMENT- 5
Simulate the following CPU scheduling algorithms:
(a) Round Robin (b) SJF (c) FCFS (d) Priority
#include<stdio.h>
#include<conio.h>
void main()
int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
clrscr();
scanf("%d",&n);
n1=n;
scanf("%d",&ts);
for(i=1;i<=n;i++)
scanf("%d",&pid[i]);
scanf("%d",&bt[i]);
need[i]=bt[i];
R20 II-I CSE UNIX PROGRAMMING LAB
for(i=1;i<=n;i++)
flag[i]=1;
wt[i]=0;
while(n!=0)
for(i=1;i<=n;i++)
if(need[i]>=ts)
for(j=1;j<=n;j++)
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
need[i]-=ts;
if(need[i]==0)
flag[i]=0;
n--;
else
for(j=1;j<=n;j++)
R20 II-I CSE UNIX PROGRAMMING LAB
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
need[i]=0;
n--;
flag[i]=0;
for(i=1;i<=n1;i++)
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
awt=(float)twt/n1;
atat=(float)ttat/n1;
for(i=1;i<=n1;i++)
printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
getch();
OUTPUT:
1 5 10 15 25
2 6 15 25 40
3 7 20 25 45
4 8 25 20 45
#include<stdio.h>
void main()
int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
int t[10],p[10];
float awt,atat;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("\n %d",&t[i]);
for(i=0;i<n;i++)
p[i]=i;
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(t[i]>t[k])
int temp;
R20 II-I CSE UNIX PROGRAMMING LAB
temp=t[i];
t[i]=t[k];
t[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
wt[0]=0;
for(i=0;i<n;i++)
sum=0;
for(k=0;k<i;k++)
wt[i]=sum+t[k];
sum=wt[i];
for(i=0;i<n;i++)
tt[i]=t[i]+wt[i];
for(i=0;i<n;i++)
R20 II-I CSE UNIX PROGRAMMING LAB
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
twt=twt+wt[i];
ttat=ttat+tt[i];
awt=(float)twt/n;
atat=(float)ttat/n;
getch();
OUTPUT:
1 3 0 3
R20 II-I CSE UNIX PROGRAMMING LAB
0 4 3 7
2 5 7 12
c) FCFS Scheduling :
#include<stdio.h>
void main()
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("\n %d",&t[i]);
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;
R20 II-I CSE UNIX PROGRAMMING LAB
ttat=ttat+tat;
printf("\n\n");
awt=(float)twt/n;
atat=(float)ttat/n;
getch();
OUTPUT:
1 0 2
2 2 7
3 7 11
d) Priority Scheduling:
#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();
scanf("%d", &n);
for (i=0;i<n;i++)
pid[i] = i;
scanf("%d",&bt[i]);
scanf ("%d",&pr[i]);
// Sorting start
for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
t = pr[i];
R20 II-I CSE UNIX PROGRAMMING LAB
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++)
printf("\n \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];
R20 II-I CSE UNIX PROGRAMMING LAB
awt = (float)twt / n;
atat = (float)ttat / n;
getch();
OUTPUT:
2 1 4 0 4
1 2 6 4 10
0 3 2 10 12
3 7 5 12 17
Experiment-6
Multiprogramming-Memory management-Implementation of fork (), wait (), exec() and
exit (), System calls.
#include<stdio.h>
void main()
{
int pid1,pid2,pid3;
printf("Parent id is %d and root id is%d\n",getpid(),getppid());
pid1=fork();
if(pid1==0)
{
printf("Process 1 id is %d and its parent id is %d\n",getpid(),getppid());
pid2=fork()
;
}
if(pid2==0)
{
printf("Process 2 id is %d and its parent id is %d\n",getpid(),getppid());
pid3=fork();
}
if(pid1==0&&pid2==0&&pid3==0)
{
printf("Process 3 id is %d and its parent id is %d\n",getpid(),getppid());
}
}
Output
$ cc process1.c$
a.out
Parent id is 3553 and root id is 2495
Process 1 id is 3554 and its parent id is 3553
EXEC1( ) :
#include<stdio.h>
#include<sys/types.h>
include<unistd.h>
void main()
{
R20 II-I CSE UNIX PROGRAMMING LAB
int pid1;
pid1=fork();
if(pid1==0)
{
printf("Process id is %d ",getpid());
printf("and its parent id is %d”,getppid());
execl("/bin/who","who",0);
}
}
Output
$ cc program2.c
$ a.out
Experiment-7
Simulate the following:
a) Multiprogramming with a fixed number of tasks (MFT)
b) Multiprogramming with a variable number of tasks (MVT)
Program:
#include<stdio.h>
#include<conio.h>
main()
int i,m,n,tot,s[20];
clrscr();
scanf("%d",&tot);
scanf("%d",&n);
scanf("%d",&m);
for(i=0;i<n;i++)
scanf("%d",&s[i]);
tot=tot-m;
for(i=0;i<n;i++)
if(tot>=s[i])
R20 II-I CSE UNIX PROGRAMMING LAB
tot=tot-s[i];
else
getch();
}
R20 II-I CSE UNIX PROGRAMMING LAB
OUTPUT:
External Fragmentation is = 2
R20 II-I CSE UNIX PROGRAMMING LAB
Program:
#include<stdio.h>
#include<conio.h>
main()
int ms,i,ps[20],n,size,p[20],s,intr=0;
clrscr();
scanf("%d",&ms);
scanf("%d",&s);
ms-=s;
scanf("%d",&n);
size=ms/n;
for(i=0;i<n;i++)
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
R20 II-I CSE UNIX PROGRAMMING LAB
printf("process%d is blocked",p[i]);
getch();
OUTPUT:
Internal Fragmentation is = 4
R20 II-I CSE UNIX PROGRAMMING LAB
Experiment-8
Simulate Bankers Algorithm for Dead Lock Avoidance
#include<stdio.h>
#include<conio.h>
void main()
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
//clrscr();
scanf("%d",&n);
scanf("%d",&r);
scanf("%d",&totext[k]);
scanf("%d",&resalloc);
scanf("%d",&p);
scanf("%d",&newreq[k]);
if(i!=p)
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
block[p]=0;
run[p]=0;
j=0;
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
R20 II-I CSE UNIX PROGRAMMING LAB
if(totext[k]-totalloc[k]<0)
u=1;
break;
if(u==0)
simalloc[k]=totalloc[k];
if(active[i]==1)
j=0;
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
j=1;
break;
if(j==0)
R20 II-I CSE UNIX PROGRAMMING LAB
active[i]=0;
simalloc[k]=resalloc[i][k];
m=0;
resreq[p][k]=newreq[k];
else
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
getch();
OUTPUT:
Enter the
no of
processes
:4 Enter
the no
ofresourc
e
classes:3
Enter the total existed
resource in each class:3 2 2
Enter the allocated
resources:1 0 0 5 1 1 2 1 1 0
0 2Enter the process making
the new request:2
Enter the requested resource:1 1 2
Enter the process which are n
blocked or running:process 2:1 2
process 4:
1 0
process 5:
1 0
R20 II-I CSE UNIX PROGRAMMING LAB
Experiment-9
Simulate Bankers Algorithm for Dead Lock Prevention.
#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();
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];
}
}
prc=0;
for(i=1;i<= pno;i++)
{
R20 II-I CSE UNIX PROGRAMMING LAB
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0;
break;
}
}
}
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]);
}
}
}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();
}
OUTPUT
available resources:
2 3 0
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!!
R20 II-I CSE UNIX PROGRAMMING LAB
Experiment-10
Simulate the following page replacement algorithms:
a) FIFO b) LRU c) LFU
a) FIFO
Program:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
clrscr();
scanf("%d",&nof);
scanf("%d",&nor);
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
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++)
{
R20 II-I CSE UNIX PROGRAMMING LAB
flag=0;
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]);
getch();
OUTPUT:
564123
...................................... 5 6 4 1 2 3
Reference np5-> 5 -1 -1 -1
Reference np6-> 5 6 -1 -1
Reference np4-> 5 6 4 -1
Reference np1-> 5 6 4 1
Reference np2-> 2 6 4 1
Reference np3-> 2 3 4 1
b) LRU
Program:
#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();
scanf("%d",&nof);
scanf("%d",&nor);
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n…............................................. ");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
R20 II-I CSE UNIX PROGRAMMING LAB
lrucal[i]=0;
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
flag=0;
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];
R20 II-I CSE UNIX PROGRAMMING LAB
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
recent[ref[i]]=i;
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;
}
R20 II-I CSE UNIX PROGRAMMING LAB
OUTPUT:
654231
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
c) Optimal
Program:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
clrscr();
printf("\n................................ ");
scanf("%d",&nof);
scanf("%d",&nor);
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n............................... ");
printf("\n....................\n");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
R20 II-I CSE UNIX PROGRAMMING LAB
frm[i]=-1;
optcal[i]=0;
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
if(frm[j]==ref[i])
flag=1;
break;
if(flag==0)
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
R20 II-I CSE UNIX PROGRAMMING LAB
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
getch();
int i,j,temp,notfound;
for(i=0;i<nof;i++)
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
notfound=0;
optcal[i]=j;
break;
if(notfound==1)
return i;
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
R20 II-I CSE UNIX PROGRAMMING LAB
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
OUTPUT:
654231
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
Experiment-11
Simulate the following File allocation strategies
(a) Sequenced (b) Indexed (c) Linked
(a) Sequenced
Program:
#include<stdio.h>
#include<conio.h>
main()
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
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]);
scanf("%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:
1 2 4
2 5 10
b) Indexed:
Program:
#include<stdio.h>
#include<conio.h>
main()
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d",&sb[i],&s[i]);
scanf("%d",&m[i]);
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]);
scanf("%d",&x);
i=x-1;
printf("Index is:%d",sb[i]);
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
OUTPUT:
2 5 4 6 7 2 6 4 7
1 2 10
2 3 5
c) Linked:
Program:
#include<stdio.h>
#include<conio.h>
struct file
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);
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
scanf("%d",&f[i].size);
for(j=1;j<=f[i].size;j++)
R20 II-I CSE UNIX PROGRAMMING LAB
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
15
45
32
25
R20 II-I CSE UNIX PROGRAMMING LAB
venkat 20 6 4--->12--->15--->45--->32--->25
rajesh 12 5 6--->5--->4--->3--->2
R20 II-I CSE UNIX PROGRAMMING LAB
EXPERIMENT – 12
Write a C program that illustrates two processes communicating using shared memory.
shm_server.c
-- simply creates the string and shared memory portion.
shm_client.c
-- attaches itself to the created shared memory portion and uses the string
shm_server.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
/*
* We'll name our shared memory segment
* "5678".
*/
key = 5678;
/*
* Create the segment.
*/
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
/*
* Now put some things into the memory for the
* other process to read.
*/
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
/*
* Finally, we wait until the other process
* changes the first character of our memory
R20 II-I CSE UNIX PROGRAMMING LAB
exit(0);
}
shm_client.c
/*
* shm-client - client program to demonstrate shared memory.
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
/*
* We need to get the segment named
* "5678", created by the server.
*/
key = 5678;
/*
* Locate the segment.
*/
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
/*
* Now read what the server put in the memory.
*/
for (s = shm; *s != NULL; s++)
R20 II-I CSE UNIX PROGRAMMING LAB
putchar(*s);
putchar('\n');
/*
* Finally, change the first character of the
* segment to '*', indicating we have read
* the segment.
*/
*shm = '*';
exit(0);
}
R20 II-I CSE UNIX PROGRAMMING LAB
EXPERIMENT -13
Write a C program to simulate producer and consumer problem using semaphores.
PROGRAM:
#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;
}
}
}
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--;
R20 II-I CSE UNIX PROGRAMMING LAB
mutex=signal(mutex);
}
OUTPUT:
[root@localhost ~]# ./a.out 1.PRODUCER 2.CONSUMER 3.EXIT
ENTER YOUR CHOICE
1producer produces the item1
ENTER YOUR CHOICE
1producer produces the item2
ENTER YOUR CHOICE
2consumer consumes item2
ENTER YOUR CHOICE
2consumer consumes item1
ENTER YOUR CHOICE
2BUFFER IS EMPTY
ENTER YOUR CHOICE
R20 II-I CSE UNIX PROGRAMMING LAB
EXPERIMENT – 14
Write C program to create a thread using pthreads library and let it run its function.
#
i
n
c
l
u
d
e
<
s
t
d
i
o
.
h
>
#
i
n
c
l
u
d
e
Compile:
<
s
• tC compiler: cc -lpthread pthread1.c
d
or
l
• iC++ compiler: g++ -lpthread pthread1.c
b
.
Run: ./a.out
h
>
Results:
Thread
# 1
Thread
i 2
Thread
n 1 returns: 0
Thread
c 2 returns: 0
l
u
d
e
<
p
t
h
r
e
a
d
.
h
>
void
*print_message_fu
R20 II-I CSE UNIX PROGRAMMING LAB
EXPERIMENT – 15
Write a C program to illustrate concurrent execution of threads using pthreads library.
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
int main()
{
pthread_t 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);
exit(0);
}
OUT PUT ::
$ cc w8.c – l pthread
$./a.out
thread1
i=1
i=2;
i=3
thread2
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
i=4
i=5
R20 II-I CSE UNIX PROGRAMMING LAB
i=6
i=7
i=8
i=9
i=10
exit from thread1
j=9
j=10
exit from thread2