0% found this document useful (0 votes)
3 views11 pages

Programs

The document contains C programs that simulate various algorithms and data structures, including page replacement algorithms (FIFO and LRU), file organization techniques (single-level and two-level directories), linked file allocation strategies, and SCAN disk scheduling algorithm. Each section provides code snippets demonstrating the implementation of these concepts. The programs are designed to illustrate how these algorithms function in managing memory and file systems.

Uploaded by

rt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Programs

The document contains C programs that simulate various algorithms and data structures, including page replacement algorithms (FIFO and LRU), file organization techniques (single-level and two-level directories), linked file allocation strategies, and SCAN disk scheduling algorithm. Each section provides code snippets demonstrating the implementation of these concepts. The programs are designed to illustrate how these algorithms function in managing memory and file systems.

Uploaded by

rt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

7.

Develop a C program to simulate page replacement algorithms:

a) FIFO b) LRU

a. FIFO
#include <stdio.h>
int main()
{
int incomingStream[] = {4 , 1 , 2 , 4 , 5,4,1,2,3,6};
int pageFaults = 0;
int frames = 3;
int m, n, s,
pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf(" Incoming \t Frame 1 \t Frame 2 \t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}

pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("\n"); printf("%d\t\t\
t",incomingStream[m]); for(n = 0; n <
frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
b. LRU
#include<stdio.h>
#include<limits.h>
int checkHit(int incomingPage, int queue[], int occupied)
{
for(int i = 0; i < occupied; i++)
{
if(incomingPage == queue[i])
return 1;
}
return 0;
}
void printFrame(int queue[], int occupied)
{
for(int i = 0; i < occupied; i++) printf("%d\t\t\
t",queue[i]);
}
int main()
{
// int incomingStream[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1};
// int incomingStream[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3, 6, 1, 2, 4, 3};
int incomingStream[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3};
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printf("Page\t Frame1 \t Frame2 \t Frame3\n");
for(int i = 0;i < n; i++)
{
printf("%d: \t\t",incomingStream[i]);
// what if currently in frame 7
// next item that appears also 7
// didnt write condition for HIT
if(checkHit(incomingStream[i], queue, occupied))
{
printFrame(queue, occupied);
}
// filling when frame(s) is/are empty
else if(occupied < frames)
{
queue[occupied] = incomingStream[i];
pagefault++;
occupied++;
printFrame(queue, occupied);
}
Else
{
int max = INT_MIN;int index;
// get LRU distance for each item in frame
for (int j = 0; j < frames; j++)
{
distance[j] = 0;
// traverse in reverse direction to find
// at what distance frame item occurred last
for(int k = i - 1; k >= 0; k--)
{
++distance[j];
if(queue[j] == incomingStream[k])
break;
}
// find frame item with max distance for LRU
// also notes the index of frame item in queue
// which appears furthest(max distance)
if(distance[j] > max)
{
max = distance[j];
index = j;
}
}
queue[index] = incomingStream[i];
printFrame(queue, occupied);
pagefault++;
}
printf("\n");
}
printf("Page Fault: %d",pagefault);
return 0;
}
1. Simulate following File Organization Techniques: a) Single level directory
b) Two level directory

a. Single level
directrry #include
<stdio.h> #include
<stdlib.h> #include
<string.h>
// Maximum number of files in the directory
#define MAX_FILES 100
// Maximum file name length
#define MAX_NAME_LENGTH 256
// File structure to represent files
typedef struct File
{
char name[MAX_NAME_LENGTH];
int size;
char content[1024]; // For simplicity, we use a fixed content size
} File;
// Directory structure to hold files
typedef struct Directory
{
File files[MAX_FILES];
int num_files;
} Directory;
// Function to create a new file
File createFile(const char* name, int size, const char* content)
{
File newFile;
strncpy([Link], name, MAX_NAME_LENGTH);
[Link] = size;
strncpy([Link], content, sizeof([Link]));
return newFile;
}
// Function to add a file to the directory
void addFileToDirectory(Directory* directory, File file)
{
if (directory->num_files < MAX_FILES)
{
directory->files[directory->num_files] =
file; directory->num_files++;
} else {
printf("Directory is full. Cannot add more files.\n");
}
}
// Function to display the contents of the directory
void displayDirectoryContents(const Directory* directory)
{
printf("Directory Contents:\n");
for (int i = 0; i < directory->num_files; i++)
{
printf("File: %s, Size: %d\n", directory->files[i].name, directory->files[i].size);
}
}
int main()
{
Directory directory;
directory.num_files =
0;
// Create and add files to the directory
File file1 = createFile("[Link]", 100, "This is the content of File1.");
addFileToDirectory(&directory, file1);
File file2 = createFile("[Link]", 200, "Content of File2 goes here.");
addFileToDirectory(&directory, file2);
// Display the directory contents
displayDirectoryContents(&directory);
return 0;
}

b. Two level
directory #include
<stdio.h> #include
<stdlib.h> #include
<string.h>
#define MAX_DIRS 100
#define MAX_FILES 100
struct FileEntry
{ char name[50];
char
content[1000];
};
struct Directory
{
char name[50];
struct FileEntry files[MAX_FILES];
int num_files;
};
int num_dirs = 0;
struct Directory directories[MAX_DIRS];
void createDirectory(char parent_name[], char dir_name[])
{
if (num_dirs >= MAX_DIRS) {
printf("Error: Maximum directories reached.\n");
return;
}
for (int i = 0; i < num_dirs; i++)
{
if (strcmp(directories[i].name, parent_name) == 0)
{
if (directories[i].num_files >= MAX_FILES)
{
printf("Error: Maximum files reached in %s.\n", parent_name);
return;
}
strcpy(directories[num_dirs].name, dir_name);
directories[i].files[directories[i].num_files].content[0] = '\0';
directories[i].num_files++;
num_dirs++;
printf("Directory %s created in %s.\n", dir_name, parent_name);
return;
}
}
printf("Error: Parent directory not found.\n");
}
void createFile(char dir_name[], char file_name[])
{
for (int i = 0; i < num_dirs; i++)
{
if (strcmp(directories[i].name, dir_name) == 0)
{
if (directories[i].num_files >= MAX_FILES)
{
printf("Error: Maximum files reached in %s.\n", dir_name);
return;
}
strcpy(directories[i].files[directories[i].num_files].name, file_name);
directories[i].files[directories[i].num_files].content[0] = '\0';
directories[i].num_files++;
printf("File %s created in %s.\n", file_name, dir_name);
return;
}
}
printf("Error: Directory not found.\n");
}
void listFiles(char dir_name[])
{
for (int i = 0; i < num_dirs; i++)
{
if (strcmp(directories[i].name, dir_name) == 0)
{ printf("Files in directory %s:\n", dir_name);
for (int j = 0; j < directories[i].num_files; j++)
{ printf("%s\n", directories[i].files[j].name);
}
return;
}
}
printf("Error: Directory not found.\n");
}
int main()
{
strcpy(directories[0].name, "root");
directories[0].num_files = 0;
num_dirs++;
char parent[50], dir[50], file[50];
createDirectory("root", "docs");
createDirectory("root", "images");
createFile("docs",
"[Link]");
createFile("docs",
"[Link]");
createFile("images", "[Link]");
listFiles("docs");
listFiles("images");
return 0;
}
2. Develop a C program to simulate the Linked file allocation strategies.
#include <stdio.h>
#include
<stdlib.h> int
main()
{
int f[50], p, i, st, len, j, c, k,
a; for (i = 0; i < 50; i++)
f[i] = 0;
printf("Enter how many blocks already allocated: ");
scanf("%d", &p);
printf("Enter blocks already allocated: ");
for (i = 0; i < p; i++)
{
scanf("%d", &a);
f[a] = 1;
}
printf("Enter index starting block and length: ");
scanf("%d%d", &st, &len);
k = len;
if (f[st] == 0)
{
for (j = st; j < (st + k); j++){
if (f[j] == 0){ f[j] = 1;
printf("%d---->", j);
}
else{
//printf("%d Block is already allocated \n", j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n", st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if (c == 1)
goto x;
else
exit(0);
return 0;
}
3. Develop a C program to simulate SCAN disk scheduling algorithm.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n"); int pos[]
= {90,120,35,122,38,128,65,68};
for(i=1;i<=n;i++)
{
//scanf("%d",&temp); temp = pos[i-1];
if(temp>=head)
{

}
else
{
queue1[temp1]=temp; temp1++;
queue2[temp2]=temp; temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{

if(queue1[i]>queue1[j])
{
temp=queue1[i]; queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i]; queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++) queue[i]=queue1[j];
//queue[i]=max;
//queue[i+1]=0; for(i=temp1+1,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]); seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

You might also like