0% found this document useful (0 votes)
5 views22 pages

Atul OS Lab File

The document provides code examples for various UNIX system calls related to process management, file management, and input/output operations. It also includes implementations of CPU scheduling policies such as FCFS, SJF, priority scheduling, and multi-level queue scheduling. Additionally, it covers file storage allocation techniques including contiguous, linked-list, and indexed allocation, along with contiguous allocation techniques like worst-fit, best-fit, and first-fit.

Uploaded by

raunakd18904
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views22 pages

Atul OS Lab File

The document provides code examples for various UNIX system calls related to process management, file management, and input/output operations. It also includes implementations of CPU scheduling policies such as FCFS, SJF, priority scheduling, and multi-level queue scheduling. Additionally, it covers file storage allocation techniques including contiguous, linked-list, and indexed allocation, along with contiguous allocation techniques like worst-fit, best-fit, and first-fit.

Uploaded by

raunakd18904
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Page No :1

1. Execute various UNIX system calls for


i. Process management
ii. File management
iii. Input/output Systems calls

1. Process Management
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
pid_t pid = fork();

if (pid < 0) {
perror("Fork failed");
exit(1);
} else if (pid == 0) {

printf("Child Process:\n");
printf(" PID = %d\n", getpid());
printf(" PPID = %d\n", getppid());
exit(0);
} else {

wait(NULL);
printf("Parent Process:\n");
printf(" PID = %d\n", getpid());
}

return 0;
}

Output :-
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> gcc process_management.c -o
process_management
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./process_management
Child Process:
PID = 5208
PPID = 5207
Parent Process:
PID = 5207

2. File Management
Page No :2

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main() {
int fd;
char buffer[100];
char *text = "This is a file created using UNIX system calls.\n";

fd = open("sample_file.txt", O_CREAT | O_RDWR, 0644);


if (fd < 0) {
perror("File open error");
exit(1);
}

write(fd, text, strlen(text));


lseek(fd, 0, SEEK_SET); // Reset position

int n = read(fd, buffer, sizeof(buffer) - 1);


buffer[n] = '\0';

printf("Content read from file:\n%s", buffer);

close(fd);
return 0;
}

Output :-
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> gcc file_management.c -o
file_management
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./file_management
Content read from file:
This is a file created using UNIX system calls.

C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> type sample_file.txt


This is a file created using UNIX system calls.

3. Input/Output System Calls


#include <unistd.h>
Page No :3
int main() {
char name[100];

write(STDOUT_FILENO, "Enter your name: ", 18);


int len = read(STDIN_FILENO, name, sizeof(name));
write(STDOUT_FILENO, "Hello, ", 7);
write(STDOUT_FILENO, name, len);

return 0;
}

Outpu :-
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> gcc io_syscalls.c -o io_syscalls
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./io_syscalls
Enter your name: Shivansh
Hello, Shivansh

2. Implement CPU Scheduling Policies:


i. SJF
ii. Priority
iii. FCFS
iv. Multi-level Queue
Page No :4

1. FCFS (First Come First Serve)


#include <stdio.h>

int main() {
int n, i;
printf("Enter number of processes: ");
scanf("%d", &n);

int bt[n], wt[n], tat[n];


wt[0] = 0;

printf("Enter burst times:\n");


for(i = 0; i < n; i++) {
printf("P[%d]: ", i+1);
scanf("%d", &bt[i]);
}

for(i = 1; i < n; i++) {


wt[i] = wt[i-1] + bt[i-1];
}

for(i = 0; i < n; i++) {


tat[i] = wt[i] + bt[i];
}

printf("\nProcess\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P[%d]\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);
}

return 0;
}

Output :-
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./fcfs
Enter number of processes: 3
Enter burst times:
P[1]: 4
P[2]: 3
P[3]: 2

Process BT WT TAT
P[1] 4 0 4
P[2] 3 4 7
P[3] 2 7 9
Page No :5

2. SJF (Shortest Job First – Non-Preemptive)

#include <stdio.h>

