OS LAB Programs - Colab
OS LAB Programs - Colab
OS LAB Programs - Colab
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create struct dirent.
STEP 3: declare the variable buff and pointer dptr.
STEP 4: Get the directory name.
STEP 5: Open the directory.
STEP 6: Read the contents in directory and print it.
STEP 7: Close the directory.
%%writefile 1.c
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main() {
char dirName[100];
struct dirent *entry;
DIR *dir;
closedir(dir);
return 0;
}
Writing 1.c
!gcc 1.c
!./a.out
Enter directory name: a
Directory does not exist.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables pid,pid1,pid2.
STEP 3: Call fork() system call to create process.
STEP 4: If pid==-1, exit.
STEP 5: Ifpid!=-1 , get the process id using getpid().
STEP 6: Print the process id.
STEP 7: Stop the program
%%writefile 2.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
int pid = fork();
if (pid == -1) {
printf("Error in process creation\n");
return 1;
}
if (pid != 0) {
printf("Parent process ID: %d\n", getpid());
} else {
printf("Child process ID: %d\n", getpid());
}
return 0;
}
Writing 2.c
!gcc 2.c
!./a.out
ALGORITHM:
Step 1 : Input Number of Processes
Step 2: Input the processes burst time (bt).
Step 3: Find waiting time (wt) for all processes.
● As first process that comes need not to wait so waiting time for process 1 will be 0, i
● Find waiting time for all other processes i.e. for process i ->wt[i] = bt[i-1] + wt[i-1
Step 4: Find turnaround time = waiting_time + burst_time for all processes.
Step 5: Find average waiting time = total_waiting_time / no_of_processes.
Step 6: Similarly, find average turnaround time = total_turn_around_time / no_of_processes.
%%writefile FCFS.c
#include <stdio.h>
int main() {
int pid[15], bt[15], wt[15], n;
float twt = 0, tat = 0;
return 0;
}
Overwriting FCFS.c
!gcc FCFS.c
!./a.out
ALGORITHM:
Step 1: Enter number of processes.
Step 2: Enter the burst time of all the processes.
Step 3: Sort all the processes according to their burst time and its respective process numbe
Step 4: Find waiting time, WT of all the processes.
Step 5: For the smallest process, WT = 0.
Step 6: For all the next processes i, find waiting time by adding burst time of all the previ
Step 7: Calculate Turnaround time = WT + BT for all the processes.
Step 8: Calculate average waiting time = total waiting time / no. of processes.
Step 9: Calculate average turnaround time= total turnaround time / no. of processes.
%%writefile SJF.c
#include <stdio.h>
int main() {
int n, pid[15], bt[15], wt[15], tat[15];
float twt = 0, ttat = 0;
return 0;
}
Writing SJF.c
!gcc SJF.c
!./a.out
ALGORITHM:
Step 1: Enter number of processes.
Step 2: Enter the burst time and arrival time of all the processes.
Step 3: Sort all the processes according to their arrival time and its respective process bur
Step 4 Traverse until all process gets completely executed.
● Find process with minimum remaining time at every single time lap.
● Reduce its time by 1.
● Check if its remaining time becomes 0
● Increment the counter of process completion.
● Completion time of current process = current_time + 1;
● Calculate waiting time for each completed process.
● wt[i]= Completion time – arrival_time-burst_time
● Increment time lap by one.
Step 5: Calculate Turnaround time = WT + BT for all the processes.
Step 6: Calculate average waiting time = total waiting time / no. of processes.
Step 7 Calculate average turnaround time= total turnaround time / no. of processes.
%%writefile SRTF.c
#include <stdio.h>
int main() {
int n, i, smallest, time, count = 0, end_time;
int at[10], bt[10], rt[10]; // Arrival Time, Burst Time, Remaining Time
int wt = 0, tat = 0, completed = 0;
float avg_wt, avg_tat;
// Find the process with the smallest remaining time that has arrived
for (i = 0; i < n; i++) {
if (at[i] <= time && rt[i] > 0) {
if (smallest == -1 || rt[i] < rt[smallest]) {
smallest = i;
}
}
}
if (smallest == -1) {
continue; // If no process is ready, skip the time unit
}
total_tat += turnaround_time;
total_wt += waiting_time;
// Calculate averages
avg_wt = (float)total_wt / n;
avg_tat = (float)total_tat / n;
return 0;
}
Overwriting SRTF.c
!gcc SRTF.c
!./a.out
%%writefile Priority.c
#include <stdio.h>
int main() {
int bt[20], p[20], pri[20], wt[20], tat[20], i, j, n, total = 0, totalT = 0, pos, temp;
float avg_wt, avg_tat;
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
temp = p[i];
p[i] = p[pos];
p[pos] = temp;
}
// Calculate turnaround time for each process and total turnaround time
printf("\nProcess\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i]; // Turnaround time = Burst Time + Waiting Time
totalT += tat[i]; // Total Turnaround Time
printf("p%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i], bt[i], pri[i], wt[i], tat[i]);
}
return 0;
}
Writing Priority.c
!gcc Priority.c
!./a.out
ALGORITHM:
Step 1: Take input for number of process and Burst time of each process.
Step 2: Take input for time quantum
Step 3: Find the waiting time for each process
Step 4: Calculate the turnaround time of each process.
Step 5: Calculate average waiting time and average turnaround time.
%%writefile RR.c
#include <stdio.h>
int main() {
// Input number of processes
int n;
printf("Enter Total Number of Processes: ");
scanf("%d", &n);
// Initialize counters
int total = 0, counter = 0, i = 0;
printf("\nProcess ID\tBurst Time\tTurnaround Time\tWaiting Time\n");
return 0;
}
Overwriting RR.c
!gcc RR.c
!./a.out
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the required variables.
Step 3: Initialize the buffer size and get maximum item you want to produce.
Step 4: Get the option, which you want to do either producer, consumer or exit from the opera
Step 5: If you select the producer, check the buffer size if it is full the producer should n
the item and increase the value buffer size.
Step 6: If you select the consumer, check the buffer size if it is empty the consumer should
the item and decrease the value of buffer size.
Step 7: If you select exit come out of the program.
Step 8: Stop the program.
%%writefile producer_consumer.c
#include <stdio.h>
#include <stdlib.h>
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);
}
int main() {
int n;
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1) {
printf("\nENTER YOUR CHOICE: ");
scanf("%d", &n);
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;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
Writing producer_consumer.c
!gcc producer_consumer.c
!./a.out
1.PRODUCER
2.CONSUMER
3.EXIT
%%writefile IPC_sender.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT); //creates shared memory segment with key 23
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf("Process attached at %p\n",shared_memory); //this prints the address where the segmen
printf("Enter some data to write to shared memory\n");
read(0,buff,100); //get some input from user
strcpy(shared_memory,buff); //data written to shared memory
printf("You wrote : %s\n",(char *)shared_memory);
}
Writing IPC_sender.c
!gcc IPC_sender.c
!./a.out
%%writefile IPC_Reciever.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is : %s\n",(char *)shared_memory);
}
Overwriting IPC_Reciever.c
!gcc IPC_Reciever.c
!./a.out
%%writefile bankers.c
#include <stdio.h>
int main() {
int p, c, count = 0, terminate = 0;
int alc[10][10], max[10][10], need[10][10], safe[10], available[10], done[10] = {0};
// Banker's Algorithm
while (count < p) {
int found = 0;
for (int i = 0; i < p; i++) {
if (!done[i]) {
int j;
for (j = 0; j < c; j++) {
if (need[i][j] > available[j]) {
break;
}
}
if (j == c) {
// Safe to execute process
safe[count++] = i;
for (int k = 0; k < c; k++) {
available[k] += alc[i][k];
}
done[i] = 1;
found = 1;
}
}
}
if (!found) {
printf("System is not in a safe state.\n");
return -1;
}
}
// Output results
printf("\nAvailable resources after completion:\n");
for (int i = 0; i < c; i++) {
printf("%d\t", available[i]);
}
printf("\n");
return 0;
}
Writing bankers.c
!gcc bankers.c
!./a.out
ALGORITHM:
Step 1: Input memory blocks with size and processes with size.
Step 2: Initialize all memory blocks as free.
Step 3: Start by picking each process and check if it can be assigned to current block.
Step 4: If size-of-process <= size-of-block if yes then assign and check for next process.
Step 5: If not then keep checking the further blocks.
%%writefile firstfit.c
#include<stdio.h>
void main() {
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
Writing firstfit.c
!gcc firstfit.c
!./a.out
ALGORITHM:
Step 1: Input memory blocks and processes with sizes.
Step 2: Initialize all memory blocks as free.
Step 3: Start by picking each process and find the minimum block size that can be assigned to
find min(bockSize[1], blockSize[2],. ... blockSize[n]) > processSize[current], if found then
Step 4: If not then leave that process and keep checking the further processes.
%%writefile BestFit.c
#include <stdio.h>
void main() {
int fragment[20], b[20], p[20], i, j, nb, np, temp, lowest = 9999;
static int barray[20], parray[20];
Overwriting BestFit.c
!gcc BestFit.c
!./a.out
Memory Management Scheme - Best Fit
Enter the number of blocks: 3
Enter the number of processes: 3