OS Answers
OS Answers
#include<stdio.h>
// Function prototypes
void acceptAvailable(int available[], int n);
void displayAllocationMax(int allocation[][3], int max[][3], int n);
void displayNeed(int allocation[][3], int max[][3], int need[][3], int n);
void displayAvailable(int available[], int n);
int main()
{
int allocation[5][3] = {{2, 3, 2}, {4, 0, 0}, {5, 0, 4}, {4, 3, 3}, {2, 2, 4}};
int max[5][3] = {{9, 7, 5}, {5, 2, 2}, {1, 0, 4}, {4, 4, 4}, {6, 5, 5}};
int available[3];
int need[5][3];
int choice, n = 5;
switch (choice) {
case 1:
acceptAvailable(available, 3);
break;
case 2:
displayAllocationMax(allocation, max, n);
break;
case 3:
displayNeed(allocation, max, need, n);
break;
case 4:
displayAvailable(available, 3);
break;
case 5:
printf("Exiting...");
break;
default:
printf("Invalid choice! Please enter a number between 1 and 5.");
}
} while (choice != 5);
return 0;
}
Q.2 Write a simulation program for disk scheduling using FCFS algorithm. Accept
total number of disk blocks, disk request string, and current head position from the
user. Display the list of request in the order in which it is served. Also display the
total number of head moments.
55, 58, 39, 18, 90, 160, 150, 38, 184
Start Head Position: 50
[15]
#include <stdio.h>
#include <stdlib.h>
return totalHeadMovements;
}
int main() {
int n, headPosition;
int requestQueue[n];
printf("Enter the disk request string: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requestQueue[i]);
}
return 0;
}
Slip2
Q.1 Write a program to simulate Linked file allocation method. Assume disk with n
number of blocks. Give value of n as input. Randomly mark some block as allocated and
accordingly maintain the list of free blocks Write menu driver program with menu
options as mentioned below and implement each option.
• Show Bit Vector
• Create New File
• Show Directory
• Exit
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void initializeFreeList(int n) {
for (int i = n - 1; i >= 0; i--) {
if (disk[i] == 0) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->block = i;
newNode->next = freeList;
freeList = newNode;
}
}
}
void showBitVector(int n) {
printf("\nBit Vector (Disk Allocation):\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}
void createNewFile(int n) {
int size;
printf("\nEnter the size of the file: ");
scanf("%d", &size);
if (freeList == NULL) {
printf("\nNo free blocks available to create the file.\n");
return;
}
void showDirectory(int n) {
printf("\nDirectory Listing:\n");
for (int i = 0; i < n; i++) {
if (disk[i] == 1) {
printf("Block %d: Allocated\n", i);
} else {
printf("Block %d: Free\n", i);
}
}
}
int main() {
int n;
initializeFreeList(n);
int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Create New File\n");
printf("3. Show Directory\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
createNewFile(n);
break;
case 3:
showDirectory(n);
break;
case 4:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Q.2 Write an MPI program to calculate sum of randomly generated 1000 numbers
(stored in array) on a cluster
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Slip 3
#include <stdio.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 4
int available[MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
void calculateNeedMatrix() {
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
int main() {
// Initialize the allocation, max, and available arrays
int i, j;
int processes[MAX_PROCESSES] = {0, 1, 2, 3, 4};
int allocation[MAX_PROCESSES][MAX_RESOURCES] =
{
{0, 0, 1, 2},
{1, 0, 0, 0},
{1, 3, 5, 4},
{0, 6, 3, 2},
{0, 0, 1, 4}
};
int max[MAX_PROCESSES][MAX_RESOURCES] =
{
{1, 5, 2, 0},
{1, 7, 5, 0},
{2, 3, 5, 6},
{0, 6, 5, 2},
{0, 6, 5, 6}
};
calculateNeedMatrix();
printf("Need Matrix:\n");
for (i = 0; i < MAX_PROCESSES; i++) {
printf("P%d: ", i);
for (j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
if (isSafeState(processes, MAX_PROCESSES)) {
printf("System is in safe state.\n");
} else {
printf("System is not in safe state.\n");
}
return 0;
}
Q.2 Write an MPI program to calculate sum and average of randomly generated 1000
numbers (stored in array) on a cluster.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Slip 4
#include <stdio.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES] = {7, 2, 6};
// Function prototypes
void acceptAvailable();
void acceptAllocationMax();
void calculateNeed();
void displayAllocationMax();
void displayNeed();
void displayAvailable();
int main() {
int choice;
do {
printf("\n***** Menu *****\n");
printf("a) Accept Available\n");
printf("b) Display Allocation, Max\n");
printf("c) Find Need and display It\n");
printf("d) Display Available\n");
printf("e) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
acceptAvailable();
break;
case 'b':
acceptAllocationMax();
break;
case 'c':
calculateNeed();
displayNeed();
break;
case 'd':
displayAvailable();
break;
case 'e':
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 'e');
return 0;
}
printf("\nMax Matrix:\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
printf("P%d: ", i);
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", max[i][j]);
}
printf("\n");
}
}
Q.2 Write a simulation program for disk scheduling using SCAN algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total
number of head moments.
86, 147, 91, 170, 95, 130, 102, 70
Starting Head position= 125
Direction: Left
#include <stdio.h>
#include <stdlib.h>
// SCAN algorithm
if (direction == 'L') {
// Move left
for (i = index; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move right
for (i = index + 1; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
} else if (direction == 'R') {
// Move right
for (i = index; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move left
for (i = index - 1; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
}
int main() {
int diskSize, startHead, i;
char direction;
int diskQueue[diskSize];
return 0;
}
Slip 5
Q.1 Consider a system with ‘m’ processes and ‘n’ resource types. Accept number of
instances for every resource type. For each process accept the allocation and maximum
requirement matrices. Write a program to display the contents of need matrix and to
check if the given request of a process can be granted immediately or not.
#include <stdio.h>
#include <stdlib.h>
// Function to display a matrix
void display_matrix(int **matrix, int m, int n, char *title) {
printf("%s\n", title);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int m, n;
int process;
printf("Enter the process number making the request: ");
scanf("%d", &process);
process--; // Adjusting to 0-based indexing
return 0;
}
Q.2 Write an MPI program to find the max number from randomly generated 1000 numbers
(stored in array) on a cluster (Hint: Use MPI_Reduce)
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Slip 6
Q.1 Write a program to simulate Linked file allocation method. Assume disk with n
number of blocks. Give value of n as input. Randomly mark some block as allocated and
accordingly maintain the list of free blocks Write menu driver program with menu
options as mentioned below and implement each option.
• Show Bit Vector
• Create New File
• Show Directory
• Exit
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void initializeFreeList(int n) {
for (int i = n - 1; i >= 0; i--) {
if (disk[i] == 0) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->block = i;
newNode->next = freeList;
freeList = newNode;
}
}
}
void showBitVector(int n) {
printf("\nBit Vector (Disk Allocation):\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}
void createNewFile(int n) {
int size;
printf("\nEnter the size of the file: ");
scanf("%d", &size);
if (freeList == NULL) {
printf("\nNo free blocks available to create the file.\n");
return;
}
void showDirectory(int n) {
printf("\nDirectory Listing:\n");
for (int i = 0; i < n; i++) {
if (disk[i] == 1) {
printf("Block %d: Allocated\n", i);
} else {
printf("Block %d: Free\n", i);
}
}
}
int main() {
int n;
initializeFreeList(n);
int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Create New File\n");
printf("3. Show Directory\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
createNewFile(n);
break;
case 3:
showDirectory(n);
break;
case 4:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Q.2 Write a simulation program for disk scheduling using C-SCAN algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments..
80, 150, 60,135, 40, 35, 170
Starting Head Position: 70
Direction: Right
#include <stdio.h>
#include <stdlib.h>
// C-SCAN algorithm
if (direction == 'R') {
// Move right
for (i = index; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move left
for (i = 0; i < index; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
} else if (direction == 'L') {
// Move left
for (i = index; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move right
for (i = diskSize - 1; i > index; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
}
int diskQueue[diskSize];
return 0;
}
Slip7
#include<stdio.h>
int main() {
int processes = 5; // Number of processes
int resources = 4; // Number of resources
int work[4];
for (i = 0; i < resources; i++) {
work[i] = available[i];
}
// Safety Algorithm
int safeSeq[5];
int count = 0;
while (count < processes) {
int found = 0;
for (i = 0; i < processes; i++) {
if (finish[i] == 0) {
int canExecute = 1;
for (j = 0; j < resources; j++) {
if (need[i][j] > work[j]) {
canExecute = 0;
break;
}
}
if (canExecute) {
for (k = 0; k < resources; k++) {
work[k] += allocation[i][k];
}
safeSeq[count++] = i;
finish[i] = 1;
found = 1;
}
}
}
if (!found) {
printf("System is not in a safe state.\n");
return -1;
}
}
return 0;
}
Q.2 Write a simulation program for disk scheduling using SCAN algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments.
82, 170, 43, 140, 24, 16, 190
Starting Head Position: 50
Direction: Right
#include <stdio.h>
#include <stdlib.h>
// C-SCAN algorithm
if (direction == 'R') {
// Move right
for (i = index; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move left
for (i = 0; i < index; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
} else if (direction == 'L') {
// Move left
for (i = index; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move to the beginning
printf("0 ");
totalHeadMovements += startHead;
// Move right
for (i = diskSize - 1; i > index; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
}
int main() {
int diskSize, startHead, i;
char direction;
int diskQueue[diskSize];
return 0;
}
Slip8
Q.1 Write a program to simulate Contiguous file allocation method. Assume disk with n
number of blocks. Give value of n as input. Randomly mark some block as allocated and
accordingly maintain the list of free blocks Write menu driver program with menu
options as mentioned above and implement each option.
• Show Bit Vector
• Create New File
• Show Directory
• Exit
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void initializeFreeList(int n) {
for (int i = n - 1; i >= 0; i--) {
if (disk[i] == 0) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->block = i;
newNode->next = freeList;
freeList = newNode;
}
}
}
void showBitVector(int n) {
printf("\nBit Vector (Disk Allocation):\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}
void createNewFile(int n) {
int size;
printf("\nEnter the size of the file: ");
scanf("%d", &size);
if (freeList == NULL) {
printf("\nNo free blocks available to create the file.\n");
return;
}
void showDirectory(int n) {
printf("\nDirectory Listing:\n");
for (int i = 0; i < n; i++) {
if (disk[i] == 1) {
printf("Block %d: Allocated\n", i);
} else {
printf("Block %d: Free\n", i);
}
}
}
int main() {
int n;
initializeFreeList(n);
int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Create New File\n");
printf("3. Show Directory\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
createNewFile(n);
break;
case 3:
showDirectory(n);
break;
case 4:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Q.2 Write a simulation program for disk scheduling using SSTF algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments.
186, 89, 44, 70, 102, 22, 51, 124
Start Head Position: 70
#include <stdio.h>
#include <stdlib.h>
int main() {
int diskSize, startHead, i;
int diskQueue[diskSize];
return 0;
}
Slip 9
#include <stdio.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 4
int available[MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
void calculateNeedMatrix() {
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
int main() {
// Initialize the allocation, max, and available arrays
int i, j;
int processes[MAX_PROCESSES] = {0, 1, 2, 3, 4};
int allocation[MAX_PROCESSES][MAX_RESOURCES] =
{
{0, 0, 1, 2},
{1, 0, 0, 0},
{1, 3, 5, 4},
{0, 6, 3, 2},
{0, 0, 1, 4}
};
int max[MAX_PROCESSES][MAX_RESOURCES] =
{
{1, 5, 2, 0},
{1, 7, 5, 0},
{2, 3, 5, 6},
{0, 6, 5, 2},
{0, 6, 5, 6}
};
printf("Need Matrix:\n");
for (i = 0; i < MAX_PROCESSES; i++) {
printf("P%d: ", i);
for (j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
if (isSafeState(processes, MAX_PROCESSES)) {
printf("System is in safe state.\n");
} else {
printf("System is not in safe state.\n");
}
return 0;
}
Q.2 Write a simulation program for disk scheduling using LOOK algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments.
176, 79, 34, 60, 92, 11, 41, 114
Starting Head Position: 65
Direction: Left
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
return 0;
}
Slip 10
Q.1 Write an MPI program to calculate sum and average of randomly generated 1000
numbers (stored in array) on a cluster.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Q.2 Write a simulation program for disk scheduling using C-SCAN algorithm. Accept
total number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total
number of head moments.
33, 99, 142, 52, 197, 79, 46, 65
Start Head Position: 72
Direction: Left
#include <stdio.h>
#include <stdlib.h>
// SCAN algorithm
if (direction == 'L') {
// Move left
for (i = index; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move left
for (i = index - 1; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
}
int main() {
int diskSize, startHead, i;
char direction;
int diskQueue[diskSize];
Slip 11
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES];
int main() {
// Initialize allocation and max matrices based on the snapshot
int allocation_snapshot[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 3},
{2, 1, 1},
{0, 0, 2}
};
int max_snapshot[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 0, 0},
{2, 0, 2},
{0, 0, 0},
{1, 0, 0},
{0, 0, 2}
};
// Copy snapshot to allocation and max matrices
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
allocation[i][j] = allocation_snapshot[i][j];
max[i][j] = max_snapshot[i][j];
}
}
// Display menu
char choice;
do {
printf("\nBanker's Algorithm Menu:\n");
printf("a) Accept Available\n");
printf("b) Display Allocation, Max\n");
printf("c) Display the contents of need matrix\n");
printf("d) Display Available\n");
printf("e) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
accept_available();
break;
case 'b':
display_allocation_max();
break;
case 'c':
display_need();
break;
case 'd':
display_available();
break;
case 'e':
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 'e');
return 0;
}
Q.2 Write an MPI program to find the min number from randomly generated 1000 numbers
(stored in array) on a cluster (Hint: Use MPI_Reduce)
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Slip 12
Q.1 Write an MPI program to calculate sum and average of randomly generated 1000
numbers (stored in array) on a cluster.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Q.2 Write a simulation program for disk scheduling using C-LOOK algorithm. Accept
total number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total number
of head moments.
23, 89, 132, 42, 187, 69, 36, 55
Start Head Position: 40
Direction: Right
#include <stdio.h>
#include <stdlib.h>
// Function to sort an array
void sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
// Move head to the beginning and serve requests to the right again
total_head_movements += abs(head_position - requests[0]);
head_position = requests[0];
serving_order[serving_index++] = head_position;
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
return 0;
}
Slip13
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES];
int main() {
// Initialize allocation and max matrices based on the snapshot
int allocation_snapshot[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 3},
{2, 1, 1},
{0, 0, 2}
};
int max_snapshot[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 0, 0},
{2, 0, 2},
{0, 0, 0},
{1, 0, 0},
{0, 0, 2}
};
// Display menu
char choice;
do {
printf("\nBanker's Algorithm Menu:\n");
printf("a) Accept Available\n");
printf("b) Display Allocation, Max\n");
printf("c) Display the contents of need matrix\n");
printf("d) Display Available\n");
printf("e) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
accept_available();
break;
case 'b':
display_allocation_max();
break;
case 'c':
display_need();
break;
case 'd':
display_available();
break;
case 'e':
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 'e');
return 0;
}
Q.2 Write a simulation program for disk scheduling using SCAN algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments.
176, 79, 34, 60, 92, 11, 41, 114
Starting Head Position: 65
Direction: Left
#include <stdio.h>
#include <stdlib.h>
// SCAN algorithm
if (direction == 'L') {
// Move left
for (i = index; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move right
for (i = index + 1; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
} else if (direction == 'R') {
// Move right
for (i = index; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move left
for (i = index - 1; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
}
int main() {
int diskSize, startHead, i;
char direction;
int diskQueue[diskSize];
return 0;
}
Slip14
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void showBitVector(int n) {
printf("\nBit Vector (Disk Allocation):\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}
void showDirectory(int n) {
printf("\nDirectory Listing:\n");
int start = -1;
for (int i = 0; i < n; i++) {
if (disk[i] == 1) {
if (start == -1) {
start = i;
}
} else {
if (start != -1) {
printf("File starting from block %d to %d\n", start, i - 1);
start = -1;
}
}
}
if (start != -1) {
printf("File starting from block %d to %d\n", start, n - 1);
}
}
void deleteFile(int n) {
int start;
printf("\nEnter the starting block of the file to delete: ");
scanf("%d", &start);
int main() {
int n;
int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Show Directory\n");
printf("3. Delete File\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
showDirectory(n);
break;
case 3:
deleteFile(n);
break;
case 4:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Q.2 Write a simulation program for disk scheduling using SSTF algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments.
55, 58, 39, 18, 90, 160, 150, 38, 184
Start Head Position: 50
#include <stdio.h>
#include <stdlib.h>
int main() {
int diskSize, startHead, i;
int diskQueue[diskSize];
return 0;
}
Slip15
Q.1 Write a program to simulate Linked file allocation method. Assume disk with n
number of blocks. Give value of n as input. Randomly mark some block as allocated and
accordingly maintain the list of free blocks Write menu driver program with menu
options as mentioned below and implement each option.
• Show Bit Vector
• Create New File
• Show Directory
• Exit
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
char choice;
do {
printf("\nMenu:\n");
printf("a) Show Bit Vector\n");
printf("b) Create New File\n");
printf("c) Show Directory\n");
printf("d) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
show_bit_vector(head);
break;
case 'b': {
int file_size;
printf("Enter file size (number of blocks): ");
scanf("%d", &file_size);
create_new_file(&head, file_size);
break;
}
case 'c':
show_directory(head);
break;
case 'd':
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 'd');
return 0;
}
Q.2 Write a simulation program for disk scheduling using C-SCAN algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments..
80, 150, 60,135, 40, 35, 170
Starting Head Position: 70
Direction: Right
#include <stdio.h>
#include <stdlib.h>
// C-SCAN algorithm
if (direction == 'R') {
// Move right
for (i = index; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move left
for (i = 0; i < index; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
} else if (direction == 'L') {
// Move left
for (i = index; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move right
for (i = diskSize - 1; i > index; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
}
int main() {
int diskSize, startHead, i;
char direction;
int diskQueue[diskSize];
printf("Enter the disk request string:\n");
for (i = 0; i < diskSize; i++) {
scanf("%d", &diskQueue[i]);
}
return 0;
}
Slip 16
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void showBitVector(int n) {
printf("\nBit Vector (Disk Allocation):\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}
void showDirectory(int n) {
printf("\nDirectory Listing:\n");
int start = -1;
for (int i = 0; i < n; i++) {
if (disk[i] == 1) {
if (start == -1) {
start = i;
}
} else {
if (start != -1) {
printf("File starting from block %d to %d\n", start, i - 1);
start = -1;
}
}
}
if (start != -1) {
printf("File starting from block %d to %d\n", start, n - 1);
}
}
void deleteFile(int n) {
int start;
printf("\nEnter the starting block of the file to delete: ");
scanf("%d", &start);
int main() {
int n;
int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Show Directory\n");
printf("3. Delete File\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
showDirectory(n);
break;
case 3:
deleteFile(n);
break;
case 4:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Q.2 Write an MPI program to find the min number from randomly generated 1000 numbers
(stored in array) on a cluster (Hint: Use MPI_Reduce)
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Slip17
Q.1 Write a program to simulate Indexed file allocation method. Assume disk with n
number of blocks. Give value of n as input. Randomly mark some block as allocated and
accordingly maintain the list of free blocks Write menu driver program with menu
options as mentioned above and implement each option.
• Show Bit Vector
• Show Directory
• Delete Already File
• Exit
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void showBitVector(int n) {
printf("\nBit Vector (Disk Allocation):\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}
void showDirectory(int n) {
printf("\nDirectory Listing:\n");
for (int i = 0; i < n; i++) {
if (indexBlock[i] != -1) {
printf("File starting from block %d\n", i);
}
}
}
void deleteFile(int n) {
int start;
printf("\nEnter the starting block of the file to delete: ");
scanf("%d", &start);
indexBlock[start] = -1;
int main() {
int n;
int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Show Directory\n");
printf("3. Delete File\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
showDirectory(n);
break;
case 3:
deleteFile(n);
break;
case 4:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Q.2 Write a simulation program for disk scheduling using LOOK algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total number
of head moments.
23, 89, 132, 42, 187, 69, 36, 55
Start Head Position: 40
Direction: Left
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
return 0;
}
Slip18
Q.1 Write a program to simulate Indexed file allocation method. Assume disk with n
number of blocks. Give value of n as input. Randomly mark some block as allocated and
accordingly maintain the list of free blocks Write menu driver program with menu
options as mentioned above and implement each option.
• Show Bit Vector
• Create New File
• Show Directory
• Delete File
• Exit
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void showDirectory(int n) {
printf("\nDirectory Listing:\n");
for (int i = 0; i < n; i++) {
if (indexBlock[i] != -1) {
printf("File starting from block %d\n", i);
}
}
}
void deleteFile(int n) {
int start;
printf("\nEnter the starting block of the file to delete: ");
scanf("%d", &start);
indexBlock[start] = -1;
int main() {
int n;
int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Show Directory\n");
printf("3. Delete File\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
showDirectory(n);
break;
case 3:
deleteFile(n);
break;
case 4:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
Q.2 Write a simulation program for disk scheduling using SCAN algorithm. Accept
total number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total
number of head moments.
33, 99, 142, 52, 197, 79, 46, 65
Start Head Position: 72
Direction: Right
#include <stdio.h>
#include <stdlib.h>
// C-SCAN algorithm
if (direction == 'R') {
// Move right
for (i = index; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move left
for (i = 0; i < index; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
} else if (direction == 'L') {
// Move left
for (i = index; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move right
for (i = diskSize - 1; i > index; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
}
int main() {
int diskSize, startHead, i;
char direction;
int diskQueue[diskSize];
return 0;
}
Slip19
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 4
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES];
int main() {
// Initialize allocation and max matrices based on the snapshot
int allocation_snapshot[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 3, 2 ,4},
{1 ,2 ,0 ,1},
{0 ,0 ,0 ,0},
{3 ,3 ,2 ,2},
{1 ,4 ,3 ,2},
{2 ,4 ,1 ,4}
};
int max_snapshot[MAX_PROCESSES][MAX_RESOURCES] = {
{6 ,5 ,4 ,4},
{4 ,4 ,4 ,4},
{0 ,0 ,1 ,2},
{3 ,9 ,3 ,4},
{2 ,5 ,3 ,3},
{4 ,6 ,3 ,4}
};
// Display menu
char choice;
do {
printf("\nBanker's Algorithm Menu:\n");
printf("a) Accept Available\n");
printf("b) Display Allocation, Max\n");
printf("c) Display the contents of need matrix\n");
printf("d) Display Available\n");
printf("e) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
accept_available();
break;
case 'b':
display_allocation_max();
break;
case 'c':
display_need();
break;
case 'd':
display_available();
break;
case 'e':
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 'e');
return 0;
}
Q.2 Write a simulation program for disk scheduling using C-SCAN algorithm. Accept
total number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total number
of head moments.
23, 89, 132, 42, 187, 69, 36, 55
Start Head Position: 40
Direction: Left
#include <stdio.h>
#include <stdlib.h>
// SCAN algorithm
if (direction == 'L') {
// Move left
for (i = index; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move right
for (i = index + 1; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
} else if (direction == 'R') {
// Move right
for (i = index; i < diskSize; i++) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
// Move left
for (i = index - 1; i >= 0; i--) {
printf("%d ", diskQueue[i]);
totalHeadMovements += abs(startHead - diskQueue[i]);
startHead = diskQueue[i];
}
}
int main() {
int diskSize, startHead, i;
char direction;
int diskQueue[diskSize];
return 0;
}
Slip20
Q.1 Write a simulation program for disk scheduling using SCAN algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total number
of head moments.
33, 99, 142, 52, 197, 79, 46, 65
Start Head Position: 72
Direction: User defined
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
char direction;
printf("Enter direction (left/l or right/r): ");
scanf(" %c", &direction);
return 0;
}
Q.2 Write an MPI program to find the max number from randomly generated 1000 numbers
(stored in array) on a cluster (Hint: Use MPI_Reduce)
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Slip 21
Q.1 Write a simulation program for disk scheduling using FCFS algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total number
of head moments.
55, 58, 39, 18, 90, 160, 150, 38, 184
Start Head Position: 50
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
// Call FCFS algorithm function
fcfs_algorithm(requests, n, head_position);
return 0;
}
Q.2 Write an MPI program to calculate sum of all even randomly generated 1000
numbers (stored in array) on a cluster
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
return 0;
}
Slip22
Q.1 Write an MPI program to calculate sum of all even randomly generated 1000
numbers (stored in array) on a cluster
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
bool allocated[MAX_BLOCKS];
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
mark_allocated(n);
char choice;
do {
printf("\nMenu:\n");
printf("a) Show Bit Vector\n");
printf("b) Delete already created file\n");
printf("c) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
show_bit_vector(n);
break;
case 'b':
delete_file(n);
break;
case 'c':
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 'c');
return 0;
}
Slip23
Q.1 Consider a system with ‘m’ processes and ‘n’ resource types. Accept number of
instances for every resource type. For each process accept the allocation and maximum
requirement matrices. Write a program to display the contents of need matrix and to
check if the given request of a process can be granted immediately or not.
#include <stdio.h>
#include <stdlib.h>
// Function to display a matrix
void display_matrix(int **matrix, int m, int n, char *title) {
printf("%s\n", title);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int m, n;
int process;
printf("Enter the process number making the request: ");
scanf("%d", &process);
process--; // Adjusting to 0-based indexing
return 0;
}
Q.2 Write a simulation program for disk scheduling using SSTF algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total number
of head moments.
24, 90, 133, 43, 188, 70, 37, 55
Start Head Position: 58
#include <stdio.h>
#include <stdlib.h>
int main() {
int diskSize, startHead, i;
int diskQueue[diskSize];
return 0;
}
Slip24
Q.1 Write an MPI program to calculate sum of all odd randomly generated 1000 numbers
(stored in array) on a cluster.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int available[MAX_RESOURCES];
// Function to check if the current process can be executed
bool can_execute(int process_id) {
for (int i = 0; i < MAX_RESOURCES; i++) {
if (need[process_id][i] > available[i]) {
return false;
}
}
return true;
}
int main() {
// Initialize allocation and max matrices based on the snapshot
int allocation_snapshot[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 3},
{2, 1, 1},
{0, 0, 2}
};
int max_snapshot[MAX_PROCESSES][MAX_RESOURCES] = {
{0, 0, 0},
{2, 0, 2},
{0, 0, 0},
{1, 0, 0},
{0, 0, 2}
};
// Display menu
char choice;
do {
printf("\nBanker's Algorithm Menu:\n");
printf("a) Accept Available\n");
printf("b) Display Allocation, Max\n");
printf("c) Display the contents of need matrix\n");
printf("d) Display Available\n");
printf("e) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
accept_available();
break;
case 'b':
display_allocation_max();
break;
case 'c':
display_need();
break;
case 'd':
display_available();
break;
case 'e':
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 'e');
return 0;
}
Slip25
Q.1 Write a simulation program for disk scheduling using LOOK algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total number
of head moments.
86, 147, 91, 170, 95, 130, 102, 70
Starting Head position= 125
Direction: User Defined
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
char direction;
printf("Enter direction (left/l or right/r): ");
scanf(" %c", &direction);
return 0;
}
Q.2 Write a program to simulate Linked file allocation method. Assume disk with n
number of blocks. Give value of n as input. Randomly mark some block as allocated and
accordingly maintain the list of free blocks Write menu driver program with menu
options as mentioned below and implement each option.
• Show Bit Vector
• Create New File
• Show Directory
• Exit
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
bool allocated[MAX_BLOCKS];
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
mark_allocated(n);
char choice;
do {
printf("\nMenu:\n");
printf("a) Show Bit Vector\n");
printf("b) Create New File\n");
printf("c) Show Directory\n");
printf("d) Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
show_bit_vector(n);
break;
case 'b':
create_new_file(n);
break;
case 'c':
show_directory(n);
break;
case 'd':
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 'd');
return 0;
}
Slip26
#include <stdio.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 4
int available[MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
int isSafeState(int processes[], int n) {
int work[MAX_RESOURCES];
int i, j, finished = 0, safeSequence[MAX_PROCESSES], count = 0;
void calculateNeedMatrix() {
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
int main() {
// Initialize the allocation, max, and available arrays
int i, j;
int processes[MAX_PROCESSES] = {0, 1, 2, 3, 4};
int allocation[MAX_PROCESSES][MAX_RESOURCES] =
{
{0, 0, 1, 2},
{1, 0, 0, 0},
{1, 3, 5, 4},
{0, 6, 3, 2},
{0, 0, 1, 4}
};
int max[MAX_PROCESSES][MAX_RESOURCES] =
{
{1, 5, 2, 0},
{1, 7, 5, 0},
{2, 3, 5, 6},
{0, 6, 5, 2},
{0, 6, 5, 6}
};
calculateNeedMatrix();
printf("Need Matrix:\n");
for (i = 0; i < MAX_PROCESSES; i++) {
printf("P%d: ", i);
for (j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
if (isSafeState(processes, MAX_PROCESSES)) {
printf("System is in safe state.\n");
} else {
printf("System is not in safe state.\n");
}
return 0;
}
Q.2 Write a simulation program for disk scheduling using FCFS algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user.
Display the list of request in the order in which it is served. Also display the total number
of head moments.
56, 59, 40, 19, 91, 161, 151, 39, 185
Start Head Position: 48
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
return 0;
}
Slip27
Q.1 Write a simulation program for disk scheduling using LOOK algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments.
176, 79, 34, 60, 92, 11, 41, 114
Starting Head Position: 65
Direction: Right
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
char direction;
printf("Enter direction (left/l or right/r): ");
scanf(" %c", &direction);
// Call LOOK algorithm function
look_algorithm(requests, n, head_position, direction);
return 0;
}
Q.2 Write an MPI program to find the min number from randomly generated 1000 numbers
(stored in array) on a cluster (Hint: Use MPI_Reduce)
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Slip28
Q.1 Write a simulation program for disk scheduling using C-LOOK algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments.
56, 59, 40, 19, 91, 161, 151, 39, 185
Start Head Position: 48
Direction: User Defined
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
char direction;
printf("Enter direction (left/l or right/r): ");
scanf(" %c", &direction);
return 0;
}
Q.2 Write an MPI program to calculate sum of randomly generated 1000 numbers
(stored in array) on a cluster
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Slip29
Q.1 Write an MPI program to calculate sum of all even randomly generated 1000 numbers
(stored in array) on a cluster.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Q.2 Write a simulation program for disk scheduling using C-LOOK algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments..
80, 150, 60,135, 40, 35, 170
Starting Head Position: 70
Direction: Right
#include <stdio.h>
#include <stdlib.h>
// Move head to the beginning and serve requests to the right again
total_head_movements += abs(head_position - requests[0]);
head_position = requests[0];
serving_order[serving_index++] = head_position;
int main() {
int n;
printf("Enter total number of disk blocks: ");
scanf("%d", &n);
int requests[n];
printf("Enter disk request string (comma-separated numbers): ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
int head_position;
printf("Enter starting head position: ");
scanf("%d", &head_position);
return 0;
}
Slip30
Q.1 Write an MPI program to find the min number from randomly generated 1000 numbers
(stored in array) on a cluster (Hint: Use MPI_Reduce)
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
Q.2 Write a simulation program for disk scheduling using FCFS algorithm. Accept total
number of disk blocks, disk request string, and current head position from the user. Display
the list of request in the order in which it is served. Also display the total number of head
moments.
65, 95, 30, 91, 18, 116, 142, 44, 168
Start Head Position: 52
#include <stdio.h>
#include <stdlib.h>
// Function to calculate total head movements
int calculateHeadMovements(int requestQueue[], int n, int headPosition)
{
int totalHeadMovements = 0;
int currentHeadPosition = headPosition;
return totalHeadMovements;
}
int main() {
int n, headPosition;
int requestQueue[n];
printf("Enter the disk request string: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requestQueue[i]);
}
return 0;
}
The END