From 51e87c037c033b686a855b0112ff2aee37c705e9 Mon Sep 17 00:00:00 2001 From: User Infidel Date: Sun, 25 Sep 2022 20:15:32 +0700 Subject: [PATCH] kqueue timer --- README.md | 27 ++++++++++++- bin/ktimer | Bin 0 -> 15816 bytes bin/test.txt | 8 ++++ kqueue_file_watch.cpp | 2 - kqueue_socket.c | 86 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 4 deletions(-) create mode 100755 bin/ktimer create mode 100644 kqueue_socket.c diff --git a/README.md b/README.md index c7e0347..8753c68 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,33 @@ -# What the fuck is this +# KQUEUE Utilization This is your lame attempt to understand how those Network Virtual Functions sofware works as well to sharpen your understanding on C++ and C programming language. ## Hints -- Compile using `g++` + +- Compile using `g++` for the file watcher + +To trace the function calls on FreeBSD based system use `truss()` instead of `strace`. + +The `kqueue_socket.c` is my attempt on create a socket watcher timer program using `kevent`. + +- ![KQUEUE NETBSD GUIDE](https://wiki.netbsd.org/tutorials/kqueue_tutorial/) + +``` + + struct kevent my_event; + struct kevent my_change; + + EV_SET(&my_change, ...); // Initialize the kevent + + for(;;){ + + ...; + + } + + +``` ## Goal - Understand the difference between c++ and C diff --git a/bin/ktimer b/bin/ktimer new file mode 100755 index 0000000000000000000000000000000000000000..ccdbd5c43c4b360096bca790ef1d7c1a111300be GIT binary patch literal 15816 zcmeHO32;=$neO-AT+#@QkT@g`Gn^K-8A)8mAj6E1;E4#M=)mCXF`5~PF=9rU1B5q5 z!U}dKm;?vM&L-J)?5(6Wj(3YBgu)Ha%_?Lj@CwR=*OZ#1 zgp9d4gX}3+R>n!d9VQ~+Dv`%AnUAm|lI+~V&MovMJR%a4P_~cSM!!nouTu4bBHBxU z>AcQD-WTAtzMcpYe(xkx;oeDJj!3w`WHSk+-N#|a+b85724pwX^oF}a{h>fjZ*WPd zx7!nsdg{oQ+Cjdlc^hJ(P(yno`{wzI44-6@4>ZH4=H~M0I#a9?{9O2n%L6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OOsTzk1rmH$V#Kq?BDAOFjF_Q%9J z_n3(P$vpe~_+JzDGzWN*>p;;_`Tdn|{}_xYe`zoGU1fiv{dZtXms;A(^}bu!A0g)` z@-oW!GOuSK%Z)G3S007Pw|tyT-EQ3_=CC333P`i;E_p-n7A*9c|<%a z`LW)bfJb_h>A`~HloP`w$JyhP%{U&Z&YEo+l*y4Iwew7aW5P~?{OdPU z*ys2E1z}GIsB(*pCE+4rpQ}T7XralWkN3Gn+DTqr0sH=|Obz+wPU`2|6P=eLSN-(W xrbVBKKNsW`_{sLm=K`GqlVjJkFzZp#A?Na(=f|gMHj(|=Wu~8Xc?NkT`!730ktF~C literal 0 HcmV?d00001 diff --git a/bin/test.txt b/bin/test.txt index 7050359..88aae09 100644 --- a/bin/test.txt +++ b/bin/test.txt @@ -4,3 +4,11 @@ aaaaa aaaaa aaaaa aaaaa +aaaaa +aaaaa +aaaaa +aaaaa +aaaaa +aaaaa +aaaaa +aaaaa diff --git a/kqueue_file_watch.cpp b/kqueue_file_watch.cpp index defb023..a2bbab8 100644 --- a/kqueue_file_watch.cpp +++ b/kqueue_file_watch.cpp @@ -40,7 +40,6 @@ int main(int argc, char **argv){ struct epoll_event *events =NULL; - static task_list_t **tasks=NULL; // printf("%s\n", task_list_t); @@ -64,7 +63,6 @@ int main(int argc, char **argv){ } // fd = open(argv[1], O_RDONLY); - // for (int i=1; i <= 2; i++){ // printf("YOUR FILES \n"); // printf("==> %s\n", argv[i]); diff --git a/kqueue_socket.c b/kqueue_socket.c new file mode 100644 index 0000000..4fc18c6 --- /dev/null +++ b/kqueue_socket.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +struct inf_kevent { + uintptr_t ident; + short filter; + u_short flags; + u_int fflags; + int64_t data; + void *udata; + u_int64_t ext[4]; +}; + +struct addrinfo *add; +struct addrinfo hints; + +void inf_info(const char *s); + +int main(void) { + struct kevent event; + struct kevent change; + pid_t pid; + int kq, nev; + + printf("\n"); + printf("---------------------------------------\n"); + printf("Stupid struct\n"); + printf("---------------------------------------\n"); + printf("\n"); + + + if ((kq = kqueue()) == -1){ + inf_info("kqueue()"); + } + + EV_SET(&change, 1, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, 2000, 0); + + for (;;) { + + nev = kevent(kq, &change, 1, &event, 1, NULL); + + printf("--> kq %d\n", kq); + printf("--> pid %d\n", pid); + + if (nev < 0){ + inf_info("kevent()"); + } + + else if (nev > 0) { + + if (event.flags & EV_ERROR) { + fprintf(stderr, "EV_ERROR..."); + exit(EXIT_FAILURE); + } + + if ((pid = fork()) < 0 ) { + fprintf(stderr, "EV_ERROR..."); + exit(EXIT_FAILURE); + } + + else if (pid == 0) { + if (execlp("date", "date", (char *)0) < 0) + inf_info("execplp()"); + } + + } + } + close(kq); +} + +void inf_info(const char *s) +{ + perror(s); + exit(EXIT_FAILURE); +} +