/ltp-full-20110228/testcases/open_posix_testsuite/conformance/interfaces/fork/8-1.c

https://github.com/barajas/Intel-GLTP · C · 184 lines · 89 code · 40 blank · 55 comment · 30 complexity · f55caef995771491ccfbb4bad79aff8f MD5 · raw file

  1. /*
  2. * Copyright (c) 2004, Bull S.A.. All rights reserved.
  3. * Created by: Sebastien Decugis
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program; if not, write the Free Software Foundation, Inc., 59
  14. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  15. * This sample test aims to check the following assertion:
  16. *
  17. * tms_{,c}{u,s}time values are set to 0 in the child process.
  18. * The steps are:
  19. * -> Work for 1 second and save the tms information
  20. * -> fork
  21. * -> Check that the child has tms values less than the saved tms.
  22. * -> join the child process
  23. * -> check tms_c{u,s}time are not 0 anymore.
  24. * The test fails if one of the described checking fails.
  25. */
  26. /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
  27. #define _POSIX_C_SOURCE 200112L
  28. #include <pthread.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <sys/wait.h>
  35. #include <errno.h>
  36. #include <sys/times.h>
  37. #include "testfrmw.h"
  38. #include "testfrmw.c"
  39. /* This header is responsible for defining the following macros:
  40. * UNRESOLVED(ret, descr);
  41. * where descr is a description of the error and ret is an int (error code for example)
  42. * FAILED(descr);
  43. * where descr is a short text saying why the test has failed.
  44. * PASSED();
  45. * No parameter.
  46. *
  47. * Both three macros shall terminate the calling process.
  48. * The testcase shall not terminate in any other maneer.
  49. *
  50. * The other file defines the functions
  51. * void output_init()
  52. * void output(char * string, ...)
  53. *
  54. * Those may be used to output information.
  55. */
  56. #ifndef VERBOSE
  57. #define VERBOSE 1
  58. #endif
  59. int main(int argc, char * argv[])
  60. {
  61. struct tms ini_tms, parent_tms, child_tms;
  62. int status;
  63. pid_t child, ctl;
  64. clock_t st_time, cur_time;
  65. output_init();
  66. st_time = times(&ini_tms);
  67. if (st_time == -1)
  68. UNRESOLVED(errno, "times failed");
  69. if (ini_tms.tms_cutime != 0 || ini_tms.tms_cstime != 0)
  70. FAILED("The process is created with non-zero tms_cutime or "
  71. "tms_cstime");
  72. #if VERBOSE > 1
  73. output("Starting loop...\n");
  74. #endif
  75. /* Busy loop for some times */
  76. do {
  77. cur_time = times(&parent_tms);
  78. if (cur_time == (clock_t) -1)
  79. UNRESOLVED(errno, "times failed");
  80. } while ((cur_time - st_time) < sysconf(_SC_CLK_TCK));
  81. #if VERBOSE > 1
  82. output("Busy loop terminated\n");
  83. output(" Real time: %ld, User Time %ld, System Time %ld, Ticks per sec %ld\n",
  84. (long) (cur_time - st_time),
  85. (long) (parent_tms.tms_utime - ini_tms.tms_utime),
  86. (long) (parent_tms.tms_stime - ini_tms.tms_stime),
  87. sysconf(_SC_CLK_TCK));
  88. #endif
  89. /* Create the child */
  90. child = fork();
  91. if (child == -1)
  92. UNRESOLVED(errno, "Failed to fork");
  93. /* child */
  94. if (child == 0) {
  95. cur_time = times(&child_tms);
  96. if (cur_time == (clock_t) -1)
  97. UNRESOLVED(errno, "times failed");
  98. if ((child_tms.tms_utime + child_tms.tms_stime) >= sysconf(_SC_CLK_TCK))
  99. FAILED("The tms struct was not reset during fork "
  100. "operation");
  101. do {
  102. cur_time = times(&child_tms);
  103. if (cur_time == -1)
  104. UNRESOLVED(errno, "times failed");
  105. } while ((child_tms.tms_utime + child_tms.tms_stime) <= 0);
  106. /* We're done */
  107. exit(PTS_PASS);
  108. }
  109. /* Parent joins the child */
  110. ctl = waitpid(child, &status, 0);
  111. if (ctl != child)
  112. UNRESOLVED(errno, "Waitpid returned the wrong PID");
  113. if (!WIFEXITED(status) || WEXITSTATUS(status) != PTS_PASS)
  114. FAILED("Child exited abnormally");
  115. /* Check the children times were reported as expected */
  116. cur_time = times(&parent_tms);
  117. #if VERBOSE > 1
  118. output("Child joined\n");
  119. output(" Real time: %ld,\n"
  120. " User Time %ld, System Time %ld,\n"
  121. " Child User Time %ld, Child System Time %ld\n",
  122. (long) (cur_time - st_time),
  123. (long) (parent_tms.tms_utime - ini_tms.tms_utime),
  124. (long) (parent_tms.tms_stime - ini_tms.tms_stime),
  125. (long) (parent_tms.tms_cutime - ini_tms.tms_cutime),
  126. (long) (parent_tms.tms_cstime - ini_tms.tms_cstime)
  127. );
  128. #endif
  129. if (cur_time == -1)
  130. UNRESOLVED(errno, "times failed");
  131. if (parent_tms.tms_cutime == 0 && parent_tms.tms_cstime == 0)
  132. FAILED("The process is created with non-zero tms_cutime or "
  133. "tms_cstime");
  134. /* Test passed */
  135. #if VERBOSE > 0
  136. output("Test passed\n");
  137. #endif
  138. PASSED;
  139. }