home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March B / SCO_CASTOR4RRT.iso / uccs / root.14 / udk / usr / ccs / demos / thr_demos / rpc / rpc.c < prev   
Encoding:
C/C++ Source or Header  |  1998-08-19  |  3.9 KB  |  161 lines

  1. /*
  2.  * Copyright (c) 1998 The Santa Cruz Operation, Inc.. All Rights Reserved. 
  3.  *                                                                         
  4.  *        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF THE               
  5.  *                   SANTA CRUZ OPERATION INC.                             
  6.  *                                                                         
  7.  *   The copyright notice above does not evidence any actual or intended   
  8.  *   publication of such source code.                                      
  9.  */
  10.  
  11. #ident    "@(#)ccsdemos:thr_demos/rpc/rpc.c    1.1"
  12. /*
  13.  * This is a simple program which creates a requested number of
  14.  * threads. Each thread then makes Remote Procedure Calls to a
  15.  * server specified by the environment variable SVC_HOST. The
  16.  * transport used is specified by the environment variable NETID.
  17.  *
  18.  * The flag passed to "thr_create()" specifies that a new Light
  19.  * Weight Process be created for each thread. This is exessive
  20.  * for multiplexed threads, and only a limited number of LWPs
  21.  * should normally be used.
  22.  *
  23.  * Note that the thread safe library call "rpc_call()" has not
  24.  * been made available in the TLP3 release, so this program is
  25.  * for source reference only.
  26.  */
  27.  
  28. #include <thread.h>
  29. #include <stdlib.h>
  30. #include <rpc/rpc.h>
  31. #include <rpc/rpcent.h>
  32. #include <rpc/nettype.h>
  33.  
  34. #define EXP_INT_RSLT    1
  35.  
  36. static    int    success;
  37. static    int    total;
  38. char        *host;
  39. int        loop_count;
  40.  
  41. /*
  42.  * This function simply makes an rpc call to the program SVC_PROG on the
  43.  * server specifies by the global string "host".
  44.  */
  45. int
  46. test_func()
  47. {
  48.     enum    clnt_stat    stat;
  49.         int            result;
  50.  
  51.     /*
  52.      * make the rpc call.
  53.      */
  54.         stat = rpc_call(host, SVC_PROG, SVC_VERS, INT_PROC, xdr_void, 0,
  55.                         xdr_int, (char *)&result, getenv("NETID"));
  56.  
  57.     /*
  58.      * check for success.
  59.      */
  60.     if (stat != RPC_SUCCESS)
  61.                 return (1);
  62.  
  63.     /*
  64.      * check the result.
  65.      */
  66.         if (result == EXP_INT_RSLT)
  67.                 return (0);
  68.         else
  69.                 return (1);
  70. }
  71.  
  72. /*
  73.  * This is where each thread starts. Note that the global variable
  74.  * "success" is not protected against simultaneous update, but this
  75.  * should be done by using a spin lock.
  76.  */
  77. void *
  78. template(void *arg)
  79. {
  80.     int    local_success = 0;
  81.     int    i;
  82.  
  83.     for (i = 0; i < loop_count; i++) {
  84.         if (test_func() == 0 && test_func() == 0)
  85.             local_success++;
  86.     }
  87.  
  88.     if (local_success == loop_count)
  89.         success++;
  90.     
  91.         thr_exit(0);
  92. }
  93.  
  94. main(int argc, char **argv)
  95. {
  96.     thread_t    thrid;
  97.         static    char    hostname[100];
  98.     int        thr_count;
  99.     int        error;
  100.     int        i;
  101.  
  102.     if (argc != 3) {
  103.         printf("%s: USAGE: %s <thread#> <loop#>\n",
  104.                         argv[0], argv[0]);
  105.         exit (1);
  106.     }
  107.  
  108.     if ((thr_count = atoi(argv[1])) <= 0) {
  109.         printf("%s: <thread#> should be > 0\n", argv[0]);
  110.         exit (1);
  111.     }
  112.  
  113.     if ((loop_count = atoi(argv[2])) <= 0) {
  114.         printf("%s: <loop#> should be > 0\n", argv[0]);
  115.         exit (1);
  116.     }
  117.  
  118.         if ((host = getenv("SVC_HOST")) == NULL) {
  119.                 gethostname(hostname, 100);
  120.                 host = hostname;
  121.         }
  122.         if (!host || !(*host)) {
  123.                 printf("%s: can't get a host name\n", argv[0]);
  124.                 exit(1);
  125.         }
  126.  
  127.     /*
  128.      * create the needed threads.
  129.      */
  130.     for (i = thr_count ; i > 0; i--) {
  131.                 for (;;) {
  132.                         error = thr_create(NULL, 0, template, NULL,
  133.                                     THR_NEW_LWP, &thrid);
  134.                         if (error) {
  135.                 printf("%s: could not create thread\n",
  136.                                                                 argv[0]);
  137.                                 _lwp_wait(0, 0);
  138.                         } else {
  139.                                 break;
  140.             }
  141.                 }
  142.                 total++;
  143.     }
  144.  
  145.     /*
  146.      * wait for all threads to exit.
  147.      */
  148.     for (i = thr_count; i > 0; i--) {
  149.         thr_join(NULL, NULL, NULL);
  150.     }
  151.  
  152.     if (success != thr_count) {
  153.         printf("%s: %d (%d) Failed.\n", argv[0], total-success, total);
  154.         exit (1);
  155.     } else {
  156.         printf("%s: Passed.\n", argv[0]);
  157.         exit (0);
  158.     }
  159. }
  160.  
  161.