- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要同步父进程和子进程以在无限循环中按顺序工作,但一段时间后我陷入僵局。
不允许使用sleep、wait,必须用信号来实现同步。经过搜索,我发现了一个类似的问题,使用了暂停方法,在答案中建议使用 sigsuspend 而不是暂停。所以我不确定我的实现是否有问题或者我需要使用另一种方法来避免死锁
这是我的代码
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
void catcher( int sig ) {
printf( "inside catcher() function\n" );
}
void timestamp( char *str ) {
time_t t;
time( &t );
printf( "%s the time is %s\n", str, ctime(&t) );
}
int main( int argc, char *argv[] ) {
struct sigaction sigact;
sigset_t block_set1,block_set2;
sigfillset( &block_set1 );
sigfillset( &block_set2 );
sigdelset( &block_set1, SIGUSR1 );
sigdelset( &block_set2, SIGUSR2 );
sigemptyset( &sigact.sa_mask );
sigact.sa_flags = 0;
sigact.sa_handler = catcher;
sigaction( SIGUSR1, &sigact, NULL );
sigaction( SIGUSR2, &sigact, NULL );
pid_t child_id;
child_id = fork ();
if (child_id == 0){
while(1){
printf("child send signal\n");
kill (getppid (), SIGUSR1);
printf("child wait signal\n");
sigsuspend( &block_set2 );
printf("child is going to die\n");
}
}else{
while(1){
timestamp( "before sigsuspend()" );
sigsuspend( &block_set1 );
timestamp( "after sigsuspend()" );
kill (child_id, SIGUSR2);
printf("parent is going to die\n");
}
}
return( 0 );
}
最佳答案
这个程序有一个问题,考虑这样一种情况:子进程/父进程甚至在使用 sigsuspend() 暂停执行之前就向父进程/子进程发送信号。在这种情况下,处理程序将被执行,之后就没有人会被执行他们中断 sigsupend() ,这可能会导致无限挂起状态。
引用手册页
int sigsuspend(const sigset_t *mask);
sigsuspend() temporarily replaces the signal mask of the calling process with the mask given by mask and then suspends the process until delivery of a signal whose action is to invoke a signal handler or to terminate a process.
我建议使用 sigwaitinfo() 代替,
int sigwaitinfo(const sigset_t *set, siginfo_t *info);
sigwaitinfo() suspends execution of the calling thread until one of the signals in set is pending (If one of the signals in set is already pending for the calling thread, sigwaitinfo() will return immediately.)
请找到相同的示例程序。
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
void timestamp( char *str ) {
time_t t;
time( &t );
printf( "%s the time is %s\n", str, ctime(&t) );
}
int main( int argc, char *argv[] ) {
sigset_t parent_set, child_set;
sigset_t set;
siginfo_t info;
/*Mask SIGUSR2 and SIGUSR1 signal, you can choose this mask
* according to your requirement*/
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGUSR2);
sigprocmask(SIG_SETMASK, &set, NULL);
sigemptyset(&child_set);
sigaddset(&child_set, SIGUSR2);
sigemptyset(&parent_set);
sigaddset(&parent_set, SIGUSR1);
pid_t child_id;
child_id = fork ();
if (child_id == 0){
while(1){
printf("child send signal\n");
kill (getppid (), SIGUSR1);
printf("child wait signal\n");
/* Child will suspend its execution if parent did not notify it
* using signal(SIGUSR2), if parent has already notified sigwaitinfo()
* will return immediately
**/
sigwaitinfo(&child_set, &info );
printf("child is going to die\n");
}
}else{
while(1){
timestamp( "before sigwaitinfo()" );
/* Parent will suspend its execution if child did not notify it
* using signal(SIGUSR1), if child has already notified sigwaitinfo()
* will return immediately
**/
sigwaitinfo(&parent_set, &info );
timestamp( "after sigwaitinfo()" );
kill (child_id, SIGUSR2);
printf("parent is going to die\n");
}
}
}
希望这对您有帮助。
关于c - 进程同步与信号死锁情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49668479/
我正在创建一个 sql server 存储过程,它将输入作为逗号分隔的 productid 或选项“全部”。当用户传入逗号分隔的产品 ID 时,查询应输出所有产品 ID 的数据。我使用“IN”语句执行
我有一个自动生成的 Web 服务客户端。我有很多复杂的类,我必须对其进行模式匹配。现在我的结构如下所示: val response = client.getResponse response matc
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 7年前关闭。 Improve this
我需要正确的 tsql 语法来解决这个问题: Select * from table where var_A='10' select * from table where var_B='10' 何时使
我遇到了这个问题。每当我运行程序并在需要时键入字母 m 时,我的 if 语句都不会识别它。有人知道为什么吗?我已经这样做了一个小时,但没有结果。 #include #include #includ
我从数据库列名称“你有护照”创建了一个表,用户回答是或否我如何将 css 应用到这个动态工作的表。 table, th, td { border: 1px solid black;
我对 LocationListener 类的 onStatusChanged 有一些疑问。 它知道它可以呈现三种状态:AVAILABLE、TEMPORARILY_UNAVAILABLE 和 OUT_O
当引入新的异常类型时,我总是不确定如何正确地做到这一点。有共同约定吗?你怎么做呢? 我对您组织它们的范围感兴趣(将它们保留在它们所使用的单元中?在组件级别有一个单元?包级别?应用程序?) 这也会影响命
我使用以下内容创建了日期维度: https://www.codeproject.com/Articles/647950/Create-and-Populate-Date-Dimension-for-D
您好,我正在使用 Android 完全 Kiosk 浏览器,该浏览器使用 chrome Webview。但是 javascript 中的某些方法或函数无法正常工作,例如 window.print()。
我有以下代码: public void OpenFile(string FileName) { if (FileName == null)
获取索引越界异常 for (int recordData = 0; recordData < recordDataList.size(); recordData++) {
我使用它在发生错误时在登录中显示一条消息: × Invalid user or password
这是我的场景,我有一个异常列表,其中包含来自不同层次结构的任意异常,下面的代码快照将解释我需要做什么 private List connectionExceptions; try { // tryin
我尝试动态更新 Jtextpane 中的左缩进。但我不能!这是我尝试过的! DefaultStyledDocument document = (DefaultStyledDocument) textp
我不知道为什么这个异常不起作用...... import java.util.*; public class a { public static void main(String[] args
我目前在 case 中使用多个 when 时遇到问题。当我删除第二个当时,它就起作用了。这是什么问题? 报告的MYSQL错误为: #1064 - You have an error in your S
例如,我有一个表记录用户查看和下载文件的事件, file_id user activity 2 Tim view 1 Ron
这是一个非常愚蠢的问题,但我需要一点安慰/帮助。我有当前的“递归”情况: void add( int value ) { // do something ... // if ( conditi
我尝试使用以下代码在按钮数组上注册回调。但我似乎无法理解如何绑定(bind)回调中需要的字符串。任何建议将不胜感激! for (var i = 0; i < this.car_types.length
我是一名优秀的程序员,十分优秀!