poll.c 857 B

1234567891011121314151617181920212223242526272829303132333435363738
  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[] = { {.fd = fd[1], .events = POLLOUT}, };
  16. ret = poll(outfds, 1, -1);
  17. if (ret <= 0) {
  18. perror("poll with POLLOUT failed");
  19. return 1;
  20. }
  21. printf("poll(POLLOUT) returned %d file descriptors\n", ret);
  22. struct pollfd infds[] = { {.fd = fd[0], .events = POLLIN}, };
  23. write(fd[1], string, (strlen(string)+1));
  24. ret = poll(infds, 1, -1);
  25. if (ret <= 0) {
  26. perror("poll with POLLIN failed");
  27. return 1;
  28. }
  29. printf("poll(POLLIN) returned %d file descriptors\n", ret);
  30. return 0;
  31. }