elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Los 10 CVE más críticos (peligrosos) de 2020


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Problemas con GetLIne y Threads en c
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problemas con GetLIne y Threads en c  (Leído 1,861 veces)
jomoza

Desconectado Desconectado

Mensajes: 5


Ver Perfil
Problemas con GetLIne y Threads en c
« 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;
}


En línea

fary


Desconectado Desconectado

Mensajes: 1.062



Ver Perfil WWW
Re: Problemas con GetLIne y Threads en c
« Respuesta #1 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.


En línea

Un byte a la izquierda.
jomoza

Desconectado Desconectado

Mensajes: 5


Ver Perfil
Re: Problemas con GetLIne y Threads en c
« Respuesta #2 en: 19 Diciembre 2014, 02:16 am »

INFINTAS GRACIAS ^^
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
getline()
Programación C/C++
Geek7 3 11,563 Último mensaje 29 Julio 2010, 20:52 pm
por Geek7
cin.getline problemas en C++
Programación C/C++
vikour92 0 1,788 Último mensaje 23 Mayo 2012, 19:24 pm
por vikour92
cin.getline y cin.ignore
Programación C/C++
ricardo2013 6 4,558 Último mensaje 25 Octubre 2012, 17:01 pm
por rir3760
EOF con string y getline C++
Programación C/C++
fehnet 3 1,895 Último mensaje 16 Julio 2013, 14:20 pm
por fehnet
getline() !!!
Programación C/C++
chicainge 2 3,139 Último mensaje 14 Septiembre 2014, 19:22 pm
por chicainge
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines