Exemplo n.º 1
0
/**
* Initializes a NodePtr and stores a JobPtr in it.
* @param data JobPtr represented by the new NodePtr
* @return New NodePtr
*/
NodePtr createNode(JobPtr data)
{
	NodePtr newNode = (NodePtr) buddy_malloc (sizeof(Node));
	newNode->next = NULL;
	newNode->prev = NULL;
	newNode->data = data;
	return newNode;
}
Exemplo n.º 2
0
/**
* Creates a ListPtr and returns it
* @returns New ListPtr
*/
ListPtr createList()
{
	ListPtr list;
	list = (ListPtr) buddy_malloc(sizeof(List));
	list->size = 0;
	list->head = NULL;
	list->tail = NULL;
	return list;
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
	if(isArgumentForShowingVersion(argv)) showVersionAndExit();
	char	   *buf;
	char       *bufCopy;
	pid_t	   pid = getpid();
	pid_t      backgroundJob;
	int		   status = 0;
	char       *prompt = getPrompt();
	int  	   bg;
	int        jobNumber = 1;
	int counts;
	int jumpSet = 0;
	char *arguments[2048];
	initialize();
	printf("\n");
	setPrompt(prompt);
	while ((buf = readline(prompt))) {
		if(strlen(buf) != 0) {
			bufCopy = buddy_malloc((strlen(buf)+1)*sizeof(char));
			strcpy(bufCopy, buf);
			add_history(bufCopy);
		}
		else
		{
			 printFinishedJobs(jobList);
			 free(buf);
			 continue;
		}
		free(buf);
		if(isCommandNotEmpty(bufCopy)) bg = isBackgroundCommand(bufCopy);
		getArgument(arguments, bufCopy, &counts);
		if(isExitCommand(bufCopy)){
			buddy_free(bufCopy);
			freeList(jobList);
			if(arguments != NULL){
				freeArguments(counts, arguments, NULL);
			}
			exit(0);
		}
		if(isCdCommand(bufCopy, arguments)){
			changeDir(arguments);
		    freeArguments(counts, arguments, bufCopy);		
		}
		else if(isJobsCommand(bufCopy)) {
			printList(jobList);
			freeArguments(counts, arguments, bufCopy);
		}
		else if(isFgCommand(bufCopy, arguments)){			
			fg(arguments, jobList, counts, bufCopy);
		}
		else if(isBgCommand(bufCopy, arguments)){
			bgS(arguments, jobList, counts, bufCopy);
		}
		else if (isCommandNotEmpty(bufCopy)) 
		{
			if(bg == 1)
			{
				backgroundJob = fork();
				if(backgroundJob == 0) {
					ignoreSignals();
					runCommand(arguments, bufCopy);
				}
				else{
					NodePtr n = createAndSetNode(&jobNumber, backgroundJob, bufCopy, bg);
					recordCommand(n, jobNumber, bg);
					freeArguments(counts, arguments, bufCopy);
				}
			}
			else
			{		
				pid = fork();
				if(pid == 0){
					runCommand(arguments, bufCopy);
				}
				else {
					NodePtr n = createAndSetNode(&jobNumber, pid, bufCopy, bg);
					ignoreSignals();
					if((waitpid(pid,&status,WUNTRACED))<0) err_sys("waitpid error");
					freeArguments(counts, arguments, bufCopy);
					restoreSignalsAndDetermineStatus(status, jobList, n);
				}
			}
		}
		else
		{
			freeArguments(counts, arguments, bufCopy);
		}
		if(jumpSet == 0){
			sigsetjmp(env, 1);
			jumpSet = 1;
		}
		continue;
	}
	if(buf == NULL) printf("\n");
	exit(0);
}
Exemplo n.º 4
0
void runRandomTests(int count, unsigned int seed, int n, ListPtr list)
{
	int i;
	int test;
	NodePtr node;
	JobPtr job;
	int *tests;

	tests = (int *) buddy_malloc(sizeof(int)*NUM_TESTS);
	for (i=0; i<NUM_TESTS; i++) 
		tests[i]=0;
   	srandom(seed);
    for (i=0; i<count; i++) {
		printf("\rRunning test #%d", i);
		test = (int) (NUM_TESTS * (double) rand()/RAND_MAX);
		tests[test]++;
        switch (test) {
            case 0:
				if (DEBUG > 1) fprintf(stderr,"addAtFront\n");
                n++;
                job = createJob(n, "some info", n);
                node = createNode(job);
                addAtFront(list, node);
                break;
            case 1:
				if (DEBUG > 1) fprintf(stderr,"addAtRear\n");
                n++;
                job = createJob(n, "some info", n);
                node = createNode(job);
                addAtRear(list, node);
                break;
            case 2:
				if (DEBUG > 1) fprintf(stderr,"removeFront\n");
                node = removeFront(list);
				freeNode(node);
                break;
            case 3:
				if (DEBUG > 1) fprintf(stderr,"removeRear\n");
                node = removeRear(list);
				freeNode(node);
                break;
            case 4:
				if (DEBUG > 1) fprintf(stderr,"removeNode\n");
                node = removeNode(list, search(list, i));
				freeNode(node);
                break;
            case 5:
				if (DEBUG > 1) fprintf(stderr,"reverseList\n");
                reverseList(list);
            case 6:
				if (DEBUG > 1) fprintf(stderr,"searchList\n");
                node = search(list, i);
				break;
            default:
                break;
        }
    }
	printf("\n");
	print_stats(tests);
	buddy_free(tests);
}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
	int i;
	char ch;
	int count;
	unsigned long int seed;
	size_t size;
	struct element x[MAX_ITEMS];
	int loc;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <num of tests> [random seed] [silent|terse|verbose|interactive]\n", argv[0]);
		exit(1);
	}
	count = atol(argv[1]);
	if (argc == 3) {
		seed = atol(argv[2]);
		srandom(seed);
	}
	if (argc == 4) {
		if (argv[3][0] == 's') {
			verbosity = SILENT;
		} else if (argv[3][0] == 't') {
			verbosity = TERSE;
		} else if (argv[3][0] == 'v') {
			verbosity = VERBOSE;
		} else if (argv[3][0] == 'i') {
			verbosity = INTERACTIVE;
			setvbuf(stdin, NULL, _IONBF, 0);
		}
	}

	if (verbosity > TERSE)
		system("clear");

	buddy_init(0);	
	if (verbosity > TERSE) {
		printf("Buddy system lists after initialization.\n");
		printBuddyLists();
	}
	if (verbosity == INTERACTIVE) {
		ch = getchar();
		system("clear");
		if (ch == 'q')
			exit(0); 
	}
	
	for (i =0; i < MAX_ITEMS; i++) {
		x[i].ptr = NULL;
		x[i].size = 0;
	}
	
	for (i=0; i < count; i++) {
		//printf("GL ");
		loc = (i) % MAX_ITEMS; // where to put in our table
		//printf("Trying(%d): ", i);
		if (x[loc].ptr) {
		//	printf("loc=%d ptr=%p size=%lu", loc, x[loc].ptr, x[loc].size);
			buddy_free(x[loc].ptr);
		//	printf(" Done");
			if (verbosity > SILENT) 
				printf("buddy_freed address %p of size %lu  in x[%d]\n", x[loc].ptr, x[loc].size, loc);
			if (verbosity > TERSE) printBuddyLists();
			x[loc].ptr = NULL;
			x[loc].size = 0;
		//	printf(" IfComplete ");
		} else {
			size = random() % MAX_REQUEST + 1; // how big a request
		//	printf("loc=%d ptr=%p size=%lu", loc, x[loc].ptr, size);
			x[loc].ptr = (char *) buddy_malloc(size*sizeof(char));
		//	printf(" Done");
			if (x[loc].ptr == NULL) {
				perror("TestBuddy:");
				exit(1);
			}
			x[loc].size = size*sizeof(char);
			memset(x[loc].ptr, '1', x[loc].size);
			if (verbosity > SILENT)
				printf("buddy_malloced %lu bytes and stored address %p at x[%d]\n", size*sizeof(char), x[loc].ptr, loc);
			if (verbosity > TERSE) printBuddyLists();

		//	printf(" IfComplete ");
		}
		//printf ("V");	
		if (verbosity == INTERACTIVE) {
			ch = getchar();
			system("clear");
			if (ch == 'q')
				exit(0); 
		}
		//printf("F\n");
		
	}
#ifdef STATS
	printBuddySystemStats();
#endif
	
	exit(0);	
}