简体   繁体   English

在Mac OS X或Linux上相当于_wspawnl / _spawnl

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

I am just porting a code to Mac OS X which is using _tspawnl on Windows. 我只是将代码移植到在Windows上使用_tspawnl Mac OSX。

Is there anything equivalent to _tspawnl on Mac OS X or Linux? 在Mac OS X或Linux上,有什么与_tspawnl等效的东西?

Or is there any posix equivalent to _tspawnl 还是有等于_tspawnl posix

You can use fork and execv system call together in the following way : 您可以通过以下方式一起使用forkexecv系统调用:

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

The fork system call creates a new process, while the execv system call starts the execution of the application specify in path. fork系统调用将创建一个新进程,而execv系统调用将开始执行path中指定的应用程序。 For example, you can use the following function spawn whose argument are the name of the application to be executed and the list of its arguments. 例如,您可以使用以下函数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; 
}

This example has been taken from the chapter 3.2.2 of this book . 该示例摘自本书第3.2.2章。 (Really good reference for development in Linux). (对于在Linux中进行开发确实是很好的参考)。

You can use fork()/exec() , as already pointed out, however a closer system call is posix_spawn() ( manpage ). 正如已经指出的,您可以使用fork()/exec() ,但是更接近的系统调用是posix_spawn()manpage )。

It can be a bit of a pain to set-up, however, but there is some example code using it is here (note that this code also provides functionality for Windows using the CreateProcess() API, which is probably what you should be using under Windows anyway). 它可以是一个有点痛来设置,但是,但使用它是一些示例代码在这里 (注意,此代码还提供功能为使用Windows的CreateProcess() API,这可能是你应该使用什么在Windows下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM