Oslabfinal
Oslabfinal
Oslabfinal
2. Shell Programming.
5. Implement Semaphores
a) date
–used to check the date and
time Syn:$date
Format Purpose Example Result
+%m To display only month $date+%m 06
+%h To display month name $date+%h June
+%d To display day of month $date+%d O1
+%y To display last two digits of years $date+%y 09
+%H To display hours $date+%H 10
+%M To display minutes $date+%M 45
+%S To display seconds $date+%S 55
a) cal
–used to display the calendar
Syn:$cal 2 2009
c)echo
–used to print the message on the screen.
Syn:$echo “text”
d)ls
–used to list the files. Your files are kept in a directory.
Syn:$lsls–s
All files (include files with prefix)
ls–l Lodetai (provide file statistics)
ls–t Order by creation time
ls– u Sort by access time (or show when last accessed together with –l)
ls–s Order by size
ls–r Reverse order
ls–f Mark directories with /,executable with* , symbolic links with @, local sockets with =, named
pipes(FIFOs)with
ls–s Show file size
ls– h“ Human Readable”, show file size in Kilo Bytes & Mega Bytes (h can be used together with –l or)
ls[a-m]*List all the files whose name begin with alphabets From „a‟ to „m‟
ls[a]*List all the files whose name begins with „a‟ or „A‟
Eg:$ls>my list Output of „ls‟ command is stored to disk file named „my list‟
e)lp
–used to take printouts
Syn:$lp filename
f)man
–used to provide manual help on every UNIX co mmands.
Syn:$man unix command
$man cat
h)uptime
–tells you how long the computer has been running since its last reboot or power-off.
Syn:$uptime
i)uname
–it displays the system information such as hardware platform, system name and processor, OS
type.
Syn:$uname–a
j)hostname
–displays and set system host name
Syn:$ hostname
Viewing: Syn:$cat
filename
Add text to an existing file:
Syn:$cat>>filename
Concatenate:
Syn:$catfile1file2>file3
$catfile1file2>>file3 (no over writing of file3)
b)grep–used to search a particular word or pattern related to that word from the file.
Syn:$grep search word filename
Eg:$grep anu student
Syn:$cut<option><filename>
Eg: $cut –c filename
$cut–c1-10emp
$cut–f 3,6emp
$ cut –f 3-6 emp
-c cutting columns
-f cutting fields
Examples:
$chmodu-wx student
Removes write and execute permission for users
$ch modu+rw,g+rwstudent
Assigns read and write permission for users and groups
$chmodg=rwx student
Assigns absolute permission for groups of all read, write and execute permissions
k)wc–it counts the number of lines, words, character in a specified file(s) with the options as –l,-w,-c
AIM:
To write simple shell programs by using conditional, branching and looping statements.
ODD ALGORITHM:
PROGRAM:
OUTPUT :
2.Write a Shell program to check the given year is leap year or not
ALGORITHM:
OUTPUT :
ALGORITHM:
PROGRAM:
OUTPUT:
Enter a Number : 5
The Factorial of the Number is 120
3.Implement the following CPU scheduling algorithms
a) Round Robin b) SJF c) FCFS d) Priority
a) Round Robin
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
struct process
{
int pid,bt,tt,wt;
};
void main()
{
struct process x[10],p[30];
int i,j,k,tot=0,m,n;
float wttime=0.0,tottime=0.0,a1,a2;
system("clear");
printf("\nEnter the number of process:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
x[i].pid=i;
printf("\nEnter the Burst Time:\t");
scanf("%d",&x[i].bt);
tot=tot+x[i].bt;
}
printf("\nTotal Burst Time:\t%d",tot);
p[0].tt=0;
k=1;
printf("\nEnter the Time Slice:\t");
scanf("%d",&m);
for(j=1;j<=tot;j++)
{
for(i=1;i<=n;i++)
{
if(x[i].bt !=0)
{
p[k].pid=i;
if(x[i].bt-m<0)
{
p[k].wt=p[k-1].tt;
p[k].bt=x[i].bt;
p[k].tt=p[k].wt+x[i].bt;
x[i].bt=0;
k++;
}
else
{
p[k].wt=p[k-1].tt;
p[k].tt=p[k].wt+m;
x[i].bt=x[i].bt-m; k++;
}
}
}
}
printf("\nProcess id \twt \ttt");
for(i=1;i<k;i++)
{
printf("\n\t%d \t%d \t%d",p[i].pid,p[i].wt,p[i].tt);
wttime=wttime+p[i].wt;
tottime=tottime+p[i].tt;
a1=wttime/n;
a2=tottime/n;
}
printf("\n\nAverage Waiting Time:\t%f",a1);
printf("\n\nAverage TurnAround Time:\t%f",a2);
}
OUTPUT:
Enter the number of process : 3
Enter the Burst Time : 3
Enter the Burst Time : 5
Enter the Burst Time : 7
Total Burst Time : 15
Enter the Time Slice : 2
Process id Waiting Time TotTime
1 0 2
2 2 4
3 4 6
1 6 7
2 7 9
3 9 11
2 11 12
3 12 14
3 14 15
Average Waiting Time: 21.66666
Average Turnaround : 26.666666
b) SJF
AIM: To write a C program for implementation of SJF scheduling algorithms.
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int pid;
int btime;
int wtime;
}
sp;
int main()
{
int i,j,n,tbm=0,towtwtime=0,totttime=0;
sp *p,t;
system("clear");
printf("\nSJF Scheduling...\n");
scanf("%d",&n);
p=(sp*)malloc(sizeof(sp));
for(i=0;i<n;i++)
{
printf("\nEnter the Burst Time %d\t",i+1);
scanf("%d",&p[i].btime);
p[i].pid=i+1;
p[i].wtime=0;
}
for(i=0;i<n;i++)
for(j=j+1;j<n;j++)
{
if(p[i].btime>p[j].btime)
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
printf("\nProcess Scheduling \n");
printf("\nProcess \tBurst Time \t Waiting Time ");
for(i=0;i<n;i++)
{
towtwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pid,p[i].btime);
printf("\t\t%d\t\t%d",p[i].wtime,p[i].btime);
}
totttime=tbm+towtwtime;
printf("\nTotal Waiting Time :%d",towtwtime);
printf("\nAverage Waitinng Time :%f",(float)towtwtime/n);
printf("\ntOTAL Turn Around Time :%d",totttime);
printf("\nAverage Turn Around Time :%f",(float)totttime/n);
return 0;
}
OUTPUT:
Enter the number of Process : 3
Enter the burst Time : 2
Enter the burst Time : 4
Enter the burst Time : 6
Process BurstTime WaitingTime Tottime
1 2 0 2
2 4 2 6
3 6 6 12
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
struct fcfs
{
int pid;
int btime;
int wtime;
int ttime;
}
p[10];
int main()
{
int i,n;
int towtwtime=0,totttime=0;
system("clear");
printf("\n fcfs scheduling...\n");
printf("enter the no of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i].pid=1;
printf("\n burst time of the process");
scanf("%d",&p[i].btime);
}
p[0].wtime=0;
p[0].ttime=p[0].btime;
totttime+=p[i].ttime;
for(i=0;i<n;i++)
{
p[i].wtime=p[i-1].wtime+p[i-1].btime;
p[i].ttime=p[i].wtime+p[i].btime;
totttime+=p[i].ttime;
towtwtime+=p[i].wtime;
}
for(i=0;i<n;i++)
{{
printf("\n Waiting Time for Process");
printf("\n Turn Around Time for Process");
printf("\n");
}}
printf("\nTotal Waiting Time :%d",towtwtime);
printf("\nAverage Waiting Time :%f",(float)towtwtime/n);
printf("\nTotal Turn Aronud Time :%d",totttime);
printf("\nAverage Turn Around Time :%f",(float)totttime/n);
return 0;
}
OUTPUT:
Enter the number of processes: 3
Enter process id of all the processes: 1 2 3
Enter burst time of all the processes: 5 11 11
Process ID Burst Time Waiting Time TurnAround Time
1 5 0 5
2 11 5 16
3 11 16 27
Total Waiting Time : 21
Avg. waiting time= 7.000000
Total Turnaround time : 48
Avg. turnaround time= 16.000000
d) Priority
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int pno,pri,btime,wtime;
}sp;
int main()
{
int i,j,n;
int tbm=0,totwtime=0,totttime=0;
sp *p,t;
system("clear");
printf("\n PRIORITY SHEDULING ");
printf("\n Enter The no of Process...\n ");
scanf("%d",&n);
p=(sp*)malloc(sizeof(sp));
printf("Enter the burst time and priority:\n");
for(i=0;i<n;i++)
{
printf("process %d:",i+1);
scanf("%d%d",&p[i].btime,&p[i].pri);
p[i].pno=i+1;
p[i].wtime=0;
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(p[i].pri>p[j].pri)
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
printf("\n Process \t Burst Time \t Waiting Time \t Turn Around TIme\n");
for(i=0;i<n;i++)
{
totwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pno,p[i].btime);
printf("\t\t%d\t\t%d",p[i].wtime,p[i].wtime+p[i].btime);
}
totttime=tbm+totwtime;
printf("\n Total Waiting Time:%d",totwtime);
printf("\n Average Waiting Time :%f",(float)totwtime/n);
printf("\n Total Turn Around Time :%d",totttime);
printf("\n AVG Turn Around Time :%f",(float)totttime/n);
return 0;
}
OUTPUT:
Enter Number of Processes: 3
Enter Burst Time and Priority Value for Process 1: 10 2
Enter Burst Time and Priority Value for Process 2: 5 0
Enter Burst Time and Priority Value for Process 3: 8 1
Order of process Execution is
P1 is executed from 0 to 10
P3 is executed from 10 to 18
P2 is executed from 18 to 23
Process Id Burst Time Wait Time TurnAround Time
2 5 0 5
3 8 5 13
1 10 13 23
Total Waiting time :18
Average Waiting Time : 6.000000
Total Turn Around Time : 41
Avg Turn Around Time : 13.666667
4. Implement all file allocation strategies
a) Sequential b) Indexed c) Linked
AIM: To write unix c program to organize the file using single level directory.
PROGRAM :
#include <stdio.h>
#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}
Program Output:
AIM: To write a Unix C program for random access file for processing the employee details.
ALGORITHM:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any consumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Program Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk
:
4
1 2 3 4
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk
:
2
7 8
A5llocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0
c) Linked
AIM: To write a Unix C program for random access file for processing the employee details.
ALGORITHM:
Program Output:
AIM: To write a Unix C program to implement the producer – consumer problem using
semaphores.
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
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;
}
}
return 0;
}
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("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
OUTPUT:
1.Producer
2.Consumer
3.Exit
Enter your choice:1
Producer produces the item 1
Enter your choice:2
Consumer consumes item 1
Enter your choice:2
Buffer is empty!!
Enter your choice:1
Producer produces the item 1
Enter your choice:1
Producer produces the item 2
Enter your choice:1
Producer produces the item 3
Enter your choice:1
Buffer is full!!
Enter your choice:3
6. Implement all File Organization Techniques
a) Single level directory b) Two level c) Hierarchical d) DAG
AIM: To write unix c program to organize the file using single level directory.
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10];
printf("Enter the directory name:");
scanf("%s",mdname);
printf("Enter the number of files:");
scanf("%d",&nf);
do
{
printf("Enter file name to be created:");
scanf("%s",name);
for(i=0;i<nf;i++)
{
if(!strcmp(name,fname[i]))
break;
}
if(i==nf)
{
strcpy(fname[j++],name);
nf++;
}
else
printf("There is already %s\n",name);
printf("Do you want to enter another file(yes - 1 or no - 0):");
scanf("%d",&ch);
}
while(ch==1);
printf("Directory name is:%s\n",mdname);
printf("Files names are:");
for(i=0;i<j;i++)
printf("\n%s",fname[i]);
getch();
}
Output:
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<conio.h>
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
{
int i,j,k,n;
Output :
AIM: To write a Unix C program to implement bankers algorithm for deadlock avoidance.
ALGORITHM:
PROGRAM :
#include<stdio.h>
int max[10][10];
int alloc[10][10];
int need[10][10];
int avail[10];
int n,r;
void input();
void show();
void cal();
void main()
{
int i,j;
printf("********** Baner's Algo ************\n");
input();
show();
cal();
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
}
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",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;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
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;
}
}
}
}
}
}
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");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
OUTPUT:
Enter the no of Processes : 5
Enter the no of resources instances : 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix :
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the available Resources
3 3 2
Process Allocation Max Available
P1 0 1 0
P2 2 0 0
P3 3 0 2
P4 2 1 1
P5 0 0 2
P1->P3->P4->P2->P0
The System is in safe state.
8. Implement an Algorithm for Dead Lock Detection
AIM: To write a C program to implement algorithm for deadlock detection.
ALGORITHM:
PROGRAM :
#include<stdio.h>
static int mark[20];
int i,j,np,nr;
void main()
{
int alloc[10][10],request[10][10],avail[10],r[10],w[10];
int deadlock=0;
printf("\nEnter the no of process: ");
scanf("%d",&np);
printf("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the request matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
for(i=0;i<np;i++)
{
int count=0;
for(j=0;j<nr;j++)
{
if(alloc[i][j]==0)
count++;
else
break;
}
if(count==nr)
mark[i]=1;
}
// initialize W with avail
for(j=0;j<nr;j++)
w[j]=avail[j];
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;
if(deadlock)
printf("\n Deadlock detected");
else
printf("\n No Deadlock possible");
}
OUTPUT:
Deadlock detected
9. Implement e all page replacement algorithms
a) FIFO b) LRU c) LFU
a) FIFO
AIM: To write a C program for implementation of FIFO page replacement algorithm.
ALGORITHM:
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",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
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;
}
OUTPUT:
ALGORITHM:
PROGRAM :
#include<stdio.h>
void 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++;
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 break;
}}
for(r=0;r<f;r++) b[r]=c2[r]; for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
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);
}
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
1 6 2
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<conio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0; 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 -- %d",pf);
getch();
}
INPUT
Enter number of page references -- 10
Enter the reference string -- 1 2 3 4 5 2 5 2 5 1 4 3
Enter the available no. of frames -- 3
OUTPUT
The Page Replacement Process is –
1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
5 2 3
5 2 3
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
ALGORITHM:
return 0;
}
Output :
/ubuntu/root$ cc server.c
./a.out
Id is 65559
Attached--> Mem contents 100
Changed mem is now 200
Detachment 0
ubuntu/root$cc client.c
./a.out
Allocate with shm id 65559
Attached ---> Mem contents 0
Start other process-->
11. Implement Paging Technique of memory management.
PROGRAM :
#include<stdio.h>
void main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
int i;
int choice=0;
printf("\nYour memsize is %d ",memsize);
printf("\nEnter page size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the frame of page%d:",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a logical address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
}
Output:
Your memsize is 15
Enter page size:5
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void * doSomeThing(void *arg)
{
unsigned long i = 0; pthread_t id = pthread_self();
if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err)); else
printf("\n Thread created successfully\n");
i++;
}
sleep(5);
return 0;
}
Output: