aboutsummaryrefslogtreecommitdiff
path: root/module-test/self_pipe.cc
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-16 07:08:27 +0000
committer_ov4 <ov4@ov4.org>2026-10-16 07:08:27 +0000
commit53784946192225525a269423a3bdf65d1e9ea326 (patch)
tree5e28a13ea851d98fd4bf5838230bcff901eb8ef4 /module-test/self_pipe.cc
parent97fbf456121ef60473d2275cd4b4bcf81594438c (diff)
add self pipe test, prepare to adapt line editor 😍
Diffstat (limited to 'module-test/self_pipe.cc')
-rw-r--r--module-test/self_pipe.cc81
1 files changed, 81 insertions, 0 deletions
diff --git a/module-test/self_pipe.cc b/module-test/self_pipe.cc
new file mode 100644
index 0000000..808cdd0
--- /dev/null
+++ b/module-test/self_pipe.cc
@@ -0,0 +1,81 @@
+/*
+ *
+ * test self pipe
+ $ g++ \
+ module-test/self_pipe.cc \
+ -std=c++26 \
+ -O0 -ggdb3 -fno-omit-frame-pointer -fno-inline -D_GLIBCXX_ASSERTIONS \
+ -o module-test/self_pipe.out
+ */
+
+#include <iostream>
+#include <sys/select.h>
+#include <unistd.h>
+#include <signal.h>
+
+using namespace std;
+
+typedef void handler_t(int);
+handler_t *Signal(int signum, handler_t *handler);
+void sigusr1_handler(int signum);
+int my_read(char *s, int len);
+
+int self_pipe_fd[2] = {-1, -1};
+
+
+
+void sigusr1_handler(int signum)
+{
+ char tmp = 'A';
+ write(self_pipe_fd[1], &tmp, 1);
+}
+
+int my_read(char *s, int len)
+{
+ int max_fd;
+ if (STDIN_FILENO > self_pipe_fd[0]) max_fd = STDIN_FILENO;
+ else max_fd = self_pipe_fd[0];
+ fd_set rset;
+ FD_ZERO(&rset);
+ FD_SET(STDIN_FILENO, &rset);
+ FD_SET(self_pipe_fd[0], &rset);
+ select(max_fd+1, &rset, nullptr, nullptr, nullptr);
+ if (FD_ISSET(self_pipe_fd[0], &rset))
+ {
+ return read(self_pipe_fd[0], s, len);
+ }
+ else if (FD_ISSET(STDIN_FILENO, &rset))
+ {
+ return read(STDIN_FILENO, s, len);
+ }
+ return -1;
+}
+
+
+handler_t *Signal(int signum, handler_t *handler)
+{
+ struct sigaction action, old_action;
+
+ action.sa_handler = handler;
+ sigemptyset(&action.sa_mask); /* block sigs of type being handled */
+ action.sa_flags = SA_RESTART; /* restart syscalls if possible */
+
+ sigaction(signum, &action, &old_action);
+ return (old_action.sa_handler);
+}
+
+int main()
+{
+ Signal(SIGUSR1, sigusr1_handler);
+ pipe(self_pipe_fd);
+ cout << "Running PID: " << getpid() << endl;
+ cout << "Do not enter anything" << endl;
+
+ constexpr int tmp_size = 1024;
+ char tmp[tmp_size];
+ while (my_read(tmp, tmp_size))
+ cout << "Hi" << endl;
+
+ cout << "Finished" << endl;
+ return 0;
+} \ No newline at end of file