函式簡介
函式名: _ spawnl
所屬庫: process.h
功 能: 創建並且執行一個新的進程
函式原型:int _spawnl( int mode, const char *pathname, const char *arg0, const char *arg1, … const char *argn, NULL );
相關函式:_spawnle、_spawnlp、_spawnlpe、_wspawnle、_wspawnlp、_wspawnlpe、_wspawnl
程式例
程式例1
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int result;
result = spawnl(P_WAIT, "tcc.exe", NULL);
if (result == -1)
{
perror("Error from spawnl");
exit(1);
}
return 0;
}
程式例2
//這個例子中顯示了8個spawn函式的用法:
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
char *my_env[] = {"THIS=environment will be","PASSED=to child.exe by the","_SPAWNLE=and","_SPAWNLPE=and","_SPAWNVE=and","_SPAWNVPE=functions",NULL};
int main( int argc,char *argv[] )
{
char *args[4];
args[0] = "child";
args[1] = "spawn??";
args[2] = "two";
args[3] = NULL;
if(argc <= 2)
{
printf( "SYNTAX: SPAWN <1-8> <childprogram>\n" );
exit( 1 );
}
switch (argv[1][0])
{
case '1': _spawnl( _P_WAIT, argv[2], argv[2], "_spawnl", "two", NULL );break;
case '2': _spawnle( _P_WAIT, argv[2], argv[2], "_spawnle", "two", NULL, my_env ); break;
case '3': _spawnlp(_P_WAIT, argv[2], argv[2], "_spawnlp", "two", NULL ); break;
case '4': _spawnlpe( _P_WAIT, argv[2], argv[2], "_spawnlpe", "two", NULL, my_env ); break;
case '5': _spawnv( _P_OVERLAY, argv[2], args ); break;
case '6': _spawnve( _P_OVERLAY, argv[2], args, my_env ); break;
case '7': _spawnvp( _P_OVERLAY,argv[2], args );break;
case '8': _spawnvpe( _P_OVERLAY, argv[2], args, my_env ); break;
default: printf( "SYNTAX: SPAWN <1-8> <childprogram>\n" );exit( 1 );
}
printf( "from SPAWN!\n" );
return 0;
}