C Winapi -Threads alles scheint OK doch bei einer Zahl von 70 Stück werdens ca 1000?

Also bis jetzt hatte ich mehr mit PThread gearbeitet so ein - zweimal xD aber wollt es einfach mal ausprobieren

Das Problem ist wie im Titel beschrieben, dass ab einer Zahl von um die 70 Threads sinnlos ca 1000 im Taskmaneger zu erkennen sind und das Programm auch lagt ohne ende. Ich frage mich woher das kommt!

Wenn ihr meint man kann was besser machen dann sagt es ruhig !

mein Code bis jetzt

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <winsock.h>
#define true  1
#define false 0
#define MAX 500 //Define Thread_Maximum


 char status[20]={'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'};
 char prot[4]={'|','/','_','\\'};

int id; // counter for threadcounting in the loop 
 struct PScan
 {
       
       int dPort[MAX];
       unsigned long dIP[MAX];
       unsigned long dThreadId[MAX];
       HANDLE hThread[MAX];
       unsigned int time[MAX];
       
 }; 
 

struct PScan ps;

   
unsigned long WINAPI scan(void *count)
{
    
    int ID =(int )count;
    WORD wVersionRequested[MAX];
    WSADATA wsaData[MAX];
    wVersionRequested[ID] = MAKEWORD (1, 1);
    WSAStartup (wVersionRequested[ID], &wsaData[ID]);
    
    
     
     FILE *fp[MAX];
     fd_set       Write[MAX], Err[MAX];
     TIMEVAL      Timeout[MAX];
     unsigned long NonBlk[MAX];
     
     NonBlk[ID]=1;
     int Ret[MAX],err[MAX],client[MAX];
     struct sockaddr_in  sin;
     client[ID] = socket(AF_INET, SOCK_STREAM, 0);
     ioctlsocket(client[ID], FIONBIO, &NonBlk[ID]);
     sin.sin_family      = AF_INET;
     sin.sin_port        = htons(ps.dPort[ID]);
     sin.sin_addr.s_addr = htonl(ps.dIP[ID]);
     
     
     fp[ID] = fopen("pscan.txt","a");
     
     
      // This call will return immediately, coz our socket is non-blocking
      Ret[ID] = connect(client[ID], (struct sockaddr*)&sin, sizeof(sin));

      // If connected, it will return 0, or error
     if (Ret[ID] == SOCKET_ERROR)
     {
            err[ID] = WSAGetLastError();
            // Check if the error was WSAEWOULDBLOCK, where we'll wait.
            if (err[ID] == WSAEWOULDBLOCK) //If Connect is Blocking
           {
                   FD_ZERO(&Write[ID]);
                   FD_ZERO(&Err[ID]);
                   FD_SET(client[ID], &Write[ID]);
                   FD_SET(client[ID], &Err[ID]);

                   Timeout[ID].tv_sec  = ps.time[ID]; //timeaout sec
                   Timeout[ID].tv_usec = 1; // your timeout

                   Ret[ID] = select (0,                    // ignored
                                       NULL,              // read,
                                       &Write[ID],       // Write Check
                                       &Err[ID],        // Error check
                                       &Timeout[ID]);  //  addr of Timeout struct

                   if (Ret[ID] == 0)
                           ExitThread(1);
                   else
                   {
                          if (FD_ISSET(client[ID], &Write[ID]))
                          {
                                fprintf(fp[ID],"\nConnected... ->\t%s \to.k\0",inet_ntoa(sin.sin_addr)); // write message to pscan.txt
                                fflush(fp[ID]);                                                         //  clean the memory of fp[ID]
                                close(client[ID]);                                                     //   close the SOCK client[ID]
                                fclose(fp[ID]);                                                       //    close the FILE fp[ID]
                                ps.hThread[ID]=NULL;                                                 //     set hThread to NULL
                                ExitThread(0);                                                      //      exiting the thread
                          }
                    
                          
                          
                   }
                    
                    
             }
             else
             {
                     ps.hThread[ID]=NULL;        // set hThread to NULL
                     fflush(fp[ID]);            //  clean the memory of hThread
                     close(client[ID]);        //   close the SOCKET
                     fclose(fp[ID]);          //    close the FILE fp[]
                     ExitThread(0);          //     exiting the thread
             }

       }
       else
       {
                fprintf(fp[ID],"\nConnected... ->\t%s \to.k\0",inet_ntoa(sin.sin_addr)); // write message to pscan.txt
                fflush(fp[ID]);                                                         //  clean the memory of fp[ID]
                close(client[ID]);                                                     //   close the SOCK client[ID]
                fclose(fp[ID]);                                                       //    close the FILE fp[ID]
                ps.hThread[ID]=NULL;                                                 //     set hThread to NULL
                ExitThread(0);                                                      //      exiting the thread
        }
        ps.hThread[ID]=NULL;
        fflush(fp[ID]);
        fclose(fp[ID]);
        ExitThread(0);
}


int main(int argc, char* argv[])
{
   FILE * lp;
   int port,i,x=0,y=0,timeout,threads,args;
   
   args = argc;
   
   if(args < 5)
   {
           printf("================================================\n");
           printf("+ PSCAN v 0.1     by Loader007\n");
           printf("+\n");
           printf("default it will scanning with 20 threads\n");
           printf("+ using: %s <IP> <IP> <PORT> <TIMEOUT> <Threads>  \n",argv[0]);
           printf("================================================\n");
           
           return 0;
   }
   
   unsigned long start=ntohl(inet_addr(argv[1])) ;
   unsigned long end = ntohl(inet_addr(argv[2]));
   unsigned long tick = GetTickCount();
   
   
   unsigned int  now=0,five=5;
   if(args == 6)
   threads =atoi(argv[5]);
   
   
   
   
   

       
   port = atoi(argv[3]);
   timeout = atoi(argv[4]);
   

  
   unsigned int  G,W=0;
   G= end-start;   
   lp=fopen("pscan.txt","a");
   fprintf(lp,"\n___________________________________________________________________\n");
   fprintf(lp,"Range %s - %s\t",argv[1],argv[2]);
   fprintf(lp,"PORT:%d\t\t%d Threads\n",port,threads);
   fprintf(lp,"\nTIMEOUT:%d sec\n___________________________________________________________________\n\n",timeout);
   fflush(lp);
   fclose(lp);
    
   system("cls");
   printf("\n___________________________________________________________________\n");
   printf("Range %s - %s\t",argv[1],argv[2]);
   printf("PORT:%d\t\t%d Threads\n\n",port,threads);
   printf("TIMEOUT:%d sec\n___________________________________________________________________\n\n",timeout);
    
   while(start <= end)
   {   a:
       ps.dPort[id]=port;
       ps.dIP[id]=start;
       ps.time[id]=8;
       W++;
      
                
             
                        
        
        
        
        
        
       //printf("G=%i W=%i \n",G,W);
         
         
         
       if(id < threads)
       {
             if((ps.hThread[id]=CreateThread(NULL,0,scan,(void *)id,0,&ps.dThreadId[id])))
             {
              id++;
              start++;
          
              now= W*100/G;
                if(now==five)
                {
                    five=five+5;
                    status[y]='#';
                    y++;
                    
                    fflush(stdout);//stdout Speicher lehren 
                } 
                
                 if(y<=20-1){
                 status[y]=prot[x++];
                 status[y+1]='\0';
                 }
                 printf("%d%c] %s\r",(int)now,0x25,status);
                 fflush(stdout);//stdout Speicher lehren                
                    if(x==4)
                       x=0;  
          
          
              }
          }
       
           else
           {
                     WaitForMultipleObjects(threads, ps.hThread, TRUE, INFINITE);
                     for(i=0; i<= threads; i++)
                     {
                             CloseHandle(ps.hThread[i]); //close the HANDLES
                             ps.hThread[id]=NULL; //clear the memory
                     }
           
          
                     
           id=0;
           goto a;
           }
       
    }

   

status[y]=0x00;
printf("%d%c] %s\n",(int)now,0x25,status);  
printf("\n\nscan with %d Threads finnished\n%d IPs\t in %d sec\n",threads,G,(GetTickCount()-tick)/1000);
WaitForMultipleObjects(id, ps.hThread, TRUE, INFINITE);
getch();
return 0;

}


bin für jeden Tip dankbar


EDIT:
@throjan

sorry kommt nicht noch mal vor! Wahr anscheinend einfach doch zu spät für sowas
 
Zuletzt bearbeitet:
Bitte ändere den Titel um, so dass man ihn lesen kann.
 
Danke für den Tip !

Wahr allerdings leider nicht das Problem ich hab mit dem Prozesswatcher noch mal alles durchgeschaut.
Es wird an die 200 MB Speicher genutzt kann auch mehr werden und es werden ca 1000 Threads gestartet.
Eindeutig liegt es nicht am Taskmaneger.
Wenn ich das Programm mit nur 50 Threads starte läuft das Programm genau wie es soll und hat nur 25 MB Speicher kaum Cpubelastung aber so bald man die 60 oder 70 überschreitet tickt das Programm sozusagen aus ;(

Das muss ja nen Grund haben vieleicht ein Fehler im Code?
Oder ein Denkfehler?
Ich hoffe wirklich , dass mir hier jemand helfen kann.
 
Zurück
Oben