Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum4 / ex4.c
diff options
context:
space:
mode:
1 files changed, 22 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practicum4/ex4.c22
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);
+ }
+}