Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 16:fb2cff495a60 | 17:d97ec3ded2ae |
|---|---|
| 1 #include <pthread.h> | 1 // #include <pthread.h> |
| 2 #include <stdio.h> | 2 // #include <stdio.h> |
| 3 #include <stdlib.h> | 3 // #include <stdlib.h> |
| 4 #define NUM_THREADS 5 | 4 // #define NUM_THREADS 5 |
| 5 // | |
| 6 // void *PrintHello(void *threadid) | |
| 7 // { | |
| 8 // long tid; | |
| 9 // tid = (long)threadid; | |
| 10 // printf("Hello World! It's me, thread #%ld!\n", tid); | |
| 11 // pthread_exit(NULL); | |
| 12 // } | |
| 13 // | |
| 14 // int main (int argc, char *argv[]) | |
| 15 // { | |
| 16 // pthread_t threads[NUM_THREADS]; | |
| 17 // int rc; | |
| 18 // long t; | |
| 19 // for(t = 0; t < NUM_THREADS; t++) | |
| 20 // { | |
| 21 // printf("In main: creating thread %ld\n", t); | |
| 22 // rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); | |
| 23 // if (rc) | |
| 24 // { | |
| 25 // printf("ERROR; return code from pthread_create() is %d\n", rc); | |
| 26 // exit(-1); | |
| 27 // } | |
| 28 // } | |
| 29 // | |
| 30 // /* Last thing that main() should do */ | |
| 31 // pthread_exit(NULL); | |
| 32 // } | |
| 5 | 33 |
| 6 void *PrintHello(void *threadid) | 34 #include <sys/event.h> |
| 35 #include <err.h> | |
| 36 #include <fcntl.h> | |
| 37 #include <stdio.h> | |
| 38 #include <stdlib.h> | |
| 39 #include <string.h> | |
| 40 | |
| 41 int main(int argc, char **argv) | |
| 7 { | 42 { |
| 8 long tid; | 43 struct kevent event; /* Event we want to monitor */ |
| 9 tid = (long)threadid; | 44 struct kevent tevent; /* Event triggered */ |
| 10 printf("Hello World! It's me, thread #%ld!\n", tid); | 45 int kq, fd, ret; |
| 11 pthread_exit(NULL); | 46 |
| 47 if (argc != 2) | |
| 48 err(EXIT_FAILURE, "Usage: %s path\n", argv[0]); | |
| 49 | |
| 50 fd = open(argv[1], O_RDONLY); | |
| 51 if (fd == -1) | |
| 52 err(EXIT_FAILURE, "Failed to open '%s'", argv[1]); | |
| 53 | |
| 54 /* Create kqueue. */ | |
| 55 kq = kqueue(); | |
| 56 if (kq == -1) | |
| 57 err(EXIT_FAILURE, "kqueue() failed"); | |
| 58 | |
| 59 /* Initialize kevent structure. */ | |
| 60 EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, | |
| 61 NOTE_WRITE | NOTE_ATTRIB, | |
| 62 0, NULL); | |
| 63 | |
| 64 /* Attach event to the kqueue. */ | |
| 65 ret = kevent(kq, &event, 1, NULL, 0, NULL); | |
| 66 if (ret == -1) | |
| 67 err(EXIT_FAILURE, "kevent register"); | |
| 68 | |
| 69 for (;;) | |
| 70 { | |
| 71 /* Sleep until something happens. */ | |
| 72 ret = kevent(kq, NULL, 0, &tevent, 1, NULL); | |
| 73 if (ret == -1) { | |
| 74 err(EXIT_FAILURE, "kevent wait"); | |
| 75 } else if (ret > 0) { | |
| 76 if (tevent.flags & EV_ERROR) | |
| 77 errx(EXIT_FAILURE, "Event error: %s", strerror(event.data)); | |
| 78 else | |
| 79 printf("Something was written in '%s'\n", argv[1]); | |
| 80 } | |
| 81 } | |
| 82 | |
| 12 } | 83 } |
| 13 | |
| 14 int main (int argc, char *argv[]) | |
| 15 { | |
| 16 pthread_t threads[NUM_THREADS]; | |
| 17 int rc; | |
| 18 long t; | |
| 19 for(t = 0; t < NUM_THREADS; t++) | |
| 20 { | |
| 21 printf("In main: creating thread %ld\n", t); | |
| 22 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); | |
| 23 if (rc) | |
| 24 { | |
| 25 printf("ERROR; return code from pthread_create() is %d\n", rc); | |
| 26 exit(-1); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 /* Last thing that main() should do */ | |
| 31 pthread_exit(NULL); | |
| 32 } |