0% found this document useful (0 votes)
3 views38 pages

Final ASR

The document outlines a series of experiments related to Unix commands, CPU scheduling algorithms (FCFS, SJF, Priority), resource allocation graphs, Banker's algorithm, and memory allocation techniques (First Fit, Best Fit, Worst Fit). Each experiment includes code implementations in C, demonstrating various concepts in operating systems and resource management. The document serves as a comprehensive guide for understanding these fundamental topics in computer science.
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)
3 views38 pages

Final ASR

The document outlines a series of experiments related to Unix commands, CPU scheduling algorithms (FCFS, SJF, Priority), resource allocation graphs, Banker's algorithm, and memory allocation techniques (First Fit, Best Fit, Worst Fit). Each experiment includes code implementations in C, demonstrating various concepts in operating systems and resource management. The document serves as a comprehensive guide for understanding these fundamental topics in computer science.
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
You are on page 1/ 38

Experiment 1: Unix Commands (Script & Explanation)

1. mkdir images/

mkdir -p movies/2004/

2. ls

2. alias ls="ls --color=auto"

3. unalias ls

4. pwd

5. cd car
6. cp file_to_copy.txt new_file.txt

7. rm file_to_copy.txt

8. mv source_file destination_folder/

9. touch abc

10. chmod +x script

11. exit

12. man mkdir

13. shutdown now


shutdown -c

14. echo "you all are good"

15. cat long_text_file.txt

16. kill 533494

17. ping google.com


18. history

19. passwd

20. wc long.txt

21. Shell Variable (Var_name=string)


22. Shell Arithmetic

23 . Word count

24. If Command
25. Case Command

26. While Loop


27. For Loop
Experiment 2: CPU Scheduling - FCFS and SJF

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

struct Process {
int pid, arrival, burst, completion, turnaround, waiting;
};

void fcfs(struct Process p[], int n) {


int currentTime = 0;
for(int i = 0; i < n; i++) {
if(currentTime < p[i].arrival)
currentTime = p[i].arrival;
p[i].completion = currentTime + p[i].burst;
p[i].turnaround = p[i].completion - p[i].arrival;
p[i].waiting = p[i].turnaround - p[i].burst;
currentTime = p[i].completion;
}
}

void sjf(struct Process p[], int n) {


int completed = 0, currentTime = 0;
int isCompleted[n];
for(int i = 0; i < n; i++) isCompleted[i] = 0;
while(completed != n) {
int shortest = -1, minBurst = 999999;
for(int i = 0; i < n; i++) {
if(p[i].arrival <= currentTime && !isCompleted[i] && p[i].burst <
minBurst) {
minBurst = p[i].burst;
shortest = i;
}
}
if(shortest == -1) {
currentTime++;
} else {
p[shortest].completion = currentTime + p[shortest].burst;
p[shortest].turnaround = p[shortest].completion - p[shortest].arrival;
p[shortest].waiting = p[shortest].turnaround - p[shortest].burst;
currentTime = p[shortest].completion;
isCompleted[shortest] = 1;
completed++;
}
}
}

int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
struct Process p[n];
for(int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
p[i].pid = i+1;
scanf("%d %d", &p[i].arrival, &p[i].burst);
}

printf("\nFCFS Scheduling:\n");
fcfs(p, n);
printf("PID\tAT\tBT\tCT\tTAT\tWT\n");
for(int i = 0; i < n; i++)
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival, p[i].burst,
p[i].completion, p[i].turnaround, p[i].waiting);

printf("\nSJF Scheduling:\n");
sjf(p, n);
printf("PID\tAT\tBT\tCT\tTAT\tWT\n");
for(int i = 0; i < n; i++)
printf("%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival, p[i].burst,
p[i].completion, p[i].turnaround, p[i].waiting);

return 0;
}
Experiment 3: Priority and Multi-level Queue
Scheduling

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

struct Process {
int pid, arrival, burst, priority, completion, turnaround, waiting;
};

void priorityScheduling(struct Process p[], int n) {


int completed = 0, currentTime = 0;
int isCompleted[n];
for(int i = 0; i < n; i++) isCompleted[i] = 0;

while(completed != n) {
int highest = -1, maxPriority = -1;
for(int i = 0; i < n; i++) {
if(p[i].arrival <= currentTime && !isCompleted[i] && p[i].priority >
maxPriority) {
maxPriority = p[i].priority;
highest = i;
}
}
if(highest == -1) {
currentTime++;
} else {
p[highest].completion = currentTime + p[highest].burst;
p[highest].turnaround = p[highest].completion - p[highest].arrival;
p[highest].waiting = p[highest].turnaround - p[highest].burst;
currentTime = p[highest].completion;
isCompleted[highest] = 1;
completed++;
}
}
}

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

