Título: creacion de thread en linux desde c Publicado por: JAHVEM13 en 15 Octubre 2013, 23:06 pm Hola gente si me pueden dar una mano muchas gracias :
como puedo controlar la creación de hilos (para ejecutar uno y poder seguir en la consola como pasa en el caso 1 con firefox) les pongo el código .. alguna sugerencia #include <pthread.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <signal.h> void func1(void) { printf("\n\tSoy el thread que ejecuta gimp %d",pthread_self()); system("gimp"); pthread_exit(0); } void func2(void) { printf("\n\tSoy el thread que ejecuta firefox %d\n",pthread_self() ); system("firefox"); pthread_exit(0); } void func3 (void) { printf("\n\tSoy el thread que ejecuta yast%d",pthread_self()); system("yast"); pthread_exit(0); } void func4(void) { printf("\n\tSoy el thread que ejecuta cheese\n",pthread_self()); system("cheese"); pthread_exit(0); } int main(int argc, char *argv[]) { pid_t pid; // pid_t es de tipo nativo de unix ,es como un entero pid = fork(); pthread_t h1,h2,h3,h4; if(pid < 0) // error { printf("Error al crear proceso hijo,\n"); return (-1); } if(pid == 0) //creacion del proceso hijo { //printf("\n\tSoy el hijo y mi PID es :%d \n", getpid()); } else { int opcion; printf("\n\n\tMENU DE OPCIONES\n"); printf("\n\t-------------------------"); printf("\n\t1-> thread que ejecuta gimp"); printf("\n\t2-> thread que ejecuta firefox"); printf("\n\t3-> thread que ejecuta yast"); printf("\n\t4-> thread que ejecuta Cheese\n\r"); scanf("%d",&opcion); printf("\n\t usted eligio la opcion %d \n",opcion); //if(opcion < 1 || opcion > 4) //VerMenu(); if(opcion >= 1 || opcion <= 4){ switch (opcion) { case 1: { pthread_create(&h1, NULL,(void*)func1, NULL); pthread_join(h1,NULL); break; } case 2: { pthread_create(&h2, NULL,(void*)func2, NULL); pthread_join(h2,NULL); break; } case 3: { pthread_create(&h3, NULL,(void*)func3, NULL); pthread_join(h3,NULL); break; } case 4: { pthread_create(&h4, NULL,(void*)func4, NULL); pthread_join(h4,NULL); break; } } } } return 0; } void EjecutarHilos(int *opcion) { /* pthread_t h1,h2,h3,h4; switch (*opcion) { case 1: { pthread_create(&h1, NULL,(void*)func1, NULL); pthread_join(h1,NULL); break; } case 2: { hread_create(&h2, NULL,(void*)func2, NULL); pthread_join(h2,NULL); break; } case 3: { hread_create(&h3, NULL,(void*)func3, NULL); pthread_join(h3,NULL); break; } case 4: { hread_create(&h4, NULL,(void*)func4, NULL); pthread_join(h4,NULL); break; } } void VerMenu() { int opcion; printf("\n\n\tMENU DE OPCIONES\n"); printf("\n\t-------------------------"); printf("\n\t1-> thread que ejecuta gimp"); printf("\n\t2-> thread que ejecuta firefox"); printf("\n\t3-> thread que ejecuta yast"); printf("\n\t4-> thread que ejecuta Cheese"); scanf("%d",&opcion); if(opcion < 1 || opcion > 4) VerMenu(); else EjecutarHilos(&opcion); */ } |