13/04/2016, 02:39 AM
Aprender shell en 1 dia, nivel trabajo de universidad
Código PHP:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
#define USLEEP_TIME 1; // 1 microsegundo por defecto
int main(int argc, char *argv[])
{
if(argc < 2) // cantidad de argumentos obligatorios
{
printf("Uso: %s iters [usecs]\n", argv[0]); // comando invocador
exit(EXIT_SUCCESS);
}
u_int iters = atoi(argv[1]); // atoi - string a int
struct rusage r_usage;
u_int usecs = (argc > 2) ? atoi(argv[2]) : USLEEP_TIME; // si denoto un 2do argumento, tomarlo en cuenta
while(iters--) // mientras haya por iterar...
{
usleep(usecs); // :D
}
getrusage(RUSAGE_SELF, &r_usage); // la parte importante
printf("%ld\n", r_usage.ru_nvcsw); // no le demos mucha pega al shell
exit(EXIT_SUCCESS);
}
Código PHP:
#!/bin/bash
echo "### ERROR DE MEDICION DE CAMBIOS DE CONTEXTO VOLUNTARIOS ###"
ctxt_iters=10000
run_command="./ctxt $ctxt_iters" # feel free to edit
if [ -z "$1" ] # primer argumento invalido?
then
max=20 # valor de iteraciones por defecto (y recomendado): 20
else
max=$1
fi
echo "Iteraciones: $max"
ctxt_sum=0
i=1
echo "Ejecutando comando:"
echo -e "\t"$run_command
while (($i <= $max))
do
ctxt_output=$(eval $run_command) # transforma de texto a codigo directo, a la vez guardamos el output en ctxt_output
ctxt_sum=$(($ctxt_output + $ctxt_sum))
echo -e "\tIteracion $i, output: $ctxt_output" # parametro -e para permitir escape de caracteres (permitir la tabulacion)
((i++))
done
promedio=$(echo "scale=2;$ctxt_iters - ($ctxt_sum / $max)" | bc -l) # man bc para mas informacion, el ejemplo esta explicito (scale=2 para los decimales)
echo "### RESULTADO ###"
echo "Margen de Error de Medicion: $promedio"