signal机制疑问

blossom007cn 2009-11-29 06:46:10
最近在看内核代码时,发现LINUX是在进程调度时执行signal关联的handler函数,这会不会导致程序数据不一致,如

struct data
{
int a;
int b;
}g_data;

int fun()
{
g_data.a = 1;
g_data.b = 2;
}

int main()
{
signal( XXX, fun );
g_data.a = 0;
g_data.b = 0;

...
}

如果程序在执行g_data.b = 0;之前执行信号处理函数fun(),执行后g_data.a = 1; g_data.b = 2;而main函数中还认为g_data.a和g_data.b都是0
会不会造成数据不一致的情况出现呀?

...全文
78 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Caballeroo 2009-11-30
  • 打赏
  • 举报
回复
要看信号处理函数是在那个线程中执行,如果在多线程的编程中,当fun函数在其他线程中执行的话,可能出现数据不一致,此时可采用同步的方法解决不一致问题。

要是在main中执行的话,则不会出现不一致。
bluejing0 2009-11-30
  • 打赏
  • 举报
回复
The occurrence of a signal may be synchronous or asynchronous to the process or thread, depending on the source of the signal and the underlying reason or cause. Synchronous signals occur as a direct result of the executing instruction stream, where an unrecoverable error (such as an illegal instruction or illegal address reference) requires an immediate termination of the process. Such signals are directed to the thread which caused the error with its execution stream. As an error of this type causes a trap into a kernel trap handler, synchronous signals are sometimes referred to as traps.

Asynchronous signals are external to (and in some cases, unrelated to) the current execution context. One obvious example would be the sending of a signal to a process from another process or thread via a kill(2), _lwp_kill(2) or sigsend(2) system call, or a thr_kill(3T), pthread_kill(3T) or sigqueue(3R) library invocation. Asynchronous signals are also aptly referred to as interrupts.



bluejing0 2009-11-30
  • 打赏
  • 举报
回复
这个。。真不清楚。。MARK下。

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