簡體   English   中英

在Mac OS X或Linux上相當於_wspawnl / _spawnl

[英]_wspawnl/_spawnl equivalent on Mac OS X or Linux

我只是將代碼移植到在Windows上使用_tspawnl Mac OSX。

在Mac OS X或Linux上,有什么與_tspawnl等效的東西?

還是有等於_tspawnl posix

您可以通過以下方式一起使用forkexecv系統調用:

if (!fork()){ // create the new process 
     execl(path,  *arg, ...); // execute  the new program
}

fork系統調用將創建一個新進程,而execv系統調用將開始執行path中指定的應用程序。 例如,您可以使用以下函數spawn其參數為要執行的應用程序的名稱及其參數的列表。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int spawn (char* program, char** arg_list)
{
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork ();
if (child_pid != 0)
    /* This is the parent process. */
     return child_pid;
else {
    /* Now execute PROGRAM, searching for it in the path. */
     execvp (program, arg_list);
    /* The execvp function returns only if an error occurs. */
    fprintf (stderr, “an error occurred in execvp\n”);
    abort ();
    }
 }

int main ()
{
/* The argument list to pass to the “ls” command. */
   char* arg_list[] = { 
   “ls”, /* argv[0], the name of the program. */
   “-l”, 
    “/”,
    NULL /* The argument list must end with a NULL. */
  };

  spawn (“ls”, arg_list); 
  printf (“done with main program\n”);
  return 0; 
}

該示例摘自本書第3.2.2章。 (對於在Linux中進行開發確實是很好的參考)。

正如已經指出的,您可以使用fork()/exec() ,但是更接近的系統調用是posix_spawn()manpage )。

它可以是一個有點痛來設置,但是,但使用它是一些示例代碼在這里 (注意,此代碼還提供功能為使用Windows的CreateProcess() API,這可能是你應該使用什么在Windows下)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM