Operating System LAB
Operating System LAB
Operating System LAB
LABORATORY RECORD
OPERATING SYSTEMS
INDEX
Expt.
Name of the Experiment PageNo Date Signature
No.
Basic of C Language
C is a programming language developed by Dennis Ritchie in 1972 at bell laboratories of AT&T (American
Telephone & Telegraph), located in U.S.A. C inherited many behavior from its ancestor programming
languages. Along with the inherited features, some more unique features were developed in C that made it the
most basic programming language and the mother of all programming languages.
Features of C:
Some unique features of C are:
Extensible
Fast Speed
Machine Independent or Portable
Mid-level programming language
Memory Management
Pointers
Rich Library
Recursion
Simple to Learn
Structured programming language
Procedure-oriented programming language
C as a mother language:
C language is used to write most of the compilers. Also the syntax of C, including its unique features and
concepts of array, strings, functions, file handling, pointers, etc are used even today in almost all languages in
some way or now. This made it to be considered as the mother language of all the programming languages.
C Compilers:
For executing a C file in a system, C compiler is a must. Various C compilers are available for downloading.
Turbo C++ is among some common C compilers.
lOMoAR cPSD| 29030338
First C Program:
Lets print “Hello C” as the first C program and than understand it in detail.
Description:
#include <stdio.h> :
“#include” is a preprocessor directive used to include a library file in the code. The header file
“stdio.h” is included in the source code to use the pre-defined functions “printf” and “scanf”.
void main() :
Void represents the data type of the main function which is a must for every C program.
{} :
{} represents the opening and closing of the main function.
GOBAL VARIABLE
LOCAL VARIABLE
lOMoAR cPSD| 29030338
lOMoAR cPSD| 29030338
lOMoAR cPSD| 29030338
lOMoAR cPSD| 29030338
Exp.no: 1
Date:
Aim:
Study :
Installing an operating system (OS) is a process that typically involves creating a bootable drive
with the installation files and then booting from that drive to start the installation process. Here
are the basic steps for installing Windows and Linux operating systems:
Installing Windows:
1. Obtain the installation media (e.g. a Windows installation USB drive or DVD).
2. Connect the installation media to your computer and restart the machine.
3. Boot the machine from the installation media by changing the boot order in the BIOS or
UEFI settings.
4. Follow the on-screen instructions to install Windows. You will need to accept the license
agreement, select the installation type (e.g. custom or upgrade), choose the installation
location (e.g. which hard drive and partition), and configure basic settings such as
language, time and currency format, and keyboard input method.
5. Once the installation is complete, the machine will restart and guide you through the
initial setup process.
lOMoAR cPSD| 29030338
Installing Linux:
Result :
lOMoAR cPSD| 29030338
Exp.no: 2
Date:
Aim:
Study :
UNIX commands are a set of basic instructions that can be executed in a terminal window to perform
various tasks in a UNIX-based operating system, such as Linux and macOS. Here are some common
UNIX commands:
This script starts with a shebang line that specifies the path to the bash interpreter,
followed by a comment that explains what the script does. The command is used
to print a message to the console.
Result:
lOMoAR cPSD| 29030338
Process Management using System Calls: Fork, Exec, Getpid, Exit, Wait, Close
Exp.no: 3
Date:
Aim:
To Process Management using System Calls : Fork, Exec, Getpid, Exit, Wait, Close.
Algorithm:
Step 3: If fork() returns a negative value, print an error message and exit with an error code
Step 5.2: Call wait() to wait for the child process to complete
Step 5.3: Print a message to the console indicating that the child process has completed
Program :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
Output:
Result :
lOMoAR cPSD| 29030338
Exp.no: 4 a
Date:
Aim:
To write C programs to implement the various CPU Scheduling Algorithms using First Come
First Serve algorithm.
Algorithm:
Step 1: Initialize the total waiting time and total turnaround time to 0.
Step 3: For each process, read in its arrival time and burst time.
Step 4: Sort the processes in increasing order of arrival time, so that the first process to arrive is
first in the queue.
Step 5: For each process, calculate its waiting time as the sum of the burst times of all
processes that arrived before it. For the first process, its waiting time is 0.
Step 6: For each process, calculate its turnaround time as the sum of its waiting time and its
burst time.
Step 7: Calculate the average waiting time and the average turnaround time by dividing the
total waiting time and total turnaround time by the number of processes.
Program:
#include <stdio.h>
int main() {
int n, i;
float avg_wt, avg_tat;
int burst_time[20], waiting_time[20], turnaround_time[20];
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 4 b
Date:
Aim:
To write C programs to implement the various CPU Scheduling Algorithms using Shortest Job
First algorithm.
Algorithm:
Step 1: When a process arrives, its expected processing time is estimated, based on its past
behaviour or other factors.
Step 2: The process with the shortest expected processing time is assigned the CPU. If there are
multiple processes with the same expected processing time, any one of them may be selected.
Step 3: The process runs on the CPU until it completes, or until it is pre-empted by a higher-
priority process.
Step 4: When a new process arrives, its expected processing time is estimated, and it is added
to the ready queue.
Step 5: When the current process completes or is pre-empted, the process with the shortest
expected processing time in the ready queue is selected and assigned the CPU.
Program:
#include <stdio.h>
int main() {
int n, i, j, temp, time = 0, total_time = 0;
float avg_waiting_time, avg_turnaround_time;
int burst_time[20], process[20], waiting_time[20], turnaround_time[20];
waiting_time[0] = 0;
avg_waiting_time = (float)total_time / n;
total_time = 0;
}
lOMoAR cPSD| 29030338
avg_turnaround_time = (float)total_time / n;
printf("\n\nAverage Waiting Time = %0.2f", avg_waiting_time);
printf("\nAverage Turnaround Time = %0.2f", avg_turnaround_time);
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 4 c
Date:
Aim:
To write C programs to implement the various CPU Scheduling Algorithms using Priority
Scheduling algorithm.
Algorithm:
Step 1: Assign a priority to each process. This priority can be based on various criteria, such as
the amount of CPU time needed, the amount of memory required, or the importance of the
process.
Step 2: Initialize the ready queue with all the processes that are ready to run.
Step 3: Find the process with the highest priority in the ready queue.
Step 5: If the scheduling is non-pre-emptive, the process will continue to run until it completes
or voluntarily gives up the CPU. If the scheduling is pre-emptive, the process will be
interrupted if a new process with a higher priority arrives in the ready queue.
Step 6: When a process completes or gives up the CPU, remove it from the system.
Step 7: If a new process arrives in the system, add it to the ready queue according to its priority.
Program:
#include<stdio.h>
int main() {
int n, i, j, temp, total_wt = 0, total_tat = 0;
float avg_wt, avg_tat;
int burst_time[20], priority[20], process[20];
temp = burst_time[i];
burst_time[i] = burst_time[j];
burst_time[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
lOMoAR cPSD| 29030338
// Displaying results
avg_wt = (float)total_wt / n;
avg_tat = (float)total_tat / n;
return 0;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 4 d
Date:
Aim:
To write C programs to implement the various CPU Scheduling Algorithms using Round Robin
algorithm.
Algorithm:
Step 1: Initialize a queue Q to hold the processes that are ready to run.
Step 5: If the process has not completed, and its remaining execution time is less than or equal
to the time slice:
Step 12: Enqueue the process back onto the end of the queue.
Step 13: Calculate the average turnaround time and average wait time for all completed
processes.
lOMoAR cPSD| 29030338
Program:
#include<stdio.h>
int main() {
int i, n, t, j, count=0, quantum;
int waiting_time = 0, turnaround_time = 0;
int arrival_time[10], burst_time[10], temp_burst_time[10];
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the time quantum: ");
scanf("%d", &quantum);
for (i = 0; i < n; i++) {
printf("Enter the arrival time and burst time of process %d: ", i+1);
scanf("%d %d", &arrival_time[i], &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}
t = 0;
while (1) {
int flag = 1;
for (i = 0; i < n; i++) {
if (temp_burst_time[i] > 0) {
flag = 0;
if (temp_burst_time[i] > quantum) {
t += quantum;
temp_burst_time[i] -= quantum;
}
else {
t = t + temp_burst_time[i];
waiting_time += t - arrival_time[i] - burst_time[i];
temp_burst_time[i] = 0;
turnaround_time += t - arrival_time[i];
}
}
}
if (flag == 1)
break;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 4 e
Date:
Aim:
To write C programs to implement the various CPU Scheduling Algorithms using Multilevel
Queue Scheduling algorithm.
Algorithm:
Step 1: Divide the ready queue into multiple queues based on some criteria, such as process
priority or type of task.
Step 2: Assign a different scheduling algorithm to each queue, such as FCFS or SJF.
Step 3: When a new process arrives, it is assigned to the appropriate queue based on its
characteristics.
Step 4: The process in the highest priority queue is executed first, and if there are multiple
processes in the same queue, the scheduling algorithm for that queue is used to determine which
process is executed next.
Step 5: If a process in a lower-priority queue becomes ready to run, it will only be executed if
there are no processes in the higher-priority queues.
Step 6: When a process completes or is blocked, it is removed from the queue and the next
process in the queue is executed.
Step 7: The process in the highest-priority queue may pre-empty a process in a lower-priority
queue if it becomes ready to run.
Step 8: The processes in each queue are scheduled independently of each other.
Step 9: The system can be configured with a different number of queues and different
scheduling algorithms for each queue, depending on the requirements of the system.
lOMoAR cPSD| 29030338
Program:
#include <stdio.h>
// Process structure
typedef struct {
int pid;
int burst_time;
int priority;
} Process;
// Queue structure
typedef struct {
int front;
int rear;
Process arr[MAX_QUEUE_SIZE];
} Queue;
// Initialize queue
void init_queue(Queue *q) {
q->front = -1;
q->rear = -1;
}
}
if (is_empty(q)) {
q->front = q->rear = 0;
} else {
q->rear++;
}
q->arr[q->rear] = p;
}
// Main function
int main() {
// Initialize queues
Queue q1, q2, q3;
init_queue(&q1);
init_queue(&q2);
init_queue(&q3);
// Create processes
Process p1 = { 1, 6, 1 };
Process p2 = { 2, 4, 2 };
Process p3 = { 3, 8, 3 };
Process p4 = { 4, 3, 1 };
Process p5 = { 5, 5, 2 };
Process p6 = { 6, 2, 3 };
enqueue(&q1, p1);
enqueue(&q2, p2);
enqueue(&q3, p3);
enqueue(&q1, p4);
enqueue(&q2, p5);
enqueue(&q3, p6);
printf("Queue 2:\n");
while (!is_empty(&q2)) {
Process p = dequeue(&q2);
printf("Running process %d with burst time %d\n", p.pid, p.burst_time);
}
printf("Queue 3:\n");
while (!is_empty(&q3)) {
Process p = dequeue(&q3);
printf("Running process %d with burst time %d\n", p.pid, p.burst_time);
}
return 0;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Date:
Aim:
Algorithm:
Step 2: Create a pipe using the pipe() system call and store the read and write ends of the pipe in fd[0] and
fd[1], respectively.
Step 3: Fork the current process using the fork() system call and store the return value in pid.
Step 4: If pid is negative, an error occurred during forking, so print an error message and exit the program.
Step 6: Generate a random number using the rand() function and print it to the console.
Step 7: Write the number to the write end of the pipe using the write() system call.
Step 8: Close the write end of the pipe using the close() system call.
Step 10: Read the number from the read end of the pipe using the read() system call.
Step 12: Write the result to the write end of the pipe using the write() system call.
Step 13: Close the read end of the pipe using the close() system call.
Step 14: Exit the child process using the exit() system call.
Step 15: Back in the parent process, wait for the child process to complete using the wait() system call.
Step 16: Read the result from the read end of the pipe using the read() system call.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int fd[2];
pid_t pid;
if (pipe(fd) == -1) {
perror("pipe");
exit(1);
}
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid > 0) {
// Parent process
int number;
close(fd[0]); // Close read end of the pipe
number = rand() % 100; // Generate a random number
printf("Parent process generated number: %d\n", number);
write(fd[1], &number, sizeof(number)); // Write number to the pipe
close(fd[1]); // Close write end of the pipe
}
else {
// Child process
int number, result;
close(fd[1]); // Close write end of the pipe
read(fd[0], &number, sizeof(number)); // Read number from the pipe
printf("Child process received number: %d\n", number);
result = number * 2; // Multiply the number by 2
write(fd[1], &result, sizeof(result)); // Write the result to the pipe
close(fd[0]); // Close read end of the pipe
}
return 0;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Date:
Aim:
Algorithm:
Step 1: Declare a semaphore variable mutex and a shared resource variable shared_resource.
Step 2: Initialize the semaphore using the sem_init() function with a value of 1, indicating that
only one thread can access the critical section at a time.
Step 3: Create several threads that will access the critical section of the code.
Step 5: Call sem_wait() on the semaphore to wait for access to the critical section.
Step 8: Call sem_post() on the semaphore to release access to the critical section.
Program:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t mutex;
int shared_resource = 0;
int main()
{
pthread_t threads[3];
int i, ret;
sem_init(&mutex, 0, 1);
sem_destroy(&mutex);
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Date:
Aim:
Algorithm:
Step 2: Allocation:
Step 4: Available:
Program:
#include <stdio.h>
#include <conio.h>
void main() {
int
k=0,a=0,b=0,instance[5],availability[5],allocated[10][5],need[10][5],MAX[10][5],process,P[10]
,no_of_resources, cnt=0,i, j;
printf("\n Enter the number of resources : ");
scanf("%d", &no_of_resources);
printf("\n enter the max instances of each resources\n");
for (i=0;i<no_of_resources;i++) {
availability[i]=0;
printf("%c= ",(i+97));
scanf("%d",&instance[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &process);
printf("\n Enter the allocation matrix \n ");
for (i=0;i<no_of_resources;i++)
printf(" %c",(i+97));
printf("\n");
for (i=0;i <process;i++) {
P[i]=i;
printf("P[%d] ",P[i]);
for (j=0;j<no_of_resources;j++) {
scanf("%d",&allocated[i][j]);
availability[j]+=allocated[i][j];
}
}
printf("\nEnter the MAX matrix \n ");
for (i=0;i<no_of_resources;i++) {
printf(" %c",(i+97));
availability[i]=instance[i]-availability[i];
}
printf("\n");
for (i=0;i <process;i++) {
printf("P[%d] ",i);
for (j=0;j<no_of_resources;j++)
scanf("%d", &MAX[i][j]);
}
lOMoAR cPSD| 29030338
printf("\n");
A: a=-1;
for (i=0;i <process;i++) {
cnt=0;
b=P[i];
for (j=0;j<no_of_resources;j++) {
need[b][j] = MAX[b][j]-allocated[b][j];
if(need[b][j]<=availability[j])
cnt++;
}
if(cnt==no_of_resources) {
op[k++]=P[i];
for (j=0;j<no_of_resources;j++)
availability[j]+=allocated[b][j];
} else
P[++a]=P[i];
}
if(a!=-1) {
process=a+1;
goto A;
}
printf("\t <");
for (i=0;i<k;i++)
printf(" P[%d] ",op[i]);
printf(">");
getch();
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Date:
Aim:
Algorithm:
Step 1
1. Let Work(vector) length = m
2. Finish(vector) length = n
3. Initialise Work= Available.
1. if Allocationi = 0 ∀ i ∈[0,N-1], then Finish[i] = true;
otherwise, Finish[i]= false.
Step 2
1. Find an index i such that both
1. Finish[i] == false
2. Work >= Requesti
If there exists no i, go to step 4.
Step 3
1. Work += Allocationi
2. Finish[i] = true
Go to Step 2.
Step 4
1. For some i in [0,N), if Finish[i]==false, then the system is considered in a deadlock
state. Moreover, if Finish[i]==false the process Pi is deadlocked.
Program:
#include
using namespace std;
int arrmax[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
lOMoAR cPSD| 29030338
void input()
{
int i, j;
cout << "Enter the no of Processes\t";
cin >> n;
cout << "Enter the no of resource instances\t";
cin >> r;
cout << "Enter the Max Matrix\n";
for (i = 0; i < n; i++)
{
for (j = 0; j < r; j++)
{
cin >> arrmax[i][j];
}
}
cout << "Enter the Allocation Matrix\n";
for (i = 0; i < n; i++)
{
for (j = 0; j < r; j++)
{
cin >> alloc[i][j];
}
}
cout << "Enter the available Resources\n";
for (j = 0; j < r; j++)
{
cin >> avail[j];
}
}
void show()
{
int i, j;
cout << "Process\t Allocation\t Max\t Available\t";
for (i = 0; i < n; i++)
{
cout << "\nP" << i + 1 << "\t ";
for (j = 0; j < r; j++)
{
cout << alloc[i][j] << " ";
}
cout << "\t\t";
lOMoAR cPSD| 29030338
{
for (k = 0; k < r; k++)
{
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
}
//cout<<"\nP%d",i;
if (finish[i] == 1)
{
i = n;
}
}
}
}
}
}
j = 0;
flag = 0;
for (i = 0; i < n; i++)
{
if (finish[i] == 0)
{
dead[j] = i;
j++;
flag = 1;
}
}
if (flag == 1)
{
cout << "\n\nSystem is in Deadlock and the Deadlock process are\n";
for (i = 0; i < n; i++)
{
cout << "P" << dead[i] << "\t";
}
}
else
{
cout << "\nNo Deadlock Occur";
}
}
lOMoAR cPSD| 29030338
int main()
{
int i, j;
cout << "********** Deadlock Detection Algorithm ************\n";
input();
show();
cal();
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Date:
Aim:
Algorithm:
Step 1: Define a function func that takes a void* argument and returns a void* pointer.
Step 2: Inside the func function, detach the current thread using pthread_detach(pthread_self())
so that it can continue running independently of the parent thread.
Step 3: Print a message indicating that the function is running.
Step 4: Exit the thread using pthread_exit(NULL) so that it terminates.
Step 5: Define a function fun.
Step 6: Declare a pthread_t variable named ptid to store the ID of the new thread that will be
created.
Step 7: Inside the fun function, create a new thread using pthread_create(&ptid, NULL, &func,
NULL) with the func function as the thread function.
Step 8: Print a message indicating that the current line may be printed before the thread
terminates.
Step 9: Compare the ID of the current thread with the ID of the newly created thread using
pthread_equal(ptid, pthread_self()).
Step 10: If the IDs are equal, print a message indicating that the threads are equal.
Step 11: If the IDs are not equal, print a message indicating that the threads are not equal.
Step 12: Wait for the newly created thread to terminate using pthread_join(ptid, NULL).
Step 13: Print a message indicating that the current line will be printed after the thread ends.
Step 14: Exit the thread using pthread_exit(NULL).
Program:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
lOMoAR cPSD| 29030338
void fun()
{
pthread_t ptid;
pthread_exit(NULL);
}
lOMoAR cPSD| 29030338
// Driver code
int main()
{
fun();
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 10
Date:
Aim:
Algorithm:
Step 1: Declare variables to store the memory size, page size, number of pages, frame numbers
for each page, frame number, offset, logical address, physical address, loop counter, and choice.
Step 2: Prompt the user to enter the memory size and print the value.
Step 3: Prompt the user to enter the page size and store the value.
Step 4: Calculate the number of pages by dividing the memory size by the page size and store
the value.
Step 5: Use a loop to prompt the user to enter the frame number for each page and store the
values in an array.
Step 6: Use a do-while loop to allow the user to enter a logical address and map it to a physical
address until the user chooses to stop.
Step 7: Inside the do-while loop, prompt the user to enter a logical address and store the value.
Step 8: Calculate the frame number by dividing the logical address by the page size and store
the value.
Step 9: Calculate the offset by finding the remainder of the logical address divided by the page
size and store the value.
Step 10: Calculate the physical address by multiplying the frame number with the page size and
adding the offset, and store the value.
Step 11: Print the physical address.
Step 12: Prompt the user to continue or stop and store the choice.
Step 13: Continue the loop if the choice is 1.
Step 14: Exit the loop if the choice is 0.
Step 15: Exit the program
lOMoAR cPSD| 29030338
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);
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 11 a
Date:
Aim:
Algorithm:
Step 1: Initialize the memory and memory status arrays to indicate that all memory blocks are
free.
Step 2: Search for the first contiguous block of free memory that is large enough to hold the
requested size.
Step 3: If a block of sufficient size is found, allocate the block by setting the status of each
memory location in the block to "allocated" and return a pointer to the beginning of the block.
Step 4: If a block of sufficient size is not found, return NULL to indicate that the allocation
failed.
Program:
#include <stdio.h>
int memory[MAX_MEMORY];
int memoryStatus[MAX_MEMORY];
void initMemory() {
for(int i = 0; i < MAX_MEMORY; i++) {
memory[i] = 0;
memoryStatus[i] = FREE;
}
}
lOMoAR cPSD| 29030338
void displayMemory() {
printf("Memory:\n");
for(int i = 0; i < MAX_MEMORY; i++) {
if(memoryStatus[i] == ALLOCATED) {
printf("%d ", memory[i]);
}
else {
printf("- ");
}
}
printf("\n");
}
int main() {
initMemory();
displayMemory();
int *p1 = (int*)firstFit(10);
if(p1 != NULL) {
*p1 = 10;
}
int *p2 = (int*)firstFit(20);
if(p2 != NULL) {
*p2 = 20;
}
displayMemory();
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 11 b
Date:
Aim:
Algorithm:
Step 1: Initialize the memory and memory status arrays to indicate that all memory blocks are
free.
Step 2: Search for the largest contiguous block of free memory that is large enough to hold the
requested size.
Step 3: If a block of sufficient size is found, allocate the block by setting the status of each
memory location in the block to "allocated" and return a pointer to the beginning of the block.
Step 4: If a block of sufficient size is not found, return NULL to indicate that the allocation
failed.
Program:
#include <stdio.h>
int memory[MAX_MEMORY];
int memoryStatus[MAX_MEMORY];
void initMemory() {
for(int i = 0; i < MAX_MEMORY; i++) {
memory[i] = 0;
memoryStatus[i] = FREE;
}
}
lOMoAR cPSD| 29030338
void displayMemory() {
printf("Memory:\n");
for(int i = 0; i < MAX_MEMORY; i++) {
if(memoryStatus[i] == ALLOCATED) {
printf("%d ", memory[i]);
}
else {
printf("- ");
}
}
printf("\n");
}
int main() {
initMemory();
displayMemory();
int *p1 = (int*)worstFit(10);
if(p1 != NULL) {
*p1 = 10;
}
int *p2 = (int*)worstFit(20);
if(p2 != NULL) {
*p2 = 20;
}
displayMemory();
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 11 c
Date:
Aim:
Algorithm:
Step 1: Initialize the memory and memory status arrays to indicate that all memory blocks are
free.
Step 2: Search for the smallest contiguous block of free memory that is large enough to hold
the requested size.
Step 3: If a block of sufficient size is found, allocate the block by setting the status of each
memory location in the block to "allocated" and return a pointer to the beginning of the block.
Step 4: If a block of sufficient size is not found, return NULL to indicate that the allocation
failed.
Program:
#include <stdio.h>
int memory[MAX_MEMORY];
int memoryStatus[MAX_MEMORY];
void initMemory() {
for(int i = 0; i < MAX_MEMORY; i++) {
memory[i] = 0;
memoryStatus[i] = FREE;
}
}
lOMoAR cPSD| 29030338
void displayMemory() {
printf("Memory:\n");
for(int i = 0; i < MAX_MEMORY; i++) {
if(memoryStatus[i] == ALLOCATED) {
printf("%d ", memory[i]);
}
else {
printf("- ");
}
}
printf("\n");
}
return NULL;
}
int main() {
initMemory();
displayMemory();
int *p1 = (int*)bestFit(10);
if(p1 != NULL) {
*p1 = 10;
}
int *p2 = (int*)bestFit(20);
if(p2 != NULL) {
*p2 = 20;
}
displayMemory();
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 12 a
Date:
Aim:
To Write C programs to implement the various Page Replacement Algorithms using First-In,
First-Out (FIFO) Page Replacement Algorithm.
Algorithm:
Program:
#include <stdio.h>
#define MAX_FRAMES 3
#define MAX_REFERENCES 20
int frames[MAX_FRAMES];
int head = 0;
int faultCount = 0;
void initFrames() {
for (int i = 0; i < MAX_FRAMES; i++) {
frames[i] = -1;
}
}
lOMoAR cPSD| 29030338
void displayFrames() {
for (int i = 0; i < MAX_FRAMES; i++) {
printf("%d ", frames[i]);
}
printf("\n");
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 12 b
Date:
Aim:
To Write C programs to implement the various Page Replacement Algorithms using Least
Recently Used algorithm.
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++;
lOMoAR cPSD| 29030338
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];
lOMoAR cPSD| 29030338
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:
Result:
lOMoAR cPSD| 29030338
Exp.no: 12 c
Date:
Aim:
To Write C programs to implement the various Page Replacement Algorithms using Optimal
Page Replacement algorithm.
.
Algorithm:
Step 1: Initialize a table to store the future references for each page.
Program:
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k,
pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
if(flag3 ==0){
max = temp[0];
pos = 0;
printf("\n");
return 0;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 12 d
Date:
Aim:
To Write C programs to implement the various Page Replacement Algorithms using Clock Page
Replacement algorithm.
Algorithm:
Program:
#include<stdio.h>
int main()
{
int n,p[100],f[10],ava,hit=0,usebit[10],i,j;
printf("enter the length of the Reference string: ");
scanf("%d",&n);
printf("enter the reference string: \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
lOMoAR cPSD| 29030338
for(i=0;i<n;i++)
{
ava=0;
// found
for(j=0;j<3;j++)
{
if(p[i]==f[j])
{
ava=1;
hit++;
usebit[j]=1;
break;
}
}
//search for usebit 0
if(ava==0)
{
for(j=0;j<3;j++)
{
if(usebit[j]==0)
{
f[j]=p[i];
usebit[j]=1;
ava=1;
break;
}
}
}
// fifo
if(ava==0)
{
for(j=0;j<3;j++)
usebit[j]=0;
}
f[0]=p[i];
usebit[0]=1;
}
printf("The number of Hits: %d",hit);
return 0;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 12 e
Date:
Aim:
To Write C programs to implement the various Page Replacement Algorithms using Least-
Frequently Used algorithm.
Algorithm:
Step 1: Initialize a table to store the frequency count for each page in memory.
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");
lOMoAR cPSD| 29030338
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;
}
}
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];
}
lOMoAR cPSD| 29030338
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;
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 13 a
Date:
Aim:
Algorithm:
Step 1:Sort the records in the file based on a specific key
Step 2: Open the file in read mode
Step 3: Read the first record
Step 4: Process the record
Step 5:Read the next record until end of file
Step 6: Close the file
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
Student s;
int i, n;
char filename[20];
scanf("%s", filename);
fp = fopen(filename, "wb");
fclose(fp);
fclose(fp);
return 0;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
lOMoAR cPSD| 29030338
Exp.no: 13 b
Date:
Aim:
Algorithm:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp, *ip;
Student s;
Index idx;
int i, n;
char filename[20], indexFilename[20];
lOMoAR cPSD| 29030338
fclose(fp);
fclose(ip);
if (!found) {
printf("\nStudent record not found.\n");
}
fclose(fp);
fclose(ip);
return 0;
}
Output:
Result
lOMoAR cPSD| 29030338
Exp.no: 13 b
Date:
Aim:
Algorithm:
Step 1:Create an index file with keys and pointers to the corresponding records in the data file
Step 2:Open the index file in read or write mode
Step 3:Read or write the index entries
Step 4:Open the data file in read or write mode
Step 5:Read or write the record using the pointer from the index file
Step 6:Close both files
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
Student s;
int i, n;
char filename[20];
scanf("%s", filename);
fp = fopen(filename, "wb");
// write data to the file
for (i = 0; i < n; i++) {
printf("\nEnter roll number: ");
scanf("%d", &s.rollNo);
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter marks: ");
scanf("%d", &s.marks);
fwrite(&s, sizeof(Student), 1, fp);
}
fclose(fp);
if (!found) {
printf("\nStudent not found.\n");
}
fclose(fp);
return 0;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 14 a
Date:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num_blocks, num_files, i, j, file_size;
char buffer[BLOCK_SIZE];
FILE *fp;
return 0;
}
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 14 b
Date:
Aim:
Algorithm:
Step 1:Calculate the number of blocks needed for the file
Step 2: Allocate the blocks using indexed allocation
Step 3: Write the records to the blocks
Step 4: Read the records from the blocks
Program:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int blockNumber;
int fileSize;
} Index;
int main() {
char filename[20], indexFilename[20];
int fileSize, file[MAX_BLOCKS];
printf("Enter file name: ");
scanf("%s", filename);
lOMoAR cPSD| 29030338
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 14 c
Date:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
Block b;
int i, n, head, tail, newBlock;
char filename[20];
lOMoAR cPSD| 29030338
// update the previous block's next pointer to point to the new block
fseek(fp, tail, SEEK_SET);
fread(&b, sizeof(Block), 1, fp);
b.next = newBlock;
fseek(fp, tail, SEEK_SET);
fwrite(&b, sizeof(Block), 1, fp);
}
fclose(fp);
lOMoAR cPSD| 29030338
fclose(fp);
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338
Exp.no: 15
Date:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int queue[20], n, head, i;
float avg_seek_time;
scanf("%d", &queue[i]);
}
avg_seek_time = (float)distance / n;
return 0;
}
Output:
Result:
lOMoAR cPSD| 29030338