ppoll.c 994 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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[] = {
  19. {.fd = fd[1], .events = POLLOUT},
  20. };
  21. ret = ppoll(outfds, 1, &tv, NULL);
  22. if (ret <= 0) {
  23. perror("ppoll with POLLOUT failed");
  24. return 1;
  25. }
  26. printf("ppoll(POLLOUT) returned %d file descriptors\n", ret);
  27. struct pollfd infds[] = {
  28. {.fd = fd[0], .events = POLLIN},
  29. };
  30. write(fd[1], string, (strlen(string) + 1));
  31. ret = ppoll(infds, 1, &tv, NULL);
  32. if (ret <= 0) {
  33. perror("ppoll with POLLIN failed");
  34. return 1;
  35. }
  36. printf("ppoll(POLLIN) returned %d file descriptors\n", ret);
  37. return 0;
  38. }