Salve stavo cercando di realizzare un pool di thread come parte di un progetto più grande il codice è questo:
codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#define SA struct sockaddr
#define MAX_USERNAME_LENGTH 30
#define MAX_COMMAND_LENGTH 9
#define MAX_FILE_NAME_LENGTH 100
#define MAX_FILE_SIZE 10000 //massima dimensione file trasferibile(in byte)
#define MAX_DIGITS_NUMBER_OF_FILE 10
#define MAX_USERNAME_LENGTH 30
#define MAX_FILE_SENDER 10 //massimo numero di file senders
#define SHARED_FILES 100 //massimo numero di files condivisi da un file sender
#define MAX_DISP_LENGTH 15
#define MAX_MSG_LENGTH 200
#define BUF_SIZE 100000
#define NUM_THREADS 1
//#define IP_ADDRESS_LENGTH_DEC_PUN 15
//#define PORT_LENGTH 5
int main(int argc, char *argv[]){ //argv[1] è l'IP del server mentre argv[2] è la porta del server
//loopback IP 127.0.0.1
u_int16_t server_port_int=atoi(argv[2]);
//printf("\nserver_port_int=%d\n", server_port_int);
//creo e configuro socket
struct sockaddr_in my_addr, cl_addr;
int ret, length, sk, cn_sk;
int read_size;
int send_size;
int numero_di_file_int;
unsigned char buf[BUF_SIZE];
int k=0;
int i=0;
int j=0;
int t=0;
/*int *taskids[NUM_THREADS];
for(t=0; t<NUM_THREADS; t++){
taskids[t] = (int *) malloc(sizeof(int));
*taskids[t] = t;
}*/
pthread_t thread[NUM_THREADS];
struct thread_arguments{
int * tid; //id del thread
int * new_sock; //void *connection_handler(void *socket_desc) //socket che passo al thread
//{
//Get the socket descriptor
//int sock = *(int*)socket_desc;
//...
//client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
int *len;
};
struct thread_arguments *ta[NUM_THREADS]; //dichiaro come puntatori per passarli poi nella funzione dei threads
for(t=0;t<NUM_THREADS;t++){
ta[t]= (struct thread_arguments*) malloc(sizeof(struct thread_arguments));
}
for(t=0;t<NUM_THREADS;t++){
ta[t]->tid=(int *) malloc(sizeof(int));
ta[t]->new_sock=(int *) malloc(sizeof(int));
ta[t]->len=(int *) malloc(sizeof(int));
*(ta[t]->tid)=t;
*(ta[t]->len)=sizeof(struct sockaddr_in);
}
//*ta[0]->tid=12;
//printf("tid %d\n", *ta[0]->tid);
//void *thread_handler(void *); //la funzione dei threads
sk = socket(AF_INET, SOCK_STREAM, 0);
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(server_port_int);
ret = inet_pton(AF_INET, argv[1], &my_addr.sin_addr);
if(ret<=0){
printf("L'IP del server inserito non rappresenta un indirizzo di rete valido\nnella famiglia degli indirizzi specificata\n");
//printf("Codice errore (errno): %d\n", errno);
return 0;
}
ret = bind(sk, (SA *) &my_addr, sizeof(my_addr));
ret = listen(sk, 10);
length = sizeof(cl_addr);
//printf("server_port_int= %d\n", server_port_int);
//printf("my_addr.sin_port= %d\n", my_addr.sin_port);
printf("In attesa di richieste...\n");
void *thread_handler(void *struct_argomenti){
//Get the socket descriptor
//printf("%d Sono qui!\n", *(((struct thread_arguments *)ta[t])->tid));
//int sock = *((((struct thread_arguments *)ta[t])->new_sock));
int sock = *((int*)(((struct thread_arguments *)ta[t])->new_sock));
printf("Sono qui!\n");
cn_sk = accept(sk, (SA *)&sock, (socklen_t*)(((struct thread_arguments *)ta[t])->len));
printf("Thread %d accetta connessione\n", *(((struct thread_arguments *)ta[t])->tid));
}
for(t=0;t<NUM_THREADS;t++){
if( pthread_create( &thread[t] , NULL , (void *)thread_handler , (void*) ta[t]) < 0)
{
printf("Non posso creare il thread\n");
return 1;
}
}
while(1);
}
Il codice viene compilato correttamente ma poi in esecuzione da errore di segmentazione e facendo debug ho visto che accade ogni volta che dentro la funzione thread_handler tento di accedere alla struttura. Ho provato a lasciare uno solo qualsiasi degli statement che lo fanno e continua a dare segmentation fault. Sicuramente qualcuno dei puntatori nel puntatore a struttura punta dove non deve ma non capisco dove. Inoltre se accedo alla struttura fuori dalla funzione non da problemi. Grazie.
Preciso che sono in ambiente linux con thread POSIX.