Buenas, veran el siguiente codigo siempre de devuelve "Violacion del Segmento". Me imajino porque es... Ya que intento pasar directamente el resultado del getline() como parametro al thread, pero ya he intentado de todo xD
¿alguna idea?
#include <pthread.h>
#include <stdio.h>
#include <err.h>
#define NUM_THREADS 10
pthread_mutex_t mutex;
pthread_cond_t tickets_ready;
int tickets = 2;
void* pthread_function(void *myline)
{
char *theline;
pthread_mutex_lock(&mutex);
while(tickets==0)
{
pthread_cond_wait(&tickets_ready,&mutex);
}
-- tickets;
pthread_mutex_unlock(&mutex);
theline = (char *)myline;
printf("Thread working: ¡this is my line! -> %s\n", theline);
sleep(1);
pthread_mutex_lock(&mutex);
++tickets;
if (tickets>=1)
{
pthread_cond_broadcast(&tickets_ready);
/* code */
}
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main(int argc, char** argv)
{
pthread_t thread[NUM_THREADS];
long int i=0;
char *line;
//char *lines[NUM_THREADS];
FILE *file = fopen("commands.txt", "r");
char *data;
size_t len;
if(file == NULL)
{
err(1, "commands.txt");
}
while( i < NUM_THREADS && getline(&line,&len,file) != -1)
{
//fprintf(&lines[i], "%s\n", &line);
pthread_create(thread+i, NULL, pthread_function, (void *)line);
++i;
}
for ( i = 0; i < NUM_THREADS; ++i)
{
pthread_join(thread[i], NULL);
}
return 0;
}
He mirado el código y he hecho unos retoques.
#include <pthread.h>
#include <stdio.h>
#include <err.h>
#define NUM_THREADS 10
pthread_mutex_t mutex;
pthread_cond_t tickets_ready;
int tickets = 2;
void* pthread_function(void *myline)
{
char *theline;
pthread_mutex_lock(&mutex);
while(tickets==0)
{
pthread_cond_wait(&tickets_ready,&mutex);
}
-- tickets;
pthread_mutex_unlock(&mutex);
theline = (char *)myline;
printf("Thread working: ¡this is my line! -> %s\n", theline);
sleep(1);
pthread_mutex_lock(&mutex);
++tickets;
if (tickets>=1)
{
pthread_cond_broadcast(&tickets_ready);
/* code */
}
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main(int argc, char** argv)
{
pthread_t thread[NUM_THREADS];
long int i=0;
char *line;
//char *lines[NUM_THREADS];
FILE *file = fopen("commands.txt", "r");
char *data;
size_t len;
if(file == NULL)
{
err(1, "commands.txt");
}
while( i < NUM_THREADS && getline(&line,&len,file) != -1)
{
//printf("%s\n", line);
pthread_create(thread+i, NULL, pthread_function, (void *)line);
pthread_join(thread[i], NULL);
++i;
}
/*for ( i = 0; i < NUM_THREADS; ++i)
{
pthread_join(thread[i], NULL);
}*/
return 0;
}
contenido de commands.txt:
1
2
3
4
5
6
7
8
9
0
Modo de compilación y ejecución del programa:
x@x:~/Escritorio$ gcc main.c -o main -lpthread
x@x:~/Escritorio$ ./main
Thread working: ¡this is my line! -> 1
Thread working: ¡this is my line! -> 2
Thread working: ¡this is my line! -> 3
Thread working: ¡this is my line! -> 4
Thread working: ¡this is my line! -> 5
Thread working: ¡this is my line! -> 6
Thread working: ¡this is my line! -> 7
Thread working: ¡this is my line! -> 8
Thread working: ¡this is my line! -> 9
Thread working: ¡this is my line! -> 0
saludos.
INFINTAS GRACIAS ^^