poll.c 967 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <poll.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. int main(void) {
  7. int ret;
  8. int fd[2];
  9. char string[] = "Hello, world!\n";
  10. ret = pipe(fd);
  11. if (ret < 0) {
  12. perror("pipe creation failed");
  13. return 1;
  14. }
  15. struct pollfd outfds[] = {
  16. {.fd = fd[1], .events = POLLOUT},
  17. };
  18. ret = poll(outfds, 1, -1);
  19. if (ret <= 0) {
  20. perror("poll with POLLOUT failed");
  21. return 1;
  22. }
  23. printf("poll(POLLOUT) returned %d file descriptors\n", ret);
  24. struct pollfd infds[] = {
  25. {.fd = fd[0], .events = POLLIN},
  26. };
  27. size_t len = strlen(string) + 1;
  28. if (write(fd[1], string, len) != len) {
  29. perror("write error");
  30. return 1;
  31. }
  32. ret = poll(infds, 1, -1);
  33. if (ret <= 0) {
  34. perror("poll with POLLIN failed");
  35. return 1;
  36. }
  37. printf("poll(POLLIN) returned %d file descriptors\n", ret);
  38. return 0;
  39. }