0% found this document useful (0 votes)
13 views57 pages

III Cs Os Record Program

This document is a certification record for a student in the PG Department of Computer Science at Government Arts and Science College, confirming their completion of the Operating System Lab practical examination for the academic year 2024-2025. It includes a detailed practical record with various programming tasks related to UNIX commands, CPU scheduling algorithms, file allocation strategies, and memory management techniques. The document outlines the structure and requirements for the practical examination submissions, including programs and their expected outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views57 pages

III Cs Os Record Program

This document is a certification record for a student in the PG Department of Computer Science at Government Arts and Science College, confirming their completion of the Operating System Lab practical examination for the academic year 2024-2025. It includes a detailed practical record with various programming tasks related to UNIX commands, CPU scheduling algorithms, file allocation strategies, and memory management techniques. The document outlines the structure and requirements for the practical examination submissions, including programs and their expected outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

GOVERNMENT ARTS AND SCIENCE COLLEGE

TIRUPATTUR – 635 901

PG DEPARTMENT OF COMPUTER SCIENCE

CERTIFICATE

This is to certify that _________________________________________________________


Register No. _________________________________ of III – B.Sc. Computer Science has
successfully completed the FPCS57: PRACTICAL VI - OPERATING SYSTEM LAB
Practical Examination Record during the Academic year 2024-2025.

Staff-in-Charge Head of the Department

Submitted for Thiruvalluvar University, Serkkadu, Vellore – 632 115, Practical Examination

November/December 2024 held on ______________________.

Internal Examiner External Examiner


GOVERNMENT ARTS AND SCIENCE COLLEGE
TIRUPATTUR – 635 901

PG DEPARTMENT OF COMPUTER SCIENCE

PRACTICAL RECORD

III B.Sc. COMPUTER SCIENCE


FPCS57: PRACTICAL VI - OPERATING SYSTEM LAB
November / December 2024

NAME : _______________________________________

REGISTER NUMBER : _______________________________________


INDEX
PAGE
S.NO. DATE LIST OF PROGRAMS SIGN
NO.

1. Basics of UNIX commands

2. Shell Programming
Implement the following CPU scheduling
algorithms
a) Round Robin
3.
b) SJF
c) FCFS
d) Priority
Implement all file allocation strategies
a) Sequential
4.
b) Indexed
c) Linked
5. Implement Semaphores
Implement all File Organization Techniques
a) Single level directory
6. b) Two level
c) Hierarchical
d) DAG
Implement Bankers Algorithm for Dead
7.
Lock Avoidance
Implement an Algorithm for Dead Lock
8.
Detection
Implement e all page replacement
algorithms
9. a) FIFO
b) LRU
c) LFU
10. Implement Shared memory and IPC
Implement Paging Technique of memory
11.
management
Implement Threading & Synchronization
12. Applications
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 1. Basics of UNIX commands

Use the following commands to help you manage your Unix account.
IMPORTANT: The Unix (Ultrix) operating system is case sensitive. All
commands must be typed in lower-case letters unless noted otherwise.

COMMADS

1. clear – clear screen


2. history – show history of previous commands
3. date – show current date and time
4. whoami – show your username
5. id – print user identity
6. groups – show which groups user belongs to
7. passwd – change user password
8. cd – change directory
9. pwd – confirm current directors
10. ln – make links and symlinks to files and directories
11. mkdir – make new directory
12. rmdir – remove directories in Unix

OUTPUT:

Result:

Hence the Basic UNIX Commands is executed successfully.

1
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 2. Shell Programming


Aim:
To write a shell program to find factorial of a given number

Program:

echo "Enter a Number"


read n
i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"

OUTPUT:

Enter a Number
5
The Factorial of the given Number is 120

...Program finished with exit code 0


Press ENTER to exit console.

-----------------------------
Enter a Number
6
The Factorial of the given Number is 720

...Program finished with exit code 0


Press ENTER to exit console.

---------------------------

Result:

Hence a shell program to find factorial of a given number is successfully


generated.
2
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

