OS_assig_1_TIA19

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

//theory assignment 1

//group roll no:TIB19


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

void bubbleSort(int arr[], int n);


void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);
void mergeSort(int arr[], int l, int r);
void merge(int arr[], int l, int m, int r);

void printArray(int arr[], int size);

int main() {
int arr[] = {47, 32, 21, 17, 14};
int n = sizeof(arr)/sizeof(arr[0]);
int status;
pid_t pid;

// Create child processes for each sorting algorithm


for (int i = 1; i <= 3; i++) {
pid = fork();

if (pid == 0) { // Child process


int childArr[n];
for (int j = 0; j < n; j++) {
childArr[j] = arr[j]; // Copy original array
}

switch(i) {
case 1: // Bubble Sort
printf("Child %d performing Bubble Sort...\n", i);
bubbleSort(childArr, n);
printf("Sorted array by Bubble Sort: ");
printArray(childArr, n);
exit(0);
break;

case 2: // Quicksort
printf("Child %d performing Quicksort...\n", i);
quickSort(childArr, 0, n-1);
printf("Sorted array by Quicksort: ");
printArray(childArr, n);
exit(0);
break;

case 3: // Merge Sort


printf("Child %d performing Merge Sort...\n", i);
mergeSort(childArr, 0, n-1);
printf("Sorted array by Merge Sort: ");
printArray(childArr, n);
exit(0);
break;
}
}
}

// Parent process waits for all children to complete


while ((pid = wait(&status)) > 0);

printf("Parent process: All child processes completed.\n");


return 0;
}

// Bubble Sort
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

// Quicksort
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);

for (int j = low; j < high; j++) {


if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}

// Merge Sort
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

void merge(int arr[], int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];

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


L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

int i = 0, j = 0, k = l;

while (i < n1 && j < n2) {


if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

// Function to print an array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

Child 1 performing Bubble Sort...


Sorted array by Bubble Sort: 14 17 21 32 47
Child 2 performing Quicksort...
Sorted array by Quicksort: 14 17 21 32 47
Child 3 performing Merge Sort...
Sorted array by Merge Sort: 14 17 21 32 47
Parent process: All child processes completed.

You might also like