[已解决]linux里有没有使进程创建后马上挂起的函数

软件和网站开发以及相关技术探讨
回复
头像
wxf
帖子: 50
注册时间: 2008-05-28 8:50

[已解决]linux里有没有使进程创建后马上挂起的函数

#1

帖子 wxf » 2023-08-10 10:25

好像没有是吧,fork exec, 甚至clone都没有这样的功能啊。

windows里,CreateProcess函数执行时,可以传递一个参数 CREATE_SUSPENDED,使进程创建后不执行,然后父进程可以对这个进程做一些设置,比如注入一个dll,然后调用ResumeThread(lpProcessInformation->hThread); 恢复进程的执行。

linux里没有这样的机制吗?
上次由 wxf 在 2023-08-10 14:36,总共编辑 1 次。
头像
astolia
论坛版主
帖子: 6452
注册时间: 2008-09-18 13:11

Re: linux里有没有使进程创建后马上挂起的函数

#2

帖子 astolia » 2023-08-10 13:44

代码: 全选

pid_t pid = fork();
if (pid == 0) {
    ptrace(PTRACE_TRACEME, 0, NULL, 0);
    execlp("ls", "ls", NULL);
} else {
    int status;
    waitpid(pid, &status, 0);
    if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
        printf("child %d suspended, press Enter to continue\n", pid);
        getchar();
        ptrace(PTRACE_CONT, pid, NULL, 0);
    }
}
头像
wxf
帖子: 50
注册时间: 2008-05-28 8:50

Re: linux里有没有使进程创建后马上挂起的函数

#3

帖子 wxf » 2023-08-10 14:35

astolia 写了: 2023-08-10 13:44

代码: 全选

pid_t pid = fork();
if (pid == 0) {
    ptrace(PTRACE_TRACEME, 0, NULL, 0);
    execlp("ls", "ls", NULL);
} else {
    int status;
    waitpid(pid, &status, 0);
    if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
        printf("child %d suspended, press Enter to continue\n", pid);
        getchar();
        ptrace(PTRACE_CONT, pid, NULL, 0);
    }
}
get到知识了!谢谢大佬
回复