Baixe o app para aproveitar ainda mais
Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original
#include <stdio.h> #include <errno.h> #include <stddef.h> #include <process.h> int flag_A = 1; int flag_B = 0; int flag_C = 0; //habilita o processamento da thread_code_c int flag_Driver = 0; int num1=0; int num2=0; int Channel[3]; int V_compara [6]; //Vetor para guradar os valores de PID das threads criadas char vector[30]; //Vetor para guardar a ordem de execução das Threads int z=0; //Variavel para controle de posição do vetor de ordem de execução das Threads int i=1; //Variaveis para controle de laços de repetição. int k=0; int j=0; void thread_code_a(void *threadno) { int result_a; while(flag_A==0){} while( z <= 27){ while(flag_A==0){} vector[z]='a'; z++; if (Channel[3]==0){ Channel[3]=1; //Canal em uso if (num1!=0 && num2!=0){ result_a= num1*num2; Channel[0]=result_a; Channel[3]=0; //Canal liberado }else{ result_a=0; } } flag_A = 0; flag_B = 1; flag_C = 0; flag_Driver = 0; } } void thread_code_b(void *threadno) { int result_b; while(flag_B==0){} while( z <= 28){ while(flag_B==0){} vector[z]='b'; z++; if (Channel[3]==0){ Channel[3]=1; //Canal em uso if (num1!=0 && num2!=0){ result_b= ((num1*num2)/num1+num2)*(num1+num2); Channel[1]=result_b; Channel[3]=0; //Canal liberado }else{ result_b=0; } } flag_A = 0; flag_B = 0; flag_C = 1; flag_Driver = 0; } } void thread_code_c(void *threadno) { int result_c; while(flag_C==0){} while( z <=29){ while(flag_C==0){} vector[z]='c'; z++; if (Channel[3]==0){ Channel[3]=1; //Canal em uso if (num1!=0 && num2!=0){ result_c= (((num1*num2)/num1+num2)*num1)+num2; Channel[1]=result_c; Channel[3]=0; //Canal liberado }else{ result_c=0; } } flag_C = 0; flag_B = 0; flag_A = 1; } printf("\n\n\nRELATORIO DO SISTEMA | INICIALIZACAO\n"); if(z == 30) { printf("\n Escalonamento de Threads: \n\n"); for(j= 0; j<= 29; j++) { printf("%c ", vector[j]); } } printf("\nPIDs relacionados as respectivas threads criadas: \n\n"); for(j= 0; j<= 2; j++) { printf("%d ", V_compara [j]); } flag_Driver=1; //Garante que todas as Threads foram criadas antes da execução } void thread_code_driver(void *threadno) { int x=0; int buf; while (flag_Driver==0){} printf("\nDigite o Valor do primeiro numero:"); scanf ("%d",& num1); printf("\nDigite o Valor do segundo numero:"); scanf ("%d",& num2); // x= receive_timeout (&buf,0,2); } int receive_timeout(int *buffer,int c ,int t) { } /* **** Teste para identificar se o PID das threads se repete */ void compara_thread() { for(j= 0; j<= 2; j++) { for(k=j+1; k<=3; k++) { if ( V_compara [j]==V_compara [k]){ i=0; //Se o PID for igual, retorno a criação de threads return; }else{ i=1;//Se os PID são diferentes, executar a thread C flag_C = 1; } } } } void start_thread() { #if defined(_WIN32_) if ((V_compara [0]= _beginthread(thread_code_a,4096, NULL))==(unsigned long)-1) #else if ((V_compara [0]= _beginthread(thread_code_a,4096, NULL))== -1) #endif { printf("Unable to create thread a.\n"); return; } printf("Created thread a.\n"); #if defined(_WIN32_) if ((V_compara [1]= _beginthread(thread_code_b,4096, NULL))==(unsigned long)-1) #else if ((V_compara [1]= _beginthread(thread_code_b,4096, NULL))== -1) #endif { printf("Unable to create thread b.\n"); return; } printf("Created thread b.\n"); #if defined(_WIN32_) if ((V_compara [2]= _beginthread(thread_code_c,4096, NULL))==(unsigned long)-1) #else if ((V_compara [2]= _beginthread(thread_code_c,4096, NULL))== -1) #endif { printf("Unable to create thread c.\n"); return; } printf("Created thread c.\n"); compara_thread(); if (i==0) { printf("Thread com mesmo ID detectada \n\n"); start_thread(); }else{ flag_C = 1; } } void start_driver() { printf("\n\nCriando Processo Driver....\n\n"); #if defined(_WIN32_) if ((V_compara [3]= _beginthread(thread_code_driver,4096, NULL))==(unsigned long)-1) #else if ((V_compara [3]= _beginthread(thread_code_driver,4096, NULL))== -1) #endif { printf("Unable to create thread driver.\n"); return; } printf("Created thread dirver.\n"); printf("\n\nCriando Threads....\n\n"); start_thread(); Channel[3]=0; //Canal inicialmente livre } int main(void) { printf("------------------------------------------------------------------"); printf("\n\n CRIACAO DE THREADS\n\n"); printf("------------------------------------------------------------------\n"); start_driver(); getchar(); return 0; }
Compartilhar