Final Os Record New
Final Os Record New
NO:1
DATE: BASIC COMMANDS IN UNIX
AIM:
COMMANDS:
a)date
–used to check the date and time Syn: $date
Format Purpose Example Result
b) cal
–used to display the calendar
Syn: $cal2 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: $ls ls–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 one very UNIX commands.
Syn: $man unix command
$man cat
g) who & whoami
–it displays data about all users who have logged into the system currently. The
next command displays about current user only.
Syn:$who$whoami
h) uptime
–tells you how long the computer has been running sincerity 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 hostname Syn: $hostname
FILEMANIPULATIONCOMMANDS
a)cat
–this create, view and concatenate files.
Creation:
Syn: $cat>filename
Viewing:
Syn: $catfilename
Add text to an existing file:
Syn: $cat>>filename
Concatenate:
Syn: $cat file1file2 > 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 file name
Eg: $grepanu student
c)rm
– deletes a file from the file system Syn: $rm filename
d)touch
– used to create a blank file.
Syn: $touch file names
e)cp
–copies the files or directories
Syn: $cp source file destination file
Eg:$cp student stud
f)mv
–to rename the file or directory
syn:$mv old file new file
Eg: $mv–I student student list(-I prompt when overwrite)
g)cut
–it cursor pick up a given number of character or fields of the file.
Syn:$cut<option><filename>
Eg: $cut– cfilename
$cut–c1-10emp
$cut–f3,6emp
$cut –f3-6 emp
-ccutting columns
-fcutting fields
h)head
–displays10 lines from the head (top)of a given file Syn:$headfilename
Eg: $head student Syn: $head-2student
i)tail
–displays last 10 lines of the file Syn:$tail filename
Eg: $tail student
To display the bottom two lines;
Syn: $tail -2student
j)chmod
–used to change the permissions of a file or directory.
Syn: $ch mod category operation permission file
Where, Category–is the user type
Operation–is used to assign or remove permission Permission–is the type of
permission
File–are used to assign or remove permission all
Examples:
$chmod u-wx student
Removes write and execute permission for users
$chmod u+rw, g+rw student
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
Category Operation Permission
u– +assign R-
usersg– -Remove Readw-
groupo– =Assign Absolutely Write-
Others Execute
Syn:
$wc–lfilename $wc–wfilename $wc–c filename
RESULT
AIM:
To write a shell program for factorial
ALGORITHM:
Step-1:Start the program
Step-2:Ask the user to enter an integer to find the factorial
Step-3:Read the integer and assign it to a variable
Step-4:From the value of the integer up to 1,
multiply each digit and update the final value
Step-5:The final value at the end of all the multiplication till 1 is the factorial
Step-6:Stop the
PROGRAM:
echo "Enter a number to find Factorial"
read num
fact=1
while [ $num -gt 1 ]
do
fact=$((fact*num))
num=$((num-1))
done
echo "Factorial of given number is $fact"
Output:
RESULT:
AIM:
ALGORITHM:
#include<stdio.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;
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];
}
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++)
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;
printf("\n\nROUND ROBIN SCHEDULING ALGORITHM\n\n");
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]);
}
Output:
RESULT:
AIM:
ALGORITHM:
Step 3: For each process in the ready Q, assign the process id and accept the
CPUburst time.
Step 4: Start the Ready Q according the shortest Burst time by sorting
according tolowest to highest burst time.
Step 5: Set the waiting time of the first process as ‘0’ and its turnaround time
as itsburst time.
Calculate
#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;
printf("Enter the number of process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the Burst Time of process:%d ",i+1);
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;
temp=t[i];
t[i]=t[k];
t[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
}
}
printf("\n\t\t\t****************************************");
printf("\n\t\t\tSHORTEST JOB FIRST SCHEDULILNG ALGORITHM");
printf("\n\t\t\t****************************************");
printf("\n\nPROCESS ID\tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
printf("\n----------\t---------\t------------\t----------------\n");
wt[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++)
{
printf("%5d\t\t%5d\t\t%5d\t\t%5d\n\n",p[i]+1,t[i],wt[i],tt[i]);
}
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;
printf("\nAVERAGE WAITAING TIME %4.2F",awt);
printf("\nAVERAGE TURNAROUND TIME %4.2f",atat);
}
Output:
RESULT
AIM :
ALGORITHM:
Step 3: For each process in the ready Q, assign the process id and accept the
CPUburst time.
Step 4: Set the waiting of the first process as ‘0’ and its burst time as its
turnaround time.
Step 5: For each process in the Ready Q calculate
OUTPUT:
RESULT
AIM:
ALGORITHM:
Step 3: For each process in the ready Q, assign the process id and accept the
CPUburst time.
RESULT
AIM :
ALGORITHM:
#include<stdio.h>
int main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no.of blocks occupied by files %d :",i+1);
scanf("%d",&b[i]);
printf("Enter the starting blockd 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 the file name:");
scanf("%d",&x);
printf("File nameis:%d",x);
printf("\nLength is%d",b[x-1]);
printf("\nblocks occpaied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
}
OUTPUT:
Enter no.of files: 2
1 2 4
2 5 10
length is:4
blocks occupied:2 3 4 5
RESULT
AIM:
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.
PROGRRAM
#include<stdio.h>
int main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter starting block and sizeof file %d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("\nEnter blocks occupied by file %d:",i+1);
scanf("%d",&m[i]);
printf("\nEnter blocks of file %d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
}
printf("\nFile\tindexed\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("Indexed is %d",sb[i]);
printf("Block Occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
}
OUTPUT:
RESULT
ALGORITHM:
PROGRAM
#include<stdio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
void main()
{
int i,j,n;
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname[i]);
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");
}
}
OUTPUT:
RESULT
AIM:
To write a C-program to implement the producer – consumer problem using
semaphores.
ALGORITHM:
Step 3: Initialize the buffer size and get maximum item you want to
produce. Step 4: Get the option, which you want to do either producer,
consumer or exitfrom the operation.
Step 5: If you select the producer, check the buffer size if it is full the
producer should not produce the item or otherwise produce the item and
increase the valuebuffer size.
Step 6: If you select the consumer, check the buffer size if it is empty the
consumer should not consume the item or otherwise consume the item and
decrease the valueof buffer size.
Step 7: If you select exit come out of the program.Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
void main()
{
int buffer[10],bufsize,in,out,produce,consume,choice=0;
in=0;
out=0;
bufsize=10;
while(choice!=3)
{
printf("\n 1.produce\t2.consume\t3.exist");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if((in+1)%bufsize==out)
printf("\n buffer is full");
else
{
printf("\n Enter the value:");
scanf("%d",&produce);
buffer[in]=produce;
in=(in+1)%bufsize;
}
break;
case 2:
if(in==out)
printf("\n buffer is empty");
else
{
consume=buffer[out];
printf("\n The consumed value is %d",consume);
}
break;
} }}
OUTPUT:
RESULT
AIM:
ALGORITHM:
number of files.
given directory
Output:
RESULT
AIM:
ALGORITHM:
Step 7: Display the connected gragh along with name using graphics
RESULT
ALGORITHM:
#include<stdio.h>
#include<stdlib.h>
struct node
{
char N[25];
int df;
struct node*pc;
struct node*ps;
};
struct node*A[20];
int in=0,c=0;
void create(struct node*P,int N)
{
int i;
struct node*Tmp,*T;
Tmp=P;
for(i=0;i<N;i++)
{
T=malloc(sizeof(struct node));
printf("Enter name:");
scanf("%s",T->N);
printf("Enter dir(1) or file(0):");
scanf("%d",&T→df);
if(T->df==1)
{
A[c]=T;
c++;
}
T->pc=NULL;
T->ps=NULL;
if(i==0)
{
Tmp->pc=T;
Tmp=T;
}
else
{
Tmp->ps=T;
Tmp=T;
}
}
}
void display(struct node*P)
{
int i;
P=P->pc;
do
{
printf("\n%s(%d)",P->N,P->df);
if(P->df==1 && P→pc!=NULL)
display(P);
P=P->ps;
}
while(P!=NULL);
}
int main()
{
int nu,nc;
int i,j,k;
struct node*Hdr;
Hdr=malloc(sizeof(struct node));
Hdr->df=1;
Hdr->pc=NULL;
Hdr->ps=NULL;
printf("Enter number of users:");
scanf("%d",&nu);
create(Hdr,nu);
for(in=0;in<c;in++)
{
printf("\nEnter number of child nodes for %s:",A[in]->N);
scanf("%d",&nc);
create(A[in],nc);
}
printf("\nHierarchical\n");
display(Hdr);
}
OUTPUT:
RESULT
AIM:
ALGORITHM:
number of files.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char N[25];
int df;
struct node *Ptr;
};
struct node *A[20];
int in=0,c=0;
void display()
{
int i;
struct node *P;
for(i=0;i<c;i++)
{
P=A[i];
printf("\n%s(%d)",P->N,P->df);
P=P->Ptr;
while(P!=NULL)
{
printf("->%s(%d)",P->N,P->df);
P=P->Ptr;}}}
void DAG()
{
struct node *T,*P,*Tmp;
int i,j,Flag,nv;
for(in=0;in<c;in++)
{
P=A[in];
printf("\nEnter number of adjacent vertices for %s:",A[in]->N);
scanf("%d",&nv);
for(i=0;i<nv;i++)
{
T=malloc(sizeof(struct node));
printf("Enter name:");
scanf("%s",T->N);
printf("Enter dir(1) or file(0):");
scanf("%d",&T->df);
T->Ptr=NULL;
P->Ptr=T;
P=T;
if(T->df==1)
{
Flag=1;
for(j=0;j<c;j++)
{
if(strcmp(A[j]->N,T->N)==0)
{
Flag=0;
break; } }
if(Flag==1)
{
Tmp=malloc(sizeof(struct node));
strcpy(Tmp->N,T->N);
Tmp->df=T->df;
Tmp->Ptr=NULL;
A[c]=Tmp;
c++;
}}}}}
void create(int N)
{
int i;
struct node *T;
for(i=0;i<N;i++)
{
T=malloc(sizeof(struct node));
printf("Enter name:");
scanf("%s",T->N);
printf("Enter dir(1) or file(0):");
scanf("%d",&T->df);
T->Ptr=NULL;
A[c]=T;
c++;
}
}
int main()
{
int nu;
printf("Enter number of users:");
scanf("%d",&nu);
create(nu);
DAG();
printf("\nDAG- Adjacency list representation\n");
display();
}
OUTPUT:
RESULT
AIM:
ALGORITHM:
STEP 2: Obtain the required data through char and int data types.
#include<stdio.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,temp=0,c=0,cz=0;
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 RESOURCES %d :",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCES %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 RESOURCES %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 RESOURCE ALLOECATED NEEDED TOTAL AVAIL");
for(i=0;i<n;i++)
{
printf("\nP%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\tAVAIL BEFORE \t AVAIL AFTER");
for(i=0;i<n;i++)
{
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("\nP%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("\nDEADLOCK OCCURED");
}
Output:
RESULT
AIM:
ALGORITHM:
#include<stdio.h>
static int mark[20];
int i,j,np,nr;
int main()
{
int alloc[10][10],request[10][10],avail[10],r[10],w[10];
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("Total Amount of thew Resources 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]);
printf("\nEnter the allocation matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
/*Available Resources calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}
//making processes with zero allocation
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];
//mark processes with request than or equal to w
for(i=0;i<np;i++)
{
int canbeprocessed=0;
if(mark[i]!=1)
{
for(j=0;j<nr;j++)
{
if(request[i][j]<=w[j])
canbeprocessed=1;
else
{
canbeprocessed=0;
break;
} }
if(canbeprocessed)
{
mark[i]=1;
for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
} } }
//checking for unmarked processes
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;
if(deadlock)
printf("\nDeadlock Detected!");
else
printf("\nNo Deadlock Possible!");
}
OUTPUT:
RESULT
AIM:
To implement page replacement algorithms FIFO (First In First Out).
ALGORITHM:
PROGRAM:
#include<stdio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
printf("\n\t\t\tFIFO PAGE REPLACEMENT ALGORITHM");
printf("\n\t\t\t*******************************");
printf("\nENTER no.of frames....");
scanf("%d",&nof);
printf("Enter number of reference string...\n");
scanf("%d",&nor);
printf("Enter the refenrence 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\tReference 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\tNo.of pages faults...%d",pf);
}
Output:
RESULT
AIM:
To implement page replacement algorithm LRU (Least Recently Used).
ALGORITHM:
STEP 1: Create a queue to hold all pages in memory.
STEP 2: When the page is required replace the page at the head of the queue.
STEP 3: Now the new page is inserted at the tail of the queue.
STEP 4: Create a stack.
STEP 5: When the page fault occurs replace page present at the bottom of thestack.
PROGRAM :
#include<stdio.h>
void main()
{
int i,j,k,min,rs[25],m[10],count[10],flag[25],n,f,pf=0,next=1;
printf("Enter the length of reference string --");
scanf("%d",&n);
printf("Enter the reference string --");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames --");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement Process is --\n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min]>count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t",m[j]);
if(flag[i]==0)
printf("PF No. -- %d",pf);
printf("\n");
}
printf("\nThe number of page fault using LRU are %d",pf);
}
OUTPUT :
RESULT
AIM:
To implement page replacement algorithms Optimal (The page which is not
used for longest time).
ALGORITHM:
STEP 1: Create a array.
STEP 2: When the page fault occurs replace page that will not be used
STEP 4: When the page fault occurs replace page present at the bottom of thestack.
PROGRAM :
#include<stdio.h>
void 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 refence 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 Raplacement 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\nTotal number of page faults -- %d",pf);
}
OUTPUT:
RESULT
AIM:
To write a c program to implement IPC using shared memory.
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<string.h>
void main()
{
int len,msgid;
char msg[15];
msgid=msgget(7,IPC_CREAT|0666);
if(msgid<0)
perror("msgget failed");
else
{
system("clear");
printf("\n\n\t enter the message:");
scanf("%[a-z,az]",msg);
len=strlen(msg);
}
if(msgsnd(msgid,&msg,len,0)==-1)
perror("message sent failed");
else
printf("\n message sent:%s\n",msg);
}
OUTPUT:
RESULT
AIM:
ALGORITHM:
#include<stdio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];
int pmize,imsize,psize,frame,page,ftable[20],frameno;
void info()
{
printf("\n\t\tMEMORY MANAGEMENT USING PAGING");
printf("\n\t\t******************************");
printf("\n\nEnter the Size of Physical memory:");
scanf("%d",&pmize);
printf("Enter the size of logical memory:");
scanf("%d",&imsize);
printf("\n\nEnter the partition size:");
scanf("%d",&psize);
frame = (int)pmize/psize;
page = (int)imsize/psize;
printf("\nThe Physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages\n",page);
}
void assign()
{
int i;
for(i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit = -1;
}
for(i=0;i<frame;i++)
ftable[i] = 32555;
for(i=0;i<page;i++)
{
printf("\nEnter the Frame number where page %d must be placed:",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno=frameno;
ptable[i].pbit = 1;
}
}
printf("\n\t\tPAGE TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for(i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\t\tFRAME TABLE\n");
printf("\nFrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
printf("\n\tProcess to create the Physical Adddress\n\n");
printf("\nEnter the Base Address:");
scanf("%d",&baddr);
printf("\nEnter the Logical Address:");
scanf("%d",&laddr);
paddr = laddr / psize;
disp = laddr % psize;
if(ptable[paddr].pbit == 1)
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe physical Address where the instruction present:%d",phyaddr);
}
void main()
{
info();
assign();
cphyaddr();
}
OUTPUT:
RESULT
AIM:
ALGORITHM:
PROGRAM:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
pthread_t tid[2];
int counter;
pthread_mutex_t lock;
void* trythis(void* arg)
{
pthread_mutex_lock(&lock);
unsigned long i = 0;
counter += 1;
printf("\n Job %d has started\n", counter);
for (i = 0; i < (0xFFFFFFFF); i++);
printf("\n Job %d has finished\n", counter);
pthread_mutex_unlock(&lock);
return NULL;
}
int main(void)
{
int i = 0;
int error;
if (pthread_mutex_init(&lock, NULL) != 0) {
printf("\n mutex init has failed\n");
return 1;
}
while (i < 2) {
error = pthread_create(&(tid[i]),NULL,&trythis, NULL);
if (error != 0)
printf("\nThread can't be created :[%s]",
strerror(error));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&lock);
return 0;
}
Output:
RESULT