コード例 #1
0
ファイル: main.c プロジェクト: JACOBFelix/philosophe
static void		*start(void *philo_uncast)
{
  t_philo		*philo;

  philo = (t_philo *)philo_uncast;
  printf(AROUND_TABLE_MSG);
  printf(REST_MSG);
  while (philo->hungry++ < MAX_HUNGRY && philo->food != 0)
    {
      if (ID_PAIR && pthread_mutex_trylock(philo->b_left) == 0)
	thinks(philo, philo->b_right);
      if (ID_UNPAIR && pthread_mutex_trylock(philo->b_right) == 0)
      	thinks(philo, philo->b_left);
      sleep(TIME_THINK);
    }
  printf(END_THREAD_MSG);
  return (NULL);
}
コード例 #2
0
ファイル: csphil.c プロジェクト: saqibjamil/Ingres
/* A philosopher thread function, called out of CSdispatch */
STATUS
philosopher(
i4 mode,
MY_SCB *scb,
i4 *next_mode)
{
    i4 bites = 0;

#ifdef EX_DEBUG
    EX_CONTEXT context;

    if (EXdeclare(ex_handler, &context) != OK) {
	/* some exception was raised */
	Psem( &SIsem );
	SIfprintf( stderr,"Error: unexpected exception in philosopher()...");
	Vsem( &SIsem );

	EXdelete();
	return FAIL;
    }
#endif

    switch( mode )
    {
    case CS_INITIATE:	/* A new philsopher is born */

	scb->phil = Threads++;
	status[scb->phil] = mode;
	while( bites < NBITES )
	{
	    getsticks( scb->phil );
	    eats( scb->phil );
	    bites++;
	    freesticks( scb->phil );
	    thinks( scb->phil );
	}

	/* fall into: */

    default:
	*next_mode = CS_TERMINATE;
	break;

    case CS_TERMINATE:
	if( Noisy )
	{
# ifdef EX_SIG_DEBUG
	    signal(SIGUSR1, intfunc);
	    if (scb->phil == 1) {
		SIfprintf(stderr, "Send a signal #%d to process #%d\n",
			SIGUSR1, getpid());
		pause();
	    }
# endif /* EX_SIG_DEBUG */
	    Psem( &SIsem );
	    SIfprintf(stderr, "%d (%s) dies, RIP.\n",
			scb->phil, Names[ scb->phil ] );
	    if ( status[scb->phil]  == mode )
	    {
		SIfprintf(stderr, "Oops this philosopher is already dead?\n");
		SIfprintf(stderr, "\t\t CS code is non-functional\n");
	    }
	    Vsem( &SIsem );

	}

	*next_mode = CS_TERMINATE;

	/* If no more threads, shutdown */
	status[scb->phil] = mode;

	if( --Threads == 0 ) {
	    /* Everyone else should be dead, no semaphore needed. */
	    SIflush( stderr );

	    CSterminate( CS_KILL, (i4 *)NULL );
	}

	break;
    }

#ifdef EX_DEBUG
    EXdelete();
#endif
    return( OK );
}