Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: jomoza en 14 Diciembre 2014, 18:23 pm



Título: Problemas con GetLIne y Threads en c
Publicado por: jomoza en 14 Diciembre 2014, 18:23 pm
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?

Código:
#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;
}


Título: Re: Problemas con GetLIne y Threads en c
Publicado por: fary en 14 Diciembre 2014, 19:20 pm
He mirado el código y he hecho unos retoques.

Código
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <err.h>
  4.  
  5. #define NUM_THREADS 10
  6.  
  7. pthread_mutex_t mutex;
  8. pthread_cond_t tickets_ready;
  9.  
  10. int tickets = 2;
  11.  
  12. void* pthread_function(void *myline)
  13. {
  14. char *theline;
  15. pthread_mutex_lock(&mutex);
  16. while(tickets==0)
  17. {
  18. pthread_cond_wait(&tickets_ready,&mutex);
  19. }
  20.  
  21. -- tickets;
  22. pthread_mutex_unlock(&mutex);
  23.  
  24. theline = (char *)myline;
  25. printf("Thread working: ¡this is my line! -> %s\n", theline);
  26. sleep(1);
  27.  
  28.  
  29. pthread_mutex_lock(&mutex);
  30. ++tickets;
  31. if (tickets>=1)
  32. {
  33. pthread_cond_broadcast(&tickets_ready);
  34. /* code */
  35. }
  36.  
  37. pthread_mutex_unlock(&mutex);
  38. pthread_exit(NULL);
  39. }
  40.  
  41. int main(int argc, char** argv)
  42. {
  43. pthread_t thread[NUM_THREADS];
  44.  
  45. long int i=0;
  46. char *line;
  47. //char *lines[NUM_THREADS];
  48.  
  49. FILE *file = fopen("commands.txt", "r");
  50. char *data;
  51. size_t len;
  52.  
  53.  
  54. if(file == NULL)
  55. {
  56. err(1, "commands.txt");
  57. }
  58.  
  59.  
  60. while( i < NUM_THREADS &&  getline(&line,&len,file) != -1)
  61. {
  62. //printf("%s\n", line);
  63. pthread_create(thread+i, NULL, pthread_function, (void *)line);
  64.                pthread_join(thread[i], NULL);
  65. ++i;
  66. }
  67.  
  68. /*for ( i = 0; i < NUM_THREADS; ++i)
  69. {
  70. pthread_join(thread[i], NULL);
  71. }*/
  72. return 0;
  73. }

contenido de commands.txt:

Código:
1
2
3
4
5
6
7
8
9
0

Modo de compilación y ejecución del programa:

Código:
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.


Título: Re: Problemas con GetLIne y Threads en c
Publicado por: jomoza en 19 Diciembre 2014, 02:16 am
INFINTAS GRACIAS ^^