#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/times.h>
#include <time.h>
#include <stdlib.h>

void myexit(int ret);

char **m_argv = 0;
char umount_command[256];

int main(int argc,char *argv[])
{
	pid_t p,pr;
	int status,nprove;
	clock_t c2,c1;
	struct tms t2,t1;
	int i;
	
	if (argc < 3) {
		fprintf(stderr,"Uso: %s <numero di prove> <parametri della mount ...>\n",argv[0]);
		fprintf(stderr,"I <parametri della mount ...> devono cominciare con il filesystem da montare\n");
		myexit(5);
	}

	if (sscanf(argv[1],"%d",&nprove) != 1 || (int)nprove <= 0) {
		fprintf(stderr,"%s non va bene come <numero di prove>\n",argv[1]);
		myexit(5);
	}

/* argv e m_argv hanno (in questo caso) lo stesso numero di elementi */

	m_argv = malloc( sizeof(char *) * argc );
	m_argv[0] = "/bin/mount";
	for(i = 1 ; i < (argc - 1) ; i++ ) m_argv[i] = argv[i+1];
	m_argv[argc - 1] = NULL;

	for (i = 0; i < argc ; i++)
		printf("argv[%d] == %s\n", i , m_argv[i] ? m_argv[i] : "NULL" );

	for (i=0; i<nprove ; i++) {
		c1 = times(&t1);

		if ( (p = fork()) == -1) {
			perror("Errore sulla fork");
			myexit(5);
		}

		if ( !p ) { /* child */
			execv("/bin/mount",m_argv);
			perror("Errore"); /* exec return just in case of error */
			myexit(5);
		}
		/* parent */

		pr = wait(&status);
		if (pr == -1 ) {
			perror("wait");
			myexit(5);
		}

		c2 = times(&t2);

		if (WIFEXITED(status)) {
			printf("Codice di ritorno di child %d\n",WEXITSTATUS(status));
		}
		if (WIFSIGNALED(status)) {
			printf("Segnale che ha terminato child %d\n",WTERMSIG(status));
		}
		if (WIFSTOPPED(status)) {
			printf("Segnale che ha causato lo stop di child %d\n",WSTOPSIG(status));
		}

		printf("Real time: %d clk == %f secs\n",
			(int)(c2-c1),((int)(c2-c1))/((double)CLK_TCK));
		printf(" Parent User time: %d clk == %f secs\n",
			(int)(t2.tms_utime-t1.tms_utime),
			(t2.tms_utime-t1.tms_utime)/((double)CLK_TCK));
		printf(" Parent Sys  time: %d clk == %f secs\n",
			(int)(t2.tms_stime-t1.tms_stime),
			(t2.tms_stime-t1.tms_stime)/((double)CLK_TCK));
		printf(" Child  User time: %d clk == %f secs\n",
			(int)(t2.tms_cutime-t1.tms_cutime),
			(t2.tms_cutime-t1.tms_cutime)/((double)CLK_TCK));
		printf(" Child  Sys  time: %d clk == %f secs\n",
			(int)(t2.tms_cstime-t1.tms_cstime),
			(t2.tms_cstime-t1.tms_cstime)/((double)CLK_TCK));

		sprintf(umount_command,"/bin/umount %1.200s",argv[2]);
		if ( system(umount_command) == -1 ) {
			perror("system(umount)");
			myexit(5);
		}
	}

	myexit(0);
}


void myexit(int ret)
{
/* free the (eventually) allocated resources */
	if (m_argv !=  0) free(m_argv);
	exit(ret);
}

-- AntonioValletta - 30 Nov 2001


This topic: Sistemioperativi1 > TimeMount
Topic revision: r1 - 2001-11-30 - AntonioValletta
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback