Unix Programming - how to solve the philosopher problem .and what's wrong with my codes.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2006 > how to solve the philosopher problem .and what's wrong with my codes.





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 how to solve the philosopher problem .and what's wrong with my codes.
DaVinci

2006-09-28, 7:33 am

Only one thread created ,the others doesn't why?
#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;
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);
}
void eatting(int i)
{
printf("phlosi%d is eatting\n",i);
}
void phlosi(int i)
{
while(1)
{
thinking(i);
getting(i);
eatting(i);
putting(i);
}
}

int main()
{

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

for(int i=0;i<5;i++)
{
pthread_t tid;
pthread_create(&tid,NULL,(void*)phlosi,(void*)i);

}
return 0;
}

Πασκαλ Βουργυιγνον

2006-09-28, 1:20 pm

Aggelos Mpimpoudis <std03092@di.uoa.gr> writes:

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


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.

--
__Pascal Bourguignon__ http://www.informatimago.com/

WARNING: This product attracts every other piece of matter in the
universe, including the products of other manufacturers, with a
force proportional to the product of the masses and inversely
proportional to the distance between them.
Giorgos Keramidas

2006-09-28, 1:21 pm

On Thu, 28 Sep 2006 16:40:46 +0200, <pjb@informatimago.com> wrote:
> Aggelos Mpimpoudis <std03092@di.uoa.gr> writes:
>
> 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.


Good point

The name is Greek though. Seeing Greek text may not be as informational
for someone who isn't Greek. The correct spelling of my Greek name is:



When posting to the Internet though, I use 'Giorgos Keramidas', which is
*almost* the same and much easier for English-speaking people.

Pascal Bourguignon

2006-09-28, 1:21 pm

Giorgos Keramidas <keramida@ceid.upatras.gr> writes:

> On Thu, 28 Sep 2006 16:40:46 +0200, _ασκαλ Βουργυιγνον <pjb@informatimago.com> wrote:
>
> Good point
>
> The name is Greek though. Seeing Greek text may not be as informational
> for someone who isn't Greek. The correct spelling of my Greek name is:
>
> Γιώργος Κεραμίδας
>
> When posting to the Internet though, I use 'Giorgos Keramidas', which is
> *almost* the same and much easier for English-speaking people.


Most people using Internet have enough mathematical education do know
the greek alphabet, or can use Wikipedia to learn it.

The purpose of world wide communication is to enrich, not to
impoverish. Please, share your alphabet! :-)

--
__Pascal Bourguignon__ http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush
DaVinci

2006-09-29, 1:45 am


Aggelos Mpimpoudis wrote:
> =D0=E1=F3=EA=E1=EB =C2=EF=F5=F1=E3=F5=E9=E3=ED=EF=ED wrote:
>
> Thx indeed =D0=E1=F3=EA=E1=EB. But its my choice to have my name with en=

glish
> 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 ???)


Yes,Thanks very much.The pholosopphers have a good dinner now.

>=20
> Lets us know!!!


DaVinci

2006-09-29, 1:45 am


Aggelos Mpimpoudis wrote:
> Pascal Bourguignon wrote:
>
> 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!!
>
> =EA. =CA=E5=F1=E1=EC=DF=E4=E1 =C9 am glad to meet a Greek here...
> Good afternoon =F0=E1=F4=F1=E9=FE=F4=E7!!


By The way,Do U think my solution to the philosopher problem is right
or not?
could U show me some better solution to the problem?

blmblm@myrealbox.com

2006-09-29, 7:28 am

In article <1159504132.995792.208790@e3g2000cwe.googlegroups.com>,
DaVinci <apple.davinci@gmail.com> wrote:

[ snip ]

> By The way,Do U think my solution to the philosopher problem is right
> or not?
> could U show me some better solution to the problem?


Unless I've missed something in your code -- not impossible --
there's nothing there that enforces the restriction that neighboring
philosophers aren't supposed to eat at the same time. ?

This is a classical problem, as you probably know, and Googling for
"dining philosophers" brings up lots of relevant info. The Wikipedia
article seems pretty good on a quick skim. Textbooks on operating
systems also often have a section describing solutions to this problem.

Of course, if this is homework, you probably should keep trying to
figure it out yourself. Otherwise, um, RTFL? (L = literature)

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Pascal Bourguignon

2006-09-29, 7:28 am

Aggelos Mpimpoudis <std03092@di.uoa.gr> writes:

> Pascal Bourguignon wrote:
>
> 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


Well if they write it in katakana, or hiragana, which are syllabic
like Korean, then I wouldn't mind. For Kanji, it would be more
difficult indeed, but (young) Japaneses would have the same problem
with them.


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


Still slightly on topic for a unix programmer: nowadays we have utf-8
and a world wide internet and we should be preprared to handle any
writing system.


> κ. Κεραμίδα Ι am glad to meet a Greek here...
> Good afternoon πατριώτη!!


--
__Pascal Bourguignon__ http://www.informatimago.com/

ADVISORY: There is an extremely small but nonzero chance that,
through a process known as "tunneling," this product may
spontaneously disappear from its present location and reappear at
any random place in the universe, including your neighbor's
domicile. The manufacturer will not be responsible for any damages
or inconveniences that may result.
Giorgos Keramidas

2006-09-29, 7:57 pm

On Thu, 28 Sep 2006 20:47:06 +0300, Aggelos Mpimpoudis <std03092@di.uoa.gr> wrote:
> Pascal Bourguignon wrote:


>
> 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 Greek here...
> Good afternoon !!


Good evening .


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com