/src/tests/system/libroot/posix/posixtestsuite/conformance/interfaces/fork/8-1.c

https://github.com/yourpalal/haiku · C · 236 lines · 115 code · 49 blank · 72 comment · 30 complexity · dd4291b752b29eb2dd28ff941f763a8b 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. /********************************************************************************************/
  29. /****************************** standard includes *****************************************/
  30. /********************************************************************************************/
  31. #include <pthread.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <sys/wait.h>
  38. #include <errno.h>
  39. #include <sys/times.h>
  40. /********************************************************************************************/
  41. /****************************** Test framework *****************************************/
  42. /********************************************************************************************/
  43. #include "testfrmw.h"
  44. #include "testfrmw.c"
  45. /* This header is responsible for defining the following macros:
  46. * UNRESOLVED(ret, descr);
  47. * where descr is a description of the error and ret is an int (error code for example)
  48. * FAILED(descr);
  49. * where descr is a short text saying why the test has failed.
  50. * PASSED();
  51. * No parameter.
  52. *
  53. * Both three macros shall terminate the calling process.
  54. * The testcase shall not terminate in any other maneer.
  55. *
  56. * The other file defines the functions
  57. * void output_init()
  58. * void output(char * string, ...)
  59. *
  60. * Those may be used to output information.
  61. */
  62. /********************************************************************************************/
  63. /********************************** Configuration ******************************************/
  64. /********************************************************************************************/
  65. #ifndef VERBOSE
  66. #define VERBOSE 1
  67. #endif
  68. /********************************************************************************************/
  69. /*********************************** Test case *****************************************/
  70. /********************************************************************************************/
  71. /* The main test function. */
  72. int main( int argc, char * argv[] )
  73. {
  74. int status;
  75. pid_t child, ctl;
  76. clock_t st_time, cur_time;
  77. struct tms ini_tms, parent_tms, child_tms;
  78. /* Initialize output */
  79. output_init();
  80. /* Initialize first times */
  81. st_time = times( &ini_tms );
  82. if ( st_time == ( clock_t ) - 1 )
  83. {
  84. UNRESOLVED( errno, "times failed" );
  85. }
  86. if ( ( ini_tms.tms_cutime != 0 ) || ( ini_tms.tms_cstime != 0 ) )
  87. {
  88. FAILED( "The process is created with non-zero tms_cutime or tms_cstime" );
  89. }
  90. #if VERBOSE > 1
  91. output( "Starting loop...\n" );
  92. #endif
  93. /* Busy loop for some times */
  94. do
  95. {
  96. cur_time = times( &parent_tms );
  97. if ( cur_time == ( clock_t ) - 1 )
  98. {
  99. UNRESOLVED( errno, "times failed" );
  100. }
  101. }
  102. while ( ( cur_time - st_time ) < sysconf( _SC_CLK_TCK ) );
  103. #if VERBOSE > 1
  104. output( "Busy loop terminated\n" );
  105. output( " Real time: %ld, User Time %ld, System Time %ld, Ticks per sec %ld\n",
  106. ( long ) ( cur_time - st_time ),
  107. ( long ) ( parent_tms.tms_utime - ini_tms.tms_utime ),
  108. ( long ) ( parent_tms.tms_stime - ini_tms.tms_stime ),
  109. sysconf( _SC_CLK_TCK ) );
  110. #endif
  111. /* Create the child */
  112. child = fork();
  113. if ( child == ( pid_t ) - 1 )
  114. {
  115. UNRESOLVED( errno, "Failed to fork" );
  116. }
  117. /* child */
  118. if ( child == ( pid_t ) 0 )
  119. {
  120. cur_time = times( &child_tms );
  121. if ( cur_time == ( clock_t ) - 1 )
  122. {
  123. UNRESOLVED( errno, "times failed" );
  124. }
  125. if ( child_tms.tms_utime + child_tms.tms_stime >= sysconf( _SC_CLK_TCK ) )
  126. {
  127. FAILED( "The tms struct was not reset during fork() operation" );
  128. }
  129. do
  130. {
  131. cur_time = times( &child_tms );
  132. if ( cur_time == ( clock_t ) - 1 )
  133. {
  134. UNRESOLVED( errno, "times failed" );
  135. }
  136. }
  137. while ( ( child_tms.tms_utime + child_tms.tms_stime ) <= 0 );
  138. /* We're done */
  139. exit( PTS_PASS );
  140. }
  141. /* Parent joins the child */
  142. ctl = waitpid( child, &status, 0 );
  143. if ( ctl != child )
  144. {
  145. UNRESOLVED( errno, "Waitpid returned the wrong PID" )
  146. ;
  147. }
  148. if ( ( !WIFEXITED( status ) ) || ( WEXITSTATUS( status ) != PTS_PASS ) )
  149. {
  150. FAILED( "Child exited abnormally" )
  151. ;
  152. }
  153. /* Check the children times were reported as expected */
  154. cur_time = times( &parent_tms );
  155. #if VERBOSE > 1
  156. output( "Child joined\n" );
  157. output( " Real time: %ld,\n"
  158. " User Time %ld, System Time %ld,\n"
  159. " Child User Time %ld, Child System Time %ld\n",
  160. ( long ) ( cur_time - st_time ),
  161. ( long ) ( parent_tms.tms_utime - ini_tms.tms_utime ),
  162. ( long ) ( parent_tms.tms_stime - ini_tms.tms_stime ),
  163. ( long ) ( parent_tms.tms_cutime - ini_tms.tms_cutime ),
  164. ( long ) ( parent_tms.tms_cstime - ini_tms.tms_cstime )
  165. );
  166. #endif
  167. if ( cur_time == ( clock_t ) - 1 )
  168. {
  169. UNRESOLVED( errno, "times failed" );
  170. }
  171. if ( ( parent_tms.tms_cutime == 0 ) && ( parent_tms.tms_cstime == 0 ) )
  172. {
  173. FAILED( "The process is created with non-zero tms_cutime or tms_cstime" );
  174. }
  175. /* Test passed */
  176. #if VERBOSE > 0
  177. output( "Test passed\n" );
  178. #endif
  179. PASSED;
  180. }