DEPARTMENT OF
AIML
AL3452 OPERATING SYSTEM
R-2025
LAB MANUAL
B.E. CSE – AIML
AL3452 OPERATING SYSTEM LAB
List of Experiments
1. Installation of Operating system : Windows
2. Illustrate UNIX commands and Shell Programming
3. Process Management using System Calls : Fork, Exec, Getpid, Exit, Wait, Close
4. Write C programs to implement the various CPU Scheduling Algorithms
5. Illustrate the inter process communication strategy
6. Implement mutual exclusion by Semaphores
7. Write a C program to avoid Deadlock using Banker's Algorithm
8. Write a C program to Implement Deadlock Detection Algorithm
9. Write C program to implement Threading
10. Implement the paging Technique using C program
11. Write C programs to implement the following Memory Allocation Methods
a. First Fit b. Worst Fit c. Best Fit
12. Write C programs to implement the various Page Replacement Algorithms
13. Write C programs to Implement the various File Organization Techniques
14. Implement the following File Allocation Strategies using C programs
a. Sequential b. Indexed c. Linked
15. Write C programs for the implementation of various disk scheduling algorithms
Ex. No: 1 INSTALLATION OF WINDOWS OPERATING SYSTEM
AIM:
To install Windows Operating System on computer.
PROCEDURE:
[Link] on your computer, insert the Windows 7 installation CD-ROM or DVD-ROM drive, and then
restart your computer.
The computer will boot from the CD-ROM automatically as the BIOS set up, or the massage of “press
any key to boot from CD or DVD” will appear, so Press any key when prompted to do , and then follow
any instructions that appear.
Setup is loading the driver files it needs to continue with installation.
[Link] first options you will come across are selections for Language, Time and currency format, and
Keyboard layout. Make your selections by using the pull down menus and press the Nextbutton
[Link] we see the License Agreement you must accept before continuing. After reading the agreement,
check the box next to “I accept the license terms” and select Next button.
[Link] which kind of installation does you want, for here we will select custom advanced.
[Link] next screen will ask you where you want to install the system, meaning on what partition.
At that point, you need to decide one of the two options:
1- Install Windows on the entire available disk space.
2- Create a partition on the hard disk, and install Windows on that partition.
If you pick option 1, then you simply click “Next” and get done with it. The setup program will create a
partition on the entire hard disk and format it with the NTFS file system. It will then install Windows on
that partition.
However we will hose option 2, because we want to create the partition exactly as we want it . Click on
“Drive options advanced”. The screen will change and show you these buttons:
[Link] create a new partition click “New”. In the “Size” box, enter the size for the new partition. When
done, click “Apply”.
Here is where Windows 7 installs major components of the OS. This process can take 10 to 60 minutes
depending on the system.
[Link] we see the system needing to reboot to continue installation tasks
[Link] we see progress as Windows 7 updates the registry settings. This process may take several minutes
before going to the next screen.
Note if you got the message of “press any key to boot from CD or DVD” do not press as it will boot from
hard disk to continue installation.
[Link] 7 starts up required services at this point in the setup process.
[Link] is where creating the first account happens. This user will have full administrator and
automatically be logged in after setup is completed.
[Link] in your user name and computer name as you would like it to be identified on the network and
then select the Next button.
[Link] is where creating the first account happens. This user will have full administrator and
automatically be logged in after setup is completed. Enter in your user name and computer name as you
would like it to be identified on the network and then select the Next button.
[Link] screen prompts you to enter a password for the account that you just created. I highly recommend
that you password your account. Enter it twice, then include a “password hint” to help remind you of
what it is if you misplace or forget it. You, of course, should not enter in your actual password in this
field, but a subtle reminder to jog your memory. After this information is entered, select the Next button.
[Link] settings, this should only take a few moments to complete. Then welcome windows will
appears, after that preparing for your desk top.
RESULT
Thus, the Windows Operating System has been installed successfully
Ex. No: 2 Illustrate UNIX commands and Shell Programming
Aim :
To write a C program to simulate basic Unix commands like ls,cp.
[Link] of ls command
Algorithm :
1. Include necessary header files for manipulating directory.
2. Declare and initialize required objects.
3. Read the directory name form the user
4. Open the directory using opendir() system call and report error if the directory
is not available
5. Read the entry available in the directory
6. Display the directory entry (ie., name of the file or sub directory.
7. Repeat the step 6 and 7 until all the entries were read.
/* 1. Simulation of ls command
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h> // Required for exit()
int main() {
char dirname[100];
DIR *p;
structdirent *d;
printf("Enter directory name: ");
scanf("%s", dirname);
p = opendir(dirname);
if (p == NULL) {
printf("Cannot find dir.\n");
exit(-1);
while ((d = readdir(p)) != NULL) {
printf("%s\n", d->d_name);
closedir(p);
return 0;
OUTPUT:
enter directory name iii
..f2
f1
2. Simulation of cpcommand
Algorithm:
1. Include required header file
2. Make necessary declarations
3. Read the source and destination file names from the user.
4. Using read() system call, read the content of the file to the buffer.
5. Uing write() system call, write the buffer content to the destination file.
6. Close the opened files.
/* using - open ,read and write system calls
file copy operation*/
#include <stdio.h>
#include <fcntl.h> // For open() flags
#include <unistd.h> // For read(), write(), close()
#include <stdlib.h>
int main() {
char buf[1024], fn1[100], fn2[100];
int fd1, fd2, n;
printf("Enter source file name: ");
scanf("%s", fn1);
printf("Enter destination file name: ");
scanf("%s", fn2);
fd1 = open(fn1, O_RDONLY);
if (fd1 == -1) {
perror("Error opening source");
exit(1);
fd2 = open(fn2, O_WRONLY | O_CREAT | O_TRUNC, 0666);
while ((n = read(fd1, buf, sizeof(buf))) > 0) {
write(fd2, buf, n);
close(fd1);
close(fd2);
printf("File copied successfully.\n");
return 0;
}
Source file:[Link]
To learn operating system
OUTPUT:
Enter source file
Name [Link]
Enter destination file
Name cse
[it2-20@localhost ~]$ cat cse
To learn operating system
I ) BASIC ARITHMETIC OPERATION USIG SHELL PROGRAMMIG
AIM:
To write a shell program to solve arithmetic operation.
ALGORITHM :
Step 1 :Include the necessary header Files.
Step 2 : get the input
Step 3 : perform the arithmetic Calculation.
Step 4 : print the result.
Step 5 : stop the execution.
Program
Echo “enter a value”
Read a
Echo “enter b value”
Read b
C=`expr $a + $b`
Echo “sum:”$c
C=`expr $a - $b`
Echo “sub:”$c
C=`expr $a \* $b`
Echo “mul:”$c
C=`expr $a / $b`
Echo “div:”$c
OUTPUT:
[2mecse25@rhes3linux ~]$ sh [Link]
Enter the a value 10
Enter b value 50
Sum: 60
Sub: -40
Mul: 500
Div: 0
Ii ) SHELL PROGRAMMING (BIGGEST OF THREENUMBERS)
AIM:
To Write a Program to Find Biggest In Three Numbers.
ALGORITHM:
STEP 1: Read The Three Numbers.
STEP 2: If A Is Greater Than B And A Is Greater Than C Then Print A Is Big.
STEP 3: Else If B is greater Than C Then C Is Big.
STEP 4: Else Print C Is Big.
STEP 5: Stop The Program.
PROGRAM:
echo "enter three numbers"
read a b c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "A is big"
else if [ $b -gt $c ]
then
echo "B is big"
else
echo "C is big"
fi
fi
OUTPUT:
RESULT:
Thus the program has been executed successfully.
EX. NO: 3A. THE FORK SYSTEM
AIM:
To Write a C program using the fork system call.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variablePid.
Step 3: Assign p=fork©.
Step 4: If pid=0, child process is executed and displays child id and child process id.
Step 5: Else parent process is executed and displays parent process id.
Step 6: Stop the program.
PROGRAM:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_tpid;
pid = fork();
if (pid< 0) {
printf("Fork failed!\n");
return 1;
}
if (pid == 0) {
printf("Child process is: %d \n", getpid());
printf("Parent process is: %d \n", getppid());
}
else {
printf("Parent process is: %d \n", getpid());
printf("Child's process ID: %d \n", pid);
}
return 0;
}
OUTPUT:
If the parent's PID is 4000 and the newly created child's PID is 4001, the output would look like this:
Parent process is: 4000
Child's process ID: 4001
Child process is: 4001
Parent process is: 4000
RESULT:
Thus the C program using fork system call was executed and output is verified
successfully
EX. NO: 3B. THE EXIT SYSTEM CALL
AIM
To Write a C program using the exit system call.
ALGORITHM
Step 1: Start the program.
Step 2: Call fork() system call.
Step 3: Pass value more than 255 in exit()
Step 4: Check status and exit.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
pid_tpid = fork();
if (pid< 0) {
perror("fork failed");
exit(1);
}
if (pid == 0) {
exit(99);
}
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
intexit_status = WEXITSTATUS(status);
printf("Exit code: %d\n", exit_status);
}
return 0;
}
OUTPUT
Exit code: 99
RESULT
Thus the C program using exit system call was executed and output is verified
successfully.
EX. NO: 3C. THE GETPID SYSTEM CALL
AIM:
To write C program to implement a getpid system call.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variable p and pp.
Step 3: Assign p=getpid( ) and pp=getppid( ).
Step 4: Display child and parent processed.
Step 5: Stop the program.
PROGRAM:
// GETPID SYSTEMCALL
#include <stdio.h>
#include <unistd.h>
int main() {
int p, pp;
p = getpid();
pp = getppid();
printf("Process ID: %d \n", p);
printf("Parent Process ID: %d \n", pp);
return 0;
}
OUTPUT:
RESULT:
Thus the C program to implement getpid system call was executed and output is
verified Successfully.
EX. NO: 3D. WAIT SYSTEM CALL
AIM:
To write a C program to implement the wait system call.
ALGORITHM:
Step 1: Start the program.
Step 2: If pid=0 then child process executes.
Step 3: If pid ! = 0 then wait() will make the parent process to wait until the child
process
Completes its execution.
Step 4: Print the status of the parent and child process.
Step 5: Stop the program.
PROGRAM:
// WAIT SYSTEM CALL
// vi wait.c
#include <stdio.h>
#include <unistd.h> // Required for fork()
#include <sys/wait.h> // Required for wait()
int main() { // 'int' must be lowercase
inti = 0, pid;
printf("Ready to fork\n");
pid = fork();
if (pid == 0) { // 'if' must be lowercase
printf("Child starts\n");
for (i = 0; i< 10; i++)
printf("%d \t", i);
printf("\nChild ends\n");
}
else {
wait(NULL); // Parent waits for child to finish
printf("Parent starts\n");
for (i = 0; i< 10; i++)
printf("%d \t", i);
printf("\nParent process ends\n");
}
return 0;
}
OUTPUT:
RESULT:
Thus the C program to implement wait system call was executed and output is
verified Successfully.
EX. NO: 3E. CLOSE SYSTEM CALL
AIM
To write a C program to implement the close system call.
ALGORITHM
Step 1: Start the program.
Step 2: Open a file [Link].
Step 3: If fd1<0 = 0 then open the file descriptor table.
Step 4: If (close(fd1) < 0) then close the file descriptor table.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
#include <stdlib.h> // Required for exit()
#include <fcntl.h> // Required for O_RDONLY
#include <unistd.h> // Required for close()
int main() {
// 'int' and 'if' must be lowercase
int fd1 = open("[Link]", O_RDONLY);
if (fd1 < 0) {
perror("c1");
exit(1);
}
printf("opened the fd = %d\n", fd1);
if (close(fd1) < 0) {
perror("c1");
exit(1);
}
printf("closed the fd.\n");
return 0;
}
OUTPUT
RESULT. : Thus the C program to implement close system call was executed
and output is verified Successfully.
EX. NO: 4A. FIRST COME FIRST SERVE SCHEDULING
AIM:
To write a C program to implement FCFS Scheduling.
ALGORITHM
Step 1: Start the program.
Step 2: Declare the variables.
Step 3: Prompt for number of process from the user.
Step 4: Get the process name and burst time for FCFS.
Step 5: Call the average waiting time and total running time.
Step 6: Set the process.
Step 7: Display the Gantt chart.
Step 8: Stop the program.
PROGRAM
// FCFS SCHEDULING
// vi fcfs.c
#include <stdio.h>
int main() {
char procno[100];
float burst[100];
intnum = 0, i;
float start = 0.00, bt = 0.00, avgwait = 0.00, wait = 0.00;
printf("Enter the number of processes:\n");
scanf("%d", &num);
printf("Enter the name (single char) and burst time:\n");
for(i = 0; i<num; i++) {
// Use " %c" with a leading space to skip any leftover newline characters
scanf(" %c", &procno[i]);
scanf("%f", &burst[i]);
}
printf("\n\t\t Gantt chart \n");
printf("ProcessName\tStartTime\tBurstTime\n");
for(i = 0; i<num; i++) {
bt = start + burst[i];
printf("%c\t\t%.2f\t\t%.2f\n", procno[i], start, bt);
wait = wait + start;
start = start + burst[i];
}
avgwait = wait / num;
printf("\nAverage waiting time = %.2f\n", avgwait);
printf("Total running time = %.2f\n", start);
return 0;
}
OUTPUT:
Enter the number of processes:
3
Enter the name (single char) and burst time:
A5
B3
C8
Gantt chart
ProcessName StartTime BurstTime
A 0.00 5.00
B 5.00 8.00
C 8.00 16.00
Average waiting time = 4.33
Total running time = 16.00
RESULT:
Thus the C program to implement the first come first serve scheduling was
executed and output is verified successfully.
EX. NO: [Link] ROBIN SCHEDULINGAIM:
AIM:
To write a C program to implement the Round Robin Scheduling.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of processes from user.
Step 3: Get the value for burst time for individual process.
Step 4: If the process burst time is less then time quantum then process is released
by CPU.
Step 5: Calculate the average waiting time and turnaround time of process.
Step 6: Display the result.
Step 7: Stop the program.
PROGRAM:
// ROUND ROBIN SCHEDULING
#include <stdio.h>
int main() {
intst[10], bt[10], wt[10], tat[10], n, tq;
inti, count = 0, swt = 0, stat = 0, temp, sq = 0;
float awt = 0.0, atat = 0.0;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter burst time for sequences:\n");
for (i = 0; i< n; i++) {
scanf("%d", &bt[i]);
st[i] = bt[i]; // st stores remaining burst time
}
printf("Enter time quantum: ");
scanf("%d", &tq);
while (1) {
count = 0; // Reset count for each pass
for (i = 0; i< n; i++) {
temp = tq;
if (st[i] == 0) {
count++;
continue;
}
if (st[i] > tq) {
st[i] = st[i] - tq;
} else {
if (st[i] >= 0) {
temp = st[i];
st[i] = 0;
}
}
sq = sq + temp;
tat[i] = sq; // Update Turnaround time
}
if (n == count)
break;
}
for (i = 0; i< n; i++) {
wt[i] = tat[i] - bt[i];
swt = swt + wt[i];
stat = stat + tat[i];
}
awt = (float)swt / n;
atat = (float)stat / n;
printf("\nProcess_no\tBurst time\tWait time\tTurnaround time\n");
for (i = 0; i< n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
printf("\nAverage wait time is %f", awt);
printf("\nAverage turnaround time is %f\n", atat);
return 0;
}
OUTPUT:
Enter number of processes: 3
Enter burst time for sequences:
24
3
3
Enter time quantum: 4
Process_no Burst time Wait time Turnaround time
1 24 6 30
2 3 4 7
3 3 7 10
Average wait time is 5.666667
Average turnaround time is 15.666667
RESULT:
Thus the C program to implement the round robin scheduling was executed and
output is Verified successfully.
EX. NO: 5 IMPLEMENTATION OF IPC USING PIPE
AIM
To write a program to implement the inter process communication using PIPE.
ALGORITHM
Step1: Start the program.
Step2: Initialize the required variables.
Step3: Create a pipe.
Step4: Send a message to the pipe.
Step5: Retrieve the message from the pipe and write it to the standard output.
Step5: Send another message to the pipe.
Step6: Retrieve the message from the pipe and write it to the standard output.
Step7: Stop the program
PROGRAM
#include <stdio.h>
#include <unistd.h>
int main() {
intpipefds[2];
intreturnstatus;
char writemessages[2][20] = {"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}
// Process Message 1
printf("Writing to pipe - Message 1 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe - Message 1 is %s\n", readmessage);
// Process Message 2
printf("Writing to pipe - Message 2 is %s\n", writemessages[1]);
write(pipefds[1], writemessages[1], sizeof(writemessages[1]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe - Message 2 is %s\n", readmessage);
return 0;
}
OUTPUT :
Writing to pipe - Message 1 is Hi
Reading from pipe - Message 1 is Hi
Writing to pipe - Message 2 is Hello
Reading from pipe - Message 2 is Hello
RESULT
Thus the C program to implement inter process communication using PIPE was
executed and output is verified successfully.
EX. NO: 6 IMPLEMENTATION OF PRODUCER CONSUMER PROBLEMUSING
SEMAPHORE
AIM:
To write a C program to implement producer consumer problem using semaphore.
ALGORITHM:
Step1: Start the program.
Step2: Declare the semaphores.
Step3: Get the choice from the user.
Step4: If choice is 1, then producer produces the item when buffer is not full.
Step5: If choice is 2, then consumer consumes the item when buffer is not empty.
Step6: If choice is 3, break the process.
Step7: Display the result.
Step8: Stop the program.
PROGRAM:
// PRODUCER CONSUMER PROBLEM
#include <stdio.h>
#include <stdlib.h>
intmutex = 1, full = 0, empty = 3, x = 0;
// Function Prototypes
void producer();
void consumer();
int wait(int);
int signal(int);
int main() {
int n;
printf("\n1. PRODUCER\n2. CONSUMER\n3. EXIT\n");
while(1) {
printf("\nENTER YOUR CHOICE: ");
if (scanf("%d", &n) != 1) break; // Basic input safety
switch(n) {
case 1:
if ((mutex == 1) && (empty != 0))
producer();
else
printf("BUFFER IS FULL\n");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer();
else
printf("BUFFER IS EMPTY\n");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s) {
return (--s);
}
int signal(int s) {
return (++s);
}
void producer() {
mutex = wait(mutex);
full = signal(full);
empty = wait(empty);
x++;
printf("\nProducer produces the item %d\n", x);
mutex = signal(mutex);
}
void consumer() {
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
printf("\nConsumer consumes item %d\n", x);
x--;
mutex = signal(mutex);
}
OUTPUT:
[Link]
[Link]
[Link]
ENTER YOUR CHOICE: 1
Producer produces the item 1
ENTER YOUR CHOICE: 1
Producer produces the item 2
ENTER YOUR CHOICE: 1
Producer produces the item 3
ENTER YOUR CHOICE: 1
BUFFER IS FULL
ENTER YOUR CHOICE: 2
Consumer consumes item 3
ENTER YOUR CHOICE: 1
Producer produces the item 3
ENTER YOUR CHOICE: 2
Consumer consumes item 3
ENTER YOUR CHOICE: 2
Consumer consumes item 2
ENTER YOUR CHOICE: 2
Consumer consumes item 1
ENTER YOUR CHOICE: 2
BUFFER IS EMPTY
ENTER YOUR CHOICE: 3
RESULT:
Thus the C program to implement the producer consumer problem was executed
and output is verified successfully.
EX. NO: 7 IMPLEMENTATION OF BANKERS ALGORITHM FOR
DEAD LOCK AVOIDANCE
AIM
To write a C program to implement for deadlock avoidance using banker’s algorithm
ALGORITHM
Step 1: Start the program.
Step 2: Get the values of resources and processes.
Step 3: Get the avail value.
Step 4: After allocation find the need value.
Step 5: Check whether it is possible to allocate.
Step 6: If it is possible then the system is in safe state.
Step 7: Else system is not in safety state.
Step 8: If the new request comes then check that the system is in safety.
Step 9: Or if not allow the request.
Step 10: Stop the program.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main() {
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10],
safeSequence[10];
int p, r, i, j, process, count = 0;
printf("Enter the no of processes: ");
scanf("%d", &p);
for (i = 0; i< p; i++) completed[i] = 0;
printf("Enter the no of resources: ");
scanf("%d", &r);
printf("\nEnter the Max Matrix for each process:");
for (i = 0; i< p; i++) {
printf("\nFor process %d: ", i + 1);
for (j = 0; j < r; j++) scanf("%d", &Max[i][j]);
}
printf("\nEnter the allocation for each process:");
for (i = 0; i< p; i++) {
printf("\nFor process %d: ", i + 1);
for (j = 0; j < r; j++) scanf("%d", &alloc[i][j]);
}
printf("\nEnter the Available Resources: ");
for (i = 0; i< r; i++) scanf("%d", &avail[i]);
// Calculate Need Matrix
for (i = 0; i< p; i++)
for (j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do {
process = -1;
for (i = 0; i< p; i++) {
if (completed[i] == 0) { // If process not finished
process = i;
for (j = 0; j < r; j++) {
if (avail[j] < need[i][j]) {
process = -1;
break;
}
} }
if (process != -1) break; {
if (process != -1) {
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for (j = 0; j < r; j++) {
avail[j] += alloc[process][j];
}
completed[process] = 1;
}
} while (count != p && process != -1);
if (count == p) {
printf("\n\nThe system is in a safe state!!");
printf("\nSafe Sequence: < ");
for (i = 0; i< p; i++) printf("%d ", safeSequence[i]);
printf(">\n");
} else {
printf("\nThe system is in an unsafe state!!");
}
return 0;
}
OUTPUT:
Enter the no of processes: 3
Enter the no of resources: 3
Enter the Max Matrix for each process:
For process 1: 7 5 3
For process 2: 3 2 2
For process 3: 9 0 2
Enter the allocation for each process:
For process 1: 0 1 0
For process 2: 2 0 0
For process 3: 3 0 2
Enter the Available Resources: 3 3 2
Process 2 runs to completion!
Process 1 runs to completion!
Process 3 runs to completion!
The system is in a safe state!!
Safe Sequence: < 2 1 3 >
RESULT:
Thus the C program to implement deadlock avoidance using banker’s algorithm was
Executed and output is verified successfully.
EX. NO: 8 IMPLEMENTATION OF DEADLOCK DETECTION ALGORITHM
AIM:
To write a C program to implement Deadlock Detection algorithm
ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and in data types.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution
PROGRAM:
//Deadlock Detection algorithm implementation
#include <stdio.h>
int main() {
int found, flag, l, p[10][10], tp, tr, c[10][10], i, j, k = 1;
int m[10], r[10], a[10], temp[10], sum = 0;
printf("Enter total no of processes: ");
scanf("%d", &tp);
printf("Enter total no of resources: ");
scanf("%d", &tr);
printf("Enter claim (Max. Need) matrix\n");
for (i = 1; i<= tp; i++) {
printf("process %d:\n", i);
for (j = 1; j <= tr; j++)
scanf("%d", &c[i][j]);
}
printf("Enter allocation matrix\n");
for (i = 1; i<= tp; i++) {
printf("process %d:\n", i);
for (j = 1; j <= tr; j++)
scanf("%d", &p[i][j]);
}
printf("Enter availability vector (available resources):\n");
for (i = 1; i<= tr; i++) {
scanf("%d", &a[i]);
temp[i] = a[i];
}
// Initialize markers
for(i=0; i<10; i++) m[i] = 0;
// Check for processes with zero allocation (already "finished")
for (i = 1; i<= tp; i++) {
sum = 0;
for (j = 1; j <= tr; j++) sum += p[i][j];
if (sum == 0) {
m[k] = i;
k++;
}
}
// Detection Logic
for (int loop = 1; loop <= tp; loop++) { // Repeat to ensure all paths are checked
for (i = 1; i<= tp; i++) {
intalready_marked = 0;
for (l = 1; l < k; l++) if (m[l] == i) already_marked = 1;
if (!already_marked) {
flag = 1;
for (j = 1; j <= tr; j++) {
if ((c[i][j] - p[i][j]) > temp[j]) { // Need must be <= Available
flag = 0;
break;
}
}
if (flag == 1) {
m[k] = i;
k++;
for (j = 1; j <= tr; j++) temp[j] += p[i][j];
}
}
}
}
printf("\nDeadlock causing processes are: ");
intdeadlock_found = 0;
for (j = 1; j <= tp; j++) {
found = 0;
for (i = 1; i< k; i++) {
if (j == m[i]) found = 1;
}
if (found == 0) {
printf("%d ", j);
deadlock_found = 1;
}
}
if (!deadlock_found) printf("None (System is safe)");
printf("\n");
return 0;
}
OUTPUT:
Enter total no of processes: 2
Enter total no of resources: 1
Enter claim (Max. Need) matrix
process 1: 2
process 2: 2
Enter allocation matrix
process 1: 1
process 2: 1
Enter availability vector: 0
Deadlock causing processes are: 1 2
RESULT:
Thus the program was executed successfully.
EX. NO: 9 IMPLEMENT THREADING & SYNCHRONIZATION
APPLICATIONS
AIM
To write a C program to implement the threading and synchronization applications.
ALGORITHM
Step1: Start the program.
Step 2: Enter the page size.
Step 3: Enter the logical memory address.
Step 4: Enter the user data.
Step 5: Enter starting position of each page.
Step 6: Enter data to which mapping address to be found.
Step 7: Calculate and display the physical address for the corresponding logical
address.
Step 8: Stop the program
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_ttid[2];
int counter;
void* doSomeThing(void *arg) {
unsigned long i = 0;
counter += 1; // Critical Section
printf("Job %d started\n", counter);
for(i=0; i<(0x0FFFFFFF); i++); // Reduced delay for faster execution
printf("Job %d finished\n", counter);
return NULL;
}
int main(void) {
int i = 0;
int err;
while(i< 2) {
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\nCan't create thread :[%s]", strerror(err));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
OUTPUT:
The output might be unpredictable because the threads run in parallel:
Job 1 started
Job 2 started
Job 2 finished
Job 2 finished
RESULT
Thus a C program to implement the threading and synchronization applications was
executed and output is verified successfully.
EX. NO: 10 MEMORY MANAGEMENT SCHEME USING PAGING
AIM:
To write a C program to implement the memory management scheme using paging.
ALGORITHM:
Step1: Start the program.
Step 2: Enter the page size.
Step3: Enter the logical memory address.
Step 3: Enter the user data.
Step 4: Enter starting position of each page.
Step5: Enter data to which mapping address to be found.
Step 5: Calculate and display the physical address for the corresponding logical
address.
Step 7: Stop the program.
PROGRAM:
#include <stdio.h>
int main() {
int a[20], lmem, pmem, ch, psize, str, b[35], c[10];
inti, j, k, index, page, frame, addr = 0;
printf("\nEnter page size: ");
scanf("%d", &psize);
printf("Enter logical memory (number of elements): ");
scanf("%d", &lmem);
printf("Enter physical memory size: ");
scanf("%d", &pmem);
printf("Enter user data:\n");
for(i = 0; i<lmem; i++)
scanf("%d", &a[i]);
// Initialize physical memory with -1
for(i = 0; i< 35; i++) b[i] = -1;
// Mapping logical pages to physical frames
for(i = 0; i<lmem / psize; i++) {
printf("Enter starting physical address (frame start) for page %d: ", i);
scanf("%d", &str);
// Storing the frame index (start/psize)
c[i] = str / psize;
for(j = str, k = i * psize; j < (str + psize); j++, k++) {
b[j] = a[k];
}
}
printf("\nPhysical Memory Layout:");
for(i = 0; i<pmem; i++) {
printf("\nIndex [%d]: %d", i, b[i]);
}
printf("\n\nEnter data to find its mapping address: ");
scanf("%d", &ch);
for(i = 0; i<lmem; i++) {
if(ch == a[i]) {
index = i;
page = index / psize;
frame = c[page];
addr = (frame * psize) + (index % psize);
break;
}
}
printf("\nThe physical address for data %d is %d\n", ch, addr);
return 0;
}
OUTPUT:
If you use a Page Size of 2, Logical Memory of 4, and map the data
accordingly:
Enter page size: 2
Enter logical memory: 4
Enter physical memory size: 10
Enter user data: 10 20 30 40
Enter starting physical address for page 0: 2
Enter starting physical address for page 1: 6
Physical Memory Layout:
Index [2]: 10
Index [3]: 20
Index [6]: 30
Index [7]: 40
Enter data to find its mapping address: 30
The physical address for data 30 is 6
RESULT:
Thus the C program to implement the memory management scheme using paging
algorithm Was executed and output is verified successfully.
EX. NO: 11A. IMPLEMENTATION OF FIRST FIT ALGORITHM
AIM:
To write a C program to implement the memory management scheme using first fit
algorithm.
ALGORITHM:
Step1: Start the program.
Step2: Declare the size.
Step3: Get number of process to be inserted.
Step4: Allocate the first hole that is big enough searching.
Step5: Start at the beginning of the set of holes.
Step6: If not start at the hole that is sharing the previous first fit search end.
Step7: Compare the hole.
Step8: If large enough then stop searching in the procedure.
Step9: Display the value.
Step10: Stop the program.
PROGRAM:
// FIRST FIT ALGORITHM
#include <stdio.h>
int main() {
intbsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
for (i = 0; i< 10; i++) {
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\n");
for (i = 0; i<bno; i++)
scanf("%d", &bsize[i]);
printf("Enter no. of processes: ");
scanf("%d", &pno);
printf("Enter size of each process:\n");
for (i = 0; i<pno; i++)
scanf("%d", &psize[i]);
// First Fit Allocation Logic
for (i = 0; i<pno; i++) {
for (j = 0; j <bno; j++) {
if (flags[j] == 0 &&bsize[j] >= psize[i]) {
allocation[j] = i; // Store process index i in block j
flags[j] = 1;
break; // Exit block loop once first fit is found
}
}
}
// Display allocation details
printf("\nBlock no.\tSize\t\tProcess no.\tSize");
for (i = 0; i<bno; i++) {
printf("\n%d\t\t%d\t\t", i + 1, bsize[i]);
if (flags[i] == 1)
printf("%d\t\t%d", allocation[i] + 1, psize[allocation[i]]);
else
printf("Not allocated");
}
printf("\n");
return 0;
}
OUTPUT:
If you have blocks of 100, 500, 200 and processes of 212, 417:
Enter no. of blocks: 3
Enter size of each block:
100
500
200
Enter no. of processes: 2
Enter size of each process:
212
417
Block no. Size Process no. Size
1 100 Not allocated
2 500 1 212
3 200 Not allocated
RESULT:
Thus the C program to implement the memory management scheme using first fit
algorithm Was executed and output is verified successfully
EX. NO: 11B. IMPLEMENTATION OF WORST FIT ALGORITHM
AIM:
To write a C program to implement the memory management scheme using worst
fit algorithm.
ALGORITHM:
Step1: Start the program.
Step2: Declare the size.
Step3: Get number of process to be inserted.
Step4: Allocate the first hole that is big enough searching.
Step5: Start at the beginning of the set of holes.
Step6: If not start at the hole that is sharing the previous first fit search end.
Step7: Compare the hole.
Step8: If large enough then stop searching in the procedure.
Step9: Display the value.
Step10: Stop the program.
PROGRAM:
// WORST FIT ALGORITHM
// vi worstfit.c
#include <stdio.h>
int main() {
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = -1;
static intblock_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d", &number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d", &number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m <number_of_blocks; m++) {
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m <number_of_files; m++) {
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
for(m = 0; m <number_of_files; m++) {
top = -1; // Reset to find the maximum possible fragment
for(n = 0; n <number_of_blocks; n++) {
if(block_arr[n] != 1) { // If block not allocated
temp = blocks[n] - files[m];
if(temp >= 0) {
if(top < temp) { // Logic to pick the largest block
file_arr[m] = n;
top = temp;
}
}
}
}
fragments[m] = top;
if (top != -1) {
block_arr[file_arr[m]] = 1; // Mark block as used
}
}
printf("\nFile No\tFile Size\tBlock No\tBlock Size\tFragment");
for(m = 0; m <number_of_files; m++) {
if (fragments[m] != -1) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",
m + 1, files[m], file_arr[m] + 1, blocks[file_arr[m]], fragments[m]);
} else {
printf("\n%d\t\t%d\t\tNot Allocated", m + 1, files[m]);
}
}
printf("\n");
return 0;
OUTPUT:
If you provide blocks of 100, 500, 200 and a file of 150:
Enter the Total Number of Blocks: 3
Enter the Total Number of Files: 1
Enter the Size of the Blocks:
Block No.[1]: 100
Block No.[2]: 500
Block No.[3]: 200
Enter the Size of the Files:
File No.[1]: 150
File No File Size Block No Block Size Fragment
1 150 2 500 350
RESULT:
Thus a C program to implement the memory management scheme using worst fit
algorithm was executed and output is verified successfully.
EX. NO: 11C. IMPLEMENTATION OF BEST FIT ALGORITHM
AIM:
To write a program to implement the memory management scheme using best fit
algorithm.
ALGORITHM:
Step1: Start the process
Step2: Declare the size
Step3: Give the number of processes to be inserted
Step4: Allocate the first block that is big enough searching
Step5: Start at the beginning of the set of blocks
Step6: If not start at the block that is sharing the pervious first fit search end
Step7: Compare the block
Step8: if large enough then stop searching in the procedure
Step9: Display the values
Step10: Stop the process
PROGRAM:
#include <stdio.h>
#define MAX 25
int main() {
int frag[MAX], b[MAX], f[MAX], i, j, nb, nf, temp, lowest = 10000;
static int bf[MAX], ff[MAX];
printf("\n\tBest Fit Algorithm");
printf("\nEnter the number of blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");
scanf("%d", &nf);
printf("\nEnter the size of the blocks:\n");
for (i = 1; i<= nb; i++) {
printf("Block %d: ", i);
scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");
for (i = 1; i<= nf; i++) {
printf("File %d: ", i);
scanf("%d", &f[i]);
}
for (i = 1; i<= nf; i++) {
for (j = 1; j <= nb; j++) {
if (bf[j] != 1) { // If block is not already allocated
temp = b[j] - f[i];
if (temp >= 0) {
if (lowest > temp) {
ff[i] = j;
lowest = temp;
}
}
}
}
frag[i] = lowest;
bf[ff[i]] = 1; // Mark block as allocated
lowest = 10000; // Reset for next file
}
printf("\nFile_no\tFile_size\tBlock_no\tBlock_size\tFragment");
for (i = 1; i<= nf&&ff[i] != 0; i++) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]], frag[i]);
}
printf("\n");
return 0;
}
OUTPUT
If you have blocks of 100, 500, 200 and a file of 150:
Best Fit Algorithm
Enter the number of blocks: 3
Enter the number of files: 1
Enter the size of the blocks:
Block 1: 100
Block 2: 500
Block 3: 200
Enter the size of the files:
File 1: 150
File_no File_size Block_no Block_size Fragment
1 150 3 200 50
RESULT:
Thus a C program to implement the memory management scheme using best fit
algorithm was executed and output is verified successfully.
EX. NO: 12A. IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM (FIFO)
AIM
To write a program to implement FIFO page replacement algorithm.
ALGORITHM
Step 1: Start the process
Step 2: Declare the size with respect to page length
Step 3: Check the need of replacement from the page to memory
Step 4: Check the need of replacement from old page to new page in memory
Step 5: Forma queue to hold all pages
Step 6: Insert the page require memory into the queue
Step 7: Check for bad replacement and page fault
Step 8: Get the number of processes to be inserted
Step 9: Display the values
Step 10: Stop the process
PROGRAM:
#include <stdio.h>
int main() {
inti, j, n, a[50], frame[10], no, k, avail, count = 0;
printf("\nEnter the number of pages: ");
scanf("%d", &n);
printf("Enter the page numbers:\n");
for (i = 0; i< n; i++)
scanf("%d", &a[i]);
printf("Enter the number of frames: ");
scanf("%d", &no);
// Initialize frames with -1 (empty)
for (i = 0; i< no; i++)
frame[i] = -1;
j = 0; // Index to track the oldest page for replacement
printf("\nRef String\tPage Frames\n");
for (i = 0; i< n; i++) {
printf("%d\t\t", a[i]);
avail = 0;
// Check if page is already in a frame (Page Hit)
for (k = 0; k < no; k++) {
if (frame[k] == a[i]) {
avail = 1;
break;
}
}
// If not available (Page Fault)
if (avail == 0) {
frame[j] = a[i]; // Replace oldest page
j = (j + 1) % no; // Circular increment
count++; // Increment fault count
for (k = 0; k < no; k++) {
if (frame[k] != -1)
printf("%d\t", frame[k]);
else
printf("-\t");
}
} else {
printf("Hit"); // Optional: Indicate no change on hit
}
printf("\n");
}
printf("\nTotal Page Faults: %d\n", count);
return 0;
}
OUTPUT:
If you use 7 pages (1, 3, 0, 3, 5, 6, 3) and 3 frames:
Ref String Page Frames
1 1 - -
3 1 3 -
0 1 3 0
3 Hit
5 5 3 0
6 5 6 0
3 5 6 3
Total Page Faults: 6
RESULT:
The program for FIFO page replacement was implemented and hence verified
EX. NO: 12B. IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM (LRU)
AIM
To write a program a program to implement LRU page replacement algorithm.
ALGORITHM
Step 1: Start the process
Step 2: Declare the size
Step 3: Get the number of pages to be inserted
Step 4: Get the value
Step 5: Declare counter and stack
Step 6: Select the least recently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the process
PROGRAM
#include <stdlib.h>
#include <stdio.h>
#define MAX 100
#define MIN 10
int ref[MAX], count, n;
void input() {
int temp;
count = 0;
printf("\n\tEnter the number of page frames: ");
scanf("%d", &n);
printf("\tEnter the reference string (-1 for end): ");
scanf("%d", &temp);
while(temp != -1 && count < MAX) {
ref[count++] = temp;
scanf("%d", &temp);
}
}
void LRU() {
inti, j, k, stack[MIN], top = 0, fault = 0;
for(i = 0; i< count; i++) {
int found = -1;
// Check if page already exists in the stack
for(j = 0; j < top; j++) {
if(stack[j] == ref[i]) {
found = j;
break;
}
}
if(found != -1) {
// Page Hit: Move the found page to the top (most recent)
int hit_page = stack[found];
for(k = found; k < top - 1; k++)
stack[k] = stack[k+1];
stack[top - 1] = hit_page;
} else {
// Page Fault
fault++;
if(top < n) {
// Stack is not full, just add to top
stack[top++] = ref[i];
} else {
// Stack is full: Remove bottom (Least Recently Used), shift others
for(k = 0; k < n - 1; k++)
stack[k] = stack[k+1];
stack[n - 1] = ref[i];
}
}
printf("\nAfter inserting %d, stack status (LRU -> MRU): ", ref[i]);
for(j = 0; j < top; j++) printf("%d ", stack[j]);
}
printf("\n\n\tTotal page faults: %d", fault);
}
int main() {
int x;
while(1) {
printf("\n\n\t----- MENU -----");
printf("\n\t1. Input");
printf("\n\t2. LRU Algorithm");
printf("\n\t0. Exit");
printf("\n\tEnter your choice: ");
scanf("%d", &x);
switch(x) {
case 1: input(); break;
case 2: LRU(); break;
case 0: exit(0);
default: printf("Invalid choice!");
}
}
return 0;
}
OUTPUT:
If you enter 3 frames and a reference string of 1 2 3 2 4 -1:
After inserting 1, stack status: 1
After inserting 2, stack status: 1 2
After inserting 3, stack status: 1 2 3
After inserting 2, stack status: 1 3 2 (Hit! 2 moved to top)
After inserting 4, stack status: 3 2 4 (Fault! 1 removed)
Total page faults: 4
RESULT:
The program for LRU page replacement was implanted and hence verified
EX. NO: 13A. IMPLEMENTATION OF FILE ORGANIZATION TECHNIQUES USING
SINGLE LEVEL DIRECTORY
AIM :
To write a C program to implement File Organization concept using the technique
Single level
Directory.
ALGORITHM:
Step 1: Start the Program
Step 2:Obtain the required data through char and int datatypes.
Step 3:Enter the filename,index block.
Step 4: Print the file name index loop.
Step 5:Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
void main() {
intgd = DETECT, gm;
int count, i, j, mid, cir_x;
char fname[10][20];
// Initialize graphics mode
// Note: Adjust the path "C:\\TC\\BGI" to your local Turbo C path
initgraph(&gd, &gm, "C:\\TC\\BGI");
cleardevice();
setbkcolor(GREEN);
printf("Enter number of files: ");
scanf("%d", &count);
for (i = 0; i< count; i++) {
cleardevice();
setbkcolor(GREEN);
printf("Enter file %d name: ", i + 1);
scanf("%s", fname[i]);
// Visual setup for Directory Structure
setfillstyle(1, MAGENTA);
mid = 640 / count;
// Draw Root Directory Box
bar3d(270, 100, 370, 150, 0, 0);
settextstyle(2, 0, 4);
settextjustify(1, 1);
outtextxy(320, 125, "Root Directory");
setcolor(BLUE);
cir_x = mid / 2; // Position files relative to screen width
// Draw connections for all files entered so far
for (j = 0; j <= i; j++, cir_x += mid) {
line(320, 150, cir_x, 250);
fillellipse(cir_x, 250, 30, 30);
outtextxy(cir_x, 250, fname[j]);
}
getch(); // Wait for keypress after each file entry
}
closegraph();
}
OUTPUT
RESULT
Thus files were organized into a single level directory.
EX. NO: 13B. IMPLEMENTATION OF FILE ORGANIZATION TECHNIQUESUSING
TWO LEVEL DIRECTORY
AIM:
To write a C program to implement File Organization concept using the technique
two level
Directory
ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and in datatypes.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution
PROGRAM:
#include <stdio.h>
#include <string.h>
struct {
char dname[20]; // User Directory Name
char fname[10][20]; // Files inside that directory
int fcnt; // Number of files for this user
} dir[10];
int main() {
int i, j, dcnt;
printf("Enter number of users: ");
scanf("%d", &dcnt);
for(i = 0; i<dcnt; i++) {
printf("\nEnter name of user %d: ", i + 1);
scanf("%s", dir[i].dname);
printf("Enter number of files for %s: ", dir[i].dname);
scanf("%d", &dir[i].fcnt);
for(j = 0; j <dir[i].fcnt; j++) {
printf("Enter name of file %d: ", j + 1);
scanf("%s", dir[i].fname[j]);
}
}
printf("\n--- TWO-LEVEL DIRECTORY STRUCTURE ---\n");
printf("ROOT\n");
for(i = 0; i<dcnt; i++) {
printf(" |-- User: %s\n", dir[i].dname);
for(j = 0; j <dir[i].fcnt; j++) {
printf(" | |-- File: %s\n", dir[i].fname[j]);
}
}
return 0;
}
OUTPUT:
Enter number of users: 2
Enter name of user 1: UserA
Enter number of files for UserA: 2
Enter name of file 1: lab1.c
Enter name of file 2: lab2.c
Enter name of user 2: UserB
Enter number of files for UserB: 1
Enter name of file 1: [Link]
--- TWO-LEVEL DIRECTORY STRUCTURE ---
ROOT
|-- User: UserA
| |-- File: lab1.c
| |-- File: lab2.c
|-- User: UserB
| |-- File: [Link]
RESULT
Thus user files have been stored in their respective directories and retrieved easily.
EX. NO: 14A. IMPLEMENTATION OF SEQUENTIAL FILE
ALLOCATIONSTRATEGIES
AIM
To write a C program to implement File Allocation Strategies.
ALGORITHM
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations to each in sequential order.
a). Randomly select a location from available location s1= random (100);
b). Check whether the required locations are free from the selected location.
c). Allocate and set flag=1 to the allocated locations.
Step 5: Print the results file no., length, Blocks allocated.
Step 6: Stop the program.
PROGRAM:
#include <stdio.h>
#include <stdlib.h> // Required for exit()
int main() {
int f[50], i, st, j, len, c;
// Initializing disk blocks to 0 (free)
for(i = 0; i< 50; i++)
f[i] = 0;
X:
printf("\nEnter the starting block & length of file: ");
scanf("%d %d", &st, &len);
// Check if the range is available
int count = 0;
for(j = st; j < (st + len); j++) {
if(f[j] == 0) {
count++;
}
}
if(len == count) {
for(j = st; j < (st + len); j++) {
if(f[j] == 0) {
f[j] = 1;
printf("%d -> %d\n", j, f[j]);
}
}
printf("The file is allocated to disk\n");
}
else {
printf("Block already allocated or not enough space\n");
}
printf("Do you want to enter more files? (Yes-1 / No-0): ");
scanf("%d", &c);
if(c == 1)
goto X;
else
exit(0);
return 0;
}
OUTPUT:
Enter the starting block & length of file: 2 4
2 -> 1
3 -> 1
4 -> 1
5 -> 1
The file is allocated to disk
Do you want to enter more files? (Yes-1 / No-0): 1
Enter the starting block & length of file: 4 2
Block already allocated or not enough space
RESULT:
Thus the C program to implement the Sequential file allocation strategies was
executed and Output is verified successfully.
EX. NO: 14B. IMPLEMENTATION OF INDEXED FILE ALLOCATION STRATEGIES
AIM
To write a C program to implement File Allocation Strategies.
ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and int datatypes.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main() {
int f[50] = {0}, i, k, j, inde[50], n, c, p;
X:
printf("\nEnter index block: ");
scanf("%d", &p);
if (f[p] == 0) {
f[p] = 1;
printf("Enter number of data blocks needed: ");
scanf("%d", &n);
} else {
printf("Index block %d is already allocated!\n", p);
goto X;
}
printf("Enter the block numbers:\n");
for (i = 0; i< n; i++) {
scanf("%d", &inde[i]);
}
// Check if any requested data block is already taken
for (i = 0; i< n; i++) {
if (f[inde[i]] == 1) {
printf("Block %d is already allocated! Try again.\n", inde[i]);
f[p] = 0; // Free the index block since allocation failed
goto X;
}
}
// Allocate the blocks
for (j = 0; j < n; j++) {
f[inde[j]] = 1;
}
printf("\nFile Allocated Successfully!");
printf("\nIndex Block: %d", p);
for (k = 0; k < n; k++) {
printf("\n %d -> %d", p, inde[k]);
}
printf("\n\nEnter 1 to add more files, 0 to exit: ");
scanf("%d", &c);
if (c == 1) goto X;
return 0;
}
OUTPUT
Enter index block: 5
Enter number of data blocks needed: 3
Enter the block numbers:
1 8 12
File Allocated Successfully!
Index Block: 5
5 -> 1
5 -> 8
5 -> 12
Enter 1 to add more files, 0 to exit: 1
Enter index block: 5
Index block 5 is already allocated!
RESULT:
Thus the C program to implement the Indexed file allocation strategies was
executed and Output is verified successfully.
EX. NO: 14C. IMPLEMENTATION OF LINKED FILE ALLOCATION
STRATEGIES
AIM
To write a C program to implement Linked File Allocation Strategies.
ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and int datatypes.
Step 3: Enter the filename, starting block ending block.
Step 4: Print the free block using loop.
Step 5: ‟for‟ loop is created to print the file utilization of linked type of entered type.
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution
PROGRAM
//Linked File Allocation Program:
#include <stdio.h>
int main() {
int f[50], p, i, j, k, a, st, len, c;
// Initialize all blocks as free (0)
for(i = 0; i< 50; i++) f[i] = 0;
printf("Enter how many blocks are already allocated: ");
scanf("%d", &p);
if(p > 0) {
printf("Enter the block numbers already allocated: ");
for(i = 0; i< p; i++) {
scanf("%d", &a);
f[a] = 1;
}
}
X:
printf("\nEnter the starting index block & length: ");
scanf("%d %d", &st, &len);
k = len;
for(j = st; j < (k + st); j++) {
if(f[j] == 0) {
f[j] = 1;
printf("%d -> %d\n", j, f[j]);
}
else {
printf("%d -> Block already allocated (skipping)\n", j);
k++; // Increase the loop range to find another free block
}
}
printf("\nDo you want to enter more files? (Yes-1/No-0): ");
scanf("%d", &c);
if(c == 1) goto X;
return 0;
}
OUTPUT:
In this example, the user wants 3 blocks starting from 2, but block 3 is already busy.
The OS skips 3 and takes 2, 4, and 5.
Enter how many blocks are already allocated: 1
Enter the block numbers already allocated: 3
Enter the starting index block & length: 2 3
2 -> 1
3 -> Block already allocated (skipping)
4 -> 1
5 -> 1
Do you want to enter more files? (Yes-1/No-0): 0
RESULT:
Thus the C program to implement the Linked file allocation strategies was executed
and Output is verified successfully.
EX. NO: 15. IMPLEMENTATION OF SSTF DISK SCHEDULING ALGORITHM
AIM
To write a C program to implement SSTF Disk Scheduling Algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables.
Step 3: Prompt for current position.
Step 4: Get the number of requests for SSTF.
Step 5: Get the request order.
Step 6: Calculate distance of each request from current position.
Step 7: Find the nearest request.
Step 8: Change the current position value to next request.
Step 9: Display the total head movement.
Step 10: Stop the program.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
inti, n, k, req[50], mov = 0, cp, index[50], min, a[50], j = 0, mini, cp1;
printf("Enter the current head position: ");
scanf("%d", &cp);
cp1 = cp; // Store original head position for final display
printf("Enter the number of requests: ");
scanf("%d", &n);
printf("Enter the request order:\n");
for (i = 0; i< n; i++) {
scanf("%d", &req[i]);
}
// SSTF Core Logic
for (k = 0; k < n; k++) {
for (i = 0; i< n; i++) {
index[i] = abs(cp - req[i]); // Distance from current position
}
// Find the nearest request
min = 9999; // Initialize with a very high value
for (i = 0; i< n; i++) {
if (min > index[i]) {
min = index[i];
mini = i;
}
}
a[j] = req[mini]; // Add closest request to sequence
j++;
cp = req[mini]; // Update current head position
req[mini] = 9999; // Mark as processed so it's not picked again
}
// Output and Seek Time Calculation
printf("\nSequence is: %d", cp1);
mov = abs(cp1 - a[0]); // First movement
printf(" -> %d", a[0]);
for (i = 1; i< n; i++) {
mov += abs(a[i] - a[i - 1]); // Add subsequent movements
printf(" -> %d", a[i]);
}
printf("\nTotal head movement = %d\n", mov);
return 0;
}
OUTPUT :
If the current head is at 50 and the requests are 82, 170, 43, 140, 24:
Enter the current head position: 50
Enter the number of requests: 5
Enter the request order: 82 170 43 140 24
Sequence is: 50 -> 43 -> 24 -> 82 -> 140 -> 170
Total head movement = 172
RESULT :
Thus a C program to implement SSTF Disk Scheduling Algorithm was executed and
output is verified successfully.