Add lab 2 demos

This commit is contained in:
Akemi Izuko 2024-09-24 13:44:04 -06:00
parent e359896da0
commit 8d07fc6fef
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC
4 changed files with 58 additions and 0 deletions

14
lab2/demos/Makefile_bad Normal file
View file

@ -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

10
lab2/demos/Makefile_good Normal file
View file

@ -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

17
lab2/demos/duplicate_fd.c Normal file
View file

@ -0,0 +1,17 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
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);
}

17
lab2/demos/pipe.c Normal file
View file

@ -0,0 +1,17 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
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);
}