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

📄 primec.c

📁 经验交流,从网上下载的好东西望大家分享
💻 C
📖 第 1 页 / 共 2 页
字号:

      EnterCriticalSection(&GlobalCriticalSection);
      ScrollConsoleScreenBuffer(hStdOut, &psrctScrollRectTesting, NULL,
         coordDestOriginTesting, &pchiFill);
      sprintf(Buffer, "%d", temp - 1);
      mxyputs((unsigned char)TESTING_X1+2, (unsigned char)(TESTING_Y2-1),
         Buffer, WHITE_ON_BLUE);
      WriteConsoleOutputAttribute(hStdOut, Normal, 7, ComputTestCoord,
         &dummy);
      mxyputs((unsigned char)(TESTING_X2-7), (unsigned char)TESTING_Y2-1,
         "LOCAL ", WHITE_ON_BLUE);
      LeaveCriticalSection(&GlobalCriticalSection);

      if(IsPrime(temp - 1) != 0)
         {
         PrimeCoord.X = PRIME_X1 + 2;
         PrimeCoord.Y = PRIME_Y2 - 1;

         EnterCriticalSection(&GlobalCriticalSection);
         ScrollConsoleScreenBuffer(hStdOut, &psrctScrollRectPrime, NULL,
            coordDestOriginPrime, &pchiFill);
         sprintf(Buffer, "%-17d", temp - 1);
         mxyputs((unsigned char)(PRIME_X1+2), (unsigned char)PRIME_Y2-1,
            Buffer, WHITE_ON_BLUE);
         WriteConsoleOutputAttribute(hStdOut, Normal, 7, ComputPrimeCoord,
            &dummy);
         mxyputs((unsigned char)(PRIME_X2-7), (unsigned char)PRIME_Y2-1,
            "LOCAL ", WHITE_ON_BLUE);
         LeaveCriticalSection(&GlobalCriticalSection);
         NoPrimeLocal++;
         }
      }
   }

void thread_remote(int count)
   {
   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)
      {
      if(!IsActiveServer[count-1])
         {
         if(Notry[count-1]++ > REMOTE_TRY)
            {  
            Notry[count - 1] = 0;
            EnterCriticalSection(&GlobalCriticalSection);
             ServerStatus((char)(count - 1));
            LeaveCriticalSection(&GlobalCriticalSection);
            }
         else        
            continue;
         }   
      if(!IsActiveServer[count-1])
         continue;       

/* To make sure that we won't test the same number twice */

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

      EnterCriticalSection(&GlobalCriticalSection);
      ScrollConsoleScreenBuffer(hStdOut, &psrctScrollRectTesting, NULL,
         coordDestOriginTesting, &pchiFill);
      sprintf(Buffer, " %-17d", temp - 1);
      mxyputs((unsigned char)TESTING_X1+1, (unsigned char)(TESTING_Y2-1),
         Buffer, RED_ON_BLUE);
      WriteConsoleOutputAttribute(hStdOut, Color, 7, ComputTestCoord,
         &dummy);
      mxyputs((unsigned char)(TESTING_X2-7), (unsigned char)TESTING_Y2-1,
         pszRemoteName[count-1], RED_ON_BLUE);
      LeaveCriticalSection(&GlobalCriticalSection);

      RpcTryExcept
         {
         if(RemoteIsPrime(BindingHandle[count-1], PrimeServerHandle[count-1],
            temp - 1) != 0)
            {
            EnterCriticalSection(&GlobalCriticalSection);
            PrimeCoord.X = TESTING_X1 + 2;
            LeaveCriticalSection(&GlobalCriticalSection);

            PrimeCoord.X = PRIME_X1 + 2;
            PrimeCoord.Y = PRIME_Y2 - 1;

            EnterCriticalSection(&GlobalCriticalSection);
            ScrollConsoleScreenBuffer(hStdOut, &psrctScrollRectPrime, NULL,
               coordDestOriginPrime, &pchiFill);
            sprintf(Buffer, " %-17d", temp - 1);
            mxyputs((unsigned char)(PRIME_X1+1), (unsigned char)PRIME_Y2-1,
               Buffer, RED_ON_BLUE);
            WriteConsoleOutputAttribute(hStdOut, Color, 7, ComputPrimeCoord,
               &dummy);
            mxyputs((unsigned char)(PRIME_X2-7), (unsigned char)PRIME_Y2-1,
               pszRemoteName[count-1], RED_ON_BLUE);
            LeaveCriticalSection(&GlobalCriticalSection);
               NoPrimeRemote[count-1]++;
            NoPrimeRemoteT++;
            }
         }
      RpcExcept(1)
         {
         /* if exceptions occurs */

         EnterCriticalSection(&GlobalCriticalSection);
         ServerStatus((char)(count - 1));
         LeaveCriticalSection(&GlobalCriticalSection);
         }
      RpcEndExcept
      }
   }

/* Tests for prime number on the local computer */

unsigned char IsPrime(unsigned long TestNumber)
   {
   unsigned long count;
   unsigned long HalfNumber = TestNumber / 2 + 1;

   for(count = 2; count < HalfNumber; count++)
      if(TestNumber % count == 0)
         return NOT_PRIME;

   return PRIME;
   }

/* Displays command line options */

