进程的创建----system()
int system(const char*filename);
建立独立进程,拥有独立的代码空间,内存空间
等待新的进程执行完毕,system才返回.(阻塞)
案例:test.c 和 system.c
使用system调用一个程序。
test.c代码:
#include <stdio.h> #include <unistd.h> int main() { printf("%d\n",getpid()); sleep(5); return 99; }
system.c代码:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> main() { int r; printf("%d\n",getpid()); r=system("./test"); //printf("%d\n",r); //printf("%d\n",r>>8&255); printf("%d\n",WEXITSTATUS(r)); system("clear"); }
1.观察进程ID。 PID不一样
2.观察阻塞。 调用进程不结束,就不返回
新的返回值与system返回值有关系。为什么不是返回99???
任何进程的返回值:不要超过255。一个字节。
system的返回值中8-15位(第二个字节)存放返回码 (r>>8&255) 一般不用这种方式
linux中提供了一个宏来获取我们的返回值 WEXITSTATUS(r) 引入头文件#include <sys/wait.h>

浙公网安备 33010602011771号