gpt4 book ai didi

c - 迭代可能信号的列表

转载 作者:行者123 更新时间:2023-11-30 16:14:56 25 4
gpt4 key购买 nike

我有一些代码想要用来打印信号列表,这些信号列表会打印调用进程的信号掩码中的信号名称。

我的起始代码如下:

#include "apue.h" 
#include <errno.h>

void
pr_mask(const char *str)
{
sigset_t sigset;
int errno_save;

errno_save = errno;

if (sigprocmask(0, NULL, &sigset) < 0) {
err_ret("sigprocmask error");
}else {
printf("%s", str);
if (sigismember(&sigset, SIGINT))
printf(" SIGINT");
if (sigismember(&sigset, SIGQUIT))
printf(" SIGQUIT");
if (sigismember(&sigset, SIGUSR1))
printf(" SIGUSR1");
if (sigismember(&sigset, SIGALRM))
printf(" SIGALRM");
printf("\n");
}
errno = errno_save;
}

问题是,我想要做更多的信号,而不仅仅是 SIGINTSIGQUITSIGUSR1SIGALRM 。我不想为每个警报列出一长串 if 语句。

我想做这样的事情:

for(int i = 0; i < 32; i++) {
printf("%s", str)
if(sigismember(sigset, signal_array[i]))
printf("%s", signal_array[i]);
}

其中 signal_array 是这样的:

signal array[32] {
SIGINT, "SIGINT"
...
...
...
}

有什么方法可以做到这一点,以便我更容易打印出每个信号吗?这会让事情变得简单很多。

最佳答案

如果您正在寻找信号列表或数组,那么恐怕 signal.h 中没有定义信号。您唯一能做的就是自己定义它并迭代它。如果需要,您可以在不同的帮助程序文件中定义它,以免污染实际代码。

示例程序:

#include "siglist.h"
#include <stdio.h>
#include <stddef.h>

int main(void) {
sigset_t sigset;

// ... initialize stuff ...

for (size_t i = 0; i < SIGLIST_LENGTH; i++) {
if (sigismember(&sigset, siglist[i].code))
printf("%d - %s: %s.\n", siglist[i].code, siglist[i].name, siglist[i].description);
}

return 0;
}

文件siglist.h(所有这些ifdef都是必需的,因为您无法事先知道哪些信号已定义以及哪些信号不在目标计算机上):

#ifndef SIGLIST_H_INCLUDED
#define SIGLIST_H_INCLUDED

#include <signal.h>

struct siginfo {
int code;
char *name;
char *description;
};

const struct siginfo siglist[] = {
#ifdef SIGABRT
{SIGABRT , "SIGABRT" , "Abort signal"},
#endif
#ifdef SIGALRM
{SIGALRM , "SIGALRM" , "Timer signal"},
#endif
#ifdef SIGBUS
{SIGBUS , "SIGBUS" , "Bus error (bad memory access)"},
#endif
#ifdef SIGCHLD
{SIGCHLD , "SIGCHLD" , "Child stopped or terminated"},
#endif
#ifdef SIGCLD
{SIGCLD , "SIGCLD" , "Child stopped or terminated"},
#endif
#ifdef SIGCONT
{SIGCONT , "SIGCONT" , "Continue if stopped"},
#endif
#ifdef SIGEMT
{SIGEMT , "SIGEMT" , "Emulator trap"},
#endif
#ifdef SIGFPE
{SIGFPE , "SIGFPE" , "Floating-point exception"},
#endif
#ifdef SIGHUP
{SIGHUP , "SIGHUP" , "Hangup detected on controlling terminal or death of controlling process"},
#endif
#ifdef SIGILL
{SIGILL , "SIGILL" , "Illegal Instruction"},
#endif
#ifdef SIGINFO
{SIGINFO , "SIGINFO" , "Power failure"},
#endif
#ifdef SIGINT
{SIGINT , "SIGINT" , "Interrupt from keyboard"},
#endif
#ifdef SIGIO
{SIGIO , "SIGIO" , "I/O now possible"},
#endif
#ifdef SIGIOT
{SIGIOT , "SIGIOT" , "IOT trap: Abort signal"},
#endif
#ifdef SIGKILL
{SIGKILL , "SIGKILL" , "Kill signal"},
#endif
#ifdef SIGLOST
{SIGLOST , "SIGLOST" , "File lock lost "},
#endif
#ifdef SIGPIPE
{SIGPIPE , "SIGPIPE" , "Broken pipe: write to pipe with no readers"},
#endif
#ifdef SIGPOLL
{SIGPOLL , "SIGPOLL" , "Pollable event: I/O now possible"},
#endif
#ifdef SIGPROF
{SIGPROF , "SIGPROF" , "Profiling timer expired"},
#endif
#ifdef SIGPWR
{SIGPWR , "SIGPWR" , "Power failure"},
#endif
#ifdef SIGQUIT
{SIGQUIT , "SIGQUIT" , "Quit from keyboard"},
#endif
#ifdef SIGSEGV
{SIGSEGV , "SIGSEGV" , "Invalid memory reference"},
#endif
#ifdef SIGSTKFLT
{SIGSTKFLT, "SIGSTKFLT", "Stack fault on coprocessor"},
#endif
#ifdef SIGSTOP
{SIGSTOP , "SIGSTOP" , "Stop process"},
#endif
#ifdef SIGTSTP
{SIGTSTP , "SIGTSTP" , "Stop typed at terminal"},
#endif
#ifdef SIGSYS
{SIGSYS , "SIGSYS" , "Bad system call "},
#endif
#ifdef SIGTERM
{SIGTERM , "SIGTERM" , "Termination signal"},
#endif
#ifdef SIGTRAP
{SIGTRAP , "SIGTRAP" , "Trace/breakpoint trap"},
#endif
#ifdef SIGTTIN
{SIGTTIN , "SIGTTIN" , "Terminal input for background process"},
#endif
#ifdef SIGTTOU
{SIGTTOU , "SIGTTOU" , "Terminal output for background process"},
#endif
#ifdef SIGUNUSED
{SIGUNUSED, "SIGUNUSED", "Bad system call "},
#endif
#ifdef SIGURG
{SIGURG , "SIGURG" , "Urgent condition on socket "},
#endif
#ifdef SIGUSR1
{SIGUSR1 , "SIGUSR1" , "User-defined signal 1"},
#endif
#ifdef SIGUSR2
{SIGUSR2 , "SIGUSR2" , "User-defined signal 2"},
#endif
#ifdef SIGVTALRM
{SIGVTALRM, "SIGVTALRM", "Virtual alarm clock "},
#endif
#ifdef SIGXCPU
{SIGXCPU , "SIGXCPU" , "CPU time limit exceeded "},
#endif
#ifdef SIGXFSZ
{SIGXFSZ , "SIGXFSZ" , "File size limit exceeded"},
#endif
#ifdef SIGWINCH
{SIGWINCH , "SIGWINCH" , "Window resize signal"},
#endif
};

const size_t SIGLIST_LENGTH = sizeof(siglist)/sizeof(siglist[0]);

#endif // SIGLIST_H_INCLUDED

关于c - 迭代可能信号的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57339363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com