3. Implement the following CPU scheduling algorithms


DATE:
a) Round Robin, b) SJF, c) FCFS, d) Priority

a) Round Robin
Aim: To implement the CPU scheduling algorithm for Round Robin.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int et[30],ts,n,i,x=0,tot=0;
char pn[10][10];
clrscr();
printf("Enter the no of processes: ");
scanf("%d",&n);
printf("Enter the time quantum: ");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("Enter Process Name & Estimated Time: ");
scanf("%s %d",pn[i],&et[i]);
}
printf("\n The Processes are: ");
for(i=0;i<n;i++)
printf("process %d: %s\n",i+1,pn[i]);
for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("\n %s --> %d",pn[i],ts);
et[i]=et[i]-ts;
}
else if((et[i]<=ts)&&et[i]!=0)
{
x=x+et[i];
printf("\n %s -> %d",pn[i],et[i]);
3
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

et[i]=0;
}
}
}
printf("\n Total Estimated Time:%d",x);
getch();
}

OUTPUT:
Enter the no of processes: 3
Enter the time quantum: 3
Enter Process Name & Estimated Time: A 14
Enter Process Name & Estimated Time: B 20
Enter Process Name & Estimated Time: C 24

The Processes are: process 1: A


process 2: B
process 3: C
A --> 3
B --> 3
C --> 3
A --> 3
B --> 3
C --> 3
A --> 3
B --> 3
C --> 3
A --> 3
B --> 3
C --> 3
A -> 2
B --> 3
C --> 3
B --> 3
C --> 3
B -> 2
C --> 3
C -> 3
Total Estimated Time: 58

Result: Hence to implement the CPU scheduling algorithm for Round Robin is
executed successfully.
4
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

b) SJF

Aim: To implement the CPU scheduling algorithm for SJF.

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of Process: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Process Name, Arrival Time & Execution Time:");
flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}

5
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

for(i=0;i<n;i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];

wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;

printf("\nProcess Name \t Arrival Time \t Execution Time \t Waiting Time \t


TATime");

for(i=0;i<n;i++)
printf("\n%s\t\t %5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

OUTPUT:

Enter the number of Process: 3


Enter Process Name, Arrival Time & Execution Time:A 3 5
Enter Process Name, Arrival Time & Execution Time:B 5 3
Enter Process Name, Arrival Time & Execution Time:C 6 7

Process Name Arrival Time Execution Time Waiting Time TATime


B 5 3 0 3
A 3 5 5 10
C 6 7 7 14
Average waiting time is:4.000000
Average turnaroundtime is:9.000000

Result: Hence to implement the CPU scheduling algorithm for SJF is executed
successfully.

6
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

c) FCFS

Aim: To implement the CPU scheduling algorithm for FCFS.

Program:

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char pn[10][10],t[10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
int totwt=0,tottat=0;
clrscr();
printf("Enter the number of Processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time: ");
scanf("%s%d%d",&*pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
}

7
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

for(i=0;i<n;i++)
{
if(i==0)
star[i]=arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
printf("\nPName ArrTime BurstTime WaitTime Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%6d\t%6d",pn[i],arr[i],bur[i],wt[i],star[i],tat
[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time: %f",(float)totwt/n);
printf("\nAverage Turn Around Time: %f",(float)tottat/n);
getch();
return 0;
}

OUTPUT:

Enter the number of Processes: 3


Enter the Process Name, Arrival Time & Burst Time: A 3 6
Enter the Process Name, Arrival Time & Burst Time: B 5 8
Enter the Process Name, Arrival Time & Burst Time: C 7 15

PName ArrTime BurstTime WaitTime Start TAT Finish


A 3 6 0 3 6 9
B 5 8 4 9 12 17
C 7 15 10 17 25 32
Average Waiting time: 4.666667
Average Turn Around Time: 14.333333

Result: Hence to implement the CPU scheduling algorithm for FCFS is


executed successfully.

8
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

d) PRIORITY

Aim: To implement the CPU scheduling algorithm for PRIORITY.

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of Process: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Process Name, Arrival Time, Execution Time & Priority: ");
flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}

9
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

for(i=0;i<n;i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPName\t Arrival Time\t Execution Time\t Priority \t Waiting Time\t
TATime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage Waiting Time is: %f",awt);
printf("\nAverage Turn Around Time is: %f",ata);
getch();
}

10
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT:
Enter the number of Process: 3
Enter Process Name, Arrival Time, Execution Time & Priority: A 3 4 3
Enter Process Name, Arrival Time, Execution Time & Priority: B 5 7 1
Enter Process Name, Arrival Time, Execution Time & Priority: C 8 14 2

PName Arrival Time Execution Time Priority Waiting Time TATime


B 5 7 1 0 7
C 8 14 2 4 18
A 3 4 3 23 27

Average Waiting Time is: 9.000000


Average Turn Around Time is: 17.333334

Result: Hence to implement the CPU scheduling algorithm for Priority is


executed successfully.

11
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 4. Implement all file allocation strategies


a) Sequential b) Indexed c) Linked

