diff --git a/Assignment2/main.c b/Assignment2/main.c index 8b30957..5fb49d7 100644 --- a/Assignment2/main.c +++ b/Assignment2/main.c @@ -77,6 +77,12 @@ int main() { printf("The total is %d\n", total); } } + if (strcmp(input, "exec\n")) { + char *args[2]; + args[0] = "/bin/ls"; + args[1] = NULL; + execv(args[0], args); + } } } diff --git a/Lab2/.idea/.gitignore b/Lab2/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Lab2/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Lab2/.idea/Lab2.iml b/Lab2/.idea/Lab2.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/Lab2/.idea/Lab2.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Lab2/.idea/misc.xml b/Lab2/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/Lab2/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Lab2/.idea/modules.xml b/Lab2/.idea/modules.xml new file mode 100644 index 0000000..1ba9a40 --- /dev/null +++ b/Lab2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Lab2/.idea/vcs.xml b/Lab2/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Lab2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Lab2/CMakeLists.txt b/Lab2/CMakeLists.txt new file mode 100644 index 0000000..91ce7a7 --- /dev/null +++ b/Lab2/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.22) +project(Lab2 C) + +set(CMAKE_C_STANDARD 99) + +add_executable(Lab2 main.c) diff --git a/Lab2/documentation/Lab2-v1.pdf b/Lab2/documentation/Lab2-v1.pdf new file mode 100644 index 0000000..1cfeed2 Binary files /dev/null and b/Lab2/documentation/Lab2-v1.pdf differ diff --git a/Lab2/main.c b/Lab2/main.c new file mode 100644 index 0000000..be5092d --- /dev/null +++ b/Lab2/main.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +int main() { + printf("Parent PID: [%d]\n", getpid()); // Initial parent + if (fork() == 0) { // Forking child + printf("Child PID: [%d]\n", getpid()); + int to_lowercase[2]; + pipe(to_lowercase); + if (fork() == 0) { // Forking grandchild + printf("Grandchild PID: [%d]\n", getpid()); + char output[100]; + read(to_lowercase[0], output, 100); // Reading from pipe + close(to_lowercase[0]); // Closing read pipe + for (int i = 0; i < 100; i++) { // Converting to lowercase using ASCII + if (output[i] >= 'A' && output[i] <= 'Z') { + output[i] += 32; + } + } + printf("%s", output); + printf("Grandchild PID: [%d]\n", getpid()); + exit(EXIT_SUCCESS); + } else { // Child + char input[100]; + fgets(input, 100, stdin); // Input + write(to_lowercase[1], input, 100); // Writing to pipe + close(to_lowercase[1]); // Closing write pipe + wait(NULL); + printf("Grandchild PID: [%d]\n", getpid()); + exit(EXIT_SUCCESS); + } + } else { // Parent + wait(NULL); + printf("Parent PID: [%d]\n", getpid()); + } + exit(EXIT_SUCCESS); +} \ No newline at end of file