Add lab 2

This commit is contained in:
Isaac Shoebottom 2023-09-26 15:45:41 -03:00
parent d119dc459f
commit ca9bab7416
9 changed files with 79 additions and 0 deletions

View File

@ -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);
}
}
}

8
Lab2/.idea/.gitignore vendored Normal file
View File

@ -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

2
Lab2/.idea/Lab2.iml Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

4
Lab2/.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
Lab2/.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Lab2.iml" filepath="$PROJECT_DIR$/.idea/Lab2.iml" />
</modules>
</component>
</project>

6
Lab2/.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

6
Lab2/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.22)
project(Lab2 C)
set(CMAKE_C_STANDARD 99)
add_executable(Lab2 main.c)

Binary file not shown.

39
Lab2/main.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>
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);
}