ppoll.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. size_t len = strlen(string) + 1;
  31. if (write(fd[1], string, len) != len) {
  32. perror("write error");
  33. return 1;
  34. }
  35. ret = ppoll(infds, 1, &tv, NULL);
  36. if (ret <= 0) {
  37. perror("ppoll with POLLIN failed");
  38. return 1;
  39. }
  40. printf("ppoll(POLLIN) returned %d file descriptors\n", ret);
  41. return 0;
  42. }