a) Sequential

Aim: To implement the File Allocation Strategies for Sequential File.

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++)
{
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 Start Block Length\n");
for(i=0;i<n;i++)
printf(" %d %d %d\n",i+1,t[i],b[i]);
printf("Enter File Name:");
scanf("%d",&x);
printf("\n File Name is: %d",x);
printf("\n Length is: %d",b[x-1]);
printf("\n Blocks Occupied: ");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}

12
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT:

Enter no.of files: 4


Enter no. of blocks occupied by file 1: 9
Enter the starting block of file 1 : 3
Enter no. of blocks occupied by file 2: 7
Enter the starting block of file 2 : 2
Enter no. of blocks occupied by file 3: 5
Enter the starting block of file 3 : 1
Enter no. of blocks occupied by file 4: 12
Enter the starting block of file 4 : 6
Filename Start Block Length
1 3 9
2 2 7
3 1 5
4 6 12
Enter File Name:2

File Name is: 2


Length is: 7
Blocks Occupied: 2 3 4 5 6 7 8

Result: Hence to implement the File Allocation Strategies for Sequential File is
executed successfully.

13
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

b) Indexed

Aim: To implement the File Allocation Strategies for Indexed File.

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++)
{ 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 Index Length\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("\nFile Name is: %d\n",x);
i=x-1;
printf("Index is: %d",sb[i]);
printf("\nBlock Occupied are: ");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}

14
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT:

Enter No. of Files: 4


Enter Starting Block and Size of File 1: 2 17
Enter Blocks occupied by File 1: 4
Enter Blocks of File 1: 2 4 7 3
Enter Starting Block and Size of File 2: 3
13
Enter Blocks occupied by File 2: 3
Enter Blocks of File 2: 8 3 5
Enter Starting Block and Size of File 3: 6 12
Enter Blocks occupied by File 3: 2
Enter Blocks of File 3: 4 6
Enter Starting Block and Size of File 4: 4 10
Enter Blocks occupied by File 4: 4
Enter Blocks of File 4: 2 5 7 1

File Index Length


1 2 4
2 3 3
3 6 2
4 4 4

Enter File Name: 3

File Name is: 3


Index is: 6
Block Occupied are: 4 6

Result: Hence to implement the File Allocation Strategies for Indexed File is
executed successfully.

15
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

c) Linked

Aim: To implement the File Allocation Strategies for Linked File.

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();
printf("Enter No. of Files: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter File Name: ");
scanf("%s",&f[i].fname);
printf("\n Enter Starting Block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("\n Enter No.of Blocks:");
scanf("%d",&f[i].size);
printf("\n Enter Block Numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("\n FILE NAME \t START \t SIZE \t BLOCK \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]);

16
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}

OUTPUT:

Enter No. of Files: 2

Enter File Name: PRIYA

Enter Starting Block:2

Enter No.of Blocks:6

Enter Block Numbers:6 5 1 2 8 6

Enter File Name: SHREE

Enter Starting Block:4