struct Process p[n];


for(int i = 0; i < n; i++) {
printf("Enter arrival time, burst time and priority for process %d: ", i+1);
p[i].pid = i+1;
scanf("%d %d %d", &p[i].arrival, &p[i].burst, &p[i].priority);
}

priorityScheduling(p, n);
printf("\nPriority Scheduling:\n");
printf("PID\tAT\tBT\tPriority\tCT\tTAT\tWT\n");
for(int i = 0; i < n; i++)
printf("%d\t%d\t%d\t%d\t\t%d\t%d\t%d\n", p[i].pid, p[i].arrival,
p[i].burst, p[i].priority, p[i].completion, p[i].turnaround, p[i].waiting);

return 0;
}
Experiment 4: Resource Allocation Graph (RAG)

#include <stdio.h>
#include <stdbool.h>
#define MAX_RESOURCES 10
#define MAX_PROCESSES 10

struct ResourceAllocationGraph {
int numProcesses;
int numResources;
int maxResources[MAX_RESOURCES];
int allocatedResources[MAX_RESOURCES];
int allocationMatrix[MAX_PROCESSES][MAX_RESOURCES];
bool finished[MAX_PROCESSES];
};

void initResourceAllocationGraph(struct ResourceAllocationGraph *graph, int


processes, int resources) {
graph->numProcesses = processes;
graph->numResources = resources;

printf("Enter maximum resources for each resource:\n");


for (int i = 0; i < resources; i++) {
printf("Resource %d: ", i);
scanf("%d", &graph->maxResources[i]);
graph->allocatedResources[i] = 0;
}

printf("Enter allocation matrix (%d x %d):\n", processes, resources);


for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) {
scanf("%d", &graph->allocationMatrix[i][j]);
graph->allocatedResources[j] += graph->allocationMatrix[i][j];
}
graph->finished[i] = false;
}
}

void displayRAG(struct ResourceAllocationGraph *graph) {


printf("\nResource Allocation Graph:\n");
printf("Processes: %d, Resources: %d\n", graph->numProcesses, graph-
>numResources);
printf("Allocation Matrix:\n");
for (int i = 0; i < graph->numProcesses; i++) {
printf("P%d: ", i);
for (int j = 0; j < graph->numResources; j++) {
printf("%d ", graph->allocationMatrix[i][j]);
}
printf("\n");
}
}

int main() {
struct ResourceAllocationGraph graph;
int processes, resources;

printf("Enter number of processes: ");


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

initResourceAllocationGraph(&graph, processes, resources);


displayRAG(&graph);

return 0;
}
Experiment 5: Banker's Algorithm

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

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

int alloc[MAX][MAX], max[MAX][MAX], avail[MAX];


int need[MAX][MAX], safeSeq[MAX];
bool finish[MAX] = {0};

printf("Enter allocation matrix:\n");


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

printf("Enter max matrix:\n");


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

printf("Enter available resources:\n");


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

// Calculate need matrix


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

int count = 0;
while (count < n) {
bool found = false;
for (i = 0; i < n; i++) {
if (!finish[i]) {
for (j = 0; j < m; j++)
if (need[i][j] > avail[j])
break;

if (j == m) {
for (k = 0; k < m; k++)
avail[k] += alloc[i][k];
safeSeq[count++] = i;
finish[i] = true;
found = true;
}
}
}
if (!found) {
printf("System is not in safe state\n");
return 0;
}
}

printf("System is in safe state\nSafe sequence: ");


for (i = 0; i < n; i++)
printf("P%d ", safeSeq[i]);
printf("\n");

return 0;
}
Experiment 6: RAG to WFG Conversion

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

void convertRAGtoWFG(int processes, int allocation[][MAX], int


request[][MAX]) {
printf("\nWait-for Graph (WFG):\n");
for (int i = 0; i < processes; i++) {
for (int j = 0; j < processes; j++) {
if (i != j) {
for (int k = 0; k < MAX; k++) {
if (request[i][k] > 0 && allocation[j][k] > 0) {
printf("P%d -> P%d (Resource R%d)\n", i, j, k);
break;
}
}
}
}
}
}

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

int allocation[MAX][MAX] = {0};


int request[MAX][MAX] = {0};

printf("Enter allocation matrix:\n");


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

printf("Enter request matrix:\n");


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

convertRAGtoWFG(n, allocation, request);

