Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

web server with pool of threads

2 views
Skip to first unread message

Mr_John

unread,
Jul 18, 2008, 9:07:57 AM7/18/08
to
i have built this web server using a pool of thread pre-allocated,to
realize the concurrency in multiple request.it works fine till i use a
page with less then 10 elements,instead it blocks and i can do
nothing.can someone help me? am i right using in this way semaphores?
thanks

#define NUM_THREADS 10

int num[NUM_THREADS],p; /*passing index to threadi thread*/
int sock[10],indx; /*i put the socket of the requests inside*/
typedef int BOOL;
BOOL gest[NUM_THREADS];/*to know which thread is busy or not*/
int busy; /*number of threads busy*/

pthread_t threads[NUM_THREADS];

sem_t semaforo[NUM_THREADS];/*for each thread*/
sem_t mainm[1]; /*to lock the main when all threads are
busy*/


pthread_mutex_t sock_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t busy_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t inde_mutex=PTHREAD_MUTEX_INITIALIZER;

void *manage(void *arg);

int main (){

...

bind();
..

listen();

..

/*costruisci tutti i thread*/
indx=0;
busy=0;
sem_init(&mainm[0],0,0);

for(p=0;p<NUM_THREADS;p++){
num[p]=p;
sem_init(&semaforo[p],0,0);
gest[p]=FALSE;
pthread_create(&threads[p],NULL,&manage,&num[p]);
}

for(;;){

if(busy<NUM_THREADS){

pthread_mutex_lock(&inde_mutex);
while(gest[indx]){indx++;
if(indx==NUM_THREADS-1)indx=0;}/*check wich is the
first thread not busy*/
pthread_mutex_unlock(&inde_mutex);

/* accept incoming request on sk */
len = sizeof(cl_addr);

cl_sk = accept(sk, (SA *) &cl_addr, &len);
if(cl_sk == -1){
printf("SERVER: Error while waiting for connections
\n");
exit(1);}

inet_ntop(AF_INET, &cl_addr.sin_addr, cl_paddr,
sizeof(cl_paddr));
cl_port = ntohs(cl_addr.sin_port);


pthread_mutex_lock(&sock_mutex);
sock[indx]=cl_sk; /*put the socket in array*/
gest[indx]=TRUE; /*mark busy the thread was
free*/
pthread_mutex_unlock(&sock_mutex);

pthread_mutex_lock(&busy_mutex);
busy++;
pthread_mutex_unlock(&busy_mutex);

/*activate the first thread */
sem_post(&semaforo[indx]);
}else sem_wait(&mainm[0]);
}

close(sk);
exit(0);

}


void *manage(void *arg){
int id;
id=*((int *)arg);
......

for(;;){

/*lock*/
sem_init(&semaforo[id],0,0);
sem_wait(&semaforo[id]); /*waiting activation by main*/

pthread_mutex_lock(&sock_mutex);
inform=fopen("/mnt/dos.1/iterv/log.txt","a");
conn=sock[id]; /*i get the socket*/
pthread_mutex_unlock(&sock_mutex);

.....

/*processo la richiesta*/
rit = recv(conn, msg,1023,0);

msg[1023] =0; /* terminate the string */

......./*send the answer and etc.*/

.......

/*and before i finish operation in thread i make this...*/

pthread_mutex_lock(&busy_mutex);
gest[id]=FALSE; /*notifico che il thread ha terminato*/
if(busy==NUM_THREADS)sem_post(&mainm[0]);/*sblocco il
main poichè tutti i thread erano occupati*/
busy--;
fclose(inform);
pthread_mutex_unlock(&busy_mutex);
close(conn); /* TCP graceful close */

}

}

0 new messages