Ejemplo n.º 1
0
void *philosopher(void *v)
{
  Phil_struct *ps;
  long st;
  long t;

  ps = (Phil_struct *) v;

  while(1) {

    /* First the philosopher thinks for a random number of seconds */

    st = (random()%ps->ms) + 1;
    printf("%3d Philosopher %d thinking for %d second%s\n", 
                time(0)-ps->t0, ps->id, st, (st == 1) ? "" : "s");
    fflush(stdout);
    sleep(st);

    /* Now, the philosopher wakes up and wants to eat.  He calls pickup
       to pick up the chopsticks */

    printf("%3d Philosopher %d no longer thinking -- calling pickup()\n", 
            time(0)-ps->t0, ps->id);
    fflush(stdout);
    t = time(0);
    pthread_mutex_lock(ps->blockmon);
    ps->blockstarting[ps->id] = t;
    pthread_mutex_unlock(ps->blockmon);

    pickup(ps);

    pthread_mutex_lock(ps->blockmon);
    ps->blocktime[ps->id] += (time(0) - t);
    ps->blockstarting[ps->id] = -1;
    pthread_mutex_unlock(ps->blockmon);

    /* When pickup returns, the philosopher can eat for a random number of
       seconds */

    st = (random()%ps->ms) + 1;
    printf("%3d Philosopher %d eating for %d second%s\n", 
                time(0)-ps->t0, ps->id, st, (st == 1) ? "" : "s");
    fflush(stdout);
    sleep(st);

    /* Finally, the philosopher is done eating, and calls putdown to
       put down the chopsticks */

    printf("%3d Philosopher %d no longer eating -- calling putdown()\n", 
            time(0)-ps->t0, ps->id);
    fflush(stdout);
    putdown(ps);
  }
}
Ejemplo n.º 2
0
int main(int argc, char* argv[])
{
	srand(time(NULL));
	apvector<apstring> yourdeck(numofcards,"0");
	apvector<type> cvalue(numofcards,land);
	apvector<int> cast(numofcards,0);
	apvector<int> clife(numofcards,0);
	apmatrix<int> castcost(numofcards,2,0);
	apvector<int> castcostco(numofcards,0);
	apvector<fly> fornot(numofcards,notflying);
	apvector<int> num(numofcards,1);
	apvector<int> cnum(numofcards,1);
	apvector<int> yhand(11,-1);
	apvector<int> chand(11,-1);
	apvector<int> table(1,-1);
	filldeck(yourdeck,cvalue,cast,clife,castcost,castcostco,fornot,num);
	cnum=num;
	firstdrow(yhand,num,cvalue);
	firstdrow(chand,cnum,cvalue);
	for(int u=0;u<11;u++)
	{
		cout<<u+1<<","<<yourdeck[yhand[u]];
		if(cvalue[yhand[u]]==land)
			cout<<" land"<<endl;
		else
			cout<<" Creature"<<endl;

	}
	int ypoints=20,cpoints=20,z=0;
	char the;
	while(the!='n'&&ypoints>0&&cpoints>0)
	{
		putdown(yhand,table,yourdeck);
		the=drawtwo(yhand,num);
		if(the!='n')
		{
			cout<<"\nhand\n-------\n";
			for(z=0;z<yhand.length();z++)
			{
				cout<<z+1<<","<<yourdeck[yhand[z]]<<endl;
			}
			cout<<"\nTabel\n-------\n";
			for(int k=0;k<table.length();k++)
			{
				cout<<k+1<<","<<yourdeck[table[k]]<<endl;
			}
		}
		
	}

	return 0;
}
Ejemplo n.º 3
0
void agent(){
  int rn,x=0;
  printf("agent here\n");
  
  while(x<10){
    rn=rand()%3;
    putdown(rn);
    sleep(1); //explain why this is needed!!!
    x++;
  }
  printf("agent done\n");
  return;
}
Ejemplo n.º 4
0
static void *philosopher(void *_who)
{
    unsigned int *who = (unsigned int *)_who;

    /* For simplicity, all philosophers eat for the same amount of time
       and think for a time that is simply related to their position at
       the table. The parameter who identifies the philosopher: 0,1,2,.. */
    for (;;) {
        pth_sleep((*who) + 1);
        pickup((*who));
        pth_sleep(1);
        putdown((*who));
    }
    return NULL;
}
   main()
  {
   int ch,a,n,i;
   printf("\nDINING PHILOSOPHERS PROBLEM :");
   for(i=0;i<5;i++)
    {
    state[i]='t';
    self[i]='s';
    spoon[i]='s';
     }
    printf("\nInitial state of each philosopher :");
    printf("\nPHIL NO:\tTHINK\tSTATUS\t\tSPOON");
                 for(i=0;i<5;i++)
    printf("\n%d\t\t%c\t\t%c\t\t%c",i+1,state[i],self[i],spoon[i]);
    printf("\n1.EXIT\n2.HUNGRY\n3.THINKING");
    printf("\nEnter your choice :");
    scanf("%d",&ch);
    while(ch!=1)
     {
    switch(ch)


   {
 case 1:
 return 0;
 case 2:
 printf("\nEnter which philosopher is hungry :");
scanf("%d",&n);
 pickup(n-1);
 break;
 case 3:
 printf("\nEnter which philosopher is thinking :");
 scanf("%d",&n);
 putdown(n-1);
 break;
 }
 printf("State of each philosopher :");
 printf("\nPHIL NO:\tTHINK/EAT\tSTATUS\t\tSPOON");
 for(i=0;i<5;i++)
printf("\n%d\t\t%c\t\t%c\t\t%c",i+1,state[i],self[i],spoon[i]);
printf("\n1.EXIT\n2.HUNGRY\n3.THINKING");
 printf("\nEnter your choice :");
 scanf("%d",&ch);
 }
 }