Professional Documents
Culture Documents
Os BCS303 Lab Manual DR - Ttit
Os BCS303 Lab Manual DR - Ttit
1. Develop a c program to implement the Process system calls (fork (), exec(),
wait(), create process, terminate process)
a. Single process
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t child_pid;
int status;
// Child process
printf("Child process (PID: %d) is running.\n", getpid());
// Replace the child process with a new program
execlp("/bin/ls", "ls", "-l", NULL);
// If exec fails, the code below will be executed
perror("Exec failed");
exit(1);
}
else
{
// Parent process
printf("Parent process (PID: %d) created a child (PID: %d).\n", getpid(), child_pid);
// Wait for the child process to finish
wait(&status);
if (WIFEXITED(status))
{
printf("Child process (PID: %d) exited with status %d.\n",
child_pid, WEXITSTATUS(status));
}
else
{
printf("Child process (PID: %d) did not exit normally.\n", child_pid);
}
}
1
OPERATING SYSTEMS - BCS303 III SEM B.E
return 0;
}
OUTPUT
Child process (PID: 48492) is running.
total 44
-rw-r--r-- 1 compiler compiler 2 Nov 9 06:55 Employee.txt
-rw-r--r-- 1 compiler compiler 16 Nov 9 08:20 INPUT.txt
-rw-r--r-- 1 compiler compiler 0 Nov 9 06:28 Students.dat
-rw-r--r-- 1 compiler compiler 11 Nov 9 07:52 archivo.txt
-rw-r--r-- 1 compiler compiler 84 Nov 9 06:55 car_data.txt
-rw-r--r-- 1 compiler compiler 35 Nov 9 07:12 example.txt
-rw-r--r-- 1 compiler compiler 35 Nov 9 07:11 o.out
-rw-r--r-- 1 compiler compiler 36 Nov 9 06:49 result.dat
-rw-r--r-- 1 compiler compiler 2709 Nov 9 08:53 sorting_comparison.csv
-rw-r--r-- 1 compiler compiler 96 Nov 9 06:36 student.dat
-rw-r--r-- 1 compiler compiler 21 Nov 9 06:30 swap.txt
-rw-r--r-- 1 compiler compiler 2003 Nov 9 09:09 zapis.txt
a. Multiple processes
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
int num_processes = 5; // Number of child processes to create
pid_t child_pid;
2
OPERATING SYSTEMS - BCS303 III SEM B.E
// If execl fails
perror("Exec failed");
exit(EXIT_FAILURE);
}
}
// Parent process waits for all child processes to complete
for (int i = 1; i <= num_processes; ++i) {
wait(NULL);
}
printf("Parent process exiting\n");
return 0;
}
OUTPUT
Parent process (PID: 46100)
Child process 1 (PID: 46101, Parent PID: 46100)
Child process 2 (PID: 46102, Parent PID: 46100)
Child process 3 (PID: 46103, Parent PID: 46100)
Child process 4 (PID: 46104, Parent PID: 46100)
Child process 5 (PID: 46105, Parent PID: 46100)
3
OPERATING SYSTEMS - BCS303 III SEM B.E
4
OPERATING SYSTEMS - BCS303 III SEM B.E
2. Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS b) SJF c) Round Robin d) Priority.
a. FCFS
#include <stdio.h>
struct Process {
int pid; // Process ID
int arrival; // Arrival time
int burst; // Burst time
};
int main()
{
struct Process processes[] = {
{1, 0, 6},
{2, 2, 3},
{3, 3, 8}
};
OUTPUT
Process Arrival Time Burst Time Waiting Time Turnaround Time
1 0 6 0 6
5
OPERATING SYSTEMS - BCS303 III SEM B.E
2 2 3 4 7
3 3 8 6 14
6
OPERATING SYSTEMS - BCS303 III SEM B.E
b. SJF
#include <stdio.h>
struct Process
{
int pid; //
Process ID
int burst; //
Burst time
};
void findWaitingTime(struct Process proc[], int n, int
wt[])
{
wt[0] = 0;
for (int i = 1; i < n; i++)
{
wt[i] = proc[i - 1].burst + wt[i - 1];
}
}
7
OPERATING SYSTEMS - BCS303 III SEM B.E
OUTPUT
Enter the number of processes: 5
Enter burst time for process 1: 5
Enter burst time for process 2: 12
Enter burst time for process 3: 8
Enter burst time for process 4: 4
Enter burst time for process 5: 3
Process Burst Time Waiting Time Turnaround Time
5 3 0 3
4 4 3 7
1 5 7 12
3 8 12 20
2 12 20 32
Average waiting time = 8.40
Average turnaround time = 14.80
8
OPERATING SYSTEMS - BCS303 III SEM B.E
c. Round Robin
#include <stdio.h>
struct Process
{
int pid; // Process ID
int burst; // Burst time
};
void roundRobinScheduling(struct Process processes[], int n, int timeQuantum)
{
int remaining[n];
int currentTime = 0;
for (int i = 0; i < n; i++)
{ remaining[i] = processes[i].burst;
}
while (1) {
int allDone = 1; // Flag to check if all processes are completed
for (int i = 0; i < n; i++)
{
if (remaining[i] > 0)
{
allDone = 0;
if (remaining[i] > timeQuantum)
{ currentTime += timeQuantum;
remaining[i] -= timeQuantum;
printf("Process %d executed for time quantum %d\n", processes[i].pid,
timeQuantum);
}
else
{
currentTime += remaining[i];
remaining[i] = 0;
printf("Process %d executed for remaining time %d\n", processes[i].pid,
remaining[i]);
}
}
}
if (allDone) break;
}
}
int main() {
struct Process processes[] = {
{1, 10},
{2, 5},
{3, 8},
{4, 12}
};
9
OPERATING SYSTEMS - BCS303 III SEM B.E
int n = sizeof(processes) /
sizeof(processes[0]);
int timeQuantum = 4;
roundRobinScheduling(processes, n, timeQuantum);
return 0;
}
OUTPUT
Process 1 executed for time quantum 4
Process 2 executed for time quantum 4
Process 3 executed for time quantum 4
Process 4 executed for time quantum 4
Process 1 executed for time quantum 4
Process 2 executed for remaining time 0
Process 3 executed for remaining time 0
Process 4 executed for time quantum 4
Process 1 executed for remaining time 0
Process 4 executed for remaining time 0
10
OPERATING SYSTEMS - BCS303 III SEM B.E
d. Priority Scheduling
#include <stdio.h>
#include <stdlib.h>
// Maximum number of processes
#define MAX_PROCESSES 10
// Process structure
typedef struct Process
{
int id; // Process ID
int priority; // Priority of the process (lower value indicates higher priority)
int burst_time; // Burst time (time required to complete the process)
} Process;
// Function to initialize processes
void initializeProcesses(Process processes[], int num_processes)
{ for (int i = 0; i < num_processes; i++) {
processes[i].id = i;
printf("Enter priority for Process %d: ", i);
scanf("%d", &processes[i].priority);
11
OPERATING SYSTEMS - BCS303 III SEM B.E
12
OPERATING SYSTEMS - BCS303 III SEM B.E
// Waiting time for the next process (waiting time = total time so
far) average_waiting_time += total_time;
}
average_waiting_time /= num_processes;
printf("\nAverage Waiting Time: %.2lf seconds\n", average_waiting_time);
}
int main() {
int num_processes;
printf("Enter the number of processes: ");
scanf("%d", &num_processes);
if (num_processes <= 0 || num_processes > MAX_PROCESSES)
{ printf("Invalid number of processes. Please enter a valid
number.\n"); return 1;
}
Process processes[MAX_PROCESSES];
initializeProcesses(processes,
num_processes);
priorityScheduling(processes, num_processes);
return 0;
}
13
OPERATING SYSTEMS - BCS303 III SEM B.E
14
OPERATING SYSTEMS - BCS303 III SEM B.E
int main()
{
pthread_t producer_thread, consumer_thread;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}
Compilation command
gcc –pthread producer-consumer.c –o producer-consumer
A Write First
// C program to implement one side of FIFO
// This side writes first, then reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
// FIFO file path
char * myfifo = "/tmp/myfifo";
// Creating the named file(FIFO)
// mkfifo(<pathname>, <permission>)
mkfifo(myfifo, 0666);
char arr1[80], arr2[80];
while (1)
{
// Open FIFO for write only
fd = open(myfifo, O_WRONLY);
15
OPERATING SYSTEMS - BCS303 III SEM B.E
16
OPERATING SYSTEMS - BCS303 III SEM B.E
b. Read First
// C program to implement one side of FIFO
// This side reads first, then reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd1;
// FIFO file path
char * myfifo = "/tmp/myfifo";
// Creating the named file(FIFO)
// mkfifo(<pathname>,<permission>)
mkfifo(myfifo, 0666);
char str1[80], str2[80];
while (1)
{
// First open in read only and read
fd1 = open(myfifo,O_RDONLY);
read(fd1, str1, 80);
// Print the read string and close
printf("User1: %s\n", str1);
close(fd1);
// Now open in write mode and write
// string taken from user.
fd1 = open(myfifo,O_WRONLY);
fgets(str2, 80, stdin);
write(fd1, str2, strlen(str2)+1);
close(fd1);
}
return 0;
}
17
OPERATING SYSTEMS - BCS303 III SEM B.E
18
OPERATING SYSTEMS - BCS303 III SEM B.E
return true;
}
// Function to simulate resource allocation
void allocateResources(int process, int
request[]) { for (int i = 0; i <
NUM_RESOURCES; i++) {
allocation[process][i] +=
request[i]; available[i] -=
request[i]; need[process][i] -=
request[i];
}
}
int main() {
calculateNeedMatrix
();
int requestProcess = 1;
int request[NUM_RESOURCES] = {1, 2, 2};
if (canGrantResources(requestProcess, request))
{ allocateResources(requestProcess, request);
printf("Resources granted to process %d.\n",
requestProcess); printf("Updated allocation matrix:\n");
for (int i = 0; i < NUM_PROCESSES; i+
+) { for (int j = 0; j <
NUM_RESOURCES; j++) {
printf("%d ", allocation[i][j]);
}
printf("\n");
}
} else {
printf("Resources cannot be granted to process %d.\n", requestProcess);
}
return 0;
}
OUTPUT
Resources granted to process 1.
Updated allocation matrix:
010
322
302
211
002
19
Dr. T. Thimmaiah Institute of Technology, KGF
}
}
if(flag==1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
}
a. Worst fit
#include <stdio.h>
// Define the maximum number of memory blocks
#define MAX_BLOCKS 100
// Structure to represent memory blocks
struct MemoryBlock {
int block_id;
int size;
int allocated;
};
// Function to perform worst-fit memory allocation
void worstFit(struct MemoryBlock blocks[], int m, int processSize) {
int worstIdx = -1;
for (int i = 0; i < m; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= processSize)
{ if (worstIdx == -1 || blocks[i].size > blocks[worstIdx].size)
{
worstIdx = i;
}
}
}
if (worstIdx != -1) {
// Allocate the memory block
blocks[worstIdx].allocated = 1;
printf("Process with size %d allocated to block %d\n", processSize, worstIdx + 1);
} else {
printf("Cannot allocate the process with size %d\n", processSize);
}
}
int main() {
int m; // Number of memory blocks
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
struct MemoryBlock blocks[MAX_BLOCKS];
for (int i = 0; i < m; i++)
{ blocks[i].block_id = i + 1;
printf("Enter the size of memory block %d: ", i + 1);
scanf("%d", &blocks[i].size);
blocks[i].allocated = 0; // Initialize as unallocated
}
int n; // Number of processes
printf("Enter the number of processes:
"); scanf("%d", &n);
for (int i = 0; i < n; i++) {
int processSize;
scanf("%d", &processSize);
worstFit(blocks, m, processSize);
}
return 0;
}
b. Best fit
#include <stdio.h>
// Define the maximum number of memory blocks
#define MAX_BLOCKS 100
// Structure to represent memory blocks
struct MemoryBlock {
int block_id;
int size;
int allocated;
};
// Function to perform best-fit memory allocation
void bestFit(struct MemoryBlock blocks[], int m, int processSize)
{ int bestIdx = -1;
for (int i = 0; i < m; i++) {
if (blocks[i].allocated == 0 && blocks[i].size >= processSize)
{ if (bestIdx == -1 || blocks[i].size < blocks[bestIdx].size) {
bestIdx = i;
}
}
}
if (bestIdx != -1) {
// Allocate the memory block
blocks[bestIdx].allocated = 1;
printf("Process with size %d allocated to block %d\n", processSize, bestIdx + 1);
} else {
printf("Cannot allocate the process with size %d\n", processSize);
}
}
int main() {
int m; // Number of memory blocks
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
struct MemoryBlock blocks[MAX_BLOCKS];
for (int i = 0; i < m; i++)
{ blocks[i].block_id = i + 1;
printf("Enter the size of memory block %d: ", i + 1);
scanf("%d", &blocks[i].size);
blocks[i].allocated = 0; // Initialize as unallocated
}
int n; // Number of processes
printf("Enter the number of processes:
"); scanf("%d", &n);
a. FIFO
#include <stdio.h>
int main()
{
int incomingStream[] = {4 , 1 , 2 , 4 , 5,4,1,2,3,6};
int pageFaults = 0;
int frames = 3;
int m, n, s,
pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf(" Incoming \t Frame 1 \t Frame 2 \t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("\n"); printf("%d\t\t\
t",incomingStream[m]); for(n = 0; n <
frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
b. LRU
#include<stdio.h>
#include<limits.h>
int checkHit(int incomingPage, int queue[], int occupied){
for(int i = 0; i < occupied; i++)
{ if(incomingPage == queue[i])
return 1;
}
return 0;
}
void printFrame(int queue[], int occupied)
{
for(int i = 0; i < occupied; i++) printf("%d\t\t\
t",queue[i]);
}
int main()
{
// int incomingStream[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1};
// int incomingStream[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3, 6, 1, 2, 4, 3};
int incomingStream[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3};
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printf("Page\t Frame1 \t Frame2 \t Frame3\n");
for(int i = 0;i < n; i++)
{
printf("%d: \t\t",incomingStream[i]);
// what if currently in frame 7
// next item that appears also 7
// didnt write condition for HIT
if(checkHit(incomingStream[i], queue, occupied)){
printFrame(queue, occupied);
}
// filling when frame(s) is/are empty
else if(occupied < frames){
queue[occupied] = incomingStream[i];
pagefault++;
occupied++;
printFrame(queue, occupied);
}
else{
int max = INT_MIN;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i]; queue1[i]=queue1[j]; queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i]; queue2[i]=queue2[j]; queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++) queue[i]=queue1[j];
//queue[i]=max;
//queue[i+1]=0; for(i=temp1+1,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head; for(j=0;j<=n-
1;j++)
{
diff=abs(queue[j+1]-queue[j]); seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg); return 0;
}