标签:not 进程 error printf lib bsp 恢复 excel style
---恢复内容开始---
代码1:(带参main函数)
#include<stdio.h>
int main(int number, char *parameter[])
{
int i = 0;
printf("number is : %d \n", number);
for(i=0; i<number; i++)
{
printf("parameter[%d] is : %s \n", i, parameter[i]);
}
if(number!=3)
printf("not 3!!!\n");
}
运行:
yisheng@yisheng-v-m:~/xtbc$ gcc main.c
yisheng@yisheng-v-m:~/xtbc$ ./a.out
number is : 1
parameter[0] is : ./a.out
not 3!!!
yisheng@yisheng-v-m:~/xtbc$ ./a.out 2 3 4
number is : 4
parameter[0] is : ./a.out
parameter[1] is : 2
parameter[2] is : 3
parameter[3] is : 4
not 3!!!
代码2:excel函数:
#include<stdio.h>
#include<unistd.h>
int main()
{
int ret;
ret=execl("./main","main","2",NULL);
if(ret<0)
perror("execl");
printf("excel over\n");
}
运行程序:
yisheng@yisheng-v-m:~/xtbc$ gcc main.c -o ./main //要先生成可执行的./main
yisheng@yisheng-v-m:~/xtbc$ gcc 33.c
yisheng@yisheng-v-m:~/xtbc$ ./a.out
number is : 2
parameter[0] is : main
parameter[1] is : 2
not 3!!!
在这种情况之下,不能够输出后面的“excle over”内容,无法得知进程是否完成,可以改进一下程序,利用fork()创建子进程:
#include<stdio.h>
#include<unistd.h>
#include<wait.h>
#include<stdlib.h>
int main()
{
int ret;
int pid;
pid=fork();
if(pid==0)
{
ret=execl("./main","main","2",NULL);
if(ret<0)
{
perror("execl");
}
exit(0);
}
if(pid>0)
{
wait(NULL);
printf("excel over\n");
}
return 0;
}
结果为:
yisheng@yisheng-v-m:~/xtbc$ ./a.out //已经存在./main number is : 2 parameter[0] is : main parameter[1] is : 2 not 3!!! excel over
标签:not 进程 error printf lib bsp 恢复 excel style
原文地址:https://www.cnblogs.com/it8343/p/9235841.html