From 8d07fc6fef67b7125e12f99f6e9dbd5c7c4fa669 Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Tue, 24 Sep 2024 13:44:04 -0600 Subject: [PATCH] Add lab 2 demos --- lab2/demos/Makefile_bad | 14 ++++++++++++++ lab2/demos/Makefile_good | 10 ++++++++++ lab2/demos/duplicate_fd.c | 17 +++++++++++++++++ lab2/demos/pipe.c | 17 +++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 lab2/demos/Makefile_bad create mode 100644 lab2/demos/Makefile_good create mode 100644 lab2/demos/duplicate_fd.c create mode 100644 lab2/demos/pipe.c diff --git a/lab2/demos/Makefile_bad b/lab2/demos/Makefile_bad new file mode 100644 index 0000000..167f908 --- /dev/null +++ b/lab2/demos/Makefile_bad @@ -0,0 +1,14 @@ +SHELL=/bin/bash +CC=gcc +CFLAGS=-std=c99 -Wall -Wextra -D_POSIX_C_SOURCE=201112L + +.PHONY: all clean + +all: server client + +server: server.c + +client: client.c + +clean: + $(RM) server client diff --git a/lab2/demos/Makefile_good b/lab2/demos/Makefile_good new file mode 100644 index 0000000..a2f0f6c --- /dev/null +++ b/lab2/demos/Makefile_good @@ -0,0 +1,10 @@ +all: server client + +server: server.c + gcc -Wall -Wextra -Werror -D_POSIX_C_SOURCE=201112L -o server server.c + +client: client.c + gcc -Wall -Wextra -Werror -D_POSIX_C_SOURCE=201112L -o client client.c + +clean: + rm -f server client diff --git a/lab2/demos/duplicate_fd.c b/lab2/demos/duplicate_fd.c new file mode 100644 index 0000000..a4ceae5 --- /dev/null +++ b/lab2/demos/duplicate_fd.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +int main(void) { + int fd = open("test.txt", O_CREAT | O_WRONLY); + + if(fd < 0) { + printf("Error opening the file\n"); + } + + dup2(fd, 1); + printf("Tester\n"); + + close(fd); +} diff --git a/lab2/demos/pipe.c b/lab2/demos/pipe.c new file mode 100644 index 0000000..df7edb2 --- /dev/null +++ b/lab2/demos/pipe.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +int main(void) { + char buf[100]; + char msg[] = "Mario"; + + int p[2]; + pipe(p); + + write(p[1], msg, sizeof(msg)); + read(p[0], buf, sizeof(msg)); + + printf("Out from the pipe: `%s`\n", buf); +}