main.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <inttypes.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <errno.h>
  12. #include <time.h>
  13. #include <poll.h>
  14. uint64_t
  15. get_time_us()
  16. {
  17. struct timespec ts;
  18. if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
  19. return 0;
  20. }
  21. return ((uint64_t)ts.tv_sec) * 1000 * 1000 + ((uint64_t)ts.tv_nsec) / 1000;
  22. }
  23. int
  24. main(int argc, char **argv)
  25. {
  26. uint64_t time_start, time_end;
  27. struct pollfd fds[2];
  28. int ret;
  29. printf("sleep 2 seconds\n");
  30. time_start = get_time_us();
  31. ret = sleep(2);
  32. time_end = get_time_us();
  33. printf("sleep return %u\n", ret);
  34. printf("time sleeped: %u\n", (uint32_t)(time_end - time_start));
  35. if (time_end - time_start < 2 * 1000000) {
  36. printf("Test sleep failed!\n");
  37. return -1;
  38. }
  39. /* watch stdin for input */
  40. fds[0].fd = STDIN_FILENO;
  41. fds[0].events = POLLIN;
  42. /* watch stdout for ability to write */
  43. fds[1].fd = STDOUT_FILENO;
  44. fds[1].events = POLLOUT;
  45. printf("poll with 5 seconds\n");
  46. ret = poll(fds, 2, 5 * 1000);
  47. if (ret == -1) {
  48. perror("poll");
  49. return 1;
  50. }
  51. if (!ret) {
  52. printf("Test poll failed, %d seconds elapsed!\n", 5);
  53. return 0;
  54. }
  55. if (fds[0].revents & POLLIN)
  56. printf("stdin is readable\n");
  57. if (fds[1].revents & POLLOUT)
  58. printf("stdout is writable\n");
  59. printf("Test finished\n");
  60. return 0;
  61. }