int main() {
int n, i, j;
printf("Enter number of processes: ");
scanf("%d", &n);

int bt[n], wt[n], tat[n], p[n], temp;

for(i = 0; i < n; i++) {


printf("Enter burst time for P[%d]: ", i+1);
scanf("%d", &bt[i]);
p[i] = i+1;
}

for(i = 0; i < n-1; i++) {


for(j = i+1; j < n; j++) {
if(bt[i] > bt[j]) {
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}

wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = wt[i-1] + bt[i-1];
}

for(i = 0; i < n; i++) {


tat[i] = wt[i] + bt[i];
}

printf("\nProcess\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P[%d]\t%d\t%d\t%d\n", p[i], bt[i], wt[i], tat[i]);
}

return 0;
}

Output :-
Page No :6

C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./sjf


Enter number of processes: 3
Enter burst time for P[1]: 6
Enter burst time for P[2]: 2
Enter burst time for P[3]: 4

Process BT WT TAT
P[2] 2 0 2
P[3] 4 2 6
P[1] 6 6 12

3. Priority Scheduling (Non-Preemptive)

#include <stdio.h>

int main() {
int n, i, j;
printf("Enter number of processes: ");
scanf("%d", &n);

int bt[n], p[n], pri[n], wt[n], tat[n], temp;

for(i = 0; i < n; i++) {


printf("Enter burst time and priority for P[%d]: ", i+1);
scanf("%d %d", &bt[i], &pri[i]);
p[i] = i+1;
}

for(i = 0; i < n-1; i++) {


for(j = i+1; j < n; j++) {
if(pri[i] > pri[j]) {
temp = pri[i]; pri[i] = pri[j]; pri[j] = temp;
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}

wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = wt[i-1] + bt[i-1];
}

for(i = 0; i < n; i++) {


tat[i] = wt[i] + bt[i];
Page No :7
}

printf("\nProcess\tBT\tPri\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P[%d]\t%d\t%d\t%d\t%d\n", p[i], bt[i], pri[i], wt[i], tat[i]);
}

return 0;
}

Output :-
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./priority
Enter number of processes: 3
Enter burst time and priority for P[1]: 5 3
Enter burst time and priority for P[2]: 2 1
Enter burst time and priority for P[3]: 3 2

Process BT Pri WT TAT


P[2] 2 1 0 2
P[3] 3 2 2 5
P[1] 5 3 5 10

4. Multi-Level Queue Scheduling


#include <stdio.h>

int main() {
int n, i;
printf("Enter number of processes: ");
scanf("%d", &n);

int queue[n], bt[n];


printf("Enter queue number (0 for system, 1 for user) and burst time:\n");

for(i = 0; i < n; i++) {


printf("P[%d] Queue and BT: ", i+1);
scanf("%d %d", &queue[i], &bt[i]);
}

printf("\nSystem Queue:\n");
for(i = 0; i < n; i++) {
if(queue[i] == 0)
printf("P[%d] BT: %d\n", i+1, bt[i]);
}

printf("\nUser Queue:\n");
Page No :8
for(i = 0; i < n; i++) {
if(queue[i] == 1)
printf("P[%d] BT: %d\n", i+1, bt[i]);
}

return 0;
}

Output :-
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./mlq
Enter number of processes: 4
Enter queue number (0 for system, 1 for user) and burst time:
P[1] Queue and BT: 0 3
P[2] Queue and BT: 1 5
P[3] Queue and BT: 0 2
P[4] Queue and BT: 1 4

System Queue:
P[1] BT: 3
P[3] BT: 2

User Queue:
P[2] BT: 5
P[4] BT: 4

3. Implement file storage allocation technique:


i. Contiguous(using array)
ii. Linked –list(using linked-list)
iii. Indirect allocation (indexing)

1. Contiguous Allocation (using array)


#include <stdio.h>

int main() {
int file[50], start, length, i, j, flag = 0;

for(i = 0; i < 50; i++) file[i] = 0;


Page No :9

printf("Enter starting block and length of file: ");


scanf("%d%d", &start, &length);

for(i = start; i < start + length; i++) {


if(file[i] == 0)
flag++;
}

if(flag == length) {
for(i = start; i < start + length; i++)
file[i] = 1;

printf("File allocated from block %d to %d\n", start, start + length - 1);


} else {
printf("File cannot be allocated — space is not contiguous.\n");
}

return 0;
}

Output :-
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./contiguous_allocation
Enter starting block and length of file: 5 4
File allocated from block 5 to 8

2. Linked Allocation (using linked list)

#include <stdio.h>
#include <stdlib.h>

struct Block {
int blockNumber;
struct Block* next;
};

int main() {
int n, i;
struct Block *head = NULL, *temp, *newBlock;

printf("Enter number of blocks to allocate: ");


scanf("%d", &n);
Page No :10
for(i = 0; i < n; i++) {
newBlock = (struct Block*)malloc(sizeof(struct Block));
printf("Enter block number for block %d: ", i+1);
scanf("%d", &newBlock->blockNumber);
newBlock->next = NULL;

if(head == NULL) {
head = newBlock;
temp = head;
} else {
temp->next = newBlock;
temp = temp->next;
}
}

printf("\nLinked list allocation:\n");


temp = head;
while(temp != NULL) {
printf("Block -> %d\n", temp->blockNumber);
temp = temp->next;
}

return 0;
}

Output :-
C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./linked_allocation
Enter number of blocks to allocate: 3
Enter block number for block 1: 10
Enter block number for block 2: 14
Enter block number for block 3: 18

Linked list allocation:


Block -> 10
Block -> 14
Block -> 18

3. Indexed Allocation (Indirect Allocation)


#include <stdio.h>
int main() {
int indexBlock, files, i, block[50];

printf("Enter index block: ");


scanf("%d", &indexBlock);

printf("Enter number of blocks for file: ");


scanf("%d", &files);
Page No :11
printf("Enter blocks:\n");
for(i = 0; i < files; i++) {
printf("Block %d: ", i+1);
scanf("%d", &block[i]);
}
printf("\nFile allocated using indexed allocation.\n");
printf("Index Block: %d -> ", indexBlock);
for(i = 0; i < files; i++) {
printf("%d ", block[i]);
}
printf("\n");

return 0;
}

Output :-

C:\Users\shivansh\OneDrive\Desktop\All programs\os lab> ./indexed_allocation


Enter index block: 7
Enter number of blocks for file: 4
Enter blocks:
Block 1: 20
Block 2: 25
Block 3: 26
Block 4: 30

File allocated using indexed allocation.


Index Block: 7 -> 20 25 26 30

4. Implementation of contiguous allocation techniques:


i. Worst-Fit
ii. Best- Fit
iii. First- Fit

1. Worst Fit Allocation


#include <stdio.h>

int main() {
int blockSize[10], processSize[10], blockCount, processCount, allocation[10];

printf("Enter number of blocks: ");


scanf("%d", &blockCount);

printf("Enter size of each block:\n");


for(int i = 0; i < blockCount; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}
Page No :12

printf("Enter number of processes: ");


scanf("%d", &processCount);

printf("Enter size of each process:\n");


for(int i = 0; i < processCount; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
allocation[i] = -1;
}

for(int i = 0; i < processCount; i++) {


int worstIdx = -1;
for(int j = 0; j < blockCount; j++) {
if(blockSize[j] >= processSize[i]) {
if(worstIdx == -1 || blockSize[j] > blockSize[worstIdx])
worstIdx = j;
}
}

if(worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock No.\n");


for(int i = 0; i < processCount; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if(allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}

return 0;
}

Output :-
Enter number of blocks: 5
Block 1: 100
Block 2: 500
Block 3: 200
Block 4: 300
Block 5: 600

Enter number of processes: 4


Process 1: 212
Process 2: 417
Process 3: 112
Process 4: 426
Page No :13

Process No. Process Size Block No.


1 212 5
2 417 2
3 112 4
4 426 Not Allocated

2. Best-Fit Allocation
#include <stdio.h>

int main() {
int blockSize[10], processSize[10], blockCount, processCount, allocation[10];

printf("Enter number of blocks: ");


scanf("%d", &blockCount);

printf("Enter size of each block:\n");


for(int i = 0; i < blockCount; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}

printf("Enter number of processes: ");


scanf("%d", &processCount);

printf("Enter size of each process:\n");


for(int i = 0; i < processCount; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
allocation[i] = -1;
}

for(int i = 0; i < processCount; i++) {


int bestIdx = -1;
for(int j = 0; j < blockCount; j++) {
if(blockSize[j] >= processSize[i]) {
if(bestIdx == -1 || blockSize[j] < blockSize[bestIdx])
bestIdx = j;
}
}

if(bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock No.\n");


for(int i = 0; i < processCount; i++) {
Page No :14
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if(allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}

return 0;
}

Output :-
Enter number of blocks: 5
Block 1: 100
Block 2: 500
Block 3: 200
Block 4: 300
Block 5: 600

Enter number of processes: 4


Process 1: 212
Process 2: 417
Process 3: 112
Process 4: 426

Process No. Process Size Block No.


1 212 4
2 417 5
3 112 3
4 426 2

3. First-Fit Allocation
#include <stdio.h>

int main() {
int blockSize[10], processSize[10], blockCount, processCount, allocation[10];

printf("Enter number of blocks: ");


scanf("%d", &blockCount);

printf("Enter size of each block:\n");


for(int i = 0; i < blockCount; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}

printf("Enter number of processes: ");


scanf("%d", &processCount);

printf("Enter size of each process:\n");


Page No :15
for(int i = 0; i < processCount; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
allocation[i] = -1;
}

for(int i = 0; i < processCount; i++) {


for(int j = 0; j < blockCount; j++) {
if(blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}

printf("\nProcess No.\tProcess Size\tBlock No.\n");


for(int i = 0; i < processCount; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if(allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}

return 0;
}

Output :-
Enter number of blocks: 5
Block 1: 100
Block 2: 500
Block 3: 200
Block 4: 300
Block 5: 600

Enter number of processes: 4


Process 1: 212
Process 2: 417
Process 3: 112
Process 4: 426

Process No. Process Size Block No.


1 212 2
2 417 5
3 112 3
4 426 Not Allocated
Page No :16

5. Calculation of external and internal fragmentation


i. Free space list of blocks from system
ii. List process file from the system

#include <stdio.h>

#define MAX 50

int main() {
int blocks[MAX], processes[MAX];
int b, p, i, j;
int totalInternal = 0, totalExternal = 0;

printf("Enter number of memory blocks: ");


scanf("%d", &b);
printf("Enter sizes of %d memory blocks:\n", b);
for(i = 0; i < b; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blocks[i]);
}

printf("\nEnter number of processes/files: ");


scanf("%d", &p);
printf("Enter sizes of %d processes:\n", p);
for(i = 0; i < p; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processes[i]);
}

int allocation[MAX];
for(i = 0; i < p; i++) allocation[i] = -1;
Page No :17

for(i = 0; i < p; i++) {


for(j = 0; j < b; j++) {
if(blocks[j] >= processes[i]) {
allocation[i] = j;
totalInternal += blocks[j] - processes[i];
blocks[j] = -1; // mark as used
break;
}
}
}

printf("\nProcess No\tProcess Size\tBlock Allocated\n");


for(i = 0; i < p; i++) {
printf("%d\t\t%d\t\t", i + 1, processes[i]);
if(allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}

for(i = 0; i < b; i++) {


if(blocks[i] != -1)
totalExternal += blocks[i];
}

printf("\nTotal Internal Fragmentation: %d", totalInternal);


printf("\nTotal External Fragmentation: %d\n", totalExternal);

return 0;
}

Output :-

Enter number of memory blocks: 5


Block 1: 100
Block 2: 500
Block 3: 200
Block 4: 300
Block 5: 600

Enter number of processes/files: 4


Process 1: 212
Process 2: 417
Process 3: 112
Process 4: 426

Process No Process Size Block Allocated


1 212 2
Page No :18
2 417 5
3 112 3
4 426 Not Allocated

Total Internal Fragmentation: 359


Total External Fragmentation: 400

6. Implementation of resource allocation graph RAG)


#include <stdio.h>
#define MAX 10

int allocation[MAX][MAX];
int request[MAX][MAX];
int rag[MAX + MAX][MAX + MAX];
int main() {
int p, r;
printf("Enter number of processes: ");
scanf("%d", &p);
printf("Enter number of resources: ");
scanf("%d", &r);

printf("Enter allocation matrix (P x R):\n");


for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
scanf("%d", &allocation[i][j]);
}
}
printf("Enter request matrix (P x R):\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < r; j++) {
scanf("%d", &request[i][j]);
}
}

for (int i = 0; i < p + r; i++) {


for (int j = 0; j < p + r; j++) {
rag[i][j] = 0;
}
}

for (int i = 0; i < p; i++) {


for (int j = 0; j < r; j++) {
Page No :19
if (allocation[i][j] == 1) {
rag[r + j][i] = 1; // Rj → Pi
}
}
}

for (int i = 0; i < p; i++) {


for (int j = 0; j < r; j++) {
if (request[i][j] == 1) {
rag[i][r + j] = 1; // Pi → Rj
}
}
}

printf("\nResource Allocation Graph (Adjacency Matrix):\n");


printf(" ");
for (int i = 0; i < p + r; i++) {
if (i < p)
printf("P%d ", i);
else
printf("R%d ", i - p);
}
printf("\n");

for (int i = 0; i < p + r; i++) {


if (i < p)
printf("P%d ", i);
else
printf("R%d ", i - p);

for (int j = 0; j < p + r; j++) {


printf(" %d ", rag[i][j]);
}
printf("\n");
}

return 0;
}

Output :-
Enter number of processes: 2
Enter number of resources: 2

Enter allocation matrix (P x R):


10
01
Enter request matrix (P x R):
01
10
Page No :20

Resource Allocation Graph (Adjacency Matrix):


P0 P1 R0 R1
P0 0 0 0 1
P1 0 0 1 0
R0 1 0 0 0
R1 0 1 0 0

7. Implementation of Banker‟s algorithm

#include <stdio.h>
#include <stdbool.h>
#define MAX 10

int main() {
int n, m;
int alloc[MAX][MAX], max[MAX][MAX], avail[MAX];
int need[MAX][MAX], finish[MAX], safeSeq[MAX];

printf("Enter number of processes: ");


scanf("%d", &n);
printf("Enter number of resources: ");
scanf("%d", &m);

printf("Enter Allocation Matrix (%d x %d):\n", n, m);


for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &alloc[i][j]);

printf("Enter Maximum Matrix (%d x %d):\n", n, m);


for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &max[i][j]);

printf("Enter Available Resources:\n");


for (int i = 0; i < m; i++)
scanf("%d", &avail[i]);

for (int i = 0; i < n; i++)


for (int j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];

for (int i = 0; i < n; i++)


finish[i] = 0;
int count = 0;
bool found;

while (count < n) {


Page No :21
found = false;
for (int i = 0; i < n; i++) {
if (!finish[i]) {
bool possible = true;
for (int j = 0; j < m; j++) {
if (need[i][j] > avail[j]) {
possible = false;
break;
}
}
if (possible) {
for (int j = 0; j < m; j++)
avail[j] += alloc[i][j];

safeSeq[count++] = i;
finish[i] = 1;
found = true;
}
}
}
if (!found) {
printf("\nSystem is in UNSAFE state! No safe sequence exists.\n");
return 0;
}
}

printf("\nSystem is in SAFE state.\nSafe sequence is: ");


for (int i = 0; i < n; i++) {
printf("P%d", safeSeq[i]);
if (i != n - 1) printf(" -> ");
}
printf("\n");

return 0;
}

Output :-
Enter number of processes: 5
Enter number of resources: 3
Enter Allocation Matrix (5 x 3):
010
200
302
211
002
Enter Maximum Matrix (5 x 3):
753
322
902
222
433
Page No :22
Enter Available Resources:
332

System is in SAFE state.


Safe sequence is: P1 -> P3 -> P4 -> P0 -> P2

You might also like