Ejemplo n.º 1
0
int main (int argc, char** argv) {
    uthread_t t[4];
    
    uthread_init (4);
    
    // TODO: Create Threads and Join
    struct Pool* p = createPool();
    
    for (int i = 0; i < NUM_PRODUCERS; i++) {
        t[i] = uthread_create(producer,p);
    }
    for (int j = NUM_PRODUCERS; j < NUM_PRODUCERS + NUM_CONSUMERS; j++) {
        t[j] = uthread_create(consumer,p);
    }
    for (int z = 0; z < NUM_PRODUCERS + NUM_CONSUMERS; z++) {
        uthread_join(t[z], NULL);
    }
    
    printf ("producer_wait_count=%d, consumer_wait_count=%d\n", producer_wait_count, consumer_wait_count);
    printf ("items value histogram:\n");
    int sum=0;
    for (int i = 0; i <= MAX_ITEMS; i++) {
        printf ("  items=%d, %d times\n", i, histogram [i]);
        sum += histogram [i];
    }
    assert (sum == sizeof (t) / sizeof (uthread_t) * NUM_ITERATIONS);
}
Ejemplo n.º 2
0
int main (int argc, char** argv) {
  // TODO create threads to run the producers and consumers
  spinlock_create(&lock);
  uthread_init(4);
  uthread_t producerThread1;
  uthread_t producerThread2;
  uthread_t consumerThread1;
  uthread_t consumerThread2;
  producerThread1 = uthread_create(producer, 0);
  producerThread2 = uthread_create(producer, 0);
  consumerThread1 = uthread_create(consumer, 0);
  consumerThread2 = uthread_create(consumer, 0);
  uthread_join(producerThread1, 0);
  uthread_join(producerThread2, 0);
  uthread_join(consumerThread1, 0);
  uthread_join(consumerThread2, 0);
  printf("Producer wait: %d\nConsumer wait: %d\n",
         producer_wait_count, consumer_wait_count);
  for(int i=0;i<MAX_ITEMS+1;i++)
    printf("items %d count %d\n", i, histogram[i]);
/*   uthread_join(producerThread1, 0);
  uthread_join(producerThread2, 0);
  uthread_join(consumerThread1, 0);
  uthread_join(consumerThread2, 0); */
}
Ejemplo n.º 3
0
int
main ()
{
  int i = 0;

  uthread_init ();

  uthread_create (threadFunc, 1, 0);
  uthread_create (threadFunc, 2, 0);
  uthread_create (threadFunc, 3, 0);
  uthread_create (threadFunc, 4, 0);
  //uthread_create (threadFunc, 5, 5);
  //uthread_create (threadFunc, 6, 3);
  //uthread_create (threadFunc, 7, 3);
  //uthread_create (threadFunc, 8, 2);

  // uncomment to test uthread_exit() on main earlier than others
  //uthread_exit();

  while( 1 ) printf("Waiting for interrupt.\n");

  for (i = 0; i < 10; i++)
    {
      printf ("This is the main function\n");
      uthread_yield ();
    }
  printf ("I am main and I am exiting\n");

  uthread_exit ();

  printf ("You shall not reach this line\n");
  return 0;
}
int main() {
    // initialized to four processors
    uthread_init(4);
    
    // semaphores
    mutex = uthread_semaphore_create (1);
    empty = uthread_semaphore_create (0);
    full  = uthread_semaphore_create (MAX_ITEMS);
    
    // create one thread for each producer and one thread for each consumer
    void* arg = (void*) NUM_ITERATIONS;
    uthread_t* producer_thread1 = uthread_create(producer, arg);
    uthread_t* producer_thread2 = uthread_create(producer, arg);
    uthread_t* consumer_thread1 = uthread_create(consumer, arg);
    uthread_t* consumer_thread2 = uthread_create(consumer, arg);
    
    // the main thread must wait for the producer and consumer threads to finish before it exits
    uthread_join(producer_thread1);
    uthread_join(producer_thread2);
    uthread_join(consumer_thread1);
    uthread_join(consumer_thread2);
    
    
    // TESTING AREA
    // the shared resources pool must be 0 when the program terminates, indicating that all produced items were consumed
    printf ("The Pool: %d\n", items);
    int total = 0;
    for (int i = 0; i < MAX_ITEMS+1; i++) {
        total += histogram[i];
        printf ("Histogram: %d\n", histogram[i]);
    }
    printf ("Total changs to items: %d\n", total);
}
Ejemplo n.º 5
0
int main (int argc, char** argv) {
  assert (NUM_PRODUCERS == NUM_CONSUMERS);

  spinlock_create(&item_lock);
  spinlock_create(&producer_wait_count_lock);
  spinlock_create(&consumer_wait_count_lock);

  uthread_init(NUM_PRODUCERS + NUM_CONSUMERS);
  uthread_t producers[NUM_PRODUCERS];
  uthread_t consumers[NUM_CONSUMERS];

  for (int i = 0; i < NUM_PRODUCERS; i++) {
    consumers[i] = uthread_create(consumer, NULL);
    producers[i] = uthread_create(producer, NULL);
  }

  for (int i = 0; i < NUM_PRODUCERS; i++) {
    uthread_join(consumers[i], NULL);
    uthread_join(producers[i], NULL);
  }

  printf("Producer Wait Time: %d\t\tConsumer Wait Time: %d\n",
         producer_wait_count, consumer_wait_count);

  printf("Histogram: [ ");
  int sum = 0;
  for (int i = 0; i < MAX_ITEMS + 1; i++) {
    sum += histogram[i];
    printf("%d ", histogram[i]);
  }
  printf("]\tSum: %d\n", sum);
}
Ejemplo n.º 6
0
int main(void)
{      
    int tid[3];
    int val[3];
    libuthread_init();    
   

    tid[0] = uthread_create(func1,&val[0]);
    tid[1] = uthread_create(func1,&val[1]);
    tid[2] = uthread_create(func1,&val[2]);

    //val contem o tid do thread com o qual deve ser sincronizado
    val[0]=tid[1];
    val[1]=tid[2];
    val[2]=tid[0];

    //tidglobal contem o inverso do 'val', o tid do proc. que fez a sincronização
    tidglobal[tid[0]]=tid[2];
    tidglobal[tid[1]]=tid[0];
    tidglobal[tid[2]]=tid[1];


    if (uthread_join(tid[0])==-1)
        handle_error("Erro de sincronização");
    
    
    printf("main: exiting\n");
    return 0;
    
}
Ejemplo n.º 7
0
int main(int argc, char *argv[]) {
    uthread_t a, b;

    uthread_create(&a, thread, NULL);
    uthread_create(&b, thread, NULL);

    printf("%d%d%d%d\n", uthread_equal(a, a), uthread_equal(a, b), uthread_equal(b, a), uthread_equal(b, b));

    return 0;
}
Ejemplo n.º 8
0
int main (int* argc, char** argv) {
  uthread_t *ping_thread, *pong_thread;
  int i;
  uthread_init (2);
  ping_thread = uthread_create (ping, 0);
  pong_thread = uthread_create (pong, 0);
  uthread_join (ping_thread);
  uthread_join (pong_thread);
  printf ("\n");
}
Ejemplo n.º 9
0
int
main(int argc, char *argv[])
{
uthread_init();
int stam = 10;
int stam1 = 12;
int stam2 = 20;
uthread_create(tester,&stam);
uthread_create(tester,&stam1);
uthread_create(tester,&stam2);
uthread_exit();
return 0;
}
Ejemplo n.º 10
0
int main() {
	utid1 = uthread_create(agent1);
	utid2 = uthread_create(agent2);
	printf("preparing to run concurrent threads ...\n");
	uthread_runall();
	for (int i = 0; i < 20; ++i) {
		printf("hello from main thread, i = %d\n", i);
		for (int j = 0; j < 100000000; ++j)
				;
	}
	printf("back to main and all clear!\n");
	return 0;
}
Ejemplo n.º 11
0
void schedule_test()
{
    schedule_t s;
    
    int id1 = uthread_create(s,func3,&s);
    int id2 = uthread_create(s,func2,&s);
    
    while(!schedule_finished(s)){
        uthread_resume(s,id2);
        uthread_resume(s,id1);
    }
    puts("main over");

}
Ejemplo n.º 12
0
int main()
{
    stack1 = (char*)malloc(4096);
    stack2 = (char*)malloc(4096);
    /*
    if use ucontext version
    char dummy_stack[4096];
    uthread_t p = uthread_create(NULL,dummy_stack,0,NULL);
    */
    uthread_t p = uthread_create(NULL,NULL,0,NULL);
    uthread_t u = uthread_create(p,stack1,4096,ufun1);
    uthread_switch(p,u,u);
    printf("main end\n");
    return 0;
};
Ejemplo n.º 13
0
int main() {
    system_init(); 
    uthread_create(do_something,2);
	uthread_exit();

    return 0;
}
Ejemplo n.º 14
0
int main( int argc, char* argv[] )
{
	int thread_id;
	int status;

	test_name = "multi-exit";

	msg ("begin");

	thread_id = uthread_create( child_thread, NULL );
	if( -1 == thread_id )
	{
		fail( "Test creation failed. Test fail" );
	}

	// we will now wait for our child
	if( -1 == uthread_join( thread_id, &status ) )
	{
		fail( "Thread join failed. Test fail" );
	}

	if( CHILD_EXIT_CODE != status )
	{
		fail( "Exit code wasn't sent properly" );
	}

	msg ("end");

	return 0;
}
Ejemplo n.º 15
0
//-----------------------------------PART 3
int main(){
   uthread_init();
   
   
    struct binary_semaphore semaphore;
    binary_semaphore_init(&semaphore, 1); 
    

    uthread_create(&foo, &semaphore);
    uthread_create(&foo, &semaphore);
//     uthread_join(2);
    
    uthread_exit();
    
    exit();
}
Ejemplo n.º 16
0
int main( int argc, char* argv[] )
{
    int thread_id[NO_OF_THREADS_TO_CREATE];
    int status;
    int i;

    test_name = "multi-tid";

    msg ("begin");

    for( i = 0; i < NO_OF_THREADS_TO_CREATE; ++i )
    {
        thread_id[i] = uthread_create( child_thread, &thread_id[i] );
        if( -1 == thread_id )
        {
            fail( "Test creation failed. Test fail" );
        }
    }

    for( i = 1; i < NO_OF_THREADS_TO_CREATE; ++i )
    {
        if( thread_id[i] != thread_id[i-1] + 1 )
        {
            fail( "TID's should be consecutive" );
        }
    }

    uthread_joinall();

    msg( "end" );


    return 0;
}
void* parent(void* n)
{	
	int i = *(int*)n;

	printf("--Entered parent thread--\n");

	int m = 2 * i;

	int child_id = uthread_create(proc3, &m);

	if(id3 != -1)
	{
		printf("--Created child thread--\n");
	}
	else
	{
		printf("--Creation of child thread failed--\n");
	}

	for(; i > 0; i--)
	{
		printf("Parent: %d\n", i);
		uthread_yield();
	}

	return NULL;
}
Ejemplo n.º 18
0
/*
 * Create N threads which spawn M threads (default 1x1)
 */
