Exemplo n.º 1
0
int main() {
    was_preempted = malloc(sizeof(int));
    *was_preempted = 1;
    srand(MAGIC); // use constant rather than time(NULL) to make program deterministic

    /************************************/
    /********** BASIC SETTINGS **********/
    /************************************/
    idle_threads = 0;
    int bongo_sorts = 5;
    use_priorities = 1; // affect only round_robin scheduler
    void* scheduler = round_robin;
    /************************************/

    fprintf(stderr,"Process Id: %d\n", (int)getpid());
    signal_stack = malloc(STACKSIZE);
    if (signal_stack == NULL) {
        perror("malloc");
        exit(1);
    }

    make_scheduler_context(scheduler);

    int i;
    for(i = 0; i < idle_threads; ++i) { // make idle contexts
        mkcontext(&contexts[i], idle_thread);
        deadcons[i] = 0;
        priorities[i] = 1;
    }
    for(; i < bongo_sorts + idle_threads; ++i) { // make bongo contexts
        mkcontext(&contexts[i], bongo_sort);
        deadcons[i] = 0;
        priorities[i] = 3; // set is as you want
    }
    for(; i < NUMCONTEXTS; ++i) { // make fibonacci contexts
        mkcontext_with_arg(&contexts[i], naive_fibonacci, FIB);
        deadcons[i] = 0;
        priorities[i] = 3;
    }

    setup_signals();

    cur_context = &contexts[0];
    swapcontext(&main_context, &signal_context);

    free(was_preempted);

    // THIS DOES NOT WORK GOOD!!! (use 'time program_name' to get real execution time)
    struct rusage usage;
    getrusage(RUSAGE_SELF, &usage);

    printf("Time spent (user mode): %d seconds, %d microseconds\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
    printf("Time spent (kernel mode): %d seconds, %d microseconds\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);

    return 0; /* make gcc happy */
}
Exemplo n.º 2
0
/**
 * Creates a new thread.
 *
 * Returns an int that points to the thread control block that is allocated to the thread.
 * Returns -1 and prints error message on failure.
 *
 */
int create_my_thread(char *threadname, void(*threadfunc)(), int stacksize) {
	/* Allocate the stack and set up the user context.
	 *
	 * The new thread should run the threadfunc when it starts.
	 *
	 * threadname is stored in the thread control block.
	 *
	 * A new thread is in the RUNNABLE  state when it is inserted into the system.
	 */

	// first check if we can create any more threads
	if (next_thread_index == (THREADS_MAX + 1)) {
		fprintf(stderr,
				"cannot create new thread, max number of threads reached: %d\n",
				THREADS_MAX);
		return -1;
	} else if (stacksize <= 0) {
		// make sure that stacksize is not 0 or less
		fprintf(stderr, "stacksize must be greater than zero!\n");
		return -1;
	}

	// malloc new thread entry
	thread_control_block_t* new_thread = (thread_control_block_t *) malloc(
			sizeof(thread_control_block_t *));

	// init thread stuff
	new_thread->run_time = 0;
	new_thread->total_time = 0;
	new_thread->stacksize = stacksize;
	new_thread->thread_name = (char *) malloc(
			sizeof(char) * strlen(threadname));
	strcpy(new_thread->thread_name, threadname); // copy name to make sure its not changed externally
	new_thread->state = RUNNABLE;
	new_thread->thread_id = next_thread_index;
	new_thread->threadfunc = threadfunc;

	new_thread->context = (ucontext_t *) malloc(sizeof(ucontext_t));

	// make new thread context
	if (mkcontext(new_thread->context, threadfunc, stacksize) != 0) {
		fprintf(stderr, "thread context creation failed");
		return -1;
	}

	// put in thread table
	threads[next_thread_index] = new_thread;

#if DEBUG == 1
	printf("new thread created: %s\n", threads[next_thread_index]->thread_name);
#endif

	next_thread_index++;

	return 0;
}
Exemplo n.º 3
0
	int
main()
{
	int i;
	struct itimerval it;

	fprintf(stderr,"Process Id: %d\n", (int)getpid());

	/* allocate the global signal/interrupt stack */
	signal_stack = malloc(STACKSIZE);
	if (signal_stack == NULL) {
		perror("malloc");
		exit(1);
	}

	/* make all our contexts */
	mkcontext(&contexts[0], (void*)thread2);
	for(i=1; i < NUMCONTEXTS; i++)
		mkcontext(&contexts[i], (void*)thread1);


	/* initialize the signal handlers */
	setup_signals();

	/* setup our timer */
	it.it_interval.tv_sec = 0;
	it.it_interval.tv_usec = INTERVAL * 1000;
	it.it_value = it.it_interval;
	if (setitimer(ITIMER_REAL, &it, NULL) ) perror("setitiimer");

	/* force a swap to the first context */
	cur_context = &contexts[0];
	setcontext(&contexts[0]);

	return 0; /* make gcc happy */
}