Enter No.of Blocks:5

Enter Block Numbers:1 2 3 4 6

FILE NAME START SIZE BLOCK


PRIYA 2 6 6--->5--->1--->2--->8--->6
SHREE 4 5 1--->2--->3--->4--->6

Result: Hence to implement the File Allocation Strategies for Indexed File is
executed successfully.

17
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 5. Implement Semaphores

Aim: To implement Semaphores.

Program:

#include <stdio.h>
#include <pthread.h>

typedef struct {
pthread_mutex_t mutex;
pthread_cond_t condition;
int value;
} semaphore;

void semaphore_init(semaphore* sem, int initial_value) {


pthread_mutex_init(&(sem->mutex), NULL);
pthread_cond_init(&(sem->condition), NULL);
sem->value = initial_value;
}

void semaphore_wait(semaphore* sem) {


pthread_mutex_lock(&(sem->mutex));
while (sem->value <= 0) {
pthread_cond_wait(&(sem->condition), &(sem->mutex));
}
sem->value--;
pthread_mutex_unlock(&(sem->mutex));
}

void semaphore_signal(semaphore* sem) {


pthread_mutex_lock(&(sem->mutex));
sem->value++;
pthread_cond_signal(&(sem->condition));
pthread_mutex_unlock(&(sem->mutex));
}

void* thread_function(void* arg) {


semaphore* sem = (semaphore*)arg;

printf("Thread waiting\n");
semaphore_wait(sem);

18
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

printf("Thread acquired the semaphore\n");


// Perform some critical section or shared resource access here

printf("Thread releasing the semaphore\n");


semaphore_signal(sem);

return NULL;
}
int main() {
semaphore sem;
semaphore_init(&sem, 1); // Initialize the semaphore with initial value 1

pthread_t thread;
pthread_create(&thread, NULL, thread_function, (void*)&sem);

// Main thread also performs some work


printf("Main thread performing some work\n");

// Main thread waits for the semaphore


printf("Main thread waiting\n");
semaphore_wait(&sem);

printf("Main thread acquired the semaphore\n");


// Perform some critical section or shared resource access here

printf("Main thread releasing the semaphore\n");


semaphore_signal(&sem);

pthread_join(thread, NULL);

return 0;
}

OUTPUT

Main thread performing some work


Main thread waiting
Main thread acquired the semaphore
Main thread releasing the semaphore
Thread waiting
Thread acquired the semaphore
Thread releasing the semaphore

Result: Hence to implement Semaphores is executed successfully.


19
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

6. Implement all File Organization Techniques:


DATE:
a) Single level directory, b) Two level,
c) Hierarchical, d) DAG

a) Single level directory

Aim: To implement the Single Level Directory for File Organization Technique.

Program:

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter Name of Directory : ");
scanf("%s", dir.dname);
while(1)
{
printf("\n1.Create File \n2.Delete File \n3.Search File \n4.Display Files \n5.Exit
\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the Name of the File: ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the Name of the File: ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is Deleted: ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]); break;
}
20
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

if(i==dir.fcnt) printf("File %s Not Found: ",f);


else
dir.fcnt--;
break;
case 3: printf("\nEnter the Name of the File: ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is Found : ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s Not Found : ",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are: ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}

21
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT

Enter Name of Directory : PRIYA

1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit

Enter your choice: 1

Enter the Name of the File: SHREE

1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit

Enter your choice: 1

Enter the Name of the File: VARSHI

1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit

Enter your choice: 1

Enter the Name of the File: ASHWIN

1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit

22
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

Enter your choice: 4

The Files are: SHREE VARSHI ASHWIN


1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit

Enter your choice: 3

Enter the Name of the File: SHREE


File SHREE is Found :
1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit

Enter your choice: 2

Enter the Name of the File: VARSHI


File VARSHI is Deleted:
1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit

Enter your choice: 4

The Files are: SHREE ASHWIN


1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit

Enter your choice: 5

Result: Hence to implement the Single Level Directory for File Organization
Technique is executed successfully.

23
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

