- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
希望你能帮我解决这个问题。
对于学校,我必须将 Ctrl+C 转换为不关闭 shell 的命令,但他通过 printf()
提醒我必须键入 exit关闭外壳。我什至不知道从哪里开始。
非常感谢。
最佳答案
这是使用 sigaction
处理 SIGINT
的简单实现这将适用于 posix 系统。为简洁起见,省略了错误检查。链接的手册应该解释sigaction
。
基本上,程序循环执行无限循环并在用户键入退出时中断。使用 write
因为您不能在信号处理程序中使用 printf。参见 signal manual
获取可在信号处理程序中安全使用的函数列表。
#include<stdio.h>
#include<signal.h>
#include<string.h>
#include<stdlib.h>
char s[]="Type 'exit' to terminate\n";
void int_handler (int signum)
{
write(fileno(stdin), s, sizeof s - 1);
}
int main (void)
{
char str[256];
struct sigaction sh;
sh.sa_handler = int_handler;
sigemptyset (&sh.sa_mask);
sh.sa_flags = 0;
sigaction (SIGINT, &sh, NULL);
printf("%s", s);
while(1) {
fgets(str, sizeof str, stdin);
char *p = strchr(str, '\n');
if(p) *p = 0;
if(!strcmp(str, "exit")) {
printf("Exiting on request...");
break;
}
}
return 0;
}
关于捕获 SIGINT 信号以终止自定义 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075177/
脚本1.sh: #!/bin/bash ./script2.sh echo after-script 脚本2.sh: #!/bin/bash function handler { exi
我正在编写一个多线程程序,我想在其中处理来自用户的可能的 Ctrl-C 命令以终止执行。据我所知,不能保证能够取消每个工作线程的主线程会捕获信号。因此,是否有必要为工作线程的代码设置一个不同的信号处理
我正在编写一个多线程程序,我想在其中处理来自用户的可能的 Ctrl-C 命令以终止执行。据我所知,不能保证能够取消每个工作线程的主线程会捕获信号。因此,是否有必要为工作线程的代码设置一个不同的信号处理
标题说明了一切。我该如何处理或捕捉 SIGINT在 Julia ?从文档中我假设我只是想 catch InterruptException使用 try/catch像下面这样块 try whil
以下是此问题的简单重现: void handler(int sig){ if(sig == SIGINT){ printf("Signal caught
鉴于此代码: #include #include #include void sigint_handler(int h) { printf("Hey! I caught a SIGINT
我正在写一个shell,现在它来控制子进程。当我在子进程中使用 signal (SIGTERM, SIG_DFL); 时, 信号 SIGINT 由 Ctrl + C 生成,该信号终止整个操作系统 sh
我正在尝试 SIGINT。我基本上希望我的程序在用户按下 control-c 时立即启动。当这种情况发生时,我将让程序制作一个管道。 现在,我决定当在信号处理程序中按下 control-c 时,它将调
我想处理来自内核的 SIGINT 信号,以便调用一个函数来优雅地关闭我的进程。 这是一个工作示例,一个线程正在等待信号,然后调用函数 handle_stop: #include #include
我有一个 bash 脚本,它启动了 2 个进程: openocd ...flags... 2>openocd.log & arm-none-eabi-gdb 在 gdb 中,使用 Ctrl+C 中断执
我有几个带有 while read line 循环的脚本,当我按下 Ctrl C 时,它们不执行我的清理功能。例如: #!/bin/bash cleanup() { stty echo exi
我想捕获从 Script-A.sh 发送到 Script-B.sh 的信号所以在 Script-A.sh 中我使用命令: (Send SIGINT to Script-B.sh) kill -2 $P
我正在尝试处理 SIGINT。 SIGINT 在我的程序中的主要目的是取消当前搜索功能并打印当前可用的结果。但是每当我 try catch SIGINT 信号时,它都会关闭我的程序。 (我搜索了这么多
我有 2 个程序(由我编写)。第一个名为“MAN”的进程将调用“D”(第二个),这是一个将在后台运行直到以某种方式终止的进程。 我想在不终止 D 的情况下终止 MAN。 我尝试使用 ctrl + c
我应该编写一个 C 程序来处理第一个带有自定义处理程序的 SIGINT,然后重置默认行为。我的自定义 SIGINT 处理程序应该只打印一条消息。这是我写的: #include #include #
如果父进程接收到 ctrl-C 信号,我一直试图阻止我的父线程杀死子进程。我已经没有想法了。 父进程已经捕捉到 SIGINT,所以我想要和现在 child 死了一样。 int main() {
我对Python很陌生,所以如果这个问题非常基本,请原谅我。 我试图在使用选择模块从套接字接受数据时处理键盘中断。因此,我有一个 select.select() 函数调用来等待来自套接字的数据,然后将
我无法理解为什么我的 SIGINT 从未被下面的代码捕获。 #!/usr/bin/env python from threading import Thread from time import sl
我正在编写以下代码。该程序应该能够使用 sigaction 处理 SIGINT。到目前为止,它几乎完成了,但我遇到了两个问题。 第一个问题是如果程序在 3 秒内收到 3 个信号,它应该打印“正在关闭”
在 C 中,我想捕获 SIGINT 信号并打印出类似这样的消息通过使用 sigaction 并通过 向其传递新的处理程序来“接收到 SIGINT” sa.sa_sigaction = handler;
我是一名优秀的程序员,十分优秀!