working pipe

This commit is contained in:
2025-04-23 21:29:23 +08:00
parent 001941a9e6
commit dc65192ef6
4 changed files with 57 additions and 3 deletions

View File

@@ -1,14 +1,13 @@
obj-m += mypipe.o
OUT_DIR := build
.PHONY: all kern_mod install uninstall clean
.PHONY: all kern_mod install uninstall clean reader writer
all: kern_mod
@true
kern_mod: mypipe.c
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) MO=$(PWD)/build
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) MO=$(PWD)/build/kern
install:
insmod ./build/mypipe.ko
@@ -16,6 +15,14 @@ install:
uninstall:
rmmod mypipe
reader:
mkdir -p $(OUT_DIR)
gcc pipe_read.c -o $(OUT_DIR)/pread
writer:
mkdir -p $(OUT_DIR)
gcc pipe_write.c -o $(OUT_DIR)/pwrite
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
rm -rf $(OUT_DIR)

24
lab6/pipe_read.c Normal file
View File

@@ -0,0 +1,24 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int fd = open("/dev/wendy_out", O_RDONLY);
if (fd < 0) {
printf("Error open pipe");
return 1;
}
char buf[128];
ssize_t n = read(fd, buf, sizeof(buf) - 1);
if (n < 0) {
printf("Error read pipe");
return 1;
}
buf[n] = '\0';
printf("Reader received: %s", buf);
close(fd);
return 0;
}

5
lab6/pipe_tester.cpp Normal file
View File

@@ -0,0 +1,5 @@
#include <iostream>
int main() {
}

18
lab6/pipe_write.c Normal file
View File

@@ -0,0 +1,18 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main() {
int fd = open("/dev/wendy_in", O_WRONLY);
if (fd < 0) {
printf("Error open pipe");
return 1;
}
const char *msg = "Hello from the other side!\n";
write(fd, msg, strlen(msg));
close(fd);
return 0;
}