b) Two level directory

Aim: To implement the Two Level Directory for File Organization Technique.

Program:

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n1.Create Directory \n2.Create File \n3.Delete File");
printf("\n4.Search File \n5.Display \n6.Exit \nEnter your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter Name of Directory: ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory Created");
break;
case 2: printf("\nEnter Name of the Directory: ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter Name of the File: ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
printf("File Created");
break;
}

24
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

if(i==dcnt)
printf("Directory %s Not Found",d);
break;

case 3: printf("\nEnter Name of the Directory: ");


scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter Name of the File: ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is Deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s Not Found",f);
goto jmp;
}
}
printf("Directory %s Not Found",d);
jmp : break;
case 4: printf("\nEnter Name of the Directory: ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the Name of the File: ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is Found ",f);

25
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

goto jmp1;
}
}
printf("File %s Not Found",f);
goto jmp1;
}
}

printf("Directory %s Not Found",d);


jmp1: break;
case 5: if(dcnt==0)
printf("\n No Directory's ");
else
{
printf("\n Directory \tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}

26
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT

1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 1

Enter Name of Directory: PRIYA


Directory Created
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit

Enter your Choice: 2

Enter Name of the Directory: SHREE


Directory SHREE Not Found
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 1

Enter Name of Directory: SHREE


Directory Created
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 5

Directory Files
PRIYA
SHREE
27
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 2

Enter Name of the Directory: PRIYA


Enter Name of the File: RAMYA
File Created
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit

Enter your Choice: 5

Directory Files
PRIYA
SHREE
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 4

Enter Name of the Directory: PRIYA


Enter the Name of the File: RAMY
File RAMY Not Found
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit

28
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

Enter your Choice: 4

Enter Name of the Directory: PRIYA


Enter the Name of the File: RAMYA
File RAMYA Not Found

1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 5

Directory Files
PRIYA
SHREE

1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 6

Result: Hence to implement the Two Level Directory for File Organization
Technique is executed successfully.

29
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

c) Hierarchy Level Directory

Aim: To implement the Hierarchy Level Directory for File Organization


Technique.

Program:

#include<stdio.h>
#include<graphics.h>
#include<string.h>
struct tree_element
{
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i, gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
30
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)>nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)>link[i]),lev+1,(*root)>name,lx+gap*i,lx+gap*i+gap,
lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root !=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root>y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
}
}

OUTPUT

Result: Hence to implement the Hierarchy Level Directory for File Organization
Technique is executed successfully.

31
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

d) Hierarchy Level Directory

Aim: To implement the Hierarchy Level Directory for File Organization


Technique.

Program:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int c = 0;

struct adj_list {
int dest;
struct adj_list *next;
}*np = NULL, *np1 = NULL, *p = NULL, *q = NULL;

struct Graph {
int v;
struct adj_list *ptr;
} array[6];

void addReverseEdge(int src, int dest) {


np1 = malloc(sizeof(struct adj_list));
np1->dest = src;
np1->next = NULL;
if (array[dest].ptr == NULL) {
array[dest].ptr = np1;
q = array[dest].ptr;
q->next = NULL;
} else {
q = array[dest].ptr;
while (q->next != NULL) {
q = q->next;
}
q->next = np1;
}
}
void addEdge(int src, int dest) {
np = malloc(sizeof(struct adj_list));
np->dest = dest;
np->next = NULL;

32
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

if (array[src].ptr == NULL) {
array[src].ptr = np;
p = array[src].ptr;
p->next = NULL;
} else {
p = array[src].ptr;
while (p->next != NULL) {
p = p->next;
}
p->next = np;
}
//addReverseEdge(src, dest);
}
void print_graph(int n) {
int i;
for (i = 0; i < n; i++) {
printf("Adjacency List of %d: ", array[i].v);
while (array[i].ptr != NULL) {
printf("%d ", (array[i].ptr)->dest);
array[i].ptr = (array[i].ptr)->next;
}
printf("\n");
}
}

int checkDAG(int n) {
int count = 0;
int size = n - 1, i, j;
for (i = 0; i < n; i++) {
//cout << "Adjacency List of " << array[i].v << ": ";
if (count == size) {
return 1;
}
if (array[i].ptr == NULL) {
count++;
for (j = 0; j < n; j++) {

while (array[j].ptr != NULL) {


if ((array[j].ptr)->dest == (array[i].ptr)->dest) {
(array[j].ptr)->dest = -1;
}

33
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

array[i].ptr = (array[i].ptr)->next;
}
}

}
}
return 0;
}
int main() {
int n = 6, i;
printf("Number of vertices: %d\n", n);

for (i = 0; i < n; i++) {


array[i].v = i;
array[i].ptr = NULL;
}
addEdge(0, 1);
addEdge(1, 2);
addEdge(1, 3);
addEdge(3, 4);
addEdge(4, 5);
addEdge(5, 3);
addEdge(5, 2);
print_graph(n);
printf("The given graph is 'Directed Acyclic Graph' :");
if (checkDAG(n) == 1)
printf(" True");
else
printf(" False");
}

OUTPUT

Number of vertices: 6
Adjacency List of 0: 1
Adjacency List of 1: 2 3
Adjacency List of 2:
Adjacency List of 3: 4
Adjacency List of 4: 5
Adjacency List of 5: 3 2
The given graph is 'Directed Acyclic Graph' : True

Result: Hence to implement the DAG for File Organization Technique is


executed successfully.
34
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 7. Implement Bankers Algorithm for Dead Lock


Avoidance

Aim: To implement the Bankers Algorithm for Dead Lock Avoidance.

Program:

#include<stdio.h>
int main() {
/* array will store at most 5 process with 3 resoures if your process or
resources is greater than 5 and 3 then increase the size of array */
int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3],
done[5], terminate = 0;
printf("Enter the number of process and resources");
scanf("%d %d", & p, & c);
// p is process and c is diffrent resources
printf("enter allocation of resource of all process %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & alc[i][j]);
}
}
printf("enter the max resource process required %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & max[i][j]);
}
}
printf("enter the available resource");
for (i = 0; i < c; i++)
scanf("%d", & available[i]);

printf("\n need resources matrix are\n");


for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
need[i][j] = max[i][j] - alc[i][j];
printf("%d\t", need[i][j]);
}
printf("\n");
}

35
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

/* once process execute variable done will stop them for again execution */
for (i = 0; i < p; i++) {
done[i] = 0;
}
while (count < p) {
for (i = 0; i < p; i++) {
if (done[i] == 0) {
for (j = 0; j < c; j++) {
if (need[i][j] > available[j])
break;
}
//when need matrix is not greater then available matrix then if j==c will
true
if (j == c) {
safe[count] = i;
done[i] = 1;
/* now process get execute release the resources and add them in available
resources */
for (j = 0; j < c; j++) {
available[j] += alc[i][j];
}
count++;
terminate = 0;
} else {
terminate++;
}
}
}
if (terminate == (p - 1)) {
printf("safe sequence does not exist");
break;
}

}
if (terminate != (p - 1)) {
printf("\n available resource after completion\n");
for (i = 0; i < c; i++) {
printf("%d\t", available[i]);
}
printf("\n safe sequence are\n");

36
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

for (i = 0; i < p; i++) {


printf("p%d\t", safe[i]);
}
}

return 0;
}

OUTPUT

Enter the number of process and resources


53
enter allocation of resource of all process 5x3 matrix
010
200
302
211
002
enter the max resource process required 5x3 matrix
753
322
902
422
533
enter the available resource 3 3 2

need resources matrix are


7 4 3
1 2 2
6 0 0
2 1 1
5 3 1

available resource after completion


10 5 7
safe sequence are
p1 p3 p4 p0 p2

Result: Hence to implement Bankers Algorithm for Dead Lock Avoidance is


executed successfully.

37
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 8. Implement an Algorithm for Dead Lock Detection

Aim: To implement the Algorithm for Dead Lock Detection.

Program:

#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("\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]);

printf("\nEnter the allocation matrix:");


for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
/*Available Resource calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}

38
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

//marking 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 less 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];
}
}
}

39
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

//checking for unmarked processes


int deadlock=0;
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

Enter the no of process: 4


Enter the no of resources: 5

Total Amount of the Resource R1: 2


Total Amount of the Resource R2: 1
Total Amount of the Resource R3: 1
Total Amount of the Resource R4: 2
Total Amount of the Resource R5: 1

Enter the request matrix:0 1 0 0 1


00101
00001
10101

Enter the allocation matrix:1 0 1 1 0


11000
00010
00000

Deadlock detected
--------------------------------

Result: Hence to implement Bankers Algorithm for Dead Lock Avoidance is


executed successfully.

40
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE:
9. Implement all page replacement algorithms
a) FIFO b) LRU c) LFU

a) FIFO

Aim: To Implement the page replacement algorithms using FIFO algorithm.

Program:

#include<stdio.h>

void fifo(int string[20],int n,int size)


{
//Creating array for block storage
int frames[n];
//Initializing each block with -1
for (int i=0;i<n;i++)
frames[i]=-1;

//Index to insert element


int index=-1;

//Counters
int page_miss=0;
int page_hits=0;

//Traversing each symbol in fifo


for (int i=0;i<size;i++)
{
int symbol=string[i];
int flag=0;

for(int j=0;j<n;j++)
{
if (symbol==frames[j])
{
flag=1;
break;
}
}

41
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

if (flag==1)
{
printf("\nSymbol: %d Frame: ",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
page_hits+=1;
}
else
{
index=(index+1)%n;
frames[index]=symbol;
page_miss+=1;
printf("\nSymbol: %d Frame: ",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
}
}
printf("\nPage hits: %d",page_hits);
printf("\nPage misses: %d",page_miss);
}

//Main function
int main(void)
{
int string[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
int no_frames=3;
int size=sizeof(string)/sizeof(int);
fifo(string,no_frames,size);
return 0;
}

42
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT

Symbol: 7 Frame: 7 -1 -1
Symbol: 0 Frame: 7 0 -1
Symbol: 1 Frame: 7 0 1
Symbol: 2 Frame: 2 0 1
Symbol: 0 Frame: 2 0 1
Symbol: 3 Frame: 2 3 1
Symbol: 0 Frame: 2 3 0
Symbol: 4 Frame: 4 3 0
Symbol: 2 Frame: 4 2 0
Symbol: 3 Frame: 4 2 3
Symbol: 0 Frame: 0 2 3
Symbol: 3 Frame: 0 2 3
Symbol: 2 Frame: 0 2 3
Symbol: 1 Frame: 0 1 3
Symbol: 2 Frame: 0 1 2
Symbol: 0 Frame: 0 1 2
Symbol: 1 Frame: 0 1 2
Symbol: 7 Frame: 7 1 2
Symbol: 0 Frame: 7 0 2
Symbol: 1 Frame: 7 0 1
Page hits: 5
Page misses: 15

Result: Hence to Implement the page replacement algorithms using FIFO


algorithm is executed successfully.

43
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

b) LRU

Aim: To Implement the page replacement algorithms using LRU algorithm.

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++;
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
{

44
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

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);
}

45
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT

Enter no of pages:10
Enter the reference string:7 1 2 5 3 2 1 6 4 2
Enter no of frames:3

7
7 1
7 1 2
5 1 2
5 3 2
1 3 2
1 6 2
1 6 4
2 6 4

The no of page faults is 9

Result: Hence to Implement the page replacement algorithms using LRU


algorithm is executed successfully.

46
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

b) LFU

Aim: To Implement the page replacement algorithms using LFU algorithm.

Program:

#include<stdio.h>
void print(int frameno,int frame[])
{
int j;
for(j=0;j<frameno;j++)
printf("%d\t",frame[j]);
printf("\n");
}
int main()
{
int
i,j,k,n,page[50],frameno,frame[10],move=0,flag,count=0,count1[10]={0},
repindex,leastcount;
float rate;
printf("Enter the number of pages\n");
scanf("%d",&n);
printf("Enter the page reference numbers\n");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter the number of frames\n");
scanf("%d",&frameno);
for(i=0;i<frameno;i++)
frame[i]=-1;
printf("Page reference string\tFrames\n");
for(i=0;i<n;i++)
{
printf("%d\t\t\t",page[i]);
flag=0;
for(j=0;j<frameno;j++)
{
if(page[i]==frame[j])
{
flag=1;
count1[j]++;
printf("No replacement\n");
break;
}
47
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

}
if(flag==0&&count<frameno)
{
frame[move]=page[i];
count1[move]=1;
move=(move+1)%frameno;
count++;
print(frameno,frame);
}
else if(flag==0)
{
repindex=0;
leastcount=count1[0];
for(j=1;j<frameno;j++)
{
if(count1[j]<leastcount)
{
repindex=j;
leastcount=count1[j];
}
}

frame[repindex]=page[i];
count1[repindex]=1;
count++;
print(frameno,frame);
}
}
rate=(float)count/(float)n;
printf("Number of page faults is %d\n",count);
printf("Fault rate is %f\n",rate);
return 0;
}

48
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT

Enter the number of pages


12
Enter the page reference numbers
134623412642
Enter the number of frames
4
Page reference string Frames
1 1 -1 -1 -1
3 1 3 -1 -1
4 1 3 4 -1
6 1 3 4 6
2 2 3 4 6
3 No replacement
4 No replacement
1 1 3 4 6
2 2 3 4 6
6 No replacement
4 No replacement
2 No replacement
Number of page faults is 7
Fault rate is 0.583333

Result: Hence to Implement the page replacement algorithms using LFU


algorithm is executed successfully.

49
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 10. Implement Shared memory and IPC

Aim: To Implement Shared memory and IPC.

Program:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);

printf("Key of shared memory is %d\n",shmid);


shared_memory=shmat(shmid,NULL,0);

printf("Process attached at %p\n",shared_memory);

printf("Enter some data to write to shared memory\n");


read(0,buff,100); //get some input from user
strcpy(shared_memory,buff);

printf("You wrote : %s\n",(char *)shared_memory);


}

OUTPUT

Key of shared memory is 0


Process attached at 0x708abe28e000
Enter some data to write to shared memory
HELLO GOOD MORNING
You wrote : HELLO GOOD MORNING

Result: Hence to Implement Shared memory and IPC is executed successfully.

50
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 11. Implement Paging Technique of memory


management

Aim: To 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);
}

51
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

OUTPUT

Your memsize is 15
Enter page size:5

Enter the frame of page1:2

Enter the frame of page2:4

Enter the frame of page3:7

Enter a logical address:3

Physical address is:13


Do you want to continue(1/0)?:0

Result: Hence to Implement Paging Technique of memory management


is executed successfully.

52
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

DATE: 12. Implement Threading & Synchronization


Applications

Aim: To Implement Threading & Synchronization Applications


.
Program:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int MAX = 10;
int count = 1;
pthread_mutex_t thr;
pthread_cond_t cond;
void *even(void *arg){
while(count < MAX) {
pthread_mutex_lock(&thr);
while(count % 2 != 0) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
void *odd(void *arg){
while(count < MAX) {
pthread_mutex_lock(&thr);
while(count % 2 != 1) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
int main(){
pthread_t thread1;
pthread_t thread2;
pthread_mutex_init(&thr, 0);
pthread_cond_init(&cond, 0);

53
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE

pthread_create(&thread1, 0, &even, NULL);


pthread_create(&thread2, 0, &odd, NULL);
pthread_join(thread1, 0);
pthread_join(thread2, 0);
pthread_mutex_destroy(&thr);
pthread_cond_destroy(&cond);
return 0;
}

OUTPUT

1 2 3 4 5 6 7 8 9 10

Result: Hence to Implement Threading & Synchronization Applications is


executed successfully.

54

You might also like