LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 625|回复: 0

线程树的深度和最大子线程数目?

[复制链接]
发表于 2003-11-19 11:17:57 | 显示全部楼层 |阅读模式
请看下面的一段 linux test project(http://ltp.sourceforge.net) 中的代码:

  1. /*
  2. *  FILE        : pth_str02.c
  3. *  DESCRIPTION : Create n threads
  4. *  HISTORY:
  5. *    05/16/2001 Paul Larson (plars@us.ibm.com)
  6. *      -Ported
  7. *
  8. */

  9. #include <pthread.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/errno.h>


  15. /* Defines
  16. *
  17. * DEFAULT_NUM_THREADS: Default number of threads to create,
  18. * user can specifiy with [-n] command line option.
  19. *
  20. * USAGE: usage statement
  21. */

  22. #define DEFAULT_NUM_THREADS                 10
  23. #define USAGE        "\nUsage: %s [-l | -n num_threads] [-d]\n\n" \
  24.                 "\t-l                     Test as many as threads as possible\n" \
  25.                 "\t-n num_threads    Number of threads to create\n" \
  26.                 "\t-d                Debug option\n\n"

  27. /*
  28. * Function prototypes
  29. *
  30. * sys_error (): System error message function
  31. * error (): Error message function
  32. * parse_args (): Parses command line arguments
  33. */

  34. static void sys_error (const char *, int);
  35. static void error (const char *, int);
  36. static void parse_args (int, char **);
  37. void *thread (void *);

  38. /*
  39. * Global Variables
  40. */

  41. int num_threads = DEFAULT_NUM_THREADS;
  42. int test_limit  = 0;
  43. int debug       = 0;


  44. /*---------------------------------------------------------------------+
  45. |                               main ()                                |
  46. | ==================================================================== |
  47. |                                                                      |
  48. | Function:  Main program  (see prolog for more details)               |
  49. |                                                                      |
  50. +---------------------------------------------------------------------*/
  51. int main (int argc, char **argv)
  52. {
  53.         /*
  54.          * Parse command line arguments and print out program header
  55.          */
  56.         parse_args (argc, argv);

  57.         if(test_limit) {
  58.           printf ("\n    Creating as many threads as possible\n\n");
  59.         } else {
  60.           printf ("\n    Creating %d threads\n\n", num_threads);
  61.         }
  62.         thread (0);       

  63.         /*
  64.          * Program completed successfully...
  65.          */
  66.         printf ("\ndone...\n\n");
  67.         fflush (stdout);
  68.         exit (0);
  69. }


  70. /*---------------------------------------------------------------------+
  71. |                               thread ()                              |
  72. | ==================================================================== |
  73. |                                                                      |
  74. | Function:  Recursively creates threads while num < num_threads       |
  75. |                                                                      |
  76. +---------------------------------------------------------------------*/
  77. void *thread (void *parm)
  78. {
  79.         intptr_t num = (intptr_t) parm;
  80.         pthread_t        th;
  81.         pthread_attr_t        attr;
  82.         size_t                stacksize = 1046528;


  83.         /*
  84.          * Create threads while num < num_threads...
  85.          */
  86.         if (test_limit || (num < num_threads)) {

  87.                 if (pthread_attr_init (&attr))
  88.                         sys_error ("pthread_attr_init failed", __LINE__);
  89.                 if (pthread_attr_setstacksize (&attr, stacksize))
  90.                         sys_error ("pthread_attr_setstacksize failed", __LINE__);
  91.                 if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE))
  92.                         sys_error ("pthread_attr_setdetachstate failed", __LINE__);
  93.                 if (pthread_create (&th, &attr, thread, (void *)(num + 1))) {
  94.                         if (test_limit) {
  95.                            printf ("Testing pthread limit, %d pthreads created.\n", (int)num);
  96.                            pthread_exit(0);
  97.                         }
  98.                         if (errno == EAGAIN) {
  99.                             fprintf (stderr, "Thread [%d]: unable to create more threads!\n", (int)num);
  100.                             return NULL;
  101.                         }
  102.                         else
  103.                             sys_error ("pthread_create failed", __LINE__);
  104.                 }
  105.                 pthread_join (th, (void *) NULL);
  106.         }

  107.         pthread_exit(0);
  108. }


  109. /*---------------------------------------------------------------------+
  110. |                             parse_args ()                            |
  111. | ==================================================================== |
  112. |                                                                      |
  113. | Function:  Parse the command line arguments & initialize global      |
  114. |            variables.                                                |
  115. |                                                                      |
  116. +---------------------------------------------------------------------*/
  117. static void parse_args (int argc, char **argv)
  118. {
  119.         int        i;
  120.         int        errflag = 0;
  121.         char        *program_name = *argv;

  122.         while ((i = getopt(argc, argv, "dln:?")) != EOF) {
  123.                 switch (i) {
  124.                         case 'd':                /* debug option */
  125.                                 debug++;
  126.                                 break;
  127.                         case 'l':                /* test pthread limit */
  128.                                 test_limit++;
  129.                                 break;
  130.                         case 'n':                /* number of threads */
  131.                                 num_threads = atoi (optarg);
  132.                                 break;
  133.                         case '?':
  134.                                 errflag++;
  135.                                 break;
  136.                 }
  137.         }

  138.         /* If any errors exit program */
  139.         if (errflag) {
  140.                 fprintf (stderr, USAGE, program_name);
  141.                 exit (2);
  142.         }
  143. }


  144. /*---------------------------------------------------------------------+
  145. |                             sys_error ()                             |
  146. | ==================================================================== |
  147. |                                                                      |
  148. | Function:  Creates system error message and calls error ()           |
  149. |                                                                      |
  150. +---------------------------------------------------------------------*/
  151. static void sys_error (const char *msg, int line)
  152. {
  153.         char syserr_msg [256];

  154.         sprintf (syserr_msg, "%s: %s\n", msg, strerror (errno));
  155.         error (syserr_msg, line);
  156. }


  157. /*---------------------------------------------------------------------+
  158. |                               error ()                               |
  159. | ==================================================================== |
  160. |                                                                      |
  161. | Function:  Prints out message and exits...                           |
  162. |                                                                      |
  163. +---------------------------------------------------------------------*/
  164. static void error (const char *msg, int line)
  165. {
  166.         fprintf (stderr, "ERROR [line: %d] %s\n", line, msg);
  167.         exit (-1);
  168. }
复制代码

我在Cirrus logic 的ep9312( ARM 9, OS 为 arm linux 2.4.19 )上运行这个程序,得到“Test Testing pthread limit,498 pthreads created.". 想问一下在linux中线程树的深度或者一个线程的子线程数目是不是有限制。如果有是多少呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表