|
请看下面的一段 linux test project(http://ltp.sourceforge.net) 中的代码:
- /*
- * FILE : pth_str02.c
- * DESCRIPTION : Create n threads
- * HISTORY:
- * 05/16/2001 Paul Larson (plars@us.ibm.com)
- * -Ported
- *
- */
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/errno.h>
- /* Defines
- *
- * DEFAULT_NUM_THREADS: Default number of threads to create,
- * user can specifiy with [-n] command line option.
- *
- * USAGE: usage statement
- */
- #define DEFAULT_NUM_THREADS 10
- #define USAGE "\nUsage: %s [-l | -n num_threads] [-d]\n\n" \
- "\t-l Test as many as threads as possible\n" \
- "\t-n num_threads Number of threads to create\n" \
- "\t-d Debug option\n\n"
- /*
- * Function prototypes
- *
- * sys_error (): System error message function
- * error (): Error message function
- * parse_args (): Parses command line arguments
- */
- static void sys_error (const char *, int);
- static void error (const char *, int);
- static void parse_args (int, char **);
- void *thread (void *);
- /*
- * Global Variables
- */
- int num_threads = DEFAULT_NUM_THREADS;
- int test_limit = 0;
- int debug = 0;
- /*---------------------------------------------------------------------+
- | main () |
- | ==================================================================== |
- | |
- | Function: Main program (see prolog for more details) |
- | |
- +---------------------------------------------------------------------*/
- int main (int argc, char **argv)
- {
- /*
- * Parse command line arguments and print out program header
- */
- parse_args (argc, argv);
- if(test_limit) {
- printf ("\n Creating as many threads as possible\n\n");
- } else {
- printf ("\n Creating %d threads\n\n", num_threads);
- }
- thread (0);
- /*
- * Program completed successfully...
- */
- printf ("\ndone...\n\n");
- fflush (stdout);
- exit (0);
- }
- /*---------------------------------------------------------------------+
- | thread () |
- | ==================================================================== |
- | |
- | Function: Recursively creates threads while num < num_threads |
- | |
- +---------------------------------------------------------------------*/
- void *thread (void *parm)
- {
- intptr_t num = (intptr_t) parm;
- pthread_t th;
- pthread_attr_t attr;
- size_t stacksize = 1046528;
- /*
- * Create threads while num < num_threads...
- */
- if (test_limit || (num < num_threads)) {
- if (pthread_attr_init (&attr))
- sys_error ("pthread_attr_init failed", __LINE__);
- if (pthread_attr_setstacksize (&attr, stacksize))
- sys_error ("pthread_attr_setstacksize failed", __LINE__);
- if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE))
- sys_error ("pthread_attr_setdetachstate failed", __LINE__);
- if (pthread_create (&th, &attr, thread, (void *)(num + 1))) {
- if (test_limit) {
- printf ("Testing pthread limit, %d pthreads created.\n", (int)num);
- pthread_exit(0);
- }
- if (errno == EAGAIN) {
- fprintf (stderr, "Thread [%d]: unable to create more threads!\n", (int)num);
- return NULL;
- }
- else
- sys_error ("pthread_create failed", __LINE__);
- }
- pthread_join (th, (void *) NULL);
- }
- pthread_exit(0);
- }
- /*---------------------------------------------------------------------+
- | parse_args () |
- | ==================================================================== |
- | |
- | Function: Parse the command line arguments & initialize global |
- | variables. |
- | |
- +---------------------------------------------------------------------*/
- static void parse_args (int argc, char **argv)
- {
- int i;
- int errflag = 0;
- char *program_name = *argv;
- while ((i = getopt(argc, argv, "dln:?")) != EOF) {
- switch (i) {
- case 'd': /* debug option */
- debug++;
- break;
- case 'l': /* test pthread limit */
- test_limit++;
- break;
- case 'n': /* number of threads */
- num_threads = atoi (optarg);
- break;
- case '?':
- errflag++;
- break;
- }
- }
- /* If any errors exit program */
- if (errflag) {
- fprintf (stderr, USAGE, program_name);
- exit (2);
- }
- }
- /*---------------------------------------------------------------------+
- | sys_error () |
- | ==================================================================== |
- | |
- | Function: Creates system error message and calls error () |
- | |
- +---------------------------------------------------------------------*/
- static void sys_error (const char *msg, int line)
- {
- char syserr_msg [256];
- sprintf (syserr_msg, "%s: %s\n", msg, strerror (errno));
- error (syserr_msg, line);
- }
- /*---------------------------------------------------------------------+
- | error () |
- | ==================================================================== |
- | |
- | Function: Prints out message and exits... |
- | |
- +---------------------------------------------------------------------*/
- static void error (const char *msg, int line)
- {
- fprintf (stderr, "ERROR [line: %d] %s\n", line, msg);
- exit (-1);
- }
复制代码
我在Cirrus logic 的ep9312( ARM 9, OS 为 arm linux 2.4.19 )上运行这个程序,得到“Test Testing pthread limit,498 pthreads created.". 想问一下在linux中线程树的深度或者一个线程的子线程数目是不是有限制。如果有是多少呢? |
|