12.07.2015 Views

Hilos - Universidad Nacional de San Antonio Abad del Cusco

Hilos - Universidad Nacional de San Antonio Abad del Cusco

Hilos - Universidad Nacional de San Antonio Abad del Cusco

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

UNIVERSIDAD NACIONAL DE SAN ANTONIO ABAD DEL CUSCOSISTEMAS DE OPERACIONES IGUIA DE LABORATORIOecp 5 <strong>de</strong> 8/* Parametros para funcion <strong>de</strong> impresion. */struct char_print_parms{char caracter; /* El caracter a imprimir. */int cuenta; /* El numero <strong>de</strong> veces a imprimir. */};/* Imprime un numero <strong>de</strong> caracteres en st<strong>de</strong>rr, segun se especifica enPARAMETROS, que es un apuntador a la estructura char_print_parms. */void* char_print (void* parametros){/* Hace un cast sobre el apuntador al tipo a<strong>de</strong>cuado. */struct char_print_parms* p = (struct char_print_parms*) parametros;int i;}for (i = 0; i < p->cuenta; ++i){fputc (p->caracter, st<strong>de</strong>rr);}return NULL;/* El programa main. */int main (){pthread_t hilo1_id;pthread_t hilo2_id;struct char_print_parms hilo1_args;struct char_print_parms hilo2_args;/* Crea un nuevo hilo para imprimir 30,000 'X's. */hilo1_args.caracter = 'X';hilo1_args.cuenta = 30000;pthread_create (&hilo1_id, NULL, &char_print, &hilo1_args);/* Crea un nuevo hilo para imprimir 20,000 'O's. */hilo2_args.caracter = 'O';hilo2_args.cuenta = 20000;pthread_create (&hilo2_id, NULL, &char_print, &hilo2_args);/* Nos aseguramos que elprimer hilo haya terminado */pthread_join(hilo1_id, NULL);/* Nos aseguramos que el segundo hilo haya terminado */pthread_join(hilo2_id, NULL);}/* Po<strong>de</strong>mos salir <strong>de</strong> forma segura */return 0;d) Escriba un programa que cree un hilo. El hilo <strong>de</strong>be <strong>de</strong>volver el N – ésimo número primo.Este valor será, mostrado por el proceso principal

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!