Esempio n. 1
0
int main(int argc, char **argv)
{
  int counter0 = 0;
  int c1, c2;


  sthread_init();

  if (sthread_create(thread_1, (void*)0, 10) == NULL) {
    printf("sthread_create failed\n");
    exit(1);
  }

  if (sthread_create(thread_2, (void*)0, 1) == NULL) {
    printf("sthread_create failed\n");
    exit(1);
  }

  printf("created threads\n");

  for(; counter0 < 30000000; counter0++);

  c1 = counter1;
  c2 = counter2;
  printf("1: %i, 2: %i\n", c1, c2);
  printf("2/1: %f\n", ((float)c2)/c1);

  if (c2/c1 >= 6 && c2/c1 <= 14)
    printf("PASSED\n");

  return 0;
}
Esempio n. 2
0
int main(int argc, char **argv)
{
  int j;

   int count = argc > 1 ? atoi(argv[1]) : 100;
   printf("Testing time slice and mutexes, impl: %s\n",
      (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");

   sthread_init();

   if (sthread_create(thread_1, (void*)count, 1) == NULL) {
      printf("sthread_create failed\n");
      exit(1);
   }

   if (sthread_create(thread_2, (void*)count, 1) == NULL) {
      printf("sthread_create failed\n");
      exit(1);
   }
   printf("created two threads\n");

   printf("if this is the last line of output, time slices aren't working!\n");

   while(!t1_complete || !t2_complete);

   printf("PASSED\n");
   return 0;
}
Esempio n. 3
0
int main(int argc, char **argv)
{
	int checks;
	
	printf("Testing sthread_mutex_*, impl: %s\n",
		   (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
	sthread_init();
	
	mutex = sthread_mutex_init();
	sthread_mutex_lock(mutex);
	
	if (sthread_create(thread_start, (void*)1, 1) == NULL) {
		printf("sthread_create failed\n");
		exit(1);
	}
	
	/* Wait until the other thread has at least started,
	 * to give it a chance at getting through the mutex incorrectly. */
	while (ran_thread == 0) {
		sthread_yield();
	}

   sthread_dump();
	
	/* The other thread has run, but shouldn't have been
	 * able to affect counter (note that this is not a great test
	 * for preemptive scheduler, since the other thread's sequence
	 * is not atomic). */   
	assert(counter == 0);
	
	/* This should let the other thread run at some point. */
	sthread_mutex_unlock(mutex);
	
	/* Allow up to 100 checks in case the scheduler doesn't
	 * decide to run the other thread for a really long time. */
	checks = 100;
	while (checks > 0) {
		sthread_mutex_lock(mutex);
		if (counter != 0) {
			/* The other thread ran, got the lock,
			 * and incrmented the counter: test passes. */
			checks = -1;
		} else {
			checks--;
		}
		sthread_mutex_unlock(mutex);
		
		/* Nudge the scheduler to run the other thread: */
		sthread_yield();
	}
	
	if (checks == -1) {
		printf("sthread_mutex passed\n");
	} else {
		printf("*** sthread_mutex failed\n");
	}
	sthread_mutex_free(mutex);
	return 0;
}
Esempio n. 4
0
int main(int argc, char **argv)
{
  printf("Testing sthread_create, impl: %s\n",
	 (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
  sthread_init();
    
  if (sthread_create(thread1, (void*)1, 10) == NULL) {
    printf("sthread_create failed\n");
    exit(-1);
  }
    
  if (sthread_create(thread2, (void*)1, 10) == NULL) {
    printf("sthread_create failed\n");
    exit(-1);
  }

  printf("in main\n");
  sthread_sleep(10000);
  
  printf("\ntwo threads runqueue active in priority 10\n");
  sthread_dump();
  sthread_sleep(10000);

  printf("\ntwo threads runqueue active in priority 10\n");
  sthread_dump();

  printf("out main\n");

  return 0;
}
Esempio n. 5
0
int main(int argc, char **argv) {
  int arg = 1;
  printf("Testing sthread_mutex_*, impl: %s\n",
      (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
  
  sthread_init();

  mon = sthread_monitor_init();
  
  sthread_monitor_enter(mon);
  if (sthread_create(thread_start, (void*)&arg, 1) == NULL) {
    printf("sthread_create failed\n");
    exit(1);
  }
  printf("thread principal vai bloquear-se\n");
  sthread_monitor_wait(mon);
  printf("thread principal desbloqueada\n");
  i = 2;
  printf("i=2\n");
  sthread_monitor_exit(mon);
  if (i==2) {
    printf("\nSUCESSO!\n");
  } else {
    printf("\nteste falhado...\n");
  }
  return 1;
}
Esempio n. 6
0
int add_to_queue(struct sthread_t * new_task,int is_new)
{
	printf("adding to queue\n");
	block();
		if(head==NULL)  //this is the first thread
		{
			init(it);
			struct sthread_t * Main_task=(struct sthread_t *)malloc(sizeof(struct sthread_t) );
			sthread_init(Main_task);
			Main_task->s_id=-1;
			printf("done timer part\n");
			head=(struct queue *)malloc(sizeof(struct queue));
			tail=(struct queue *)malloc(sizeof(struct queue));
			tail->next=NULL;
			head->next=tail;
			head->task=Main_task;
			tail->task=new_task;
			new_task->s_id=id;
			add_to_running(new_task->s_id);
			currrent_thread=head->task;
			id++;
			count++;
			unblock();
			return 1;
		}
		else
		{
			if(count==MAX_THREAD)
			{
				unblock();
				return -1;
			}
			else
			{
				struct queue * temp=(struct queue *)malloc(sizeof(struct queue));
				temp->next=NULL;
				temp->task=new_task;
				if(is_new){
					new_task->s_id=id;
					id++;
					count++;
					add_to_running(new_task->s_id);
				}
				tail->next=temp;
				tail=temp;
				if(is_new==1)
				unblock();
				return 0;
			}
		}
}
Esempio n. 7
0
int main(int argc, char **argv)
{
  void *ret;
  int i;

  printf("Testing sthreads, impl: %s\n",
	 (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
  sthread_init();

  mon1 = sthread_monitor_init();
  mon2 = sthread_monitor_init();

    
  if (sthread_create(thread0, (void*)1, 10) == NULL) {
    printf("sthread_create failed\n");
    exit(-1);
  }

  sthread_monitor_enter(mon1);
  for (i = 0; i < NUMBER; i++){
    if ((thr[i] = sthread_create(thread1, (void*)i, 10)) == NULL) {
      printf("sthread_create failed\n");
      exit(-1);
    }
    sthread_yield();
  }
  for (i = 0; i < NUMBER; i++){
    sthread_monitor_wait(mon1);
  }
    
  printf("in main\n");
  
  sthread_monitor_exit(mon1);
  
  sthread_sleep(10000);

  sthread_monitor_enter(mon2);
  sthread_monitor_signalall(mon2);
  sthread_monitor_exit(mon2);
  
  for (i = 0; i < NUMBER; i++){
    sthread_join(thr[i], &ret);
  }
  printf("\nSUCCESS in creating %i threads\n", NUMBER);
  printf("out main\n");

  return 0;
}
Esempio n. 8
0
int main(int argc, char **argv) {
    sthread_t thread = NULL;
    int * res = (int *) malloc(sizeof(int));

    printf("Testing sthread_mutex_*, impl: %s\n",
           (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");

    sthread_init();
    if ((thread = sthread_create(thread_start, (void *) NULL, 1)) == NULL) {
        printf("sthread_create failed\n");
        exit(1);
    }
    sthread_join(thread, (void**)&res);
    printf("\nteste concluido: consultar dump*.tsv\n");
    return 1;
}
Esempio n. 9
0
int main(int argc, char **argv)
{
  int i;

  printf("Testing sthread_sleep, impl: %s\n",
	 (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
  
  sthread_init();
  
  for (i = 1; i <= 15; i++){
    sthread_sleep(1000000);
    printf("elapsed %i secs\n", i);
  }

  return 0;
}
Esempio n. 10
0
int main(int argc, char **argv)
{
  sthread_t thr;
  void *ret;
  int i;

	printf("Testing sthread_join, impl: %s\n",
		   (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
	sthread_init();
    
	if ((thr = sthread_create(thread_start, (void*)1, 1)) == NULL) {
		exit(-1);
	}
    
	sthread_join(thr, &ret);
	printf("back in main\n");
	return 0;
}
Esempio n. 11
0
void Test1(){
    sthread_mutex_init(mut_ptr);
    sthread_t thr1,thr2,thr3;

    if (sthread_init() == -1){}

    printf("Creating Thread 1\n");

    if (sthread_create(&thr1, threadmain, (void *)1) == -1){}

    sleep(2);
    printf("Creating Thread 2\n");
    if (sthread_create(&thr2, threadmain, (void *)2) == -1){}

    sleep(2);
    printf("Creating Thread 3\n");
    if (sthread_create(&thr3, threadmain, (void *)3) == -1){}

}
Esempio n. 12
0
int main(int argc, char **argv) {
    int arg = 1;

    sthread_init();

    mon = sthread_monitor_init();

    sthread_monitor_enter(mon);
    if (sthread_create(thread_start, (void*)&arg, 10) == NULL) {
        printf("sthread_create failed\n");
        exit(1);
    }
    printf("thread principal vai bloquear-se\n");
    sthread_monitor_wait(mon);
    printf("thread principal desbloqueada\n");
    sthread_monitor_exit(mon);
    printf("\nSUCESSO!\n");
    return 1;
}
Esempio n. 13
0
int main(int argc, char **argv) {
  sthread_mutex_t mutex = NULL;
  int i = 0;

  printf("Testing sthread_mutex_*, impl: %s\n",
     (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
 
  sthread_init();

  mutex = sthread_mutex_init();
  while (i < 10) {
    sthread_mutex_lock(mutex);
    printf("i=%i\n",i);
    i++;
    sthread_yield();
    sthread_mutex_unlock(mutex);
  }
  printf("\nSUCESSO!\n");
  return 1;
}
Esempio n. 14
0
int main(int argc, char **argv)
{
	sthread_t threads[NUM_TC];
	sthread_t prodthr;
	int i;
	available_reqs = 0;
	
	// initialize sthread lib	
	sthread_init();
	
	//initialize filesystem
	snfs_init(argc, argv);
		
   	// initialize SNFS layer
       struct sockaddr_un servaddr;
                	
	// initialize communications
	srv_init_socket(&servaddr);
			
	// initialize  monitor
        mon = sthread_monitor_init();
        
	// create thread_consumer threads
	for(i = 0; i < NUM_TC; i++) {
		threads[i] = sthread_create(thread_consumer, (void*) NULL,1);
		if (threads[i] == NULL) {
			printf("Error while creating threads. Terminating...\n");
			exit(-1);
		}
	}
	
	// create producer thread
	prodthr = sthread_create(thread_producer, (void*) NULL,1);
	
	
	sthread_join(prodthr, (void**)NULL);
	for(i = 0; i < NUM_TC; i++)
		sthread_join(threads[i], (void **)NULL);

	return 0;
}
Esempio n. 15
0
int main(int argc, char **argv) {
  int n;
  int *arg[MAXTHREADS];
  int *retptr;
  sthread_t child[MAXTHREADS];

  printf("Testing sthread_create, impl: %s\n",
         (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");

  if ( argv[1] ) nThreads = atoi(argv[1]);
  if ( nThreads > MAXTHREADS ) nThreads = MAXTHREADS;
  printf("Creating %d threads\n", nThreads);

  sthread_init();

  for (n = 0; n < nThreads; ++n) {
    arg[n] = malloc(sizeof(arg[n]));
    if (!arg[n]) {
      printf("malloc() failed\n");
      exit(1);
    }
    *(arg[n]) = n;

    child[n] = sthread_create(thread_start, (void*) arg[n], 1);
    if (child[n] == NULL) {
      printf("sthread_create failed\n");
      exit(1);
    }
  }

  for (n = nThreads - 1; n >= 0; --n) {
    retptr = (int *)sthread_join(child[n]);
    printf("main: joined with %d: %d\n", n, *retptr);
    free(arg[n]);
  }

  return 0;
}
Esempio n. 16
0
File: test.c Progetto: Nukem9/Dune
int main(int argc, char *argv[])
{
	printf("==== start\n");

	if (sthread_init())
		err(1, "sthread_init()");

	printf("==== Basic sthread\n");
	test_sthread();
	test_sthread();

	printf("==== Globals\n");
	_global = 2;
	test_global();
	if (_global != 2)
		errx(1, "global in master %d\n", _global);
	test_global();

	printf("==== Stack\n");
	test_stack();
	test_stack();

	printf("==== smalloc\n");
	test_smalloc();

	printf("==== fd\n");
	test_fd();

	printf("==== syscall\n");
	test_syscall();

	printf("==== parallel\n");
	test_parallel();

	printf("==== end\n");
	exit(0);
}
Esempio n. 17
0
int main(int argc, char **argv) {
	int i;
	int *arg = (int *)malloc(sizeof(int));
	if (!arg) {
		printf("error: malloc failed\n");
		exit(1);
	}
	*arg = 1;

	printf("Testing sthread_create, impl: %s\n",
			(sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");

	sthread_init();

	if (sthread_create(thread_start, (void *)arg, 0) == NULL) {
		printf("sthread_create failed\n");
		exit(1);
	}

	/* Without using other thread primitives (which we don't want to
	 * rely on for this first test), we can't know for sure that the
	 * child thread runs and completes if we yield just once (in fact,
	 * with x86_64 pthreads the child almost never completes after the
	 * main thread yields just once). So, we yield an arbitrary number
	 * of times here before exiting (100 isn't always enough, but 1000
	 * seems to be). Even if the child thread doesn't finish running by
	 * the time we're done looping, the success/fail of this test shouldn't
	 * change, but the output may appear in an unexpected order. */
	for (i = 0; i < 1000; i++) {
		sthread_yield();
	}
	printf("back in main\n");

	free(arg);
	return 0;
}
Esempio n. 18
0
int main(int argc, char *argv[])
{
  sthread_t thr1, thr2;

  if (sthread_init() == -1)
    fprintf(stderr, "%s: sthread_init: %s\n", argv[0], strerror(errno));

  if (sthread_create(&thr1, threadmain, (void *)1) == -1)
    fprintf(stderr, "%s: sthread_create: %s\n", argv[0], strerror(errno));

  if (sthread_create(&thr2, threadmain, (void *)2) == -1)
    fprintf(stderr, "%s: sthread_create: %s\n", argv[0], strerror(errno));

  sleep(1);
  sthread_wake(thr1);
  sleep(1);
  sthread_wake(thr2);
  sleep(1);
  sthread_wake(thr1);
  sthread_wake(thr2);
  sleep(1);

  return 0;
}
Esempio n. 19
0
File: test-cond.c Progetto: kunw/P2
int main(int argc, char **argv) {
	int sent, checks, i;
	sthread_t child[MAXTHREADS];


	printf("Testing sthread_cond_*, impl: %s\n",
			(sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	assert(num_threads <= MAXTHREADS);

	sthread_init();

	mutex = sthread_mutex_init();
	avail_cond = sthread_cond_init();

	sthread_mutex_lock(mutex);

	for (i = 0; i < num_threads; i++) {
		child[i] = sthread_create(thread_start, NULL, 1);
		if (child[i] == NULL) {
			printf("sthread_create %d failed\n", i);
			exit(1);
		}
	}

	assert(transfered == 0);

	/* This should let the other thread run at some point. */
	sthread_mutex_unlock(mutex);

	/* Send a bunch of things for the other threads to take */
	sent = 0;
	while (sent < max_transfer) {
		sthread_mutex_lock(mutex);
		waiting++;
		sent++;
		sthread_cond_signal(avail_cond);
		sthread_mutex_unlock(mutex);
		sthread_yield();
	}

	printf("Sent %d\n", sent);

	/* Now give the other threads 100 tries to get
	 * them all across. We assume that's enough
	 * for the sake of not running this test forever. */
	checks = 10000;  //arbitrary??
	while (checks > 0) {
		sthread_mutex_lock(mutex);
		if (transfered != max_transfer)
			checks--;
		else {
			/* broadcast to let the consumers know we've
			 * finished, so they can exit
			 * (othrewise, they may still be holding the lock
			 * when we try to free it below) */
			sthread_cond_broadcast(avail_cond);
			checks = -1;
		}
		sthread_mutex_unlock(mutex);
		sthread_yield();
	}

	if (checks == -1) {
		/* Wait for child threads to finish, otherwise we could try to
		 * free the mutex before they've unlocked it! */
		printf("joining on children\n");
		for (i = 0; i < num_threads; i++) {
			sthread_join(child[i]);
			printf("joined with child %d\n", i);
		}
		printf("sthread_cond passed\n");
	} else {
		printf("*** sthread_cond failed\n");
		/* If we failed, don't bother joining on threads. */
	}

	sthread_mutex_free(mutex);
	sthread_cond_free(avail_cond);
	return 0;
}