Upload files to "OS/C/Week5"
This commit is contained in:
parent
5d67a041f0
commit
6f880a6525
72
OS/C/Week5/q2.c
Normal file
72
OS/C/Week5/q2.c
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
// stdlib import
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// bash utilities import
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
87
OS/C/Week5/q3.c
Normal file
87
OS/C/Week5/q3.c
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
// os exec libraries
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
// std C libraries
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// 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<n;i++){
|
||||||
|
printf("%s ",arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void selection(char *arr[], int n) {
|
||||||
|
int i,j;
|
||||||
|
for (i = 0; i < n - 1; i++) {
|
||||||
|
int min_idx = i;
|
||||||
|
for (j = i + 1; j < n; j++) {
|
||||||
|
if (strcmp(arr[j], arr[min_idx]) < 0) {
|
||||||
|
min_idx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char *temp = arr[min_idx];
|
||||||
|
arr[min_idx] = arr[i];
|
||||||
|
arr[i] = temp;
|
||||||
|
}
|
||||||
|
for(i=0;i<n;i++){
|
||||||
|
printf("%s ",arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int N,i;
|
||||||
|
|
||||||
|
printf("Enter the number of strings:\n ");
|
||||||
|
scanf("%d", &N);
|
||||||
|
|
||||||
|
char **strings = (char **)malloc(N * sizeof(char *));
|
||||||
|
printf("Enter %d strings:\n", N);
|
||||||
|
|
||||||
|
for (i = 0; i < N; i++) {
|
||||||
|
strings[i] = (char *)malloc(100 * sizeof(char));
|
||||||
|
printf("String %d: ", i + 1);
|
||||||
|
scanf("%s", strings[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
pid_t pid1;
|
||||||
|
pid_t pid2;
|
||||||
|
|
||||||
|
int status;
|
||||||
|
pid1=fork();
|
||||||
|
|
||||||
|
if (pid1 == 0) {
|
||||||
|
printf("\nFirst child: Bubble sort\n");
|
||||||
|
bubble(strings,N);
|
||||||
|
exit(0);
|
||||||
|
} else if (pid1 > 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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user