- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在用 C 实现简单的 shell 模拟器。它应该支持在后台运行命令(例如 sleep 5s &)。因此,我使用 fork() -> exec() 序列运行此命令,并使用 SIGCHLD 信号处理程序等待该命令完成。我的问题是,是否有可能获得在 exec() 调用中指定的进程命令(名称)。我举个例子:
//SIGCHLD signal handler (use for notification, when background process ends)
void sighandler(int sig) {
int status;
int pid = waitpid(-1, &status, WNOHANG);
if (WIFEXITED(status)) {
if (pid != -1 && pid != 0) {
printf("\n[%d] Done: \n> ", (int) pid); //also need to provide user name (command) of exited process (ex. [PID] Done: sleep)
fflush(stdout);
}
}
}
pid_t fork_pid = fork()
if (fork_pid == 0) { //child
execl("sleep", "sleep", "5s");
} else { //parent
....
}
我需要的是以某种方式访问命令的名称,它在 exec() 调用中指定(请参阅 execl("sleep", "sleep", "5s");)在信号处理程序中(请参阅 signalhandler(int sig) ) 输出类似 [PID] Done: sleep 的内容。
我不能使用上次运行的命令的简单全局变量,因为在后台运行一些命令后,前台可能会出现更多新命令,并将重写该全局变量。示例:
> sleep 1m & //background command - variable is "sleep"
[PID of sleep process] Running in background
> ls //foreground command - variable is "ls"
> cat output //foreground command - variable is "cat"
.
.
.
[PID of sleep process] Done: sleep
>
有什么办法吗?非常感谢!
最佳答案
您可以创建一个列表来将 pids 与命令相关联:
void sighandler(int sig) {
...
char *cmd = task_list.get(pid);
printf("\n[%d] %s, Done: \n> ", (int) pid, cmd);
...
}
...
pid_t fork_pid = fork();
char command[1024] = "sleep";
if (fork_pid == 0) { //child
execl(command, command, "5s");
} else { //parent
task_list.insert(pid, command);
}
关于c - 从进程中提取命令(使用 PID),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9532634/
manpage of reboot()说 Behavior inside PID namespaces Since Linux 3.4, if reboot() is called from a PI
我今天第一次在 Nvidia jetson Xavier 中安装和配置并使用了电视, 但重启后我遇到了这个问题: teamviewerd.service: 启动后无法打开 PID 文件/var/run
我在我的服务器上安装了 hhvm,在我重新启动服务器之前它一直运行良好。在 hhvm 的日志中,我看到了这个错误: Unable to read pid file /var/run/hhvm/pid
我正在尝试为我的应用程序精简,但随后无法生成 pid: $ thin -C /var/www/project_path/current/config/myproject.testing.yml sta
我正在从我的私有(private) git 存储库安装应用程序。我安装了所有依赖项并且我正在使用 Capistrano。我能够在我的本地计算机上成功运行应用程序。我正在使用 rails -v 3.2.
我已按照 DigitalOcean 指南中的步骤进行操作 here和 here使用 nginx 和 Unicorn 设置 Sinatra 服务器。我在倒数第二步: start the Unicorn
我在 C 程序中连续进行了 3 个 fork 。 1.它会以相同的顺序执行吗? (我的猜测是肯定的)。 2. 如果我做 pgrep myexecutable从 shell 中,它会按照启动的顺序给出进
我尝试通过FT_Prog更改FTDI芯片(R232R)中的PID。它可以工作,但之后我发现 Windows 7 自动重新安装 USB 设备的驱动程序,而不是 FTDI 设备。所以我想将PID改回默认值
第一次在这里发表 简单情况:在 PUTTY 中,我必须创建一个名为 admin.pid 的文件,当用户启动我正在创建的“应用程序”时,它会在其中存储 PID。我怎样才能做到这一点?谢谢 最佳答案 使用
我设法为每个单独的进程输出正确的进程 ID 顺序,但我的问题是我无法显示子进程的 PID。 我的程序能够打印 parent 的 PID 和孙子的 PID。我确实看到了 child 的 PID,但它显示
我正在从事一个项目,其中有许多 PID,我必须找出其中哪些是僵尸进程,然后终止它们的父进程以终止初始僵尸进程。我不确定是否有任何方法可以找出给定 PID 的 PPID 是什么。任何帮助将不胜感激。 最
我正在使用 htop,所以看看哪些进程占用了大量内存,以便我可以杀死它们。我有很多 tmux session 和很多类似的过程。如何检查 PID 所在的 tmux Pane ,以便确定我正在杀死我想杀
我正在通过运行跟踪应用程序: strace -f -y -qq -z -etrace=execve,... -o app.trace ./app 有没有办法确定哪个进程产生了哪个 child_proc
在我使用 exec 之后docker 容器内的命令我可以使用 exec inspect 获取 PID .问题是这个 ID 不是容器本地的,而是系统 PID。所以我会得到类似 22620 的东西,而 d
我有一个我开发的用于启动 Java 程序的 System V 初始化脚本。由于某种原因,无论何时创建 PID 文件,它都包含多个 PID 而不是一个。 下面是启动服务并写入PID文件的相关代码: da
我有一个变量 pidfile,它存储进程的 PID。 如何使用 Ruby 以编程方式终止 pidfile 中的 pid,假设我只知道文件名,而不是其中的实际 PID。 最佳答案 Process.kil
我读入了Beej's fork() primer当我调用 pid = fork(); 时,父进程获取子进程的 pid,而在子进程内部 pid = 0。 现在,由于子进程开始执行 在 fork() 语句
我正在尝试从另一个 Python 脚本运行一个 Python 脚本,并获取它的 pid 以便稍后可以终止它。 我尝试使用参数 shell=True' 的 subprocess.Popen(),但是pi
我有一个用 Cygwin 生成的进程shell 脚本,我无法用 kill 杀死它命令。即使与 Cygwin kill与 -f选项,我收到此消息: kill: couldn't open pid 123
我尝试在我的模型中为阀门构建一个 PID Controller ,我计划进行一些过程识别,获得系统对阶跃脉冲的响应和系统的传递函数,然后我可以设计 PID Controller 。但我不确定是否有用于
我是一名优秀的程序员,十分优秀!