带参数的main函数以及execl函数的应用
2024-08-20 21:44:48
---恢复内容开始---
代码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
最新文章
- VQuery选择器
- C 记录
- C和C++混合编程
- centos6.7下编译安装lnmp
- swfupdate flash上传工具
- C++例题1:输出可打印字符
- 2013 长沙网络赛 B 题 Bizarre Routine
- 自定义DB连接池实现
- POJ 3621 Sightseeing Cows [最优比率环]
- Chapter 5 Blood Type——21
- Ubuntu16.04多个版本GCC编译器的安装和切换
- Netty 超时机制及心跳程序实现
- OneZero产品视频
- LaTeX使用学习
- gispro试用版账户注册
- 阿里云-centos7.2-LNMP-编译安装-记录
- hexo d 报错‘fatal: could not read Username for &#39;https://github.com&#39;: No error’
- Python Threading多线程简单例子
- NSObject之二
- 洛咕 P2468 [SDOI2010]粟粟的书架
热门文章
- MYSQL与TiDB的执行计划
- JS StartMove源码-简单运动框架
- MySql:SELECT 语句(二)排序 ORDER BY 的使用
- Python爬虫中文小说网点查找小说并且保存到txt(含中文乱码处理方法)
- 请求不同域的数据方法:requests Jsonp cors
- jQuery之必会增删改查Dom操作
- Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)
- Nginx搭建
- 中间件之Kafka
- lambda正则化参数的大小影响