int main(int argc, char *argv[])
{
	int n, m;
	int rc;
	void *prev_tid = NULL, *last_tid = NULL;

	if (argc < 3)
		n = m = 1;
	else {
		n = atoi(argv[1]);
		m = atoi(argv[2]);
	}

	for (int i = 0; i < n; ++i) {
		last_tid = uthread_create(def_attrs, fun_with_threads, &m);

		if (last_tid) {
			printf("created thread: %p\n", last_tid);
			prev_tid = last_tid;
		} else {
			printf("out of memory, exiting\n");
			break;
		}
	}

	rc = uthread_joinall();
	printf("join returned: %d\n", rc);

	return EXIT_SUCCESS;
}
Ejemplo n.º 19
0
int main (int argc, char** argv) {
  // setup mutex and condition variables
  mx = uthread_mutex_create();
  smoker_waiting = uthread_cond_create(mx);
  for (int i = 0; i < 3; i++) {
    resource[i] = uthread_cond_create(mx);
  }
  done_smoking = uthread_cond_create(mx);

  // setup threads and start agent
  uthread_init(1);
  uthread_mutex_lock(mx);
  for (int i = 0; i < 3; i++) {
    threads[i] = uthread_create(smoker, (void*)(intptr_t) i);
    uthread_cond_wait(smoker_waiting);
  }
  uthread_mutex_unlock(mx);
  threads[3] = uthread_create(agent, NULL);

  // wait for threads to finish
  for (int i = 3; i >= 0; i--) {
    uthread_join(threads[i], NULL);
  }

  // verify that the histograms of actual/expected match
  for (int i = 0; i < 3; i++) {
    assert(histo_expected[i] == histo_actual[i]);
  }

  // print histograms of expected and actual smoke counts
  int sum;
  printf("Expected: [ ");
  sum = 0;
  for (int i = 0; i < 3; i++) {
    sum += histo_expected[i];
    printf("%d ", histo_expected[i]);
  }
  printf("]\tSum: %d\n", sum);

  sum = 0;
  printf("Actual:   [ ");
  for (int i = 0; i < 3; i++) {
    sum += histo_actual[i];
    printf("%d ", histo_actual[i]);
  }
  printf("]\tSum: %d\n", sum);
}
Ejemplo n.º 20
0
int main(void)
{      
    int arg=0, tid1, tid2;
    libuthread_init();    
    
    tid1 = uthread_create(func1,&arg);
    tid2 = uthread_create(func2,&arg);

    if (tid1==-1 )
	handle_error("Erro ao criar thread");
    
    uthread_join(tid1);
    uthread_join(tid2);
        
    printf("main: exiting\n");
    return 0;
}
Ejemplo n.º 21
0
int main(int argc, char* argv[])
{
	if(argc < 2)
        {
		printf("Please enter a positive integer as argument.\n");
		return 0;
	}

        int n = atoi(argv[1]);

        if(n < 0)
        {
		printf("Negative argument. Please enter a positive integer as argument.\n");
		return 0;
	}

	uthread_init();
	id1 = uthread_create(proc1, &n);

	if(id1 != -1)
	{
		printf("--Created thread 1--\n");
	}
	else
	{
		printf("--Creation of thread 1 failed--\n");
	}

	id2 = uthread_create(proc2, &n);

	if(id2 != -1)
	{
		printf("--Created thread 2--\n");
	}
	else
	{
		printf("--Creation of thread 2 failed--\n");
	}

	uthread_join(id1);
	uthread_join(id2);

	printf("--End of main--\n");

	return 0;
}
Ejemplo n.º 22
0
int main()
{
  int i;
  system_init();
  uthread_create(do_something);
  uthread_exit();
//  sleep(5);
  return 0;
}
Ejemplo n.º 23
0
int
main(int ac, char **av)
{
    int	i;

    uthread_init();
    
    uthread_mtx_init(&mtx1);
    uthread_mtx_init(&mtx2);
    uthread_mtx_init(&mtx3);

    uthread_cond_init(&cond);
    
    void (*tester[3]) (long a0, void* a1);
    tester[0] = tester1;
    tester[1] = tester2;
    tester[2] = tester3;

    for (i = 0; i < NUM_THREADS; i++)
    {
        
        uthread_create(&thr[i], tester[i], i, NULL,  UTH_MAXPRIO - i % UTH_MAXPRIO
                                        //2
                                        );
    }

    //uthread_setprio(thr[0], 6);

    for (i = 0; i < NUM_THREADS; i++)
    {
        char pbuffer[SBUFSZ];
        int	tmp, ret;

        uthread_join(thr[i], &tmp);
    
        sprintf(pbuffer, "joined with thread %i, exited %i.\n", thr[i], tmp);  
        ret = write(STDOUT_FILENO, pbuffer, strlen(pbuffer));
        if (ret < 0) 
        {
            perror("uthreads_test");
            return EXIT_FAILURE;
        }   

        /* 
        uthread_mtx_lock(&mtx);
        uthread_cond_signal(&cond);
        uthread_mtx_unlock(&mtx);
        */
        
    }
    printf("Main ends\n");
    uthread_exit(0);

    return 0;

}
Ejemplo n.º 24
0
void do_something()
{
    int id;

    id=myid;
    myid++;

    printf("This is ult %d\n", id);
    if(n_threads<10){
        uthread_create(do_something,2);
        n_threads++;
        uthread_create(do_something,2);
        n_threads++;
    }
    printf("This is ult %d again\n",id);
    uthread_yield(1);
    printf("This is ult %d once more\n",id);
	uthread_exit();
}
Ejemplo n.º 25
0
/* Main: Compute ((1 + 2) - (3 + 4)) + 7 with threads. */
int 
main ( int* argc, char** argv ) 
{
		uthread_init( 2 );

		/* Create structs for (1 + 2) and (3 + 4). */
		struct Triple *t1 = (struct Triple*) malloc( sizeof( struct Triple ) );
		t1->arg0 = 1;
		t1->arg1 = 2;
		struct Triple *t2 = (struct Triple*) malloc( sizeof( struct Triple ) );
		t2->arg0 = 3;
		t2->arg1 = 4;

		/* Create threads for (1 + 2) and (3 + 4). */
		uthread_t *t1_thread, *t2_thread;
		t1_thread = uthread_create( add, (void*) t1 );
		t2_thread = uthread_create( add, (void*) t2 );

		/* Execute (1 + 2) and (3 + 4) and store results. */
		int r1 = *( (int *) uthread_join( t1_thread ) );
		int r2 = *( (int *) uthread_join( t2_thread ) );

		/* Create struct for (r1 - r2). */
		struct Triple *t3 = (struct Triple*) malloc( sizeof( struct Triple ) );
		t3->arg0 = r1;
		t3->arg1 = r2;

		/* Create thread for (r1 - r2). */
		uthread_t *t3_thread = uthread_create ( sub, (void *) t3 );
		int r3 = *( (int *) uthread_join( t3_thread ) );

		/* Create struct for (r3 + 7). */
		struct Triple *t4 = (struct Triple*) malloc( sizeof( struct Triple ) );
		t4->arg0 = r3;
		t4->arg1 = 7;

		/* Create thread for (r3 + 7). */
		uthread_t *t4_thread = uthread_create ( add, (void *) t4 );
		int r4 = *( (int *) uthread_join( t4_thread ) );
		
		printf( "%d\n", r4 );
}
Ejemplo n.º 26
0
int dcc_tcp_start(armice_ctrl_t * ctrl)
{
	int th;

	th = uthread_create(dcc_read_stack, sizeof(dcc_read_stack), 
						(uthread_task_t)dcc_tcp_read_task, 
						(void *)ctrl, 0, NULL); 

	printf("<%d> ", th);

	return 0;
}
Ejemplo n.º 27
0
Archivo: fssp.c Proyecto: bitc/2
int
main(int argc, char *argv[])
{
  int i;

  if(argc != 2) {
    printf(2, "You must pass the number of soldiers\n");
    exit();
  }

  n = atoi(argv[1]);

  if(n < 1) {
    printf(2, "Number of soldiers must be positive\n");
    exit();
  }

  uthread_init();

  for(i = 0; i < n; ++i) {
    binary_semaphore_init(&soldier_ready[i], 0);
    binary_semaphore_init(&soldier_waiting[i], 0);
    binary_semaphore_init(&soldier_prepared[i], 0);
    binary_semaphore_init(&soldier_update[i], 0);
  }

  for(i = 0; i < n; ++i) {
    uthread_create(run_soldier, (void*)i);
  }

  for(;;) {
    for(i = 0; i < n; ++i) {
      binary_semaphore_down(&soldier_ready[i]);
    }

    print_cells();
    if(all_firing()) {
      exit();
    }

    for(i = 0; i < n; ++i) {
      binary_semaphore_up(&soldier_waiting[i]);
    }

    for(i = 0; i < n; ++i) {
      binary_semaphore_down(&soldier_prepared[i]);
    }

    for(i = 0; i < n; ++i) {
      binary_semaphore_up(&soldier_update[i]);
    }
  }
}
Ejemplo n.º 28
0
int main(int argc, char **argv) {
  printf("Testing uthread_create\n");
	    
  uthread_init();
    
  uthread_create(thread_start, 1, 0); 
      
  uthread_yield();
  printf("back in main\n");
  uthread_exit();
  return 0;
}
Ejemplo n.º 29
0
int main (int argc, char** argv) {
  uthread_init (7);
  struct Agent*  a = createAgent();
  // TODO
  uthread_join (uthread_create (agent, a), 0);
  assert (signal_count [MATCH]   == smoke_count [MATCH]);
  assert (signal_count [PAPER]   == smoke_count [PAPER]);
  assert (signal_count [TOBACCO] == smoke_count [TOBACCO]);
  assert (smoke_count [MATCH] + smoke_count [PAPER] + smoke_count [TOBACCO] == NUM_ITERATIONS);
  printf ("Smoke counts: %d matches, %d paper, %d tobacco\n",
          smoke_count [MATCH], smoke_count [PAPER], smoke_count [TOBACCO]);
}
Ejemplo n.º 30
0
int main()
{
	uthread_arg_t *uarg;
	int inx, i ,j;


	gtthread_app_init();

	init_matrices();
//	print_matrix(&A);
//	print_matrix(&B);
//	print_matrix(&C);
	gettimeofday(&tv1,NULL);
	j = 0;
	for (i=0; i<4; ++i) 
	{
		for(inx=0; inx<NUM_THREADS; inx++)
		{
			uarg = &uargs[inx];
			uarg->_A = &A[i];
			uarg->_B = &B[i];
			uarg->_C = &C[i];

			uarg->tid = inx;

			uarg->gid = i;  // Every matrix in a different group
                        uarg->matrix_size = sizes[i];

			uarg->start_row = (inx * sizes[i]/NUM_THREADS);
#ifdef GT_GROUP_SPLIT
		/* Wanted to split the columns by groups !!! */
			uarg->start_col = (uarg->gid * PER_GROUP_COLS);
#endif

			uthread_create(&utids[j], uthread_mulmat, uarg, uarg->gid);
			j++;
		}
	}
//	for(i=0; i < 100000; ++i) {
//		for(j=0; j<9000; ++j) {
//
//              }
//        }

	gtthread_app_exit();

//	print_matrix(&A);
//	print_matrix(&B);
//	print_matrix(&C);
	// fprintf(stderr, "********************************");
	return(0);
}