Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
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);
+ }
+}