aboutsummaryrefslogtreecommitdiffstats
path: root/csci4061/100520_breakout
diff options
context:
space:
mode:
authorMatt Strapp <matt@mattstrapp.net>2022-05-24 11:18:46 -0500
committerMatt Strapp <matt@mattstrapp.net>2022-05-24 11:19:55 -0500
commit7a73162607544204032aa66cce755daf21edebda (patch)
tree58578e01f15f34a855d99c32898db9d7a1603e67 /csci4061/100520_breakout
parentdo some stuff (diff)
downloadhomework-7a73162607544204032aa66cce755daf21edebda.tar
homework-7a73162607544204032aa66cce755daf21edebda.tar.gz
homework-7a73162607544204032aa66cce755daf21edebda.tar.bz2
homework-7a73162607544204032aa66cce755daf21edebda.tar.lz
homework-7a73162607544204032aa66cce755daf21edebda.tar.xz
homework-7a73162607544204032aa66cce755daf21edebda.tar.zst
homework-7a73162607544204032aa66cce755daf21edebda.zip
Graduate
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'csci4061/100520_breakout')
-rw-r--r--csci4061/100520_breakout/exercise.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/csci4061/100520_breakout/exercise.c b/csci4061/100520_breakout/exercise.c
new file mode 100644
index 0000000..6d560d5
--- /dev/null
+++ b/csci4061/100520_breakout/exercise.c
@@ -0,0 +1,47 @@
+/*
+* Recitation Section Number:
+* Breakout Number:
+* Member Name
+* Member Name
+* Member Name
+* Member Name
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#define BUFSIZE 20
+#define PERM 0644
+int main(int argc, char *argv[]) {
+ int infile; char buffer[BUFSIZE];
+ // create/open a file named write.txt
+ infile = open("write.txt", O_CREAT | O_RDWR | O_TRUNC, PERM);
+ // write hello world to write.txtS
+ write(infile, "hello world\n", 12);
+ // spawn a child process and use this child process to reroute stdout to write.txt
+ pid_t pid = fork();
+ if (!pid) {
+ dup2(infile, 1);
+ }
+ // print pid
+ printf("Process %d\n", pid);
+
+ // use the child process spawned previously to read the contents from write.txt, restore output to stdout
+ // and then prints the contents of write.txt to stdout.
+ if (!pid) {
+ read(infile, buffer, 20);
+ int stdout = open("/dev/tty", O_WRONLY);
+ dup2(1, stdout);
+ printf("%s\n", buffer);
+ return 0;
+ }
+ // print pid
+ printf("%d\n", pid);
+ // close the file write.txt
+ close(infile);
+ return 0;
+} \ No newline at end of file