For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2006 > Re: how to solve the philosopher problem .and what's wrong with my









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: how to solve the philosopher problem .and what's wrong with my
Aggelos Mpimpoudis

2006-09-28, 8:00 am

DaVinci wrote:
> Only one thread created ,the others doesn't why?


You create pthread_t tid in the loop. You need an array of thread ids
outside the loop.
Here it is. Try it.


pthread_t tid[N];

> int main()
> {
>
> for(int i = 0;i<N;i++)
> {
> forks[i] = (struct Fork*)malloc(sizeof(struct Fork));
> forks[i]->flag = FREE;
> forks[i]->num = i;
> }
>
> for(int i=0;i<N;i++)
> {
>
> pthread_create(tid+i,NULL,(void*)phlosi
,(void*)i);
>
> }
> return 0;
> }
>

Renato Golin

2006-09-28, 8:00 am

Aggelos Mpimpoudis wrote:
(...)

Hey, which one are you? Mpimpoudis or Biboudis ? Is it random?

--renato
Aggelos Mpimpoudis

2006-09-28, 8:00 am

Renato Golin wrote:
> Aggelos Mpimpoudis wrote:
> (...)
>
> Hey, which one are you? Mpimpoudis or Biboudis ? Is it random?
>
> --renato

First time at newsgroup... (I didnt like my previous name spelling :P:P:P:P)

sryyyyy :/ :) :):)
Renato Golin

2006-09-28, 8:00 am

Aggelos Mpimpoudis wrote:
> Renato Golin wrote:
> First time at newsgroup... (I didnt like my previous name spelling
> :P:P:P:P)
>
> sryyyyy :/ :) :):)



Hehe, that's funny, I might change my name once in a while too! ;)

--otaner
Aggelos Mpimpoudis

2006-09-28, 7:00 pm

Πασκαλ Βουργυιγνον wrote:
> You can use a different encoding in the comment part of the address
> fields. Try to enter your name with the correct spelling, any
> sufficiently modern UA should be able to encode it correctly, as
> something starting with: =?iso-8859-7?Q?...?= and the UA of your
> correspond should be able to decode it correctly, displaying the
> correct spelling.


Thx indeed Πασκαλ. But its my choice to have my name with english
characters only :)Thx very much for the guide though, I really
appreciate it. I have just configured my client to use Usenet and i saw
that the name wasnt fitting well. If I knew that i would bring such a
mess, i wouldnt repost the message. hehehehheehe :):)
But what about DaVinci? Did he managed to settle the pholosophers to eat
(or I said something wrong at my reply earlier and he gave up :O :( ???)

Lets us know!!! :)
Renato Golin

2006-09-28, 7:00 pm

Aggelos Mpimpoudis wrote:
> But what about DaVinci? Did he managed to settle the pholosophers to eat
> (or I said something wrong at my reply earlier and he gave up :O :( ???)


Ah, good point, I almost forgot of that. I could make his code work with
your advice and by putting the pthread_join later (otherwise it just sat
and waited). And btw, I put (i--) on phlosi so it won't go forever... ;)

Here's my full code:


#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define NUM 5
#define LEFT (i+NUM-1)%NUM
#define RIGHT (i+1)%NUM
#define FREE 0
#define USED 1

struct Fork
{
int flag;
int num;
};
static struct Fork *forks[NUM];

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_t tid[NUM];

void
getting (int i)
{
pthread_mutex_lock (&lock);

if (forks[LEFT]->flag == FREE)
{
forks[LEFT]->flag = USED;
printf ("pholos%d picking fork %d\n", i, LEFT);
}
if (forks[RIGHT]->flag == FREE)
{
forks[RIGHT]->flag = USED;
printf ("pholos%d picking fork %d\n", i, RIGHT);
}
pthread_mutex_unlock (&lock);
return;

}

void
putting (int i)
{
pthread_mutex_lock (&lock);
forks[LEFT]->flag = FREE;
printf ("phlosi%d putting fork%d\n", i, LEFT);
pthread_mutex_unlock (&lock);

pthread_mutex_lock (&lock);
forks[RIGHT]->flag = FREE;
printf ("phlosi%d putting fork%d\n", i, RIGHT);
pthread_mutex_unlock (&lock);

}

void
thinking (int i)
{
printf ("phlosi%d is thingking\n", i);
sleep(1);
}

void
eatting (int i)
{
printf ("phlosi%d is eatting\n", i);
}

void
phlosi (int i)
{
while (i--)
{
thinking (i);
getting (i);
eatting (i);
putting (i);
}
}

int
main ()
{
int i=0, ret=0;

for (i=0; i<NUM; i++)
{
forks[i] = (struct Fork *) malloc (sizeof (struct Fork));
forks[i]->flag = FREE;
forks[i]->num = i;
}

for (i=0; i<NUM; i++)
{
ret = pthread_create (tid+i, NULL, (void *) phlosi, (void *) i);
if (ret)
return ret;
}

for (i=0; i<NUM; i++)
{
pthread_join(tid[i], NULL);
}
return 0;
}
Aggelos Mpimpoudis

2006-09-28, 7:00 pm

Pascal Bourguignon wrote:
>
> Most people using Internet have enough mathematical education do know
> the gr alphabet, or can use Wikipedia to learn it.
>
> The purpose of world wide communication is to enrich, not to
> impoverish. Please, share your alphabet! :-)
>


I like your point of view, but I dont think anyone else is going to use
Wikipedia to learn or just try to communicate this way. I wonder if 10
japanese guys here write their names with their encoding, what would u
think? ehehehe

But..wait a sec...this isn't the place to have a conversation like this.
Lets stay focused!!

κ. Κεραμίδα Ι am glad to meet a Gr here...
Good afternoon πατριώτη!! :)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com