void Usage(void)
   {
   printf("\nDistributed client/server prime number example.\n\n");
   printf("Usage: PRIME\n");
   printf(" -p protocol_sequence\n");
   printf(" -n network_address\n");
   printf(" -e endpoint\n");
   printf(" -o options\n");
   printf(" -f first number\n");
   exit(1);
   }

/* Screen Initialization and colors and title lines */

void InitializeApplication(void)
   {
   set_vid_mem();

   clearscreen(BACKGROUND_CYAN);
   StartTime = GetTickCount();

   box(0, 0, 79, 24, DOUBLE);
   mxyputs(37, 0, " PRIMEC ", WHITE_ON_CYAN);

   mxyputs(TESTING_X1, 2, "Testing...", WHITE_ON_CYAN);
   box(TESTING_X1, TESTING_Y1, TESTING_X2, TESTING_Y2, SINGLE);

   mxyputs(PRIME_X1, 2, "Prime!",WHITE_ON_CYAN);
   box(PRIME_X1, PRIME_Y1, PRIME_X2, PRIME_Y2, SINGLE);
   mxyputs(32, 23, "Press Esc to exit", WHITE_ON_CYAN);
   }

/*
   Check the Server status if it is of line, then try to connect, return
   server status
*/

char ServerStatus(char iserver)
   {
   char value = FALSE;

   RpcTryExcept
      {
      PrimeServerHandle[iserver] = InitializePrimeServer(
         BindingHandle[iserver], &phContext[iserver], computer_name_buffer);
      IsActiveServer[iserver] = TRUE;
      }      
   RpcExcept(1)
      {
      value = TRUE;
      IsActiveServer[iserver] = FALSE;
      }
   RpcEndExcept

   return value;
   }

/* Let the Server know that we are about to exit */

void NotifyServer(char iserver)
   {
   RpcTryExcept
      {
      TerminatePrimeServer(BindingHandle[iserver],
         PrimeServerHandle[iserver]);
      }
   RpcExcept(1)
      {
      return;
      }
   RpcEndExcept
   }

void thread_client()
   {
   unsigned char no_active =0;

   while(1)
      {
      char Buffer[STRING_LENGTH];
      int  i;

      if(VK_ESCAPE == get_character_no_wait())
         {
         EnterCriticalSection(&GlobalCriticalSection);
         Sleep(WAIT);
         for(i = 0;i < ipszNetAdd; i++)
            NotifyServer((char)i);
         clearscreen(0);
         exit(0);
         LeaveCriticalSection(&GlobalCriticalSection);
         }

      mxyputs(58, 2, "Number of Primes", WHITE_ON_CYAN);
      sprintf(Buffer, "%s:%5d", "LOCAL  ", NoPrimeLocal);
      mxyputs((unsigned char)(60), (unsigned char)(4), Buffer,
         WHITE_ON_CYAN);
         {
         no_active = 0;
         EnterCriticalSection(&GlobalCriticalSection);
         for(i = 0; i < ipszNetAdd; i++)
            if(IsActiveServer[i])
               {
               sprintf(Buffer, " %7s:%5d ", pszRemoteName[i],
                  NoPrimeRemote[i]);
               mxyputs(59, (unsigned char)(4+1+no_active), Buffer,
                  RED_ON_CYAN);
               no_active++;
               }
         box(58, 3, 74, (unsigned char)(5 + no_active), SINGLE);
         mxyputc(58, (unsigned char)(6 + no_active), (char)32, 17,
            CYAN_ON_CYAN);
         }

      no_active = 0;
      for(i = 0; i < ipszNetAdd; i++)
         if(!IsActiveServer[i])
            {
            sprintf(Buffer, " %7s:%5d ", pszRemoteName[i], NoPrimeRemote[i]);
            mxyputs(59, (unsigned char)(4+15+no_active), Buffer,
               RED_ON_CYAN);
            no_active ++;
            }

      if(no_active)
         {
         mxyputs(58, (unsigned char)(17), "Inactive Servers",
            WHITE_ON_CYAN);
         box(58, (unsigned char)(18), 74, (unsigned char)(19 + no_active),
            SINGLE);
         mxyputc(58, (unsigned char)(20 + no_active), (char)32, 17,
            CYAN_ON_CYAN);
         }
      else
         for(i = 0; i < 5; i++)
            mxyputc(58, (unsigned char)(17 + no_active + i), (char)32, 17,
               CYAN_ON_CYAN);

      LeaveCriticalSection(&GlobalCriticalSection);
      CurrTime = (GetTickCount() - StartTime) / 1000;
      sprintf(Buffer, "Primes = %d", NoPrimeLocal + NoPrimeRemoteT);
      mxyputs(58, (unsigned char)(7 + ipszNetAdd), Buffer, WHITE_ON_CYAN);
      sprintf(Buffer, "Time = %d.%02d min.", CurrTime/60, CurrTime%60);
      mxyputs(58, (unsigned char)(8 + ipszNetAdd), Buffer, WHITE_ON_CYAN);

      sprintf(Buffer,"First = %d", StartNumber);
      mxyputs(58,(unsigned char)(1+5+3+ipszNetAdd), Buffer, WHITE_ON_CYAN);
      }
   }

void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
    return(malloc(len));
}

void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
    free(ptr);
}

⌨️ 快捷键说明

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