return 0;
}
Experiment 7: System Calls Implementation

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() {
pid_t pid;
int fd, status;

printf("Demonstrating various system calls:\n\n");

// Process management - fork()


printf("1. Process Management - fork()\n");
pid = fork();
if (pid == 0) {
printf("Child process: PID = %d, Parent PID = %d\n", getpid(), getppid());
_exit(0);
} else if (pid > 0) {
wait(&status);
printf("Parent process: PID = %d, Child PID = %d\n", getpid(), pid);
}
// File management - open, write, close
printf("\n2. File Management\n");
fd = open("test.txt", O_CREAT | O_WRONLY, 0644);
if (fd != -1) {
write(fd, "Hello System Calls!\n", 20);
close(fd);
printf("File created and written successfully\n");
}

// Directory operations
printf("\n3. Directory Operations\n");
if (mkdir("testdir", 0755) == 0) {
printf("Directory created successfully\n");
rmdir("testdir");
printf("Directory removed successfully\n");
}

return 0;
}
Experiment 8: Contiguous Allocation Techniques

#include <stdio.h>
#define MAX 25

void firstFit(int blockSize[], int m, int processSize[], int n) {


int allocation[n];
for (int i = 0; i < n; i++) allocation[i] = -1;

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


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

printf("\nFirst Fit Allocation:\n");


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

void bestFit(int blockSize[], int m, int processSize[], int n) {


int allocation[n];
for (int i = 0; i < n; i++) allocation[i] = -1;

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


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

printf("\nBest Fit Allocation:\n");


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

void worstFit(int blockSize[], int m, int processSize[], int n) {


int allocation[n];
for (int i = 0; i < n; i++) allocation[i] = -1;

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


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

printf("\nWorst Fit Allocation:\n");


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

int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);

int blockSizeCopy1[MAX], blockSizeCopy2[MAX], blockSizeCopy3[MAX];


for (int i = 0; i < m; i++) {
blockSizeCopy1[i] = blockSize[i];
blockSizeCopy2[i] = blockSize[i];
blockSizeCopy3[i] = blockSize[i];
}

firstFit(blockSizeCopy1, m, processSize, n);


bestFit(blockSizeCopy2, m, processSize, n);
worstFit(blockSizeCopy3, m, processSize, n);
return 0;
}
Experiment 9: Producer-Consumer Problem

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5

sem_t empty, full, mutex;


int buffer[BUFFER_SIZE];
int in = 0, out = 0;

void *producer(void *arg) {


int item;
for (int i = 0; i < 5; i++) {
item = rand() % 100;
sem_wait(&empty);
sem_wait(&mutex);

buffer[in] = item;
printf("Producer produced: %d at position %d\n", item, in);
in = (in + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&full);
sleep(1);
}
return NULL;
}

void *consumer(void *arg) {


int item;
for (int i = 0; i < 5; i++) {
sem_wait(&full);
sem_wait(&mutex);

item = buffer[out];
printf("Consumer consumed: %d from position %d\n", item, out);
out = (out + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&empty);
sleep(2);
}
return NULL;
}

int main() {
pthread_t prod, cons;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);

pthread_create(&prod, NULL, producer, NULL);


pthread_create(&cons, NULL, consumer, NULL);

pthread_join(prod, NULL);
pthread_join(cons, NULL);

sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);

return 0;
}
Experiment 10: Readers-Writers Problem

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

sem_t mutex, writeblock;


int data = 0, rcount = 0;

void *reader(void *arg) {


int f = *((int *)arg);

sem_wait(&mutex);
rcount++;
if (rcount == 1)
sem_wait(&writeblock);
sem_post(&mutex);

printf("Reader %d read data: %d\n", f, data);


sleep(1);

sem_wait(&mutex);
rcount--;
if (rcount == 0)
sem_post(&writeblock);
sem_post(&mutex);

return NULL;
}

void *writer(void *arg) {


int f = *((int *)arg);

sem_wait(&writeblock);
data++;
printf("Writer %d wrote data: %d\n", f, data);
sleep(1);
sem_post(&writeblock);

return NULL;
}

int main() {
pthread_t readers[3], writers[2];
int reader_ids[3] = {1, 2, 3};
int writer_ids[2] = {1, 2};

sem_init(&mutex, 0, 1);
sem_init(&writeblock, 0, 1);

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


pthread_create(&readers[i], NULL, reader, &reader_ids[i]);

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


pthread_create(&writers[i], NULL, writer, &writer_ids[i]);

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


pthread_join(readers[i], NULL);

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


pthread_join(writers[i], NULL);

sem_destroy(&mutex);
sem_destroy(&writeblock);

return 0;
}

You might also like