[英]bus error on sem_wait()
我正在使用命名信號量編寫多進程程序,在主進程中,我使用以下代碼打開信號量
semaphore = sem_open("/msema",O_RDWR|O_CREAT|O_TRUNC,00777,1);
if (semaphore == SEM_FAILED)
perror("SEMAPHORE");
並在子程序中
count_sem=sem_open("/msema",O_RDWR);
if(count_sem==SEM_FAILED)
{
perror("sem_open");
return 1;
}
在sem_wait()上
do {
errno=0;
printf("BeforeSemWait\n");
rtn=sem_wait(count_sem);
printf("afterSemWait\n");
} while(errno==EINTR);
if(rtn < 0) {
printf("Error\n");
perror("sem_wait()");
sem_close(count_sem);
return 1;
}
我從sem_wait()中收到總線錯誤
BeforeSemWait
Program received signal SIGBUS, Bus error.
0x00a206c9 in sem_wait@@GLIBC_2.1 () from /lib/libpthread.so.0`
我究竟做錯了什么?
編輯:整個代碼:master.c: http ://pastebin.com/3MnMjUUM worker.c http://pastebin.com/rW5qYFqg
您的程序中必須存在其他錯誤。 這里有以下工作(不需要O_TRUNC):
semproducer.c:
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>
int main () {
sem_t *sem=sem_open("/msema",O_RDWR|O_CREAT /* |O_TRUNC*/ ,00777,1);
if (sem==SEM_FAILED) {
perror("sem_open");
}
else {
while (1) {
sem_post (sem);
printf ("sem_post done\n");
sleep (5);
}
}
}
semconsumer.c:
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>
#include <errno.h>
int main () {
sem_t *count_sem=sem_open("/msema",O_RDWR);
if(count_sem==SEM_FAILED) {
perror("sem_open");
return 1;
}
do {
int rtn;
do {
errno=0;
rtn=sem_wait(count_sem);
} while(errno==EINTR);
if(rtn < 0) {
perror("sem_wait()");
sem_close(count_sem);
return 1;
}
printf ("sema signalled\n");
} while (1);
}
用gcc semproducer.c -o semproducer -lrt
和gcc semconsumer.c -o semconsumer -lrt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.