Example #1
0
int main(int argc, char** argv) {
	int status, pid;
	argv++;
	struct timeval before;
	gettimeofday(&before, NULL);
	pid = fork();
	if(pid == 0) {
		int error = execvp(argv[0], argv);
		if(error == -1) {
			if(errno == ENOENT) {
					printf("Filename or path not found.\n");
					return errno;
			}
			else return errno;
		}
	}
	wait(&status);
	struct rusage usage;
	struct timeval after;
	gettimeofday(&after, NULL);
	getrusage(RUSAGE_CHILDREN, &usage);
	printf("\nChild %d died with status %d.\n\n", pid, status);
	long int cputime = (usage.ru_stime.tv_sec * 1000) + (usage.ru_stime.tv_usec / 1000);
	long int usertime = (usage.ru_utime.tv_sec * 1000) + (usage.ru_utime.tv_usec / 1000);
	long int realtime = ((after.tv_sec * 1000) + (after.tv_usec / 1000)) - ((before.tv_sec * 1000) + (before.tv_usec / 1000));
	printf("Process statistics: \n");
	printf("	Elapsed Time: %ld ms\n", realtime);
	printf("	CPU Time: %ld ms\n", cputime);
	printf("	User Time: %ld ms\n", usertime);
	printf("	Involuntary Context Switches: %ld\n", usage.ru_nivcsw);
	printf("	Voluntary Context Switches: %ld\n", usage.ru_nvcsw);
	printf("	Page Faults: %ld\n", usage.ru_majflt);
	printf("	Reclaimed Page Faults: %ld\n", usage.ru_minflt);
	struct processinfo* info = malloc(sizeof(struct processinfo));
	testCall2(info);
	printf("_________________________________\n");
	printf("|\tProcess statistics\t|\n");
	printf("|PID: %d\t\t\t|\n", info->pid);
	printf("|Parent PID: %d\t\t|\n", info->parent_pid);
	printf("|Youngest child PID: %d\t\t|\n", info->youngest_child);
	printf("|Oldest sibling PID: %d\t|\n", info->older_sibling);
	printf("|Start time: %lld\t|\n", info->start_time);
	printf("|User time: %lld\t\t\t|\n", info->user_time);
	printf("|System time: %lld\t\t\t|\n", info->sys_time);
	printf("|Child user time: %lld\t\t|\n", info->cutime);
	printf("|Child system time: %lld\t\t|\n", info->cstime);
	printf("_________________________________\n");
	free(info);
	return status;
}
Example #2
0
int main () {
	/*
	long test2;
	unsigned short target=0;
	int numSmited=0;
	int smitedPids[100]={0};
	long pidState[100]={0};

	printf("for target 0 The return values of the system calls are:\n");
	printf("\tcs3013_syscall1: %ld\n", testCall1());
	test2=testCall2(&target,&numSmited,smitedPids,pidState);
	printf("\tcs3013_syscall2: %ld\n", test2);
	if(test2==0){
		printf("\tcs3013_syscall3: %ld\n", testCall3(&numSmited,smitedPids,pidState));
	}
	else{
		printf("\ttestcall2 error\n");
	}
*/
	unsigned short target2=1001;
	int numSmited2=0;
	int smitedPids2[100];
	long pidState2[100];
	long test2;
	int index;
	//printf("\nfor target 1001 The return values of the system calls are:\n");
	//printf("\tcs3013_syscall1: %ld\n", testCall1());
	test2=testCall2(&target2,&numSmited2,smitedPids2,pidState2);
	//printf("\tcs3013_syscall2: %ld\n", test2);
	//printf("\tcs3013_syscall2: %ld\n", test2);
	if(test2!=0){
		printf("test2 error\n");
	}
	else{
		printf("%d ",numSmited2);
		for(index=0;index<numSmited2;index++){
			printf("%d %ld ",smitedPids2[index],pidState2[index]);
			//printf("at %d pid is %d, state is %ld\n",index,smitedPids2[index],pidState2[index]);
		}
	}
	// if(test2==0){
	// 	printf("\tcs3013_syscall3: %ld\n", testCall3(&numSmited2,smitedPids2,pidState2));
	// }else{
	// 	printf("\ttestcall2 error\n");
	// }

	
	return 0;
}