From 6f880a6525263c05b37f7cce624f77ce93af7fec Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Fri, 31 Jan 2025 11:18:35 +0530 Subject: [PATCH] Upload files to "OS/C/Week5" --- OS/C/Week5/q2.c | 72 ++++++++++++++++++++++++++++++++++++++++ OS/C/Week5/q3.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 OS/C/Week5/q2.c create mode 100644 OS/C/Week5/q3.c diff --git a/OS/C/Week5/q2.c b/OS/C/Week5/q2.c new file mode 100644 index 0000000..fdb30d3 --- /dev/null +++ b/OS/C/Week5/q2.c @@ -0,0 +1,72 @@ +// stdlib import +#include +#include +#include + +// bash utilities import +#include +#include +#include + +void main () { + + // array declaration + char *strings[5]; + char *string_cpy[5]; + char temp[100]; + + // variables reqd + int i,j; + pid_t pid; + int status; + + // input + printf("Enter strings to print: \n"); + for (i = 0; i < 5; i++) { + strings[i] = malloc (100 * sizeof(char)); + string_cpy[i] = malloc (100 * sizeof(char)); + fgets(strings[i],100,stdin); + strcpy(string_cpy[i], strings[i]); + } + + // forking + pid = fork(); + + // child process + if (pid == 0) { + printf("\n Child Process: \n"); + for (i = 0; i < 5; i++) { + for (j = 0; j < 5 - i - 1; j++) { + if (strcmp(strings[j], strings[j + 1]) > 0){ + strcpy(temp, strings[j]); + strcpy(strings[j], strings[j+1]); + strcpy(strings[j+1], temp); + } + } + } + + + for (i = 0; i < 5; i++) { + printf("\n Child: %s \n", strings[i]); + } + + printf("\n"); + exit (00); + + // parent process + } else if (pid > 0) { + wait(&status); + printf("\n Parent Process: \n"); + for (i = 0; i < 5; i++) { + printf("\n Parent: %s \n", string_cpy[i]); + printf("%d",WEXITSTATUS(status)); + } + + // exception handling (catchall statement) + } else { + printf("THE SYSTEM ENCOUNTERED AN ERROR. Please try again later or debug the code. \n"); + } + +} + + diff --git a/OS/C/Week5/q3.c b/OS/C/Week5/q3.c new file mode 100644 index 0000000..ba324d7 --- /dev/null +++ b/OS/C/Week5/q3.c @@ -0,0 +1,87 @@ +// os exec libraries +#include +#include +#include + +// std C libraries +#include +#include +#include + +// bubble sort +void bubble(char *arr[], int n) { + int i,j; + for (i = 0; i < n - 1; i++) { + for (j = 0; j < n - i - 1; j++) { + if (strcmp(arr[j], arr[j + 1]) > 0) { + char *temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + for(i=0;i 0) { + pid2 = fork(); + if (pid2 == 0) { + printf("\nSecond child: Selection sort\n"); + selection(strings,N); + exit(0); + } else if (pid2 > 0) { + wait(&status); + printf("\n Parent: Child process terminated\n"); + } else { + printf("The system encountered an error."); + } + } else { + printf("Error"); + } +}