Last active
November 20, 2023 10:51
-
-
Save maojr/f8e4746982bd6c7d8108004beef90959 to your computer and use it in GitHub Desktop.
examples of I/O multiplexing(select/poll/epoll)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // an example of epoll level trigger | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <sys/epoll.h> | |
| #define MAX_EVENTS 10 | |
| int | |
| main(void) | |
| { | |
| struct epoll_event ev, events[MAX_EVENTS]; | |
| int nfds; | |
| int epollfd; | |
| int stdin_fd = 0; | |
| /* Your input length should less than 20 */ | |
| char input[20]; | |
| epollfd = epoll_create1(0); | |
| if (epollfd == -1) { | |
| perror("epoll_create1"); | |
| exit(EXIT_FAILURE); | |
| } | |
| ev.events = EPOLLIN; | |
| // ev.events = EPOLLIN | EPOLLET; | |
| ev.data.fd = stdin_fd; | |
| if (epoll_ctl(epollfd, EPOLL_CTL_ADD, stdin_fd, &ev) == -1) { | |
| perror("epoll_ctl"); | |
| exit(EXIT_FAILURE); | |
| } | |
| for (;;) { | |
| nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); | |
| if (nfds == -1) { | |
| perror("epoll_wait"); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (nfds > 0) { | |
| fgets(input, 20, stdin); | |
| printf("Your input is: %s", input); | |
| } | |
| } | |
| exit(EXIT_SUCCESS); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| #include <poll.h> | |
| int | |
| main(void) | |
| { | |
| int retval; | |
| struct pollfd pfd; | |
| /* Your input length should less than 20 */ | |
| char input[20]; | |
| /* Watch stdin (fd 0) to see when it has input. */ | |
| pfd.fd = 0; | |
| pfd.events = POLLIN|POLLRDNORM; | |
| retval = poll(&pfd, 1, 5000); | |
| /* Don't rely on the value of tv now! */ | |
| if (retval == -1) | |
| perror("an error occurred"); | |
| else if (retval == 0) | |
| printf("No data within five seconds.\n"); | |
| else { | |
| fgets(input, 20, stdin); | |
| printf("Your input is: %s", input); | |
| } | |
| exit(EXIT_SUCCESS); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <sys/time.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| int | |
| main(void) | |
| { | |
| fd_set rfds; | |
| struct timeval tv; | |
| int retval; | |
| /* Your input length should less than 20 */ | |
| char input[20]; | |
| /* Watch stdin (fd 0) to see when it has input. */ | |
| FD_ZERO(&rfds); | |
| FD_SET(0, &rfds); | |
| /* Wait up to five seconds. */ | |
| tv.tv_sec = 5; | |
| tv.tv_usec = 0; | |
| retval = select(1, &rfds, NULL, NULL, &tv); | |
| /* Don't rely on the value of tv now! */ | |
| if (retval == -1) | |
| perror("select()"); | |
| else if (retval) { | |
| fgets(input, 20, stdin); | |
| printf("Your input is: %s", input); | |
| } | |
| else | |
| printf("timeout! no data within five seconds.\n"); | |
| exit(EXIT_SUCCESS); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment