- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是信号量的新手,想在我的程序中添加多线程,但我无法解决以下问题:只要我没有设置 SA_RESTART,sem_wait() 应该能够接收 EINTR 并解锁旗帜。我正在向在 sem_wait() 中阻塞的工作线程发送一个 SIGUSR1,它确实接收到信号并被中断,但它会继续阻塞,所以它永远不会给我一个 -1 返回码和 errno = EINTR .但是,如果我从主线程执行 sem_post,它将解除阻塞,给我一个 EINTR 的错误号,但 RC 为 0。我对这种行为感到非常困惑。它是一些奇怪的 NetBSD 实现还是我在这里做错了什么?根据手册页,sem_wait 符合 POSIX.1 (ISO/IEC 9945-1:1996)。一个简单的代码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
typedef struct workQueue_s
{
int full;
int empty;
sem_t work;
int sock_c[10];
} workQueue_t;
void signal_handler( int sig )
{
switch( sig )
{
case SIGUSR1:
printf( "Signal: I am pthread %p\n", pthread_self() );
break;
}
}
extern int errno;
workQueue_t queue;
pthread_t workerbees[8];
void *BeeWork( void *t )
{
int RC;
pthread_t tid;
struct sigaction sa;
sa.sa_handler = signal_handler;
sigaction( SIGUSR1, &sa, NULL );
printf( "Bee: I am pthread %p\n", pthread_self() );
RC = sem_wait( &queue.work );
printf( "Bee: got RC = %d and errno = %d\n", RC, errno );
RC = sem_wait( &queue.work );
printf( "Bee: got RC = %d and errno = %d\n", RC, errno );
pthread_exit( ( void * ) t );
}
int main()
{
int RC;
long tid = 0;
pthread_attr_t attr;
pthread_attr_init( &attr );
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
queue.full = 0;
queue.empty = 0;
sem_init( &queue.work, 0, 0 );
printf( "I am pthread %p\n", pthread_self() );
pthread_create( &workerbees[tid], &attr, BeeWork, ( void * ) tid );
pthread_attr_destroy( &attr );
sleep( 2 );
sem_post( &queue.work );
sleep( 2 );
pthread_kill( workerbees[tid], SIGUSR1 );
sleep( 2 );
// Remove this and sem_wait will stay blocked
sem_post( &queue.work );
sleep( 2 );
return( 0 );
}
我知道 printf 在信号处理程序中并不大声,但只是为了它,如果我删除它,我会得到相同的结果。
这些是没有 sem_post 的结果:
I am pthread 0x7f7fffc00000
Bee: I am pthread 0x7f7ff6c00000
Bee: got RC = 0 and errno = 0
Signal: I am pthread 0x7f7ff6c00000
还有 sem_post:
I am pthread 0x7f7fffc00000
Bee: I am pthread 0x7f7ff6c00000
Bee: got RC = 0 and errno = 0
Signal: I am pthread 0x7f7ff6c00000
Bee: got RC = 0 and errno = 4
我知道我并不真的需要解锁并且可以简单地从 main 中退出,但我还是希望看到它正常工作。我使用 sem_wait 的原因是因为我想让工作线程保持事件状态,并在有来自 Postfix 的新客户端连接时立即使用 sem_post 从主线程唤醒等待时间最长的线程。我不想一直执行 pthread_create,因为我每秒会收到多次调用,我不想失去速度并使 Postfix 对新的 smtpd 客户端无响应。它是 Postfix 的策略守护进程,服务器非常繁忙。
我是不是漏掉了什么? NetBSD 只是搞砸了吗?
最佳答案
我的帖子是关于 Linux 上的行为,但我认为您可能有类似的行为,或者至少我认为可能会有帮助。如果没有,请告诉我,我会删除这个无用的“噪音”。
我试图重现您的设置,看到您所描述的情况我感到非常惊讶。更深入地观察帮助我弄清楚实际上还有更微妙的东西;如果你看看 strace,你会看到类似的东西:
[pid 6984] futex(0x6020e8, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
[pid 6983] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
[pid 6983] rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
[pid 6983] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
[pid 6983] nanosleep({2, 0}, 0x7fffe5794a70) = 0
[pid 6983] tgkill(6983, 6984, SIGUSR1 <unfinished ...>
[pid 6984] <... futex resumed> ) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
[pid 6983] <... tgkill resumed> ) = 0
[pid 6984] --- SIGUSR1 {si_signo=SIGUSR1, si_code=SI_TKILL, si_pid=6983, si_uid=500} ---
[pid 6983] rt_sigprocmask(SIG_BLOCK, [CHLD], <unfinished ...>
[pid 6984] rt_sigreturn( <unfinished ...>
[pid 6983] <... rt_sigprocmask resumed> [], 8) = 0
[pid 6984] <... rt_sigreturn resumed> ) = -1 EINTR (Interrupted system call)
查看带有ERESTARTSYS
和EINTR
的行:被中断的系统调用实际上是rt_sigreturn resumed
,而不是futex
(sem_wait 下的系统调用)如您所料。我必须说我很困惑,但阅读这个人给出了一些有趣的线索(man 7 信号):
If a blocked call to one of the following interfaces is interrupted by
a signal handler, then the call will be automatically restarted after
the signal handler returns if the SA_RESTART flag was used; otherwise
the call will fail with the error EINTR:
[...]
* futex(2) FUTEX_WAIT (since Linux 2.6.22; beforehand, always
failed with EINTR).
所以我猜你有一个具有类似行为的内核(请参阅 netBSD 文档?)并且你可以观察到系统调用自动重启而你没有任何机会看到它。
就是说,我从你的程序中完全删除了 sem_post() 并且只是发送信号“中断” sem_wait() 并查看我看到的 strace(在蜜蜂线程上过滤):
[pid 8309] futex(0x7fffc0470990, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
[pid 8309] <... futex resumed> ) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
[pid 8309] --- SIGUSR1 {si_signo=SIGUSR1, si_code=SI_TKILL, si_pid=8308, si_uid=500} ---
[pid 8309] rt_sigreturn() = -1 EINTR (Interrupted system call)
[pid 8309] madvise(0x7fd5f6019000, 8368128, MADV_DONTNEED) = 0
[pid 8309] _exit(0)
我必须说我不掌握细节,但内核似乎找出了我试图站在哪里并使整个事情具有正确的行为:
Bee: got RC = -1 and errno = Interrupted system call
关于c - sem_wait 未使用 EINTR 解锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853901/
许多 POSIX 阻塞函数在有信号的情况下返回 EINTR。这个想法是信号处理程序首先设置一个标志(在 SIGINT 的情况下说“停止”标志),然后阻塞函数取消阻塞返回 EINTR,并且应用程序看到该
如果读取大量数据,是否需要检查errno == EINTR?我使用 pread() 函数来读取。在我的一生中,我从未见过 EINTR 返回,但我在网上看到了一些代码,其中明确地检查了它。 那么真的有必
tl;博士 在 Python 中读取管道时,我是否应该处理 EINTR“系统调用中断”错误?如果是,我该如何测试此类代码? 描述 在下面的回溯中,self._dataq 是一个 multiproces
我正在运行一个 epoll 循环,有时我对 epoll_wait 的调用返回 -1,errno 设置为 EINTR。有时,我希望这样可以结束 epoll 循环,例如 SIGTERM 或 SIGINT。
简而言之,您如何对系统调用中的 EINTR 等错误条件进行单元测试。 我正在处理的一个特定示例(它本身可能就是一个案例)是当 fclose 返回带有 (errno==EINTR) 的 EOF 时是否有
我看到了处理 EINTR 错误的代码(读、写系统调用)。我了解中断发生在两种情况下。首先,它发生在调度的每个时间片上。其次,它是通过信号发生的。但是 stackoverflow 中的每个人都说只是信号
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
在我的程序中,我有一个系统调用“sendmsg()”。我想测试如果这个系统调用被中断会发生什么。我应该怎么做? int test(args) { ----- /
我有一个线程负责轮询各种fds。我正在使用设置了超时的 epoll_wait。以下是代码片段: do { n = epoll_wait(epollFd, eventsList, eventsT
我研究发现用socket编程时会出现信号中断的情况。我搜索了一下,发现如果信号中断,我们应该重试。也就是说,我必须捕获错误并重试。我必须像这样创建套接字。 int create_sock() {
有没有专家可以帮助我解决以下问题? 我在 C 中有以下系统调用: access() unlink() setsockopt() fcntl() setsid() socket() bind() lis
来自 man -e 2 wait : The call wait(&status) is equivalent to: waitpid(-1, &status, 0); 错误号值: EINTR WNO
使用 timer_create,我们向等待 select 函数的线程发送实时信号。 这个信号在线程中被捕获和处理。基于事实,当捕获到信号时,选择将被中断,如果 select 失败并显示错误号 EINT
我发现这段代码被使用了好几次(还有一个类似的代码,它使用 open() 而不是 write())。 int c = write(fd, &v, sizeof(v)); if (c == -1 && e
我正在为嵌入式 Linux 系统编写用户应用程序,并且我正在为设备使用诸如打开、关闭、读取、ioctl 等常用功能。现在,我阅读了有关 EINTR 的信息,表明该函数被信号中断,但我不确定其含义。在我
有一些系统调用支持的 I/O 函数(我最近发现至少两个:stat() 和 mkdir() 但几乎可以肯定还有更多)根据 POSIX,可能的 errno 值中没有 EINTR,但肯定可能由于磁盘繁忙或网
在规范和两个实现中: 根据 POSIX,dup2()可能返回 EINTR。 linux 手册页将其列为允许的。 FreeBSD 手册页表明它从未返回。这是一个错误吗 - 因为它的紧密实现可以 EINT
到目前为止我的网络代码工作正常,但我有点担心我隐藏在地毯下的东西: accept、close、connect、recv 和 send 的手册页> 提到 errno.EINTR 可以在系统调用被信号中断
注意: This question与我的相当接近,但我至少可以对提供的解决方案使用一些工作示例,也许 ZeroMQ 会带来一些我不知道的魔力。 目前,我在阻止 ZeroMQ 调用时对异常使用react
我是信号量的新手,想在我的程序中添加多线程,但我无法解决以下问题:只要我没有设置 SA_RESTART,sem_wait() 应该能够接收 EINTR 并解锁旗帜。我正在向在 sem_wait() 中
我是一名优秀的程序员,十分优秀!