test_switch.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:2k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* ==== test_switch.c ============================================================
  2.  * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
  3.  *
  4.  * Description : Test context switch functionality.
  5.  *
  6.  *  1.00 93/08/04 proven
  7.  *      -Started coding this file.
  8.  */
  9. #include <pthread.h>
  10. #include <stdio.h>
  11. #include <errno.h>
  12. #define OK      0
  13. #define NOTOK  -1
  14. const char buf[] = "abcdefghijklmnopqrstuvwxyz";
  15. char x[sizeof(buf)];
  16. int fd = 1;
  17. /* ==========================================================================
  18.  * usage();
  19.  */
  20. void usage(void)
  21. {
  22.     printf("test_switch [-d?] [-c count]n");
  23. printf("count must be between 2 and 26n");
  24.     errno = 0;
  25. }
  26. void* new_thread(void* arg)
  27. {
  28. while(1) {
  29. write (fd, (char *) arg, 1);
  30. x[(char *)arg - buf] = 1;
  31. }
  32. fprintf(stderr, "Compiler errorn");
  33. exit(1);
  34. }
  35. main(int argc, char **argv)
  36. {
  37. pthread_t thread;
  38. int count = 2;
  39. int debug = 0;
  40. int eof = 0;
  41. long i;
  42. /* Getopt variables. */
  43. extern int optind, opterr;
  44. extern char *optarg;
  45. while (!eof)
  46.   switch (getopt (argc, argv, "c:d?"))
  47.     {
  48.     case EOF:
  49.       eof = 1;
  50.       break;
  51.     case 'd':
  52.       debug++;
  53.       break;
  54.     case 'c':
  55.       count = atoi(optarg);
  56.       if ((count > 26) || (count < 2)) {
  57.   count = 2;
  58.       }
  59.       break;
  60.     case '?':
  61.       usage();
  62.       return(OK);
  63.     default:
  64.       usage();
  65.       return(NOTOK);
  66.     }
  67. for (i = 0; i < count; i++) {
  68. if (pthread_create(&thread, NULL, new_thread, (void*)(buf+i))) {
  69. fprintf (stderr, "error creating new thread %dn", i);
  70. exit (1);
  71. }
  72. }
  73. #if 0 /* This would cause the program to loop forever, and "make
  74.  check" would never complete.  */
  75. pthread_exit (NULL);
  76. fprintf(stderr, "pthread_exit returnedn");
  77. exit(1);
  78. #else
  79. sleep (10);
  80. for (i = 0; i < count; i++)
  81. if (x[i] == 0) {
  82. fprintf (stderr, "thread %d never rann", i);
  83. return 1;
  84. }
  85. printf ("n%s PASSEDn", argv[0]);
  86. return 0;
  87. #endif
  88. }