Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
Ambos lados da revisão anterior Revisão anterior Próxima revisão | Revisão anterior | ||
so:criacao_de_threads [2020/07/19 14:47] – maziero | so:criacao_de_threads [2022/11/04 12:54] (atual) – maziero | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
+ | ====== Criação de threads POSIX ====== | ||
+ | A criação de threads em UNIX é feita através da biblioteca padrão PThreads (//POSIX Threads//). Estes exercícios visam estudar exemplos do uso de threads e estimular o aluno a compreender sua dinâmica. | ||
+ | |||
+ | ===== Exercício 1 ===== | ||
+ | |||
+ | Dado o programa abaixo: | ||
+ | |||
+ | <code c thread-create.c> | ||
+ | /* | ||
+ | Criação de threads POSIX em UNIX. | ||
+ | |||
+ | Compilar com gcc -Wall thread-create.c -o thread-create -lpthread | ||
+ | |||
+ | Carlos Maziero, DINF/UFPR 2020 | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define NUM_THREADS 16 | ||
+ | |||
+ | void *threadBody (void *id) | ||
+ | { | ||
+ | long tid = (long) id ; // ID da thread | ||
+ | |||
+ | printf (" | ||
+ | sleep (3) ; | ||
+ | printf (" | ||
+ | pthread_exit (NULL) ; | ||
+ | } | ||
+ | |||
+ | int main (int argc, char *argv[]) | ||
+ | { | ||
+ | pthread_t thread [NUM_THREADS] ; | ||
+ | long i, status ; | ||
+ | |||
+ | for (i=0; i< | ||
+ | { | ||
+ | printf (" | ||
+ | status = pthread_create (& | ||
+ | if (status) | ||
+ | { | ||
+ | perror (" | ||
+ | exit (1) ; | ||
+ | } | ||
+ | } | ||
+ | printf (" | ||
+ | pthread_exit (NULL) ; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | - Analise seu código e o comente detalhadamente. | ||
+ | - A ordem de criação, ativação e encerramento das threads é a mesma? Por que? | ||
+ | - Desenhe o [[diagrama de tempo]] de sua execução. | ||
+ | |||
+ | ===== Exercício 2 ===== | ||
+ | |||
+ | |||
+ | Dado o programa abaixo: | ||
+ | |||
+ | <code c thread-join.c> | ||
+ | /* | ||
+ | Criação de threads POSIX em UNIX, com operação join(). | ||
+ | |||
+ | Compilar com gcc -Wall thread-join.c -o thread-join -lpthread | ||
+ | |||
+ | Carlos Maziero, DINF/UFPR 2020 | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define NUM_THREADS 16 | ||
+ | |||
+ | void *threadBody (void *id) | ||
+ | { | ||
+ | long tid = (long) id ; | ||
+ | |||
+ | printf (" | ||
+ | sleep (3) ; | ||
+ | printf (" | ||
+ | pthread_exit (NULL) ; | ||
+ | } | ||
+ | |||
+ | int main (int argc, char *argv[]) | ||
+ | { | ||
+ | pthread_t thread [NUM_THREADS] ; | ||
+ | pthread_attr_t attr ; | ||
+ | long i, status ; | ||
+ | |||
+ | // para permitir a operação " | ||
+ | pthread_attr_init (&attr) ; | ||
+ | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE) ; | ||
+ | |||
+ | for (i=0; i< | ||
+ | { | ||
+ | printf (" | ||
+ | status = pthread_create (& | ||
+ | if (status) | ||
+ | { | ||
+ | perror (" | ||
+ | exit (1) ; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | for (i=0; i< | ||
+ | { | ||
+ | printf (" | ||
+ | status = pthread_join (thread[i], NULL) ; | ||
+ | if (status) | ||
+ | { | ||
+ | perror (" | ||
+ | exit (1) ; | ||
+ | } | ||
+ | } | ||
+ | printf (" | ||
+ | pthread_attr_destroy (&attr) ; | ||
+ | pthread_exit (NULL) ; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | - Analise seu código e o comente detalhadamente. | ||
+ | - Explique o objetivo do parâmetro '' | ||
+ | - Desenhe o [[diagrama de tempo]] de sua execução. | ||
+ | |||
+ | ===== Exercício 3 ===== | ||
+ | |||
+ | Dado o programa abaixo: | ||
+ | |||
+ | <code c thread-print.c> | ||
+ | /* | ||
+ | Criação de threads POSIX em UNIX, com impressão de variáveis. | ||
+ | |||
+ | Compilar com gcc -Wall thread-print.c -o thread-print -lpthread | ||
+ | |||
+ | Carlos Maziero, DINF/UFPR 2020 | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define NUM_THREADS 16 | ||
+ | |||
+ | int x = 0 ; // variável global | ||
+ | |||
+ | void *threadBody (void *id) | ||
+ | { | ||
+ | long tid = (long) id ; | ||
+ | |||
+ | x++ ; | ||
+ | printf (" | ||
+ | sleep (3) ; | ||
+ | x++ ; | ||
+ | printf (" | ||
+ | pthread_exit (NULL) ; | ||
+ | } | ||
+ | |||
+ | int main (int argc, char *argv[]) | ||
+ | { | ||
+ | pthread_t thread [NUM_THREADS] ; | ||
+ | long i, status ; | ||
+ | |||
+ | for (i=0; i< | ||
+ | { | ||
+ | printf (" | ||
+ | status = pthread_create (& | ||
+ | if (status) | ||
+ | { | ||
+ | perror (" | ||
+ | exit (1) ; | ||
+ | } | ||
+ | } | ||
+ | printf (" | ||
+ | pthread_exit (NULL) ; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | - Analise seu código e o comente detalhadamente. | ||
+ | - Compare a evolução da variável '' | ||
+ | - Desenhe o [[diagrama de tempo]] de sua execução, mostrando a evolução do valor da variável '' | ||
+ | |||
+ | |||
+ | ===== Observações ===== | ||
+ | |||
+ | * Os programas devem ser compilados com a opção '' | ||
+ | * Informações detalhadas sobre as chamadas de sistema utilizadas podem ser encontradas nas páginas de manual do sistema UNIX e também no site [[https:// | ||
+ | * Um relatório [[teaching: |