Mercurial
diff playground/main.c @ 17:d97ec3ded2ae
[Seobeo] Few changes...
- Fixed seobeo edge for macos
- Updated so that socket creation can be used for both client and server
- Started on a cutelient library for making connection to the server.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sat, 04 Oct 2025 07:53:12 -0700 |
| parents | adcfad6e86fb |
| children | fa2b8af609d9 |
line wrap: on
line diff
--- a/playground/main.c Fri Oct 03 09:55:51 2025 -0700 +++ b/playground/main.c Sat Oct 04 07:53:12 2025 -0700 @@ -1,32 +1,83 @@ -#include <pthread.h> -#include <stdio.h> -#include <stdlib.h> -#define NUM_THREADS 5 +// #include <pthread.h> +// #include <stdio.h> +// #include <stdlib.h> +// #define NUM_THREADS 5 +// +// void *PrintHello(void *threadid) +// { +// long tid; +// tid = (long)threadid; +// printf("Hello World! It's me, thread #%ld!\n", tid); +// pthread_exit(NULL); +// } +// +// int main (int argc, char *argv[]) +// { +// pthread_t threads[NUM_THREADS]; +// int rc; +// long t; +// for(t = 0; t < NUM_THREADS; t++) +// { +// printf("In main: creating thread %ld\n", t); +// rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); +// if (rc) +// { +// printf("ERROR; return code from pthread_create() is %d\n", rc); +// exit(-1); +// } +// } +// +// /* Last thing that main() should do */ +// pthread_exit(NULL); +// } -void *PrintHello(void *threadid) -{ - long tid; - tid = (long)threadid; - printf("Hello World! It's me, thread #%ld!\n", tid); - pthread_exit(NULL); -} +#include <sys/event.h> +#include <err.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> -int main (int argc, char *argv[]) +int main(int argc, char **argv) { - pthread_t threads[NUM_THREADS]; - int rc; - long t; - for(t = 0; t < NUM_THREADS; t++) - { - printf("In main: creating thread %ld\n", t); - rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); - if (rc) - { - printf("ERROR; return code from pthread_create() is %d\n", rc); - exit(-1); - } - } + struct kevent event; /* Event we want to monitor */ + struct kevent tevent; /* Event triggered */ + int kq, fd, ret; + + if (argc != 2) + err(EXIT_FAILURE, "Usage: %s path\n", argv[0]); + + fd = open(argv[1], O_RDONLY); + if (fd == -1) + err(EXIT_FAILURE, "Failed to open '%s'", argv[1]); + + /* Create kqueue. */ + kq = kqueue(); + if (kq == -1) + err(EXIT_FAILURE, "kqueue() failed"); + + /* Initialize kevent structure. */ + EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, + NOTE_WRITE | NOTE_ATTRIB, + 0, NULL); - /* Last thing that main() should do */ - pthread_exit(NULL); + /* Attach event to the kqueue. */ + ret = kevent(kq, &event, 1, NULL, 0, NULL); + if (ret == -1) + err(EXIT_FAILURE, "kevent register"); + + for (;;) + { + /* Sleep until something happens. */ + ret = kevent(kq, NULL, 0, &tevent, 1, NULL); + if (ret == -1) { + err(EXIT_FAILURE, "kevent wait"); + } else if (ret > 0) { + if (tevent.flags & EV_ERROR) + errx(EXIT_FAILURE, "Event error: %s", strerror(event.data)); + else + printf("Something was written in '%s'\n", argv[1]); + } + } + }