int main(int argc, char* argv[]) { pid_t pid; int status, i; int ans; int init_host, quantity; char *pch; int total_response = 0; struct str_addr addr; if(argc != 3){ printf("error arguments\n"); printf("./a.out <network> \n"); printf("%s 192.168.0.10 \n", argv[0]); exit(1); } pch=strrchr(argv[1],'.'); //Busca el ultimo punto y *pch = '\0'; //lo reemplaza por el caracter nulo init_host = atoi(pch+1); strcpy(addr.network, argv[1]); quantity = atoi(argv[2]); printf("network %s\n", addr.network); printf("host %d\n\n", init_host); for(i=0; i<quantity; i++){ pid =fork(); if (pid==0){ printf("Child running %d\n", init_host+i); addr.host = init_host+i; ans = exec_ping(&addr); exit(ans); } } while(wait(&status)>0){ //if(WEXITSTATUS(status)) // printf("Host %s.%d no response\n",addr.network, WEXITSTATUS(status)); if(WEXITSTATUS(status) == 0) total_response++; }; printf("\nTotal response =%d%%\n", (int)(total_response*100/quantity)); printf("Total no response =%d%%\n", (int)((quantity-total_response)*100/quantity)); printf("End Parent\n"); }
int main(int argc, char* argv[]) { //pid_t pid; int status, i,j, responden=0, porcentaje=0; int ans; int init_host, quantity; char *pch; struct str_addr addr; if(argc != 3){ printf("error arguments\n"); printf("./a.out <network> <quantity> \n"); printf("%s 192.168.0.10 5\n", argv[0]); exit(1); } pch=strrchr(argv[1],'.'); //Busca el ultimo punto y *pch = '\0'; //lo reemplaza por el caracter nulo init_host = atoi(pch+1); strcpy(addr.network, argv[1]); quantity = atoi(argv[2]); printf("network %s\n", addr.network); printf("host %d\n\n", init_host); for(i=0; i<quantity; i++){ if(fork()==0){ printf("running host %d\n", init_host+i); addr.host = init_host+i; ans = exec_ping(&addr); exit(ans); } } for(j=0; j<quantity; j++){ wait(&status); if(status==0){ responden++; } } porcentaje=(responden*100)/quantity; printf("Responden: %d Porcentaje: %d %c\n", responden,porcentaje, '%' ); printf("No responden: %d Porcentaje: %d %c\n", quantity-responden, 100-porcentaje, '%'); }
void func(int host,struct str_addr addr){ printf("running host %d\n",host); addr.host = host; ans = exec_ping(&addr); }