ppoll.c 973 B

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