当前位置: 技术问答>linux和unix
semaphore的问题?
来源: 互联网 发布时间:2016-04-15
本文导语: 写了个dining philosopher的程序 运行的时候有fragmentation fault的错误 dbx调试后的结果如下: rohan% dbx -r ph Running: ph (process id 22629) dbx: no symbol table loaded yet -- use "prog -readsyms" to load them (l@2) signal SEGV (no mappi...
写了个dining philosopher的程序
运行的时候有fragmentation fault的错误
dbx调试后的结果如下:
rohan% dbx -r ph
Running: ph
(process id 22629)
dbx: no symbol table loaded yet -- use "prog -readsyms" to load them
(l@2) signal SEGV (no mapping at the fault address) at 0xff375120 at 0xff375120
0x00000000ff375120: lduh [%i0 + 6], %i2
Entering debugger ...
(dbx) prog -readsyms
Reading ph
Reading ld.so.1
Reading libpthread.so.1
Reading librt.so.1
Reading libc.so.1
Reading libaio.so.1
Reading libmd.so.1
Reading libc_psr.so.1
Current function is get_chopstick
16 sem_wait(&chops[id]);
看了程序,看不出哪里错了
请高人指点 谢谢
程序如下:
#include
#include
#include
#include
#include
#define PILOSOPHER_NUM 5 //the number of philosopher
sem_t chops[PILOSOPHER_NUM]; //set semaphore for PILOSOPHER_NUM chopsticks
sem_t pnt; //set semaphore for print
void get_chopstick(int id)
{
if (id%2 == 0) //even number philosophers get chopsticks from id to id+1
{
sem_wait(&chops[id]);
sem_wait(&chops[(id+1)%PILOSOPHER_NUM]);
}
else //odd number philosophers get chopsticks from id+1 to id
{
sem_wait(&chops[(id+1)%PILOSOPHER_NUM]);
sem_wait(&chops[id]);
}
}
void release_chopstick(int id)
{
sem_post(&chops[id]);
sem_post(&chops[(id+1)%PILOSOPHER_NUM]);
}
//prevent from different threads interleaving printing onto stdout
void draw(int id, char *str)
{
sem_wait(&pnt);
printf( "Philosopher %d, is %s", id, str);
sem_post(&pnt);
}
void phil_sleep(long t)
{
struct timespec slp;
//set up sleep interval
slp.tv_sec = 0;
slp.tv_nsec = t * 1000000; //transfer to microsec
if (nanosleep(&slp, NULL) == -1)
{
perror("Failed to nanosleep");
}
}
void philosopher(int id)
{
int i;
for (i=0; i
运行的时候有fragmentation fault的错误
dbx调试后的结果如下:
rohan% dbx -r ph
Running: ph
(process id 22629)
dbx: no symbol table loaded yet -- use "prog -readsyms" to load them
(l@2) signal SEGV (no mapping at the fault address) at 0xff375120 at 0xff375120
0x00000000ff375120: lduh [%i0 + 6], %i2
Entering debugger ...
(dbx) prog -readsyms
Reading ph
Reading ld.so.1
Reading libpthread.so.1
Reading librt.so.1
Reading libc.so.1
Reading libaio.so.1
Reading libmd.so.1
Reading libc_psr.so.1
Current function is get_chopstick
16 sem_wait(&chops[id]);
看了程序,看不出哪里错了
请高人指点 谢谢
程序如下:
#include
#include
#include
#include
#include
#define PILOSOPHER_NUM 5 //the number of philosopher
sem_t chops[PILOSOPHER_NUM]; //set semaphore for PILOSOPHER_NUM chopsticks
sem_t pnt; //set semaphore for print
void get_chopstick(int id)
{
if (id%2 == 0) //even number philosophers get chopsticks from id to id+1
{
sem_wait(&chops[id]);
sem_wait(&chops[(id+1)%PILOSOPHER_NUM]);
}
else //odd number philosophers get chopsticks from id+1 to id
{
sem_wait(&chops[(id+1)%PILOSOPHER_NUM]);
sem_wait(&chops[id]);
}
}
void release_chopstick(int id)
{
sem_post(&chops[id]);
sem_post(&chops[(id+1)%PILOSOPHER_NUM]);
}
//prevent from different threads interleaving printing onto stdout
void draw(int id, char *str)
{
sem_wait(&pnt);
printf( "Philosopher %d, is %s", id, str);
sem_post(&pnt);
}
void phil_sleep(long t)
{
struct timespec slp;
//set up sleep interval
slp.tv_sec = 0;
slp.tv_nsec = t * 1000000; //transfer to microsec
if (nanosleep(&slp, NULL) == -1)
{
perror("Failed to nanosleep");
}
}
void philosopher(int id)
{
int i;
for (i=0; i