---恢复内容开始---
代码1:(带参main函数)
#include<stdio.h>
int main(int number, char *parameter[])
{
int i = ;
printf("number is : %d \n", number);
for(i=; i<number; i++)
{
printf("parameter[%d] is : %s \n", i, parameter[i]); }
if(number!=)
printf("not 3!!!\n");
}
运行:
yisheng@yisheng-v-m:~/xtbc$ gcc main.c
yisheng@yisheng-v-m:~/xtbc$ ./a.out
number is :
parameter[] is : ./a.out
not !!!
yisheng@yisheng-v-m:~/xtbc$ ./a.out
number is :
parameter[] is : ./a.out
parameter[] is :
parameter[] is :
parameter[] is :
not !!!
代码2:excel函数:
#include<stdio.h>
#include<unistd.h> int main()
{
int ret;
ret=execl("./main","main","",NULL); if(ret<)
perror("execl");
printf("excel over\n"); }
运行程序:
yisheng@yisheng-v-m:~/xtbc$ gcc main.c -o ./main //要先生成可执行的./main
yisheng@yisheng-v-m:~/xtbc$ gcc .c
yisheng@yisheng-v-m:~/xtbc$ ./a.out
number is :
parameter[] is : main
parameter[] is :
not !!!
在这种情况之下,不能够输出后面的“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==)
{
ret=execl("./main","main","",NULL); if(ret<)
{
perror("execl"); }
exit();
}
if(pid>)
{
wait(NULL);
printf("excel over\n");
}
return ;
}
结果为:
yisheng@yisheng-v-m:~/xtbc$ ./a.out //已经存在./main
number is :
parameter[] is : main
parameter[] is :
not !!!
excel over