Btech 2-2 O.S Lab MANUAL-1
Btech 2-2 O.S Lab MANUAL-1
who: This command is used to show the user's list logged in currently.
Syntax:who [option] ... [file][arg1]
cat: This command is used to concatenate files and display them on stdout.
Syntax:cat [OPTION]…[FILE]
2.Write programs using the following UNIX operating system calls fork, exec, getpd,
exit, wait, close, stat, opendir and readdir?
i) fork() and exec():
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) { // Child process
execlp("ls", "ls", "-l", NULL); // Execute `ls -l`
} else if (pid > 0) { // Parent process
wait(NULL); // Wait for the child process to finish
} else {
perror("Fork failed");
}
return 0;
}
1. Exit():
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Program is exiting.\n");
exit(0); // Exit with status code 0
}
2. wait() :
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid == 0) { // Child process
printf("Child process\n");
_exit(0);
} else { // Parent process
wait(NULL); // Wait for child process to finish
printf("Child process finished\n");
}
return 0;
}
3. close():
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
int fd = open("example.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd != -1) {
write(fd, "Hello, World!\n", 14);
close(fd); // Close the file descriptor
} else {
perror("Failed to open file");
}
return 0;
}
4. stat():
#include <stdio.h>
#include <sys/stat.h>
int main() {
struct stat fileStat;
if (stat("example.txt", &fileStat) == 0) {
printf("File size: %ld bytes\n", fileStat.st_size);
printf("Last modified: %ld\n", fileStat.st_mtime);
} else {
perror("stat failed");
}
return 0;
}
5. opendir() and readdir():
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir = opendir(".");
if (dir) {
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
} else {
perror("Failed to open directory");
}
return 0;
}
OUTPUT:
pugazh@Pugazh-PC:/mnt/e/ospgm$ ./a.out cp.c cp1.c
OUTPUT:
pugazh@Pugazh-PC:/mnt/e/ospgm$ gcc ls.c
pugazh@Pugazh-PC:/mnt/e/ospgm$ ./a.out
Enter directory name
Hello
Cannot find directory: No such file or directory
pugazh@Pugazh-PC:/mnt/e/ospgm$ ./a.out
Enter directory name
Sam
HillCipher.javaMD5.javaObjectSigningExample.javaplayfair.javaRailFence2.java
RSA.javaSHA1.javaVigenereCipher.java
pugazh@Pugazh-PC:/mnt/e/ospgm$
OUTPUT:
pugazh@Pugazh-PC:/mnt/e/ospgm$ gcc grep.c
pugazh@Pugazh-PC:/mnt/eospgm$ ./a.out
Enter file name
grep.c
Enter pattern to be searched
printf(“Enter file name\n”);
printf(“Enter pattern to be searched\n”);
printf(“%s”,temp);
pugazh@Pugazh-PC:/mnt/e/ospgm$
B)SJF Scheduling
Code:
#include<stdio.h>
int main()
{
int i,j,k,n,sum,wt[10],tt[10],twt,ttat; int t[10],p[10];
float awt,atat;
printf("Enter number of process\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of Process %d",i); scanf("\n %d",&t[i]);
}
for(i=0;i<n;i++) p[i]=i; for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if(t[i]>t[k])
{
int temp; temp=t[i]; t[i]=t[k]; t[k]=temp; temp=p[i]; p[i]=p[k]; p[k]=temp;
}
}
printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM");
printf("\n PROCESS ID \t BURST TIME \t WAITING TIME \t TURNAROUNDTIME \n\n");
wt[0]=0;
for(i=0;i<n;i++)
{
sum=0; for(k=0;k<i;k++)
{
wt[i]=sum+t[k]; sum=wt[i];
}
}
for(i=0;i<n;i++)
{
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
printf("%5d \t\t5%d \t\t %5d \t\t %5d \n\n",p[i],t[i],wt[i],tt[i]);
}
twt=0; ttat=t[0]; for(i=1;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tt[i];
}
awt=(float)twt/n; atat=(float)ttat/n;
printf("\n AVERAGE WAITING TIME %4.2f",awt); printf("\n AVERAGE TURN AROUND TIME
%4.2f",atat);
}
}
OUTPUT:
Enter number of process 3
Enter the Burst Time of Process 04
Enter the Burst Time of Process 13
Enter the Burst Time of Process 25
SHORTEST JOB FIRST SCHEDULING ALGORITHM
PROCESS ID BURST TIME WATING TIME TURNAROUND TIME
1 3 0
0 4 3
2 5 7
AVERAGE WAITING TIME 3.33
AVERAGE TURN AROUND TIME 7.33
Process exited after 16.62 seconds with return value 0
Press any key to continue …
C)Priority Scheduling.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0; float awt,atat;
printf("\n-----------PRIORITY SCHEDULING \n");
printf("Enter the No of Process: ");
scanf("%d", &n);
for (i=0;i<n;i++)
{
pid[i] = i;
printf("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);
}
// Sorting start for (i=0;i<n;i++) for(j=i+1;j<n;j++)
{
if (pr[i] > pr[j] )
{
t = pr[i]; pr[i] = pr[j]; pr[j] = t;
t = bt[i]; bt[i] = bt[j]; bt[j] = t;
}
}
// Sorting finished tat[0] = bt[0]; wt[0] = 0;
for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}
printf("\n \n");
printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n"); printf("\n \
n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat+tat[i]; twt = twt + wt[i];
}
awt = (float)twt / n; atat = (float)ttat / n;
printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time: %f\n",awt,atat);
}
OUTPUT:
___PRIORITY SCHEDULING ____
Enter the No of Process: 4
Enter the Burst time of Pid 0 : 2
Enter the Priority of Pid 0 : 3
Enter the Burst time of Pid 1 : 6
Enter the Priority of Pid 1 : 2
Enter the Burst time of Pid 2 : 4
Enter the Priority of Pid 2 : 1
Enter the Burst time of Pid 3 : 5
Enter the Priority of Pid 3 : 7
Enter the Burst Time for the process1 0 Enter the process ID 2 6
Enter the Burst Time for the process1 5 Enter the process ID 3 7
Enter the Burst Time for the process2 0 Enter the process ID 4 8
Enter the Burst Time for the process2 5
1 5 10 15
2 6 15 25
3 7 20 25
4 8 25 20
a) Semaphore:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define MAX_PORTS 3 // Maximum number of ports that can be opened concurrently
sem_t port_semaphore; // Semaphore to control port access
void *use_port(void *arg)
{
int thread_id = *(int *)arg;
printf("Thread %d: Waiting to open a port...\n", thread_id);
sem_wait(&port_semaphore); // Decrement semaphore
printf("Thread %d: Port opened. Using port...\n", thread_id);
printf("Thread %d: Done. Closing port...\n", thread_id);
sem_post(&port_semaphore); // Increment semaphore
return NULL;
}
int main()
{
pthread_t threads[5];
int thread_ids[5]; sem_init(&port_semaphore, 0, MAX_PORTS);
OUTPUT:
Thread 1: Waiting to open a port...
Thread 2: Waiting to open a port...
Thread 1: Port opened. Using port...
Thread 2: Port opened. Using port...
Thread 3: Waiting to open a port...
Thread 1: Done. Closing port...
Thread 3: Port opened. Using port...
...
b)Monitor:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
typedef struct
{
int availablePorts;
pthread_mutex_t lock;
pthread_cond_t condition;
}
PortMonitor;
void initializeMonitor(PortMonitor *monitor, int maxPorts)
{
monitor->availablePorts = maxPorts;
pthread_mutex_init(&monitor->lock, NULL);
pthread_cond_init(&monitor->condition, NULL);
}
return 0;
}
OUTPUT:
Thread 1 has opened a port. Available ports: 2
Thread 2 has opened a port. Available ports: 1
Thread 3 has opened a port. Available ports: 0
Thread 4 is waiting for a port...
Thread 5 is waiting for a port...
Thread 1 has closed a port. Available ports: 1
Thread 4 has opened a port. Available ports: 0
Thread 2 has closed a port. Available ports: 1
Thread 5 has opened a port. Available ports: 0
...
6) Write a program to illustrate concurrent execution of threads using the pthreads library?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
// Thread function
void *printMessage(void *arg)
{
int threadId = *(int *)arg;
printf("Thread %d: Starting execution\n", threadId);
printf("Thread %d: Finishing execution\n", threadId);
return NULL;
}
int main()
{
const int numThreads = 5; // Number of threads to create
pthread_t threads[numThreads];
int threadIds[numThreads];
for (int i = 0; i < numThreads; i++)
{
threadIds[i] = i + 1;
if (pthread_create(&threads[i], NULL, printMessage, &threadIds[i]) != 0)
{
perror("Failed to create thread");
}
printf("Main: Thread %d has been created\n", threadIds[i]);
}
// Wait for all threads to complete
for (int i = 0; i < numThreads; i++)
{
pthread_join(threads[i], NULL);
printf("Main: Thread %d has completed\n", threadIds[i]);
}
printf("Main: All threads have finished execution\n");
return 0;
}
OUTPUT:
Main: Thread 1 has been created
Main: Thread 2 has been created
Main: Thread 3 has been created
Main: Thread 4 has been created
Main: Thread 5 has been created
Thread 1: Starting execution
Thread 2: Starting execution
Thread 3: Starting execution
Thread 4: Starting execution
Thread 5: Starting execution
Thread 1: Finishing execution
Main: Thread 1 has completed
Thread 2: Finishing execution
Main: Thread 2 has completed
Thread 3: Finishing execution
Main: Thread 3 has completed
Thread 4: Finishing execution
Main: Thread 4 has completed
Thread 5: Finishing execution
Main: Thread 5 has completed
Main: All threads have finished execution
7) Write a program to solve producer-consumer problem using Semaphores?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5 // Size of the shared buffer
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
sem_t empty;
sem_t full;
pthread_mutex_t mutex;
void *producer(void *arg)
{
for (int i = 1; i <= 10; i++)
{
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = i;
printf("Producer produced: %d\n", i);
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
return NULL;
}
void *consumer(void *arg)
{
for (int i = 1; i <= 10; i++)
{
sem_wait(&full);
pthread_mutex_lock(&mutex);
int main()
{
pthread_t prodThread, consThread;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_create(&prodThread, NULL, producer, NULL);
pthread_create(&consThread, NULL, consumer, NULL);
pthread_join(prodThread, NULL);
pthread_join(consThread, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
OUTPUT:
Producer produced: 1
Consumer consumed: 1
Producer produced: 2
Producer produced: 3
Consumer consumed: 2
Producer produced: 4
Consumer consumed: 3
Producer produced: 5
Consumer consumed: 4
Producer produced: 6
First Fit allocates the first partition that is large enough to hold the process.
2. Worst Fit:
Worst Fit allocates the largest available partition to the process, leaving the most
unused space in the partition.
3. Best Fit:
Best Fit allocates the smallest available partition that is large enough to
accommodate the process, leaving the least unused space.
CODE:
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a Partition
typedef struct {
int size;
int isOccupied;
int processID;
}
Partition;
typedef struct
{
int processID;
int size;
}
Process;
void firstFit(Partition partitions[], int numPartitions, Process processes[], int
numProcesses);
void worstFit(Partition partitions[], int numPartitions, Process processes[], int
numProcesses);
void bestFit(Partition partitions[], int numPartitions, Process processes[], int
numProcesses);
void printPartitions(Partition partitions[], int numPartitions);
int main()
{
Partition partitions[] =
{
{100, 0, -1}, {200, 0, -1}, {300, 0, -1}, {150, 0, -1}, {250, 0, -1},
{120, 0, -1}, {170, 0, -1}, {220, 0, -1}, {50, 0, -1}, {180, 0, -1}
};
Process processes[] = {
{1, 120}, {2, 90}, {3, 180}, {4, 150}, {5, 60},
{6, 110}, {7, 200}, {8, 130}
};
int numPartitions = sizeof(partitions) / sizeof(partitions[0]);
int numProcesses = sizeof(processes) / sizeof(processes[0]);
printf("First-Fit Allocation:\n");
firstFit(partitions, numPartitions, processes, numProcesses);
printPartitions(partitions, numPartitions);
for (int i = 0; i < numPartitions; i++) {
partitions[i].isOccupied = 0;
partitions[i].processID = -1;
}
printf("\nWorst-Fit Allocation:\n");
worstFit(partitions, numPartitions, processes, numProcesses);
printPartitions(partitions, numPartitions);
for (int i = 0; i < numPartitions; i++)
{
partitions[i].isOccupied = 0;
partitions[i].processID = -1;
}
printf("\nBest-Fit Allocation:\n");
bestFit(partitions, numPartitions, processes, numProcesses);
printPartitions(partitions, numPartitions);
return 0;
}
// First Fit allocation
void firstFit(Partition partitions[], int numPartitions, Process processes[], int
numProcesses)
{
for (int i = 0; i < numProcesses; i++)
{
for (int j = 0; j < numPartitions; j++)
{
if (partitions[j].isOccupied == 0 && partitions[j].size >= processes[i].size)
{
partitions[j].isOccupied = 1;
partitions[j].processID = processes[i].processID;
printf("Process %d allocated to Partition %d\n", processes[i].processID, j);
break;
}
}
}
}
if (worstIndex != -1)
{
partitions[worstIndex].isOccupied = 1;
partitions[worstIndex].processID = processes[i].processID;
printf("Process %d allocated to Partition %d\n", processes[i].processID,
worstIndex);
}
}
}
if (bestIndex != -1) {
partitions[bestIndex].isOccupied = 1;
partitions[bestIndex].processID = processes[i].processID;
printf("Process %d allocated to Partition %d\n", processes[i].processID,
bestIndex);
}
}
}
Worst-Fit Allocation:
Process 1 allocated to Partition 2
Process 2 allocated to Partition 1
Process 3 allocated to Partition 9
Process 4 allocated to Partition 6
Process 5 allocated to Partition 4
Process 6 allocated to Partition 3
Process 7 allocated to Partition 7
Process 8 allocated to Partition 0
Partition 0: Occupied by Process 8, Size 100
Partition 1: Occupied by Process 2, Size 200
Partition 2: Occupied by Process 1, Size 300
Partition 3: Occupied by Process 6, Size 120
Partition 4: Occupied by Process 5, Size 250
Partition 5: Free, Size 120
Partition 6: Occupied by Process 4, Size 150
Partition 7: Occupied by Process 7, Size 170
Partition 8: Free, Size 50
Partition 9: Occupied by Process 3, Size 180
Best-Fit Allocation:
Process 1 allocated to Partition 5
Process 2 allocated to Partition 0
Process 3 allocated to Partition 9
Process 4 allocated to Partition 6
Process 5 allocated to
9)Simulate the following page replacement algorithms
a) FIFO b) LRU c) LFU
FIFO Page replacement algorithm.
include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of frames . . ");
scanf("%d",&nof);
printf("Enter number of reference string..\n"); scanf("%d",&nor);
printf("\n Enter the reference string.."); for(i=0;i<nor;i++) scanf("%d",&ref[i]);
printf("\nThe given reference string:"); for(i=0;i<nor;i++) printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n"); for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]); for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1; break;
}
}
if(flag==0)
{
pf++; victim++;
victim=victim%nof; frm[victim]=ref[i]; for(j=0;j<nof;j++) printf("%4d",frm[j]);
}
}
printf("\n\n\t\t No.of pages faults. .%d",pf);
}
OUTPUT:
Enter no.of frames . . 4
Enter number of reference string . 6
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n ");
printf("\nEnter the no.of frames");
scanf("%d",&nof);
printf("Enter the no.of reference string");
scanf("%d",&nor);
printf("Enter the reference string"); for(i=0;i<nor;i++) scanf("%d",&ref[i]);
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\n ");
printf("\nThe given string");
printf("\n \n");
for(i=0;i<nor;i++)\
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for(i=0;i<10;i++) recent[i]=0; printf("\n"); for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tref no % ->\t",ref[i]); for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1; break;
}
}
if(flag==0) { count++; if(count<=nof) victim++;
else victim=optvictim(i); pf++; frm[victim]=ref[i];
for(j=0;j<nof;j++) printf("%4d",frm[j]);
}
}
printf("\n Number of page faults: %d",pf); getch();
}
int optvictim(int index)
{
int i,j,temp,notfound; for(i=0;i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1) return i;
}
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
}
OUTPUT:
11) Implement Bankers Algorithm for Dead Lock avoidance and prevention
}
printf("\n Enter total numbers of each resources:"); for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
printf("\n Enter Max resources for each process:"); for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i); for(j=1;j<= rno;j++) scanf("%d",&max[i][j]);
}
printf("\n Enter allocated resources for each process:"); for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i); for(j=1;j<=rno;j++) scanf("%d",&allocated[i][j]);
}
printf("\n available resources:\n"); for(j=1;j<= rno;j++)
{
avail[j]=0; total=0;
for(i=1;i<=pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total; work[j]=avail[j]; printf(" %d\t",work[j]);
}
do{ for(i=1;i<=pno;i++)
{
for(j=1;j<=rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
}
printf("\n Allocated matrix Max need"); for(i=1;i<=pno;i++)
{
printf("\n"); for(j=1;j<=rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|"); for(j=1;j<=rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|"); for(j=1;j<=rno;j++)
{
printf("%4d",need[i][j]);
}
}
prc=0; for(i=1;i<=pno;i++)
{
if(flag[i]==0)
{
prc=i; for(j=1;j<=rno;j++)
{
if(work[j]<need[i][j])
{
prc=0; break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process %d completed",i); count++;
printf("\n Available matrix:"); for(j=1;j<=rno;j++)
{
work[j]+=allocated[prc][j]; allocated[prc][j]=0; max[prc][j]=0; flag[prc]=1;
printf(" %d",work[j]);
}
}
}
while(count!=pno&&prc!=0); if(count==pno)
printf("\nThe system is in a safe state!!"); else
printf("\nThe system is in an unsafe state! ");
}
Enter number of process:5 Enter number of resources:3
Enter total numbers of each resources:10
5
7
Enter Max resources for each process: for Process1:7
5
3
for process 2:3
2
2
for process 3:2
2
2
for process 4:9
0
2
for process 5:4
3
3
Enter allocated resources for each process: for process 1:0
1
0
for process 2:3
0
2
for process 3:3
0
2
for process 4:2
1
1
for process 5:0
0
2
available resources: 2 3 0
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
3 0 2| 3 2 2| 0 2 0
3 0 2| 2 2 2| -1 2 0
2 1 1| 9 0 2| 7 -1 1
0 0 2| 4 3 3| 4 3 1
Process 2 completed
Available matrix: 5 3 2 Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 2 2 2| -1 2 0
2 1 1| 9 0 2| 7 -1 1
0 0 2| 4 3 3| 4 3 1
Process 3 completed
Available matrix: 8 3 4 Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
2 1 1| 9 0 2| 7 -1 1
0 0 2| 4 3 3| 4 3 1
Process 4 completed
Available matrix: 10 4 5 Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
00 2| 4 3 3| 4 3 1
Process 1 completed
Available matrix: 10 5 5 Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed
Available matrix: 10 5 7 The system is in a safe state!!
Process exited after 137 seconds with return value 32 Press any key to continue .
12)Simulate the following file allocation strategies
a) Sequential b) Indexed c) Linked
a) Sequential Allocation:
#include <stdio.h>
#include <stdlib.h>
#define MAX_BLOCKS 10:
int disk[MAX_BLOCKS] = {0};
void sequentialAllocation(int fileSize) {
int blocksRequired = fileSize;
printf("\nSequential Allocation:\n");
// Find contiguous free blocks for the file
for (int i = 0; i < MAX_BLOCKS && blocksRequired > 0; i++) {
if (disk[i] == 0) { // If the block is free
disk[i] = 1; // Allocate the block
blocksRequired--;
printf("Block %d allocated\n", i);
}
}
if (blocksRequired > 0)
{
printf("Not enough space for the file.\n");
} else {
printf("File allocated successfully.\n");
}
}
int main()
{
int fileSize;
printf("Enter the file size for sequential allocation (number of blocks): ");
scanf("%d", &fileSize);
sequentialAllocation(fileSize);
return 0;
}
OUTPUT:
Enter the file size for sequential allocation (number of blocks): 5
Sequential Allocation:
Block 0 allocated
Block 1 allocated
Block 2 allocated
Block 3 allocated
Block 4 allocated
File allocated successfully.
b) Indexed Allocation:
#include <stdio.h>
#include <stdlib.h>
#define MAX_BLOCKS 10
#define MAX_FILE_SIZE 5
if (blocksRequired > 0)
{
printf("Not enough space for the file.\n");
} else {
printf("File allocated successfully.\n");
}
}
int main()
{
int fileSize;
printf("Enter the file size for indexed allocation (number of blocks): ");
scanf("%d", &fileSize);
indexedAllocation(fileSize);
return 0;
}
OUTPUT:
Enter the file size for indexed allocation (number of blocks): 4
Indexed Allocation:
Block 0 allocated at index 0
Block 1 allocated at index 1
Block 2 allocated at index 2
Block 3 allocated at index 3
File allocated successfully.
C) Linked Allocation:
#include <stdio.h>
#include <stdlib.h>
#define MAX_BLOCKS 10 // Number of available blocks in the disk
int disk[MAX_BLOCKS] = {0};
int nextBlock[MAX_BLOCKS];
void linkedAllocation(int fileSize)
{
int blocksRequired = fileSize;
int firstBlock = -1, prevBlock = -1;
printf("\nLinked Allocation:\n");
if (prevBlock != -1)
{
nextBlock[prevBlock] = i;
printf("Block %d linked to block %d\n", i, prevBlock);
}
prevBlock = i;
blocksRequired--;
}
}
if (blocksRequired > 0)
{
printf("Not enough space for the file.\n");
}
Else
{
printf("File allocated successfully.\n");
}
}
int main()
{
int fileSize;
printf("Enter the file size for linked allocation (number of blocks): ");
scanf("%d", &fileSize);
linkedAllocation(fileSize);
return 0;
}
OUTPUT:
Enter the file size for linked allocation (number of blocks): 3
Linked Allocation:
File starts at block 0
Block 1 linked to block 0
Block 2 linked to block 1
File allocated successfully.
13)Download and install Nachos operating system and experiment with it.
Downloading Nachos
1. Visit the Nachos website: Go to the official Nachos website ((link unavailable)) and click on
the "Releases" tab.
2. Download the Nachos source code: Click on the latest release (e.g., "Nachos-6.0") and
download the source code zip file (e.g., "nachos-6.0.zip").
3. Extract the source code: Extract the contents of the zip file to a directory on your
computer (e.g., C:\nachos or ~/nachos).
Installing Nachos
1. Install a C++ compiler: Nachos requires a C++ compiler to build. Install a C++ compiler such
as GCC (GNU Compiler Collection) or Clang.
2. Install the Nachos dependencies: Nachos requires several dependencies, including make
and gcc. Install these dependencies using your package manager (e.g., apt-get on Ubuntu or
brew on macOS).
3. Build Nachos: Navigate to the Nachos directory and run the make command to build
Nachos.
Running Nachos
1. Run Nachos: After building Nachos, run the nachos command to start the operating
system.
2. Explore the Nachos shell: Once Nachos is running, you'll see a shell prompt. You can use
this shell to explore the Nachos file system, run commands, and execute programs.
1. Run Nachos programs: Nachos comes with several example programs, such as hello.c and
sort.c. Run these programs using the nachos command.
2. Write your own Nachos programs: Try writing your own programs in C++ and compiling
them for Nachos.
3. Experiment with Nachos system calls: Nachos provides several system calls, such as Create
and Open. Experiment with these system calls to learn more about how Nachos works.
4. Modify the Nachos source code: Try modifying the Nachos source code to add new
features or fix bugs.