如何判断线程是否响应唤醒呼叫?
How to tell if a thread responded to a wake up call?
我想直接从输入设备读取键盘输入。从这样的文件中读取需要 root 权限,对于程序的其余部分,我不需要,也不想要。
我的计划是以root权限启动程序,然后启动一个工作线程,最后在主线程中删除root权限。然后主线程可以发送请求,要求工作线程处理下一个键盘输入。
我构建了这个最小的例子:
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
cnd_t wakeup;
mtx_t mutex;
enum request {
REQUEST_NOTHING,
REQUEST_PING,
REQUEST_TERMINATION
} request;
int daemon(void *arg) {
(void)arg;
int retval;
retval = mtx_lock(&mutex);
assert(retval == thrd_success);
for(;;) {
request = REQUEST_NOTHING;
retval = cnd_wait(&wakeup, &mutex);
assert(retval == thrd_success);
switch(request) {
case REQUEST_NOTHING:
break;
case REQUEST_PING:
puts("pong.");
break;
case REQUEST_TERMINATION:
retval = mtx_unlock(&mutex);
assert(retval == thrd_success);
return 0;
default:
assert(false);
}
}
}
void send(enum request req) {
int retval;
retval = mtx_lock(&mutex);
assert(retval == thrd_success);
request = req;
retval = mtx_unlock(&mutex);
assert(retval == thrd_success);
// TODO race condition: worker thread my not be listening yet
retval = cnd_signal(&wakeup);
assert(retval == thrd_success);
}
int main() {
int retval;
retval = mtx_init(&mutex, mtx_plain);
assert(retval == thrd_success);
retval = cnd_init(&wakeup);
assert(retval == thrd_success);
thrd_t thread;
retval = thrd_create(&thread, daemon, NULL);
assert(retval == thrd_success);
puts("ping.");
send(REQUEST_PING);
// TODO wait for the worker thread to complete
send(REQUEST_TERMINATION);
retval = thrd_join(thread, NULL);
assert(retval == thrd_success);
cnd_destroy(&wakeup);
mtx_destroy(&mutex);
}
我目前还没有实现权限的东西,但我已经有足够的问题了:
cnd_signal
不表示是否有人收到信号:
Unblocks one thread that currently waits on condition variable pointed to by cond. If no threads are blocked, does nothing and returns thrd_success
.
Source: cppreference.com
这会导致竞争条件,因为主线程在写入 request
之前需要锁定互斥量,但工作线程在等待信号之前也需要锁定互斥量。
主线程不等待工作线程完成。在这个例子中这不是问题,因为 (1) 主线程在发送下一个请求之前必须等待获得锁,并且 (2) 线程可以在退出前简单地加入。
不过在我的实际程序中我需要等待。显而易见的解决方案是引入另一对 cnd_t
和 mtx_t
允许工作线程唤醒主线程。但是对于这样一个简单的问题来说,这似乎过于复杂了。
我找不到很多资源来演示 C11 的线程库的使用,而且可能都在错误的轨道上。我会很感激一些反馈,也许是上述问题的解决方案。
在您的情况下,您不必在您的进程中永久保留 root
特权。
您不需要 root
从输入设备读取。只有在打开它时才会检查权限。
打开设备后,您的进程可以放弃 root
特权并继续其工作,以非特权用户身份从打开的文件描述符中读取,因此您不需要单独的进程或线程。
尽早放弃扩展权限是实现对特殊资源的访问同时将安全风险降至最低的标准程序。
另见 https://www.oreilly.com/library/view/secure-programming-cookbook/0596003943/ch01s03.html
在其他情况下,您可能需要永久保留特殊权限。在这种情况下,将需要提升权限的任务与其他任务分开的想法很好,但您必须使用单独的进程而不是线程。
免责声明:虽然我的问题已经被@dunes 解决了,而且我了解到权限是在流程基础上处理的(感谢@solomon),但我发现离开这个很恼火没有解决竞争条件本身的答案的问题。
事实证明,我只是误解了这些 cnd_t
变量应该如何使用。诀窍是在启动工作线程之前 锁定互斥量 并让它在等待信号时解锁互斥量。当向线程发出信号时,在向线程发出信号之前获得锁,并且在请求完成之前不会释放
已发送,信号消失。
这个程序不再有竞争条件:
#include <assert.h>
#include <threads.h>
mtx_t mx_wakeup;
cnd_t cd_wakeup;
enum request {
REQ_NOTHING,
REQ_TERMINATE
} request;
int daemon(void *arg) {
(void)arg;
for(;;) {
request = REQ_NOTHING;
int retval = cnd_wait(&cd_wakeup, &mx_wakeup);
assert(retval == thrd_success);
if(request == REQ_TERMINATE) {
return 0;
}
}
}
void send(enum request request_) {
int retval;
// The worker thread will unlock the mutex implicitly when
// waiting for a signal, block until that happens.
retval = mtx_lock(&mx_wakeup);
assert(retval == thrd_success);
request = request_;
retval = cnd_signal(&cd_wakeup);
assert(retval == thrd_success);
// The worker thread needs to lock the mutex before waking up,
// this ensures that it doesn't before receiving the signal.
retval = mtx_unlock(&mx_wakeup);
assert(retval == thrd_success);
}
int main() {
int retval;
retval = mtx_init(&mx_wakeup, mtx_plain);
assert(retval == thrd_success);
retval = cnd_init(&cd_wakeup);
assert(retval == thrd_success);
// The mutex will be unlocked by the worker thread when listening.
retval = mtx_lock(&mx_wakeup);
assert(retval == thrd_success);
thrd_t thread;
retval = thrd_create(&thread, daemon, NULL);
assert(retval == thrd_success);
send(REQ_TERMINATE);
retval = thrd_join(thread, NULL);
assert(retval == thrd_success);
cnd_destroy(&cd_wakeup);
mtx_destroy(&mx_wakeup);
}
等待工作线程现在只是添加另一个条件变量。只有终止代码
必须通过释放mutex,否则主线程会一直等待锁。
#include <assert.h>
#include <threads.h>
mtx_t mx_wakeup;
cnd_t cd_wakeup, cd_complete;
enum request {
REQ_NOTHING,
REQ_TERMINATE
} request;
int daemon(void *arg) {
(void)arg;
int retval;
for(;;) {
request = REQ_NOTHING;
retval = cnd_wait(&cd_wakeup, &mx_wakeup);
assert(retval == thrd_success);
// The request can be processed here.
// Inform the main thread that the request was completed. The main
// thread can choose to wait or not.
retval = cnd_signal(&cd_complete);
assert(retval == thrd_success);
// Termination is different because the mutex wouldn't be released
// by the next `cnd_wait`, and must happend after the signal was send.
if(request == REQ_TERMINATE) {
retval = mtx_unlock(&mx_wakeup);
assert(retval == thrd_success);
return 0;
}
}
}
void send(enum request request_) {
int retval;
retval = mtx_lock(&mx_wakeup);
assert(retval == thrd_success);
request = request_;
retval = cnd_signal(&cd_wakeup);
assert(retval == thrd_success);
// This unlocks the mutex thus allowing the worker thread to process the
// request, thus the mutex can be reused here.
retval = cnd_wait(&cd_complete, &mx_wakeup);
assert(retval == thrd_success);
retval = mtx_unlock(&mx_wakeup);
assert(retval == thrd_success);
}
int main() {
int retval;
retval = mtx_init(&mx_wakeup, mtx_plain);
assert(retval == thrd_success);
retval = cnd_init(&cd_wakeup);
assert(retval == thrd_success);
// Remember to initialize the new conditional variable.
retval = cnd_init(&cd_complete);
assert(retval == thrd_success);
retval = mtx_lock(&mx_wakeup);
assert(retval == thrd_success);
thrd_t thread;
retval = thrd_create(&thread, daemon, NULL);
assert(retval == thrd_success);
send(REQ_TERMINATE);
retval = thrd_join(thread, NULL);
assert(retval == thrd_success);
cnd_destroy(&cd_wakeup);
mtx_destroy(&mx_wakeup);
}
我想直接从输入设备读取键盘输入。从这样的文件中读取需要 root 权限,对于程序的其余部分,我不需要,也不想要。
我的计划是以root权限启动程序,然后启动一个工作线程,最后在主线程中删除root权限。然后主线程可以发送请求,要求工作线程处理下一个键盘输入。
我构建了这个最小的例子:
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
cnd_t wakeup;
mtx_t mutex;
enum request {
REQUEST_NOTHING,
REQUEST_PING,
REQUEST_TERMINATION
} request;
int daemon(void *arg) {
(void)arg;
int retval;
retval = mtx_lock(&mutex);
assert(retval == thrd_success);
for(;;) {
request = REQUEST_NOTHING;
retval = cnd_wait(&wakeup, &mutex);
assert(retval == thrd_success);
switch(request) {
case REQUEST_NOTHING:
break;
case REQUEST_PING:
puts("pong.");
break;
case REQUEST_TERMINATION:
retval = mtx_unlock(&mutex);
assert(retval == thrd_success);
return 0;
default:
assert(false);
}
}
}
void send(enum request req) {
int retval;
retval = mtx_lock(&mutex);
assert(retval == thrd_success);
request = req;
retval = mtx_unlock(&mutex);
assert(retval == thrd_success);
// TODO race condition: worker thread my not be listening yet
retval = cnd_signal(&wakeup);
assert(retval == thrd_success);
}
int main() {
int retval;
retval = mtx_init(&mutex, mtx_plain);
assert(retval == thrd_success);
retval = cnd_init(&wakeup);
assert(retval == thrd_success);
thrd_t thread;
retval = thrd_create(&thread, daemon, NULL);
assert(retval == thrd_success);
puts("ping.");
send(REQUEST_PING);
// TODO wait for the worker thread to complete
send(REQUEST_TERMINATION);
retval = thrd_join(thread, NULL);
assert(retval == thrd_success);
cnd_destroy(&wakeup);
mtx_destroy(&mutex);
}
我目前还没有实现权限的东西,但我已经有足够的问题了:
cnd_signal
不表示是否有人收到信号:Unblocks one thread that currently waits on condition variable pointed to by cond. If no threads are blocked, does nothing and returns
thrd_success
.
Source: cppreference.com这会导致竞争条件,因为主线程在写入
request
之前需要锁定互斥量,但工作线程在等待信号之前也需要锁定互斥量。主线程不等待工作线程完成。在这个例子中这不是问题,因为 (1) 主线程在发送下一个请求之前必须等待获得锁,并且 (2) 线程可以在退出前简单地加入。
不过在我的实际程序中我需要等待。显而易见的解决方案是引入另一对
cnd_t
和mtx_t
允许工作线程唤醒主线程。但是对于这样一个简单的问题来说,这似乎过于复杂了。
我找不到很多资源来演示 C11 的线程库的使用,而且可能都在错误的轨道上。我会很感激一些反馈,也许是上述问题的解决方案。
在您的情况下,您不必在您的进程中永久保留 root
特权。
您不需要 root
从输入设备读取。只有在打开它时才会检查权限。
打开设备后,您的进程可以放弃 root
特权并继续其工作,以非特权用户身份从打开的文件描述符中读取,因此您不需要单独的进程或线程。
尽早放弃扩展权限是实现对特殊资源的访问同时将安全风险降至最低的标准程序。
另见 https://www.oreilly.com/library/view/secure-programming-cookbook/0596003943/ch01s03.html
在其他情况下,您可能需要永久保留特殊权限。在这种情况下,将需要提升权限的任务与其他任务分开的想法很好,但您必须使用单独的进程而不是线程。
免责声明:虽然我的问题已经被@dunes 解决了,而且我了解到权限是在流程基础上处理的(感谢@solomon),但我发现离开这个很恼火没有解决竞争条件本身的答案的问题。
事实证明,我只是误解了这些 cnd_t
变量应该如何使用。诀窍是在启动工作线程之前 锁定互斥量 并让它在等待信号时解锁互斥量。当向线程发出信号时,在向线程发出信号之前获得锁,并且在请求完成之前不会释放
已发送,信号消失。
这个程序不再有竞争条件:
#include <assert.h>
#include <threads.h>
mtx_t mx_wakeup;
cnd_t cd_wakeup;
enum request {
REQ_NOTHING,
REQ_TERMINATE
} request;
int daemon(void *arg) {
(void)arg;
for(;;) {
request = REQ_NOTHING;
int retval = cnd_wait(&cd_wakeup, &mx_wakeup);
assert(retval == thrd_success);
if(request == REQ_TERMINATE) {
return 0;
}
}
}
void send(enum request request_) {
int retval;
// The worker thread will unlock the mutex implicitly when
// waiting for a signal, block until that happens.
retval = mtx_lock(&mx_wakeup);
assert(retval == thrd_success);
request = request_;
retval = cnd_signal(&cd_wakeup);
assert(retval == thrd_success);
// The worker thread needs to lock the mutex before waking up,
// this ensures that it doesn't before receiving the signal.
retval = mtx_unlock(&mx_wakeup);
assert(retval == thrd_success);
}
int main() {
int retval;
retval = mtx_init(&mx_wakeup, mtx_plain);
assert(retval == thrd_success);
retval = cnd_init(&cd_wakeup);
assert(retval == thrd_success);
// The mutex will be unlocked by the worker thread when listening.
retval = mtx_lock(&mx_wakeup);
assert(retval == thrd_success);
thrd_t thread;
retval = thrd_create(&thread, daemon, NULL);
assert(retval == thrd_success);
send(REQ_TERMINATE);
retval = thrd_join(thread, NULL);
assert(retval == thrd_success);
cnd_destroy(&cd_wakeup);
mtx_destroy(&mx_wakeup);
}
等待工作线程现在只是添加另一个条件变量。只有终止代码 必须通过释放mutex,否则主线程会一直等待锁。
#include <assert.h>
#include <threads.h>
mtx_t mx_wakeup;
cnd_t cd_wakeup, cd_complete;
enum request {
REQ_NOTHING,
REQ_TERMINATE
} request;
int daemon(void *arg) {
(void)arg;
int retval;
for(;;) {
request = REQ_NOTHING;
retval = cnd_wait(&cd_wakeup, &mx_wakeup);
assert(retval == thrd_success);
// The request can be processed here.
// Inform the main thread that the request was completed. The main
// thread can choose to wait or not.
retval = cnd_signal(&cd_complete);
assert(retval == thrd_success);
// Termination is different because the mutex wouldn't be released
// by the next `cnd_wait`, and must happend after the signal was send.
if(request == REQ_TERMINATE) {
retval = mtx_unlock(&mx_wakeup);
assert(retval == thrd_success);
return 0;
}
}
}
void send(enum request request_) {
int retval;
retval = mtx_lock(&mx_wakeup);
assert(retval == thrd_success);
request = request_;
retval = cnd_signal(&cd_wakeup);
assert(retval == thrd_success);
// This unlocks the mutex thus allowing the worker thread to process the
// request, thus the mutex can be reused here.
retval = cnd_wait(&cd_complete, &mx_wakeup);
assert(retval == thrd_success);
retval = mtx_unlock(&mx_wakeup);
assert(retval == thrd_success);
}
int main() {
int retval;
retval = mtx_init(&mx_wakeup, mtx_plain);
assert(retval == thrd_success);
retval = cnd_init(&cd_wakeup);
assert(retval == thrd_success);
// Remember to initialize the new conditional variable.
retval = cnd_init(&cd_complete);
assert(retval == thrd_success);
retval = mtx_lock(&mx_wakeup);
assert(retval == thrd_success);
thrd_t thread;
retval = thrd_create(&thread, daemon, NULL);
assert(retval == thrd_success);
send(REQ_TERMINATE);
retval = thrd_join(thread, NULL);
assert(retval == thrd_success);
cnd_destroy(&cd_wakeup);
mtx_destroy(&mx_wakeup);
}