Baixe o app para aproveitar ainda mais
Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original
/** *@file main.c * *@create by David Fernandes <david.fernandes22@yahoo.com> * Lucas de Castro <castrolucas65@gmail.com> * Daniel Lima <danieldla87@gmail.com> *@version 1.0 *@date of last actualization: 07/04/2014 */ #include <stdio.h> #include <errno.h> #include <stddef.h> #include <process.h> #include <time.h> #include <windows.h> // Biblioteca para a função Sleep #include <conio.h> #include <math.h> /** *@ifce Variáveis Globais do Projeto * */ int i; int flag_inicio_thread = 0; int V_compara [10]; int Channel[10]={0,0,0,0,0,0,0,0,0,0}; int Send_Block = 0; int Send_Block_f = 0; int Send_Block_g = 0; int Send_Block_h = 0; int Send_Block_i = 0; int Send_Block_j = 0; /** *@ifce Funções Send e Receive * Comunicação Temporizada a partir de um vetor de Canais */ int send_timeout(int *buffer,int c ,int t) { time_t inicio; time_t fim; Channel[c]= *buffer; //enquanto o canal for diferente de -1 e não estourar o tempo ele aguarda inicio = time(NULL); //para transmitir...se o canal é igual a -1 a msg foi recebida do{ fim=time(NULL); }while(difftime (fim,inicio)<= t && Channel[c]!=-1); if(Channel[c] == -1){ return 0; //MSG recebida no timeout }else{ return 1; //MSG não recebida no timeout } } int receive_timeout(int *buffer,int c ,int t) { time_t inicio; time_t fim; //enquanto o canal for igual a -1 e não estourar o tempo ele aguarda inicio = time(NULL); //para receber...canal diferente de -1 (tem conteudo), logo joga o valor do { //para buffer e atribui ao canal -1 mostrando que recebeu ok fim=time(NULL); }while(difftime (fim,inicio)<= t && Channel[c]==-1); if (Channel[c] != -1){ *buffer=Channel[c]; Channel[c]=-1; return 0; //MSG enviada no timeout }else{ return 1; //MSG não enviada no timeout } } /** *@ifce Threads_Users * Executarão a troca de Mensagens */ void thread_code_a(void *threadno) { int Send; int x; int status; int t; char PID_Send[5] = "05"; char Data[5]="200"; char Menssage[5] = ""; while (flag_inicio_thread == 0) {} strcat (PID_Send,Data); strcpy (Menssage,PID_Send); Send = atoi (Menssage); while(1){ while (Send_Block == 1){} //Controle determinado pelo Mail Box x = send_timeout(&Send,0,5); //Copiar os caracteres de Mens em Mensagem if(x==0){ // printf("\nMensagem 1 lida no tempo\n"); t=0; }else{ // printf("\nMensagem 1 nao lida no tempo\n"); t=1; } if (t==0){ x= receive_timeout(&status,1,5); if (x==0 && status==0) // printf("Mensagem 1 recebido OK por Mail_Box!!!!!\n"); if (x==0 && status==1){ // printf("Status negativo e finaliza Thread 1...\n"); // return; if (x==1){ // printf("Thread 1 nao recebido no tempo por Mail_Box ...\n"); } } } } } void thread_code_b(void *threadno) { int Send; int x; int status; int t; char PID_Send[5] = "06"; char Data[5]="55"; char Menssage[5] = ""; while (flag_inicio_thread == 0) {} strcat (PID_Send,Data); strcpy (Menssage,PID_Send); Send = atoi (Menssage); while(1){ while (Send_Block == 1){} //Controle determinado pelo Mail Box x = send_timeout(&Send,2,5); if(x==0){ // printf("\nMensagem 2 lida no tempo\n"); t=0; }else{ // printf("\nMensagem 2 nao lida no tempo\n"); t=1; } if (t==0){ x= receive_timeout(&status,3,5); if (x==0 && status==0) // printf("Mensagem 2 recebido OK por Mail_Box!!!!!\n"); if (x==0 && status==1){ // printf("Status negativo e finaliza Thread 1...\n"); // return; if (x==1){ // printf("Mensagem 2 nao recebido no tempo por Mail_Box ...\n"); } } } } } void thread_code_c(void *threadno) { int Send; int x; int status; int t; char PID_Send[5] = "07"; char Data[5]="120"; char Menssage[5] = ""; while (flag_inicio_thread == 0) {} strcat (PID_Send,Data); strcpy (Menssage,PID_Send); Send = atoi (Menssage); while(1){ while (Send_Block == 1){} //Controle determinado pelo Mail Box x = send_timeout(&Send,4,5); if(x==0){ // printf("\nMensagem 3 lida no tempo\n"); t=0; }else{ // printf("\nMensagem 3 nao lida no tempo\n"); t=1; } if (t==0){ x= receive_timeout(&status,5,5); if (x==0 && status==0) // printf("Mensagem 3 recebido OK por Mail_Box!!!!!\n"); if (x==0 && status==1){ // printf("Status negativo e finaliza Thread 3...\n"); // return; if (x==1){ // printf("Mensagem 3 nao recebido no tempo por Mail_Box ...\n"); } } } } } void thread_code_d(void *threadno) { int Send; int x; int status; int t; char PID_Send[5] = "08"; char Data[5]="155"; char Menssage[5] = ""; while (flag_inicio_thread == 0) {} strcat (PID_Send,Data); strcpy (Menssage,PID_Send); Send = atoi (Menssage); while(1){ while (Send_Block == 1){} //Controle determinado pelo Mail Box x = send_timeout(&Send,6,5); if(x==0){ // printf("\nMensagem 4 lida no tempo\n"); t=0; }else{ // printf("\nMensagem 4 nao lida no tempo\n"); t=1; } if (t==0){ x= receive_timeout(&status,7,5); if (x==0 && status==0) // printf("Mensagem 4 recebido OK por Mail_Box!!!!!\n"); if (x==0 && status==1){ // printf("Status negativo e finaliza Thread 4...\n"); // return; if (x==1){ // printf("Mensagem 4 nao recebido no tempo por Mail_Box ...\n"); } } } } } void thread_code_e(void *threadno) { int Send; int x; int status; int t; char PID_Send[5] = "09"; char Data[5]="20"; char Menssage[5] = ""; while (flag_inicio_thread == 0) {} strcat (PID_Send,Data); strcpy (Menssage,PID_Send); Send = atoi (Menssage); while(1){ while (Send_Block == 1){} //Controle determinado pelo Mail Box x = send_timeout(&Send,8,5); if(x==0){ // printf("\nMensagem 5 lida no tempo\n"); t=0; }else{ // printf("\nMensagem 5 nao lida no tempo\n"); t=1; } if (t==0){ x= receive_timeout(&status,9,5); if (x==0 && status==0) // printf("Mensagem 5 recebido OK por Mail_Box!!!!!\n"); if (x==0 && status==1){ // printf("Status negativo e finaliza Thread 5...\n"); // return; if (x==1){ // printf("Mensagem 5 nao recebido no tempo por Mail_Box ...\n"); } } } } } void thread_code_f(void *threadno) { int x; int z; int t; int buf; while (flag_inicio_thread == 0) {} while(1){ while (Send_Block_f == 0){} //Controle determinado pelo Mail Box x= receive_timeout(&buf,0,5); if (x==0){ z = buf; t=0; printf("\nMensagem recebida OK !!!!\nThread 05 tem conteudo: %d !!!!!\n",z); }else{ t=1; // printf("Mensagem nao recebida OK por Thread 5....\n"); // return; } send_timeout(&t,1,5); } } void thread_code_g(void *threadno) { int x; int z; int t; int buf; while (flag_inicio_thread == 0) {} while(1){ while (Send_Block_g == 0){} //Controle determinado pelo Mail Box x= receive_timeout(&buf,2,5); if (x==0){ z = buf; printf("\nMensagem recebida OK !!!!\nThread 06 tem conteudo: %d !!!!!\n",z); t=0; }else{ t=1; } send_timeout(&t,3,5); } } void thread_code_h(void *threadno) { int x; int z; int t; int buf; while (flag_inicio_thread == 0) {} while(1){ while (Send_Block_h == 0){} //Controle determinado pelo Mail Box x= receive_timeout(&buf,4,5); if (x==0){ z = buf; printf("\nMensagem recebida OK !!!!\nThread 07 tem conteudo: %d !!!!!\n",z); t=0; }else{ t=1; } send_timeout(&t,5,5); } } void thread_code_i(void *threadno) { int x; int t; int z; int buf; while (flag_inicio_thread == 0) {} while(1){ while (Send_Block_i == 0){} //Controle determinado pelo Mail Box x= receive_timeout(&buf,6,5); if (x==0){ z = buf; printf("\nMensagem recebida OK !!!!\nThread 08 tem conteudo: %d !!!!!\n",z); t=0; }else{ t=1; } send_timeout(&t,7,5); } } void thread_code_j(void *threadno) { int x; int t; int z; int buf; while (flag_inicio_thread == 0) {} while(1){ while (Send_Block_j == 0){} //Controle determinado pelo Mail Box x= receive_timeout(&buf,8,5); if (x==0){ z = buf; printf("\nMensagem recebida OK !!!!\nThread 09 tem conteudo: %d !!!!!\n",z); t=0; }else{ t=1; } send_timeout(&t,9,5); } } /** *@ifce Thread Mail Box * Thread Mail Box */ int select_wait () { while(1){ if (Channel[0] != -1){ return 1; } if (Channel[2] != -1){ return 2; } if (Channel[4] != -1){ return 3; } if (Channel[6] != -1){ return 4; } if (Channel[8] != -1){ return 5; } } } /** *@ifce Thread Mail Box * Thread Mail Box */ void thread_code_mail_box(void *threadno) { int t; int x = 0; int prog; int z = 0; int buf; int ID_destination = 0;//endereço da mensagem de destino int b = 0; int Send = 0; char length[4]; int SizeNumber; int j=0; int w=0; int Buffer [5]={0,0,0,0,0}; int status; int A=0; int B=0; int C=0; int D=0; int E=0; while (flag_inicio_thread = 0) {} while(Channel[0]==0 && Channel[2]==0 && Channel[4]==0 && Channel[6]==0 && Channel[8]==0){} //evitar que os votos deixem de ser lidos antes de compara-los printf("\n\n Sending Messages, Please Wait.....\n\n"); do{ prog = select_wait (); if (prog == 1){ x= receive_timeout(&buf,0,5); if (x==0){ z = buf; A = z; }else{ return; } } if (prog == 2){ x= receive_timeout(&buf,2,5); if (x==0){ z = buf; B = z; }else{ return; } } if (prog == 3){ x= receive_timeout(&buf,4,5); if (x==0){ z = buf; C = z; }else{ return; } } if (prog == 4){ x= receive_timeout(&buf,6,5); if (x==0){ z = buf; D = z; }else{ return; } } if (prog == 5){ x= receive_timeout(&buf,8,5); if (x==0){ z = buf; E = z; }else{ return; } } Buffer[j] = z; j++; if (z==0 || Buffer[j] == Buffer[j-1])//Se nada foi recebido, tentar preencher a mesma posição do buffer j--; z=0; printf ("\nSelect wait : %d %d %d %d %d",A,B,C,D,E); printf("\nBuffer alloc: %d %d %d %d %d ",Buffer[0],Buffer[1],Buffer[2],Buffer[3],Buffer[4]); A=0; B=0; C=0; D=0; E=0; }while (Buffer[0]==0 || Buffer[1]==0 || Buffer[2]==0 || Buffer[3]==0 || Buffer[4]==0); printf("\n\n\nBuffer Mail Box: %d %d %d %d %d \n",Buffer[0],Buffer[1],Buffer[2],Buffer[3],Buffer[4]); Send_Block = 1; //Controle das Threads de envio de MSG for(w=0; w<=4;w++){ itoa (Buffer[w],length,10);//int para sting char (inteiro,string, base<inteiro>) SizeNumber = strlen (length); ID_destination = (Buffer[w]/(pow(10,SizeNumber-1))); //ID de destino = valor da msg / 1000 se houverem 4 num, 100 se Send = Buffer[w] - (ID_destination * (pow(10,SizeNumber-1))); //foram enviados 3 e etc... printf("\n%d",ID_destination); printf("_%d",Send); if (ID_destination == 5){ b = 0; Send_Block_f = 1; } if (ID_destination == 6){ b = 2; Send_Block_g = 1; } if (ID_destination == 7){ b = 4; Send_Block_h = 1; } if (ID_destination == 8){ b = 6; Send_Block_i = 1; } if (ID_destination == 9){ b = 8; Send_Block_j = 1; } printf("_%d",b); x = send_timeout(&Send,b,5); if(x==0){ t=0; // printf("\nMensagem Mail_Box lida no tempo\n"); if(b == 0) Send_Block_f = 0; if(b == 2) Send_Block_g = 0; if(b == 4) Send_Block_h = 0; if(b == 6) Send_Block_i = 0; if(b == 8) Send_Block_j = 0; }else{ // printf("\nMensagem Mail_Box %d nao lida no tempo\n",b); t=1; } b++; if (t==0){ x= receive_timeout(&status,b,5); if (x==0 && status==0) // printf("Mensagem recebido OK por Thread Final!!!!!\n"); if (x==0 && status==1){ // printf("Status negativo...\n"); // return; } if (x==1){ // printf("Mensagem nao recebido no tempo por Thread Final...\n"); } b = 0; } } } /** *@ifce Função de Criação de Threads * Criação de Threads */ void start_thread() { printf("\n\nStarting Mail Box Process....\n\n"); #if defined(_WIN32_) if ((V_compara [0]= _beginthread(thread_code_mail_box,4096, NULL))==(unsigned long)-1) #else if ((V_compara [0]= _beginthread(thread_code_mail_box,4096, NULL))== -1) #endif { printf("Unable to create thread Mail Box.\n"); return; } printf("Created thread Mail Box!!!\n"); printf("\n\nStarting Sends and Receives process.....\n\n"); Sleep (500); printf("\n\nBeginning Users Threads....\n\n"); Sleep (2000); #if defined(_WIN32_) if ((V_compara [1]= _beginthread(thread_code_a,4096, NULL))==(unsigned long)-1) #else if ((V_compara [1]= _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 [2]= _beginthread(thread_code_b,4096, NULL))==(unsigned long)-1) #else if ((V_compara [2]= _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 [3]= _beginthread(thread_code_c,4096, NULL))==(unsigned long)-1) #else if ((V_compara [3]= _beginthread(thread_code_c,4096, NULL))== -1) #endif { printf("Unable to create thread c.\n"); return; } printf("Created thread c.\n"); #if defined(_WIN32_) if ((V_compara [4]= _beginthread(thread_code_d,4096, NULL))==(unsigned long)-1) #else if ((V_compara [4]= _beginthread(thread_code_d,4096, NULL))== -1) #endif { printf("Unable to create thread d.\n"); return; } printf("Created thread d.\n"); #if defined(_WIN32_) if ((V_compara [5]= _beginthread(thread_code_e,4096, NULL))==(unsigned long)-1) #else if ((V_compara [5]= _beginthread(thread_code_e,4096, NULL))== -1) #endif { printf("Unable to create thread e.\n"); return; } printf("Created thread e.\n"); #if defined(_WIN32_) if ((V_compara [6]= _beginthread(thread_code_f,4096, NULL))==(unsigned long)-1) #else if ((V_compara [6]= _beginthread(thread_code_f,4096, NULL))== -1) #endif { printf("Unable to create thread f.\n"); return; } printf("Created thread f.\n"); #if defined(_WIN32_) if ((V_compara [7]= _beginthread(thread_code_g,4096, NULL))==(unsigned long)-1) #else if ((V_compara [7]= _beginthread(thread_code_g,4096, NULL))== -1) #endif { printf("Unable to create thread g.\n"); return; } printf("Created thread g.\n"); #if defined(_WIN32_) if ((V_compara [8]= _beginthread(thread_code_h,4096, NULL))==(unsigned long)-1) #else if ((V_compara [8]= _beginthread(thread_code_h,4096, NULL))== -1) #endif { printf("Unable to create thread h.\n"); return; } printf("Created thread h.\n"); #if defined(_WIN32_) if ((V_compara [9]= _beginthread(thread_code_i,4096, NULL))==(unsigned long)-1) #else if ((V_compara [9]= _beginthread(thread_code_i,4096, NULL))== -1) #endif { printf("Unable to create thread i.\n"); return; } printf("Created thread i.\n"); #if defined(_WIN32_) if ((V_compara [10]= _beginthread(thread_code_j,4096, NULL))==(unsigned long)-1) #else if ((V_compara [10]= _beginthread(thread_code_j,4096, NULL))== -1) #endif { printf("Unable to create thread j.\n"); return; } printf("Created thread j.\n"); Sleep (3000); flag_inicio_thread = 1; } /** *@fn Programa principal_main() *@brief Programa principal Main */ int main(void) { printf("------------------------------------------------------------------"); printf("\n\n PROGRAM MAIL BOX\n\n"); printf("------------------------------------------------------------------\n\n\n\n"); printf("IIIIIIIII FFFFFFFFF CCCCCCCCC EEEEEEEEE\n"); printf("IIIIIIIII FFFFFFFFF CCCCCCCCC EEEEEEEEE\n"); printf(" III FFF CCC EEE\n"); printf(" III FFF CCC EEE\n"); printf(" III FFFFFFF CCC EEEEEEE\n"); printf(" III FFFFFFF CCC EEEEEEE\n"); printf(" III FFF CCC EEE\n"); printf(" III FFF CCC EEE\n"); printf("IIIIIIIII FFF CCCCCCCCC EEEEEEEEE\n"); printf("IIIIIIIII FFF CCCCCCCCC EEEEEEEEE\n"); Sleep (1000); start_thread(); getchar(); return 0; }
Compartilhar