diff options
| Commit | 65bdc35e6f82ba0fa0259166005263a1fc9795c9 (patch) | |
|---|---|---|
| Author | 2023-10-25 11:02:02 +0300 | |
| Committer | 2023-10-25 11:02:02 +0300 | |
| Files | e6a47557ac218cff5803b48dd172867955248f75 | |
| uni-projects-65bdc35e6f82ba0fa0259166005263a1fc9795c9.tar uni-projects-65bdc35e6f82ba0fa0259166005263a1fc9795c9.tar.gz uni-projects-65bdc35e6f82ba0fa0259166005263a1fc9795c9.zip | ||
| Parent | 497f44a6c31b3976428e62db1f62c76be754f024 (diff) | |
[SP/P4] Solved all exercises
4 files changed, 64 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practicum4/ex1.c | 11 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum4/ex2.c | 18 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum4/ex3.c | 13 | ||||
| -rw-r--r-- | Year 3/System programming/Practicum4/ex4.c | 22 |
diff --git a/Year 3/System programming/Practicum4/ex1.c b/Year 3/System programming/Practicum4/ex1.c new file mode 100644 index 0000000..7958de4 --- /dev/null +++ b/Year 3/System programming/Practicum4/ex1.c @@ -0,0 +1,11 @@ +#include <sys/types.h> +#include <unistd.h> +#include <stdio.h> + +int main() { + pid_t pid = getpid(); + pid_t parentPID = getppid(); + + printf("Current: %d\nParent: %d\n", pid, parentPID); + execlp("ps", "ps", (char*)NULL); +} diff --git a/Year 3/System programming/Practicum4/ex2.c b/Year 3/System programming/Practicum4/ex2.c new file mode 100644 index 0000000..579b247 --- /dev/null +++ b/Year 3/System programming/Practicum4/ex2.c @@ -0,0 +1,18 @@ +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <stdio.h> + +int main() { + pid_t pid = getpid(); + pid_t fpid = fork(); + printf("own: %d\nfork: %d\n", pid, fpid); + if (fpid == 0) { + printf("parent: %d\n", getppid()); + } + else if (fpid > 0) { + int childStatus; + wait(&childStatus); + printf("child exit status: %d\n", childStatus); + } +} diff --git a/Year 3/System programming/Practicum4/ex3.c b/Year 3/System programming/Practicum4/ex3.c new file mode 100644 index 0000000..8df352b --- /dev/null +++ b/Year 3/System programming/Practicum4/ex3.c @@ -0,0 +1,13 @@ +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int status = -1; + int fpid = fork(); + if (fpid == 0) + execl(argv[1], argv[1], (char*)NULL); + wait(&status); + printf("Exit status: %d\n", status); +} diff --git a/Year 3/System programming/Practicum4/ex4.c b/Year 3/System programming/Practicum4/ex4.c new file mode 100644 index 0000000..3492784 --- /dev/null +++ b/Year 3/System programming/Practicum4/ex4.c @@ -0,0 +1,22 @@ +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f1 = fork(); + int f2 = fork(); + if (f1 == 0) { + execlp(argv[1], argv[1], (char*)NULL); + } + else if (f2 == 0) { + execlp(argv[2], argv[2], (char*)NULL); + } + else { + int firstStatus, secondStatus; + int firstEnded = wait(&firstStatus); + int secondEnded = wait(&secondStatus); + printf("%s %d\n", argv[1 + (firstEnded != f1)], firstStatus); + printf("%s %d\n", argv[1 + (secondEnded != f1)], secondStatus); + } +} |
