Mis*_*ash 5 c operating-system signals process
我正在编写一个程序,它从文件中获取UNIX命令列表并按顺序执行它们.为了保持一切顺序,我必须初始化每个命令并保持等待SIGUSR1通过sigwait().初始化每个命令时,每个命令都可以执行.
用法: > program.c input.txt
然而,似乎SIGUSR1被反复调用,完全超越sigwait().这里发生了什么?我尝试了很多不同的东西,但它最近是在这个答案之后建模的.换句话说,我希望在初始化后立即为命令引发信号.我希望在完全初始化所有命令时解除阻塞信号
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void on_sigusr1(int sig)
{
// Note: Normally, it's not safe to call almost all library functions in a
// signal handler, since the signal may have been received in a middle of a
// call to that function.
printf("SIGUSR1 received!\n");
}
int main(int arc, char* argv[])
{
FILE *file;
file = fopen(argv[1] ,"r");
int BUF_SIZE = 100;
char *token;
char buffer[BUF_SIZE];
char programs[BUF_SIZE];
char *commands[BUF_SIZE];
int i = 0;
int counter = 1;
while (fgets(buffer, sizeof buffer, file ) != NULL)
{
strcpy(programs, buffer);
int length = strlen(buffer)-1;
if (buffer[length] == '\n')
{
buffer[length] = '\0';
}
i = 0;
token = strtok(buffer," ");
while(token != NULL)
{
commands[i++] = token;
token = strtok(NULL, " ");
}
commands[i] = 0;
pid_t pids[counter];
// Set a signal handler for SIGUSR1
signal(SIGUSR1, &on_sigusr1);
// At program startup, SIGUSR1 is neither blocked nor pending, so raising it
// will call the signal handler
raise(SIGUSR1);
// Now let's block SIGUSR1
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGUSR1);
sigprocmask(SIG_BLOCK, &sigset, NULL);
// SIGUSR1 is now blocked, raising it will not call the signal handler
printf("About to raise SIGUSR1\n");
raise(SIGUSR1);
printf("After raising SIGUSR1\n");
for(i = 0; i < counter; ++i)
{
pids[i] = fork();
if(pids[i] > 0)
{
printf("Child process %d ready to execute command %s", getpid(), programs);
// SIGUSR1 is now blocked and pending -- this call to sigwait will return
// immediately
int sig;
int result = sigwait(&sigset, &sig);
if(result == 0) {
printf("Child process %d executing command %s", getpid(), programs);
execvp(commands[0], commands);
}
}
}
// All programs have been launched
for(i = 0; i < counter; ++i)
{
wait(&pids[i]);
}
// All programs are waiting to execute
for (i = 0; i < counter; ++i)
{
// SIGUSR1 is now no longer pending (but still blocked). Raise it again and
// unblock it
raise(SIGUSR1);
printf("About to unblock SIGUSR1\n");
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
printf("Unblocked SIGUSR1\n");
}
}
exit(0);
fclose(file);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更新:尝试signal()改为sigaction().没变.
在检查该 pid 是否是子进程后,您应该考虑调用 sigwait 。
所以也许把
整数信号;
和
int 结果 = sigwait(&sigset, &sig);
在 if 语句中检查 pid 是否 == 0,这表明它是一个子进程。否则你将等待父进程。
如果 pid 大于 0,则它是父进程 id;如果它小于零,则它是一个错误。
然后,对于 pid 数组中的每个进程,您将调用kill(pid_array[i], SIGUSR1) 来解锁它。
| 归档时间: |
|
| 查看次数: |
751 次 |
| 最近记录: |