kqueue timer
This commit is contained in:
parent
b5d0bdf3fd
commit
51e87c037c
27
README.md
27
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
|
||||
|
BIN
bin/ktimer
Executable file
BIN
bin/ktimer
Executable file
Binary file not shown.
@ -4,3 +4,11 @@ aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
aaaaa
|
||||
|
@ -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]);
|
||||
|
86
kqueue_socket.c
Normal file
86
kqueue_socket.c
Normal file
@ -0,0 +1,86 @@
|
||||
#include <sys/event.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user