Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice04 / ex4.c
diff options
context:
space:
mode:
1 files changed, 22 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practice04/ex4.c22
diff --git a/Year 3/System programming/Practice04/ex4.c b/Year 3/System programming/Practice04/ex4.c
new file mode 100644
index 0000000..1014f5c
--- /dev/null
+++ b/Year 3/System programming/Practice04/ex4.c
@@ -0,0 +1,22 @@
+#include <unistd.h> // fork, execlp
+#include <sys/types.h> // pid_t
+#include <sys/wait.h> // wait
+#include <stdio.h> // printf
+
+int main(int argc, char* argv[]) {
+ pid_t fork1 = fork();
+ pid_t fork2 = fork();
+ if (fork1 == 0) { // in child 1
+ execlp(argv[1], argv[1], (char*)NULL);
+ }
+ else if (fork2 == 0) { // in child 2
+ execlp(argv[2], argv[2], (char*)NULL);
+ }
+ else {
+ int firstStatus, secondStatus;
+ pid_t firstPID = wait(&firstStatus);
+ pid_t secondPID = wait(&secondStatus);
+ printf("%s %d\n", argv[1 + (firstPID != fork1)], firstStatus);
+ printf("%s %d\n", argv[1 + (secondPID != fork1)], secondStatus);
+ }
+}