Beispiel #1
0
threadpool* create_threadpool(unsigned int num_threads_in_pool)
{
    if (num_threads_in_pool <= 0) return NULL;

    threadpool *pool = (threadpool*) malloc(sizeof(threadpool));
    pool->num_threads = num_threads_in_pool; pool->qsize = 0;
    pool->pending_job_requests = initialize_queue();
    pool->completed_threads = initialize_queue();
    pool->free_threads = initialize_queue();
    pool->busy_threads = (spec_thread*) malloc(sizeof(spec_thread) * num_threads_in_pool);
    pool->shutdown = 0;

    //initialize mutex and condition variables.  
    pthread_mutex_init(&pool->pending_job_requests_lock, NULL);
    pthread_mutex_init(&pool->completed_threads_lock, NULL);
    pthread_cond_init(&pool->pending_job_requests_cond, NULL);
    //pthread_cond_init(&pool->completed_threads_cond, NULL);
    
    //make threads
    int i;
    for (i = 0; i < num_threads_in_pool; i++)
    {   
        spec_thread* th = new_spec_thread(i, pool);
        enqueue(pool->free_threads, (void*) th); 
    }
  
    return pool;
}
Tqueue CreateQueue(Tboolean fCircular){
	circular = fCircular;
	Tqueue q;
	initialize_queue(&q);

	return q;
}
/* Application */
int main() {
    Tqueue queue;
    Tboolean succeed;
    char chr;

    initialize_queue(&queue);
    printf("\nEnter a letter to be queued ");
    printf("\nor digit 1 to dequeue a letter");
    printf("\nor Return to quit a program\n");

    chr = getche();
    while (chr != '\n' && chr != '\r') {
        if (isalpha(chr)) {
			succeed=enqueue(&queue, chr);
			print_queue(&queue);
			if (!succeed)
				printf("\n Enqueue operation failed\n");
		}
		if (chr == '1') {
			succeed = dequeue(&queue, &chr);
			if  (succeed) {
				printf("\na letter dequeued %c ", chr);
				print_queue(&queue);
			} else
				printf("\nDequeue operation failed\n ");
		}

		chr = getche();
     }
}
Beispiel #4
0
int main(void) {
     Tqueue queue;
     Tboolean succeed;
     char chr;

     initialize_queue(&queue);
     printf("\nEnter a letter to be queued ");
     printf("\nor digit 1 to dequeue a letter");
     printf("\nor Return to quit a program\n");
	 
     while (chr != 10 && chr != 13) {
	  scanf("%c", &chr);
      getchar();
	  if (isalpha(chr)) {
		succeed=enqueue(&queue, chr);
		print_queue(&queue);
		}
	  if (chr == '1') {
		succeed = dequeue(&queue, &chr);
		if  (succeed) {
		    printf("\na letter dequeued %c ", chr);
		    print_queue(&queue);
		    }
		else printf("\nDequeue operation failed\n ");
	  }
     }
	 return 0;
}
Beispiel #5
0
int main(int argc, char* argv[]) {
  int  no_threads, no_operations;
  struct timeval start;
  struct timeval end;
  long diff;
    
    if(argc <= 2)
    {
        printf("Usage: %s <#threads> <#operations>\n", argv[0]);
        return 1;
    }
    
   no_threads = atoi(argv[1]);

   no_operations = atoi(argv[2]);
    
    assert( no_threads > 0 );
    assert( no_operations > 0 );
  
  initialize_queue();
    
 
     printf("\n\n");
    
    /**** TEST with random operations ***/
    gettimeofday(&start, NULL);
        spawner(no_threads, no_operations);
    gettimeofday(&end, NULL);


    diff = MILL * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
    printf("\tElapsed %ld microseconds!\n", diff);
    
    
     /**** TEST FIFO ordering for each concurrent object ***/
    //gettimeofday(&start, NULL);
    MPSC(no_threads, no_operations);
    printf("\n\n");
    SPMC(no_threads, no_operations);
   // gettimeofday(&end, NULL);
    
   // diff = MILL * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
   // printf("\tElapsed %ld microseconds!\n", diff);

    printf("\n\n");
  exit(0);
  
}
Beispiel #6
0
void test_queue() {
  queue q;
  q = initialize_queue(3);
  insert(&q, 1);
  insert(&q, 5);
  insert(&q, 2);
  insert(&q, 7);
  insert(&q, 10);
  print_queue(q);
  int x = get_first(&q);
  while (x != -1) {
    printf("Remove: %d\n", x);
    print_queue(q);
    x = get_first(&q);
  }
}
Beispiel #7
0
void QueueTest::run_tests() {
  // Tests enqueue
  initialize_queue();
  // Tests isEmpty
  check_queue_is_not_empty();
  // Tests size
  check_queue_size();
  // Tests front
  check_queue_front();
  // Ensure size did not change
  check_queue_size();
  // Tests dequeue
  check_queue_dequeue();
  // Ensure size changed
  check_queue_size();
  // Ensure front is updated to next value
  check_queue_front();
  // Exercises isEmpty and dequeue
  clear_queue();
  // Ensure empty
  check_queue_is_empty();
  // Ensure default value returned
  check_queue_default_value();
}
Beispiel #8
0
int main(){
  struct Queue * q = initialize_queue();
  int ret = create_server(&handle_string, "3443", q);
  free(q);
  return ret;
}
Beispiel #9
0
/*========================================================================
** The Main Procedure
** Initialization and FIFO command dispatch routine.
========================================================================*/
static int real_main(int argc, char *argv[])
	{
	const char function[] = "real_main";
	int option_foreground = FALSE;
	int FIFO;					/* First-in-first-out which feeds us requests */
	sigset_t lock_set;
	struct timeval next_tick;

	/* Initialize internation messages library. */
	#ifdef INTERNATIONAL
	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE_PPRD, LOCALEDIR);
	textdomain(PACKAGE_PPRD);
	#endif

	/*
	** Set some environment variables, (PATH, IFS, and
	** SHELL) for safety and for the convenience of the
	** programs we launch (HOME, and PPR_VERSION).
	** Remove unnecessary and potentially misleading
	** variables.
	*/
	set_ppr_env();
	prune_env();

	parse_command_line(argc, argv, &option_foreground);

	/* Switch all UIDs to USER_PPR, all GIDS to GROUP_PPR. */
	adjust_ids();

	/* If the --forground switch wasn't used, then dropt into background. */
	if(! option_foreground)
		gu_daemon(PPR_PPRD_UMASK);
	else
		umask(PPR_PPRD_UMASK);

	/* Change the home directory to the PPR home directory: */
	chdir(HOMEDIR);

	/* Create /var/spool/ppr/pprd.pid. */
	create_lock_file();

	/* Signal handlers for silly stuff. */
	signal_restarting(SIGPIPE, signal_ignore);
	signal_restarting(SIGHUP, signal_ignore);

	/* Signal handler for shutdown request. */
	signal_interupting(SIGTERM, sigterm_handler);

	/* Arrange for child termination to be noted. */
	signal_restarting(SIGCHLD, sigchld_handler);

	/* Move /var/spool/ppr/logs/pprd to pprd.old before we call debug()
	   for the first time (below). */
	rename_old_log_file();

	/*
	** This code must come after adjust_ids() and gu_daemon().
	** It makes the first log entry and tells queue-display
	** programs that we are starting up.
	*/
	debug("PPRD startup, pid=%ld", (long)getpid());
	state_update("STARTUP");

	/* Make sure the local node gets the node id of 0. */
	if(! nodeid_is_local_node(nodeid_assign(ppr_get_nodename())))
		fatal(1, "%s(): line %d: assertion failed", function, __LINE__);

	/* Initialize other subsystems. */
	question_init();

	/* Load the printers database. */
	DODEBUG_STARTUP(("loading printers database"));
	load_printers();

	/* Load the groups database. */
	DODEBUG_STARTUP(("loading groups database"));
	load_groups();

	/* Set up the FIFO. */
	DODEBUG_STARTUP(("opening FIFO"));
	FIFO = open_fifo();

	/* Initialize the queue.  This is likely to start printers. */
	DODEBUG_STARTUP(("initializing the queue"));
	initialize_queue();

	/* Schedule the first timer tick. */
	gettimeofday(&next_tick, NULL);
	next_tick.tv_sec += TICK_INTERVAL;

	/*
	** Create a signal block set which will be used to block SIGCHLD except
	** when we are calling select().
	*/
	sigemptyset(&lock_set);
	sigaddset(&lock_set, SIGCHLD);
	sigprocmask(SIG_BLOCK, &lock_set, (sigset_t*)NULL);

	/*
	** This is the Main Loop.  It runs until the sigterm_handler
	** sets sigterm_received.
	*/
	while(!sigterm_received)
		{
		int readyfds;					/* return value from select() */
		fd_set rfds;					/* list of file descriptors for select() to watch */
		struct timeval time_now;		/* the current time */

		DODEBUG_MAINLOOP(("top of main loop"));

		gettimeofday(&time_now, NULL);

		/* If it is time for or past time for the next tick, */
		if(gu_timeval_cmp(&time_now, &next_tick) >= 0)
			{
			readyfds = 0;
			}

		/* If it is not time for the next tick yet, */
		else
			{
			/* Set the select() timeout so that it will return in time for the
			   next tick(). */
			gu_timeval_cpy(&select_tv, &next_tick);
			gu_timeval_sub(&select_tv, &time_now);

			/* Create a file descriptor list which contains only the descriptor
			   of the FIFO. */
			FD_ZERO(&rfds);
			FD_SET(FIFO, &rfds);

			/* Call select() with SIGCHLD unblocked. */
			sigprocmask(SIG_UNBLOCK, &lock_set, (sigset_t*)NULL);
			readyfds = select(FIFO + 1, &rfds, NULL, NULL, &select_tv);
			sigprocmask(SIG_BLOCK, &lock_set, (sigset_t*)NULL);
			}

		/* If there is something to read, */
		if(readyfds > 0)
			{
			if(!FD_ISSET(FIFO, &rfds))
				fatal(0, "%s(): assertion failed: select() returned but FIFO not ready", function);
			do_command(FIFO);
			continue;
			}

		/* If the SIGCHLD handler set the flag, handle child termination.  Once
		   we have done that, we must go back to the top of the loop because
		   we don't really know if it is time for a tick() call yet. */
		if(sigchld_caught)
			{
			sigchld_caught = FALSE;
			reapchild();
			continue;
			}

		/* If there was no error and no file descriptors are ready, then the 
		   timeout must have expired.  Call tick(). */
		if(readyfds == 0)
			{
			tick();
			next_tick.tv_sec += TICK_INTERVAL;
			continue;
			}

		/* If interupted by a system call, restart it. */
		if(errno == EINTR)
			continue;

		/* If we get this far, there was an error. */
		fatal(0, "%s(): select() failed, errno=%d (%s)", function, errno, gu_strerror(errno));
		} /* end of endless while() loop */

	state_update("SHUTDOWN");
	fatal(0, "Received SIGTERM, exiting");
	} /* end of real_main() */