我有一个有点愚蠢的问题,如果我有一系列创建的进程......这些不一定是某种继承,进程的pid必须是数字连续或随机的pid?
我在读Beej的fork()底漆,当我打电话pid = fork();,父进程得到子进程的PID,而孩子的过程中pid = 0.
现在,因为子进程开始执行代码的一部分后的fork()声明,请问pid获得价值0?
如何ls在 bash 中找到正在运行的进程的 PID ?假设我知道进程的名称。使用ps会很容易。如果我要搜索 firefox,它只会是ps ax | grep firefox. 但是为了争论,假设我不能这样做,我必须使用ls.
任何帮助都会很棒!
我有一个充满进程句柄的数组,现在我正在尝试获取每个进程句柄的相关进程ID.
但是,我的所有Process ID都回归零.任何人都能指出我错过了哪个明显的问题?
非常感谢
'child'数组填充了进程ID,因此:
currChild = FindWindowEx(hParent, prevChild, null, null);
Run Code Online (Sandbox Code Playgroud)
然后我尝试获取进程ID:
for (int i = 0; i < children.Count; ++i)
{
handle = children[i];
pid = GetProcessId(handle);
Console.WriteLine(children[i].ToString("X") + " : " + pid.ToString());
Run Code Online (Sandbox Code Playgroud)
API:
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("kernel32.dll", EntryPoint = "GetProcessId", CharSet = CharSet.Auto)]
static extern int GetProcessId(IntPtr handle);
Run Code Online (Sandbox Code Playgroud)
输出:
2417E2 : 0
B20D9A : 0
48108C : 0
8809D6 : 0
B5140E …Run Code Online (Sandbox Code Playgroud) 我有这个:
trap on_ql_trap EXIT;
trap on_ql_trap INT;
trap on_ql_trap TERM;
echo "pid that called trap: $$"
Run Code Online (Sandbox Code Playgroud)
这可能可以变成速记:
trap on_ql_trap EXIT INT TERM;
echo "pid that called trap: $$"
Run Code Online (Sandbox Code Playgroud)
当我杀死调用陷阱的进程/pid 时,使用
kill <pid>
Run Code Online (Sandbox Code Playgroud)
或者
kill -9 <pid>
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,什么也不会发生。这个过程继续存在。在使用 -9 的第二种情况下,进程终止,但不会调用陷阱。所以这两个 kill 命令都没有做我想要的!我希望进程退出,但我需要先调用陷阱。有谁知道为什么会这样?
打开-Wextra标志gcc似乎具有禁止structs部分初始化的效果。例如:
// main.c
#include <pthread.h>
typedef struct S {
int i;
pid_t pid;
} S;
int main( int argc, char* argv[] ) {
(void)argc;
(void)argv;
S s = { 42 };
(void)s;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ gcc --version && gcc -Wall -Wextra -pedantic -Werror ./main.c
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for …Run Code Online (Sandbox Code Playgroud) 我启动gen_fsm:
start_link() ->
gen_fsm:start_link({global, ?MODULE}, [],
[]).
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到它的PID?
谢谢.
这是测试fork()系统调用的C代码:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<wait.h>
int main(int argc, char *argv[])
{
printf("I am: %d\n", (int)getpid());
pid_t pid = fork();
printf("fork returned: %d\n", (int)pid);
if (pid < 0)
{
perror("fork failed");
}
if (pid==0)
{
printf("I am the child with pid %d\n", (int)getpid());
sleep(5);
printf("Child exiting...\n");
exit(0);
}
printf("I am the parent with pid %d, waiting for the child\n", (int)getpid());
wait(NULL);
printf("Parent ending. \n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
终端输出是:
I am: 25110
fork returned: 25111
I am the parent with pid …Run Code Online (Sandbox Code Playgroud)