Gestion des threads complète (avec positionnement dans le tableau)

This commit is contained in:
xdrm-brackets 2015-12-17 13:12:38 +01:00
parent c49d6264c7
commit eb3a4b7063
4 changed files with 51 additions and 20 deletions

View File

@ -40,15 +40,33 @@ int main(int argc, char* argv[]){
/* [2] Attente d'une demande de connection, puis création d'un THREAD
============================================================================*/
int index = 0;
while( index <= maxListLen ){
THREADABLE_SOCKET = -1; // on initialise la SOCKET en attendant la connexion
int index = -1;
while( 1 ){
/* 1. On initialise la SOCKET en attendant la connexion et le rang du "manager" inactif */
THREADABLE_SOCKET = -1;
index = -1;
/* 2. On attends une connection */
THREADABLE_SOCKET = accept(LISTENSOCK, (struct sockaddr *) &clientInfo, &len);
// on lance un thread pour le traitement
/* 3. On cherche un "manager" libre */
int i;
// si on trouve un "manager" inactif, on l'enregistre dans index
for( i = 0 ; i < maxListLen ; i++ )
if( activeManagers[i] == 0 ){ index = i; break; }
// si on a trouvé un "manager" libre
if( index != -1 ){
/* 4. On lance un thread pour le traitement de ce client */
pthread_create(&managers[index], NULL, manageConnection, (void*)(intptr_t) THREADABLE_SOCKET);
index++;
if( DEBUGMOD&THR ) printf("THREAD[%d] démarré\n", index);
/* 5. On signale que ce "manager" est maintenant actif */
activeManagers[index] = 1;
}else
if( DEBUGMOD&THR ) printf("Demande de thread refusée!\n");
}
@ -154,6 +172,17 @@ void* manageConnection(void* THREADABLE_SOCKET){
close(USER_SOCKET);
close(FTP_SOCKET);
// on arrête le THREAD
/* [10] Arrêt du THREAD
============================================================================*/
/* 1. On récupère le rang dans les "managers" */
int i, index = -1;
for( i = 0 ; i < maxListLen ; i++ )
if( managers[i] == pthread_self() ){ index = i; break; }
/* 2. On met à jour "activeManagers" */
if( index != -1 )
activeManagers[index] = 0;
if( DEBUGMOD&THR ) printf("THREAD[%d] libéré\n", index);
pthread_exit(NULL);
}

View File

@ -22,23 +22,25 @@
#define REVEALS 0x08 // debug EXPLICITATION des strings
#define BUFFERS 0x10 // debug des BUFFERS
#define HEADERS 0x20 // debug des HEADERS de fonctions
#define THREADS 0x40 // debug des THREADS
#define SCK 0x01 // FILTRE pour ONLY_SOCKET
#define CMD 0x02 // FILTRE pour ONLY_COMMAND
#define DAT 0x04 // FILTRE pour ONLY_DATA
#define RVL 0x08 // FILTRE pour ONLY_REVEALS
#define BUF 0x10 // FILTRE pour ONLY_BUFFERS
#define HDR 0x20 // FILTRE pour ONLY_HEADERS
#define SCK 0x01 // FILTRE pour SOCKET
#define CMD 0x02 // FILTRE pour COMMAND
#define DAT 0x04 // FILTRE pour DATA
#define RVL 0x08 // FILTRE pour REVEALS
#define BUF 0x10 // FILTRE pour BUFFERS
#define HDR 0x20 // FILTRE pour HEADERS
#define THR 0x40 // FILTRE pour THREADS
// possibilité de cumuler les DEBUGMODES
#define DEBUGMOD COMMANDS // REVEALS + HEADER
#define DEBUGMOD 0 // REVEALS + HEADER + THREADS
/* vars */
#define remoteHost "localhost"
#define remotePort "80"
#define maxBuffLen 4096
#define maxListLen 2
#define maxListLen 10
#define maxHostLen 64
#define maxPortLen 6
@ -112,4 +114,4 @@ void* manageConnection(void* THREADABLE_SOCKET);
// VARIABLES
static pthread_t managers[maxListLen]; // contiendra les THREADS
static int activeManagers = 0x00; // sert à détecter les THREADS utilisés
static int activeManagers[maxListLen] = {0}; // contiendra les THREADS actifs

BIN
test

Binary file not shown.