poll.c 879 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. write(fd[1], string, (strlen(string) + 1));
  28. ret = poll(infds, 1, -1);
  29. if (ret <= 0) {
  30. perror("poll with POLLIN failed");
  31. return 1;
  32. }
  33. printf("poll(POLLIN) returned %d file descriptors\n", ret);
  34. return 0;
  35. }