Ç信号灯:sem_wait抛出莫名其妙的错误 [英] C semaphores: sem_wait throwing inexplicable error

查看:681
本文介绍了Ç信号灯:sem_wait抛出莫名其妙的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,我们必须使用信号量来解决问题。我有一个包含两个信号阵列, gsem ,并给予一定的条件下称 sem_wait(及(gsem [我])),这是为了等到这个特定的进程被唤醒。但是,由于某种原因,它给我的错误错误的文件描述符。我抬头一看 sem_wait 和公开组规范说这是不是一个错误 sem_wait 可引起。这是使我的整个程序疯了,我不知道这是为什么失败了。

I'm working on a problem which we have to use semaphores to solve. I have an array which contains two semaphores, gsem, and given certain conditions call sem_wait(&(gsem[me])), which is supposed to waiting until that particular process is woken up. However, for some reason it gives me the error Bad file descriptor. I looked up sem_wait and the Open Group spec says this is not an error sem_wait can cause. This is making my whole program crazy and I have no idea why this is failing.

编辑:如果违反,code,如要求

Offending code, as requested.

120     sem_wait(&mutex);
121     if (inside[opp] > 0 || waiting[opp] > 0) {
122         sem_wait(&screen);
123         printf("%s %u waiting\n", names[me], t);
124         sem_post(&screen);
125         waiting[me]++;
126         sem_post(&mutex);
127         int hg = sem_wait(&(gsem[me]));
128         if (hg < 0)
129             printf("%s\n", strerror(errno));
130     } 

我要指出这是我们需要使用信号灯家庭作业。教授称之为中性浴室。无论男女都可以使用它,但不能同时使用。 里面的[OPP] 是在浴室异性的人数。 等待[OPP] 是异性等待使用它的次数。 屏幕是锁定至标准输出访问的信号。该解决方案基于解决我们的教科书它使用传递指挥棒给出的读/写器的问题。

I should note this is a homework assignment for which we are required to use semaphores. The professor calls it the "unisex bathroom". Either men and women can use it, but not simultaneously. inside[opp] is the number of people of the opposite sex in the bathroom. waiting[opp] is the number of the opposite sex waiting to use it. screen is a semaphore which locks access to stdout. The solution is based on a solution to the readers/writers problem given in our textbook which uses passing the baton.

我也应该注意到,我们首先要code在艾达的解决方案,然后将其转换为C.我的Ada解决方案的工作,我把它翻译逐字。我敢肯定,这是一些小的语法细节。最后,我工作的雪豹,是否有帮助。

I should also note that we first had to code a solution in Ada and then convert it to C. My Ada solution works, and I translated it verbatim. I'm sure it's some minor syntactical detail. Lastly, I'm working on Snow Leopard, if that helps.

推荐答案

请记住,单一UNIX规格未必是你的情况的控制文件。诚然,它可能应该是,但因为你没有实际指定的平台上,这可能是因为您在决定遵循不同的规则和/或有其他收益codeS的环境是。

Keep in mind that the Single UNIX Spec may not necessarily be the controlling document in your case. Granted, it probably should be, but since you haven't actually specified the platform, it may be that you're in an environment that decided to follow different rules and/or have other return codes.

几件事情来检查。

1 /你的确定的是 sem_wait 将返回-1?我见过codeRS只需检查错误号下一个电话,却没有意识到大多数呼叫,不要将其设置为零的成功,而他们只是不要管它。这是可能的,如果错误号被设置为 EBADF 会出现这样的情况的的的 sem_wait 电话。

1/ Are you sure that sem_wait is returning -1? I've seen coders simply check errno following a call, not realising that most calls don't set it to zero on success, rather they simply leave it alone. It's possible that such a situation would arise if errno were set to EBADF before the sem_wait call.

2 /你有没有遵循创建信号灯的所有规则,如初始化呢?

2/ Have you followed all the rules in creating the semaphores, such as initialising them?

3 /你指的是一个有效的信号?首先,你肯定的是,指数不越界?

3/ Are you referring to a valid semaphore? Primarily, are you certain that the me index is not out of range?

看到一些code短,这就是所有我必须给的建议。

Short of seeing some code, that's about all the advice I have to give.

一件事,我发现了一个粗略的谷歌 sem_wait EBADF 是的此处。原来这是用错误号在多线程环境,而不包括正确的头一个问题。

One thing I found with a cursory google of sem_wait ebadf is here. Turns out this was a problem with using errno in a threaded environment without including the correct headers.

通过这样做,全球错误号值正在使用,而不是正确的螺纹的的(的这将给线程特定的错误号)。

By doing that, the global errno value was being used rather than the correct threaded macro (which would give the thread-specific errno).

这是否是你的问题,我不知道,但它可能是值得研究的。

Whether that's your problem, I have no idea, but it may be worth looking into.

和多一点的密切关注消息的连锁,也有一些其他的可能性。

And following that chain of messages a little more closely, there are some other possibilities.

4 /您是否使用 sem_init 来初始化信号量。如果是这样,检查它的返回值。该消息是从2008年,因此它可能会过时的信息,但OSX仍可能不支持 sem_init ,preferring sem_open (见这里)。你真的应该的所有的你的 SEM _ 功能以及,仅仅是某些(如果你使用<$检查返回codeS C $ C> sem_init 为他们所有的(如果它不支持),并且只检查一个,你会发现,他们的所有的失败)。

4/ Are you using sem_init to initialise the semaphores. If so, check its return value. The messages are from 2008 so it may be dated information, but OSX may still not support sem_init, preferring sem_open (see here). You really should check the return codes from all your sem_ functions as well, just to be certain (if you're using sem_init for them all (and if it's unsupported) and are only checking one, you may find that they're all failing).

5 /有(是?)一个竞争条件在线程错误号 OSX下的函数链,其中所谓的另一个库调用<$ C $的 __误差功能C> pthread_self 使用前错误号(在主线程,或 current_thread-&GT;错误号在其他线程)。从技术上讲这是不允许的,并有可能在哪里出现问题的小窗口。

5/ There is (was?) a race condition in the thread errno function chain under OSX where the __error function called another library call pthread_self before using errno (in main thread, or current_thread->errno in other threads). Technically this is not allowed and there was a small window where problems could occur.

这篇关于Ç信号灯:sem_wait抛出莫名其妙的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