c调用外部程序

函数名称: spawnl、spawnle、spawnlp、spawnlpe、spawnv、spawnve、spawnvp、spawnvpe
函数原型: int spawnl(int mode, char *path, char *arg 0, … )
  int spawnle(int mode, char *path, char *arg0, … )
  int spawnlp(int mode, char *path, char *arg0, … )
  int spawnlpe(int mode, char *path, char *arg0, … )
  int spawnv(int mode, char *path, char *argv[])
  int spawnve(int mode, char *path, char *argv[], char **env)
  int spawnvp(int mode, char *path, char *argv[])
  int spawnvpe(int mode, char *path, char *argv[], char **env)
函数功能: 在一个程序中调用另外一个程序
函数返回: -1:调用失败,0:调用成功
参数说明: path-被调用程序路径,arg-调用的参数,mode-调用模式,具体如下:
  P_WAIT 0 将父过程挂起,直到子过程执行完毕
  P_NOWAIT 1 父子过程同时执行,Turboc不支持
  P_OVERLAY 2 子过程覆盖父过程
所属文件: <process.h>

C/C++ code
    
    
    
    
#include < stdio.h > #include < stdlib.h > #include < process.h > #include < errno.h > #include < string .h > int main() { int process_id; #if defined(__OS2__)||defined(__NT__) int status, rc; #endif process_id = spawnl(P_NOWAIT, " child.exe " , " child " , " 5 " ,NULL); if (process_id ==- 1 ){ printf( " spawn failed-%s/n " ,strerror(errno)); exit(EXIT_FAILURE); } printf( " Process id=%d/n " ,process_id); #if defined(__OS2__)||defined(__NT__) rc = cwait( & status,process_id,WAIT_CHILD); if (rc ==- 1 ){ printf( " wait failed-%s/n " ,strerror(errno)); } else { printf( " wait succeeded-%x/n " ,status); switch (status & 0xff ){ case 0 : printf( " Normal termination code=%d/n " ,status >> 8 ); break ; case 1 : printf( " Hard-error abort/n " ); break ; case 2 : printf( " Trap operation/n " ); break ; case 3 : printf( " SIGTERM signal not intercepted/n " ); break ; default : printf( " Bogus return status/n " ); } } #endif printf( " spawn completed/n " ); return 0 ; } /* [child.c] #include <stdio.h> #include <stdlib.h> #include <dos.h> int main(int argc,char *argv[]) { int delay; if(argc<=1) exit(EXIT_FAILURE); delay=atoi(argv[1]); printf("I am a child going to sleep for %d seconds/n",delay); sleep(delay); printf("I am a child awakening/n"); exit(123); return 0; }
http://www.programfan.com/club/showtxt.asp?id=198953

 

你可能感兴趣的:(c调用外部程序)