⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 primec.c

📁 经验交流,从网上下载的好东西望大家分享
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <STDIO.H>
#include <LIMITS.H>
#include <WINDOWS.H>
#include "directio.h"
#include "Server\prime.h"

/* If we found a prime then return PRIME, otherwise return NOT_PRIME */

#define PRIME           1
#define NOT_PRIME       0

/* Coordinates for displaying numbers to be tested */

#define TESTING_X1      3
#define TESTING_Y1      3
#define TESTING_X2     23
#define TESTING_Y2     18

/* Coordinates for displaying prime numbers */

#define PRIME_X1       30
#define PRIME_Y1        3
#define PRIME_X2       50
#define PRIME_Y2       18

#define ERROR_EXIT      2
#define SUCCESS_EXIT    0
#define STRING_LENGTH 256

/* Delay time in microseconds */

#define WAIT          350
#define WAIT_DISPLAY 2000

/* Colors used in this application */

#define WHITE_ON_BLUE FOREGROUND_WHITE|FOREGROUND_INTENSITY|BACKGROUND_BLUE
#define WHITE_ON_CYAN FOREGROUND_WHITE|FOREGROUND_INTENSITY|BACKGROUND_CYAN
#define RED_ON_BLUE   FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_BLUE
#define RED_ON_CYAN   FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_CYAN
#define CYAN_ON_CYAN  FOREGROUND_CYAN|BACKGROUND_CYAN
#define BLUE_ON_CYAN  FOREGROUND_BLUE|FOREGROUND_INTENSITY|BACKGROUND_CYAN

/* Max number of threads for this application */

#define MAX_THREADS      15
#define COMP_NAME_LENGTH 7

int NumThreads;

/* Prototypes of functions used in this application */

unsigned char IsPrime(unsigned long TestNumber);
extern char ServerStatus(char);
extern void thread_local(void);
extern void thread_remote(int count);
extern void Usage(void);
extern void InitializeApplication(void);
extern void NotifyServer(char);
extern void thread_client(void);
char IsActiveServer[MAX_THREADS];

DWORD  lpIDThreadClient;
HANDLE hthread_client;

/* Critical Section for choosing next available number for testing */

CRITICAL_SECTION GlobalCriticalSection;

/* Storage for Computer Name */

char computer_name_buffer[MAX_COMPUTERNAME_LENGTH];

/* Unique number returned by the server */

unsigned long PrimeServerHandle[MAX_THREADS];

/*
NextNumber    - Next available number to test
StartTime     - Time that we start to compute primes
CurrTime      - Current Computer time
NoPrimeLocal  - Number of primes computed locally
NoPrimeRemote - Number of primes computed on the remote computer
StartNumber   - The starting number for testing primes
*/

unsigned long NextNumber = 1, StartTime, CurrTime, NoPrimeLocal,
   NoPrimeRemoteT, StartNumber = 1;

handle_t BindingHandle[MAX_THREADS];
unsigned long NoPrimeRemote[MAX_THREADS];

PCONTEXT_HANDLE_TYPE phContext[MAX_THREADS];

int ipszNetAdd = 0;

RPC_NS_HANDLE ImportContext;

unsigned char pszRemoteName[MAX_THREADS][MAX_COMPUTERNAME_LENGTH];

#define REMOTE_TRY 100

unsigned char Notry[MAX_THREADS];

void main(int argc, char **argv)
   {
   RPC_STATUS status; /* returned by RPC API function */

   unsigned char *pszUuid                          = NULL;
   unsigned char *pszProtocolSequence              = "ncacn_np";
   unsigned char *pszNetworkAddress[MAX_THREADS]   = { NULL };
   unsigned char *pszEndpoint[MAX_THREADS]         =
      {
      "\\pipe\\prime", "\\pipe\\prime", "\\pipe\\prime", "\\pipe\\prime",
      "\\pipe\\prime", "\\pipe\\prime", "\\pipe\\prime", "\\pipe\\prime",
      "\\pipe\\prime"
      };
   unsigned char *pszOptions                       = NULL;
   unsigned char *pszStringBinding[MAX_THREADS]    = { NULL };

   DWORD  Max_ComputerName_Length = MAX_COMPUTERNAME_LENGTH;
   DWORD  lpIDThread[MAX_THREADS];
   HANDLE hthread_remote[MAX_THREADS];

   int count;

   /* Allow the user to override settings with command line switches */

   for(count = 1; count < argc; count++)
      {
      if((*argv[count] == '-') || (*argv[count] == '/'))
         {
         switch(tolower(*(argv[count]+1)))
            {
            case 'p': /* protocol sequence */
               pszProtocolSequence = argv[++count];
               break;

            case 'n': /* network address */
               {
               char tokensep[] = " \t,;", *token;
               token = strtok(argv[++count], tokensep);

               while(token != NULL)
                  {
                  pszNetworkAddress[ipszNetAdd] = token;
                  token = strtok(NULL, tokensep);
                  strcpy(pszRemoteName[ipszNetAdd],
                     strupr(&pszNetworkAddress[ipszNetAdd][2]));
                  pszRemoteName[ipszNetAdd][COMP_NAME_LENGTH] = 0;
                  ipszNetAdd++;
                  }
               }
               break;

            case 'e':
               {
               char tokensep[] = " \t,;", *token;
               token =strtok(argv[++count], tokensep);

               while(token != NULL)
                  {
                  pszEndpoint[ipszNetAdd] = token;
                  token = strtok(NULL, tokensep);
                  ipszNetAdd++;
                  }
               }
               break;

            case 'o':
               pszOptions = argv[++count];
               break;

            case 'f': /* first number */
               NextNumber = StartNumber = atol(argv[++count]);
               break;

            case 't': /* number of threads */
               NumThreads = atoi(argv[++count]);
               if(NumThreads > MAX_THREADS)
                  NumThreads = MAX_THREADS;
               break;

            case 'h':
            case '?':
            default:
               Usage();
            }
         }
      else
         Usage();
      }

   if(pszNetworkAddress)
      {
      int i;

      /*
      Use a convenience function to concatenate the elements of
      the string binding into the proper sequence
      */

      for(i = 0; i < ipszNetAdd; i++)
         {
         status = RpcStringBindingCompose(
            pszUuid,
            pszProtocolSequence,
            pszNetworkAddress[i],
            pszEndpoint[i],
            pszOptions,
            &pszStringBinding[i]);

         if(status)
            exit(ERROR_EXIT);
         }

      /* Set the binding handle that will be used to bind to the server */

      for(i = 0; i < ipszNetAdd; i++)
         {
         status = RpcBindingFromStringBinding(pszStringBinding[i],
            &BindingHandle[i]);

         if(status)
            exit(ERROR_EXIT);
         }
      }

   GetComputerName(computer_name_buffer, &Max_ComputerName_Length);

   InitializeCriticalSection(&GlobalCriticalSection);

      {
      char i;

      for(i = 0; i < ipszNetAdd; i++)
         ServerStatus(i);
      }

   for(count = 1; count <= ipszNetAdd ; count++)
       hthread_remote[count-1] = CreateThread(NULL, 0,
          (LPTHREAD_START_ROUTINE)thread_remote, (LPVOID)count, 0,
          &lpIDThread[count-1]);

   InitializeApplication();

   hthread_client = CreateThread(NULL, 0,
      (LPTHREAD_START_ROUTINE)thread_client, NULL,
      CREATE_SUSPENDED, &lpIDThreadClient);

   SetThreadPriority(hthread_client, THREAD_PRIORITY_NORMAL);
   ResumeThread(hthread_client);
   thread_local();
   DeleteCriticalSection(&GlobalCriticalSection);

   /*
   The calls to the remote procedures are complete
   Free the string and the binding handle
   */

   /* remote calls done - unbind */

   if(pszNetworkAddress)
      {
      int i;

      for(i = 0; i < ipszNetAdd; i++)
         {
         status = RpcStringFree(&pszStringBinding[i]);

         if(status)
            exit(ERROR_EXIT);
         }
      }

   exit(SUCCESS_EXIT);
   }

void thread_local(void)
   {
   unsigned long temp;
   char Buffer[STRING_LENGTH];
   int loop;

   SMALL_RECT psrctScrollRectTesting, psrctScrollRectPrime;
   COORD coordDestOriginTesting, coordDestOriginPrime;
   CHAR_INFO pchiFill;
   WORD Color[TESTING_X2-TESTING_X1-3], Normal[TESTING_X2-TESTING_X1-3];

   COORD ComputTestCoord;
   COORD ComputPrimeCoord;

   DWORD dummy;
   COORD PrimeCoord;

   psrctScrollRectTesting.Left   = TESTING_X1+1;
   psrctScrollRectTesting.Top    = TESTING_Y1+2;
   psrctScrollRectTesting.Right  = TESTING_X2-1;
   psrctScrollRectTesting.Bottom = TESTING_Y2-1;

   coordDestOriginTesting.X = TESTING_X1+1;
   coordDestOriginTesting.Y = TESTING_Y1+1;

   psrctScrollRectPrime.Left   = PRIME_X1+1;
   psrctScrollRectPrime.Top    = PRIME_Y1+2;
   psrctScrollRectPrime.Right  = PRIME_X2-1;
   psrctScrollRectPrime.Bottom = PRIME_Y2-1;

   coordDestOriginPrime.X = PRIME_X1+1;
   coordDestOriginPrime.Y = PRIME_Y1+1;

   pchiFill.Char.AsciiChar = (char)32;
   pchiFill.Attributes = WHITE_ON_BLUE;

   ComputTestCoord.X  = TESTING_X2-7;
   ComputTestCoord.Y  = TESTING_Y2-1;
   ComputPrimeCoord.X = PRIME_X2-7;
   ComputPrimeCoord.Y = PRIME_Y2-1;

   for(loop = 0; loop < TESTING_X2-TESTING_X1-3; loop++)
      {
      Color[loop]  = RED_ON_BLUE;
      Normal[loop] = WHITE_ON_BLUE;
      }

   while(1)
      {
      EnterCriticalSection(&GlobalCriticalSection);
      if((temp = ++NextNumber) >= ULONG_MAX)
         break;
      LeaveCriticalSection(&GlobalCriticalSection);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -