Linux-linux和windows创建新进程的区别以及posix_spawn

Linux上创建一个新的进程可以使用fork函数,这个函数执行的效果是复制当前进程创建一个新的进程,这个进程执行的代码与父进程相同。通过fork返回的pid我们可以在fork后的代码中区分父进程和子进程需要执行的逻辑。示例代码如下:

c 复制代码
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main() {
    pid_t pid = fork();
    
    if (pid < 0) {
        fprintf(stderr, "Fork failed");
        return 1;
    } else if (pid == 0) {
        // 子进程代码
        printf("Child process (PID: %d)\n", getpid());
    } else {
        // 父进程代码
        printf("Parent process (PID: %d), Child PID: %d\n", getpid(), pid);
    }
    return 0;
}

而在window上,使用CreateProcess接口可以直接指定一个可执行程序路径创建新的进程,新进程的执行代码不在父进程中。

cpp 复制代码
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // Command line 这里指定程序路径
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

可以看到两者的不同之处:fork复制当前进程开启新进程,执行的代码都在父进程中,createProcess指定可执行程序从而开启新进程,执行代码的代码在指定的可执行程序中。那么在linux上如何实现类似createProcess的效果呢,一种是fork+exec:

复制代码
pid_t parent = getpid();
pid_t pid = fork();

if (pid == -1)
{
    // error, failed to fork()
} 
else if (pid > 0)
{
    int status;
    waitpid(pid, &status, 0);
}
else 
{
    // we are the child
    execve(...);
    _exit(EXIT_FAILURE);   // exec never returns
}

exec函数族会替换当前的进程映像(process image),我的理解是相当于子进程变成了exec指定的新的可执行程序在执行。还可以使用posix_spawn函数,用法类似windows的createProcess:

cpp 复制代码
pid_t pid;
  char *argv[] = {"ls", (char *) 0};
  int status;
  puts("Testing posix_spawn");
  fflush(NULL);
  status = posix_spawn(&pid, "/bin/ls", NULL, NULL, argv, environ);
  if (status == 0) {
    printf("Child id: %i\n", pid);
    fflush(NULL);
    if (waitpid(pid, &status, 0) != -1) {
      printf("Child exited with status %i\n", status);
    } else {
      perror("waitpid");
    }
  } else {
    printf("posix_spawn: %s\n", strerror(status));
  }

Linux设计上将"创建一个新进程"和"加载新的可执行程序到新的进程"两个步骤分离了,而windows的CreateProcess则是把两个步骤合并了。

  1. https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes
  2. https://stackoverflow.com/questions/5883462/linux-createprocess
  3. https://superuser.com/questions/1737519/why-a-fork-is-often-followed-by-an-exec
相关推荐
半桔9 分钟前
【Linux手册】冯诺依曼体系结构
linux·缓存·职场和发展·系统架构
网硕互联的小客服36 分钟前
如何利用Elastic Stack(ELK)进行安全日志分析
linux·服务器·网络·安全
数字芯片实验室1 小时前
寄存器模型生成:从手工到自动化
运维·自动化
代码搬运媛1 小时前
“packageManager“: “[email protected]“ 配置如何正确启动项目?
windows·webpack
冰橙子id1 小时前
linux——磁盘和文件系统管理
linux·运维·服务器
咕噜企业签名分发-淼淼1 小时前
应用app的服务器如何增加高并发
运维·服务器
b***25112 小时前
18650锂电池组点焊机:高效组装锂电池的关键工具|比斯特自动化
运维·自动化
leagsoft_10032 小时前
联软NSPM自动化策略管理 助力上交所加速国产化替代提升运维效率
运维·网络·自动化
小道士写程序2 小时前
Qt 5.12 上读取 .xlsx 文件(Windows 平台)
开发语言·windows·qt
无聊的小坏坏2 小时前
环境变量深度解析:从配置到内核的全链路指南
linux·bash