updates to articles

This commit is contained in:
hello 2025-02-07 01:25:19 +05:30
parent 4c3b20b2f4
commit c2bf60bebc
82 changed files with 30210 additions and 8 deletions

BIN
OS/C/Week5/hello Executable file

Binary file not shown.

BIN
OS/C/Week5/q1 Executable file

Binary file not shown.

View file

@ -22,11 +22,17 @@ void main () {
// input
printf("Enter strings to print: \n");
const size_t str_size = 100;
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]);
strings[i] = malloc(str_size);
string_cpy[i] = malloc(str_size);
if (!strings[i] || !string_cpy[i]) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
if (fgets(strings[i], str_size, stdin)) {
memcpy(string_cpy[i], strings[i], str_size);
}
}
// forking
@ -68,5 +74,3 @@ void main () {
}
}

76
OS/C/Week5/q2_opt.c Normal file
View file

@ -0,0 +1,76 @@
// 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");
const size_t str_size = 100;
for (i = 0; i < 5; i++) {
strings[i] = malloc(str_size);
string_cpy[i] = malloc(str_size);
if (!strings[i] || !string_cpy[i]) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
if (fgets(strings[i], str_size, stdin)) {
memcpy(string_cpy[i], strings[i], str_size);
}
}
// 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");
}
}

BIN
OS/C/Week5/q2opt Executable file

Binary file not shown.

27
OS/C/Week5/q2opt.c Normal file
View file

@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int compare_strings(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main(int argc, char *argv[]) {
pid_t pid = fork();
if (pid == 0) {
qsort(&argv[1], argc - 1, sizeof(char *), compare_strings);
printf("Child Sorted:\n");
for (int i = 1; i < argc; ++i)
printf("%s\n", argv[i]);
exit(EXIT_SUCCESS);
} else {
wait(NULL);
printf("\nParent Unsorted:\n");
for (int i = 1; i < argc; ++i)
printf("%s\n", argv[i]);
}
return 0;
}

BIN
OS/C/Week5/q3opt Executable file

Binary file not shown.

93
OS/C/Week5/q3opt.c Normal file
View file

@ -0,0 +1,93 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int compare_strings(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
void bubble_sort_strings(char **arr, int n) {
for (int i = 0; i < n - 1; ++i)
for (int 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;
}
}
int partition(char **arr, int low, int high) {
char *pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; ++j)
if (strcmp(arr[j], pivot) < 0) {
i++;
char *temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
char *temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;
return (i + 1);
}
void quick_sort_strings(char **arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quick_sort_strings(arr, low, pi - 1);
quick_sort_strings(arr, pi + 1, high);
}
}
void print_strings(char **strings, int n, const char *process, const char *sort) {
printf("\n%s (%s):\n", process, sort);
for (int i = 0; i < n; ++i) printf("%s\n", strings[i]);
}
char **read_strings(int n) {
char **strings = malloc(n * sizeof(char *));
for (int i = 0; i < n; ++i) {
size_t size = 100;
strings[i] = malloc(size);
printf("String %d: ", i + 1);
getline(&strings[i], &size, stdin);
strings[i][strcspn(strings[i], "\n")] = 0; // Remove newline
}
return strings;
}
int main() {
int n;
printf("N strings: ");
scanf("%d", &n);
while (getchar() != '\n');
char **input = read_strings(n);
pid_t pid_bubble = fork();
if (pid_bubble == 0) {
char **sorted_bubble = malloc(n * sizeof(char *));
for (int i = 0; i < n; ++i) sorted_bubble[i] = strdup(input[i]);
bubble_sort_strings(sorted_bubble, n);
print_strings(sorted_bubble, n, "Child BubbleSort", "Bubble Sort");
for (int i = 0; i < n; ++i) free(sorted_bubble[i]);
free(sorted_bubble);
exit(0);
}
pid_t pid_quick = fork();
if (pid_quick == 0) {
char **sorted_quick = malloc(n * sizeof(char *));
for (int i = 0; i < n; ++i) sorted_quick[i] = strdup(input[i]);
quick_sort_strings(sorted_quick, 0, n - 1);
print_strings(sorted_quick, n, "Child QuickSort", "Quick Sort");
for (int i = 0; i < n; ++i) free(sorted_quick[i]);
free(sorted_quick);
exit(0);
}
wait(NULL);
printf("\nParent (Unsorted):\n");
for (int i = 0; i < n; ++i) printf("%s\n", input[i]);
for (int i = 0; i < n; ++i) free(input[i]);
free(input);
return 0;
}

1
OS/bash/Week2/12h.c Normal file
View file

@ -0,0 +1 @@
30 find . -type f -name '*[0-9]*' -exec wc {} +xy

View file

@ -0,0 +1,10 @@
1001:Rahul:CSE:AI:A:78:82:90
1002:Priya:ICT:Information Technology:B:88:79:85
1003:Anil:ELE:ECE:A:75:80:70
1004:Sunita:CSE:CSE:C:90:92:88
1005:Vikram:ICT:CCE:D:65:70:75
1006:Anita:ELE:EEE:A:80:85:90
1007:Ravi:CSE:AI:B:70:68:74
1008:Meena:ICT:Information Technology:A:92:88:95
1009:Suresh:ELE:ECE:B:55:60:65
1010:Kavita:CSE:CSE:A:85:90:87

View file

@ -21,7 +21,7 @@ Q3.
Q4.
```bash
grep -rl "MIT" . | xargs sed -i 's/MIT/Manipal Institute of Technology/g'
grep "MIT" * | sed 's/MIT/Manipal Institute of Technology/g'
```
Q5.
@ -38,4 +38,4 @@ Q6.
```bash
pkill wc
```
```

View file

@ -0,0 +1,2 @@
IIT
MIT

View file

@ -0,0 +1,2 @@
MIT Bangalore
MIT Manipal

View file

@ -0,0 +1,11 @@
RegistrationNo:Name:Department:Branch:Section:Sub1:Sub2:Sub3
1001:Rahul:CSE:AI:A:78:82:90
1002:Priya:ICT:IT:B:88:79:85
1003:Anil:ELE:ECE:A:75:80:70
1004:Sunita:CSE:CSE:C:90:92:88
1005:Vikram:ICT:CCE:D:65:70:75
1006:Anita:ELE:EEE:A:80:85:90
1007:Ravi:CSE:AI:B:70:68:74
1008:Meena:ICT:IT:A:92:88:95
1009:Suresh:ELE:ECE:B:55:60:65
1010:Kavita:CSE:CSE:A:85:90:87

0
OS/bash/Week3/q4.sh Normal file → Executable file
View file