c调用外部程序

本文介绍了C语言中调用外部程序的多种函数,包括spawnl、spawnle、spawnlp、spawnlpe、spawnv、spawnve、spawnvp和spawnvpe。这些函数允许在程序中启动另一个程序,并提供了不同的调用模式,如P_WAIT、P_NOWAIT和P_OVERLAY,以控制父进程和子进程的行为。当调用成功时,函数返回0,失败则返回-1。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数名称: 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; }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值