一个简单的测试程序健壮性的c程序
2014-08-11 20:11
316 查看
下面的程序用于不断的启动hello,并将其杀死。将hello换为别的可执行文件即可简单测试程序的健壮性。
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
int i;
int flag;
pid_t pid;
char buf[128] = {0};
printf("argc:%d\n", argc);
if (2 != argc)
{
printf("Usage:./a.out 3\n");
return 0;
}
int total_times = atoi(argv[1]);
for(i = 0; i < total_times; i++)
{
// 如果不指定全路径,则只检查PATH变量中存储的命令
if((pid = fork())==0) {
printf("in child process 1......\n");
//flag = execvp("./hello", NULL);
//envp变量的用
char *envp[]={"PATH=.", NULL};
flag = execve("/usr/local/test/code/hello", NULL, envp);
if(flag == -1)
printf("exec error!\n");
return 0;
}
else
{
printf("pid is:%d\n", pid);
sprintf(buf, "kill -9 %d", pid);
printf("buf is:%s\n", buf);
sleep(1);
system(buf);
}
}
printf("in parent process ......\n");
return 0;
}
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
int i;
int flag;
pid_t pid;
char buf[128] = {0};
printf("argc:%d\n", argc);
if (2 != argc)
{
printf("Usage:./a.out 3\n");
return 0;
}
int total_times = atoi(argv[1]);
for(i = 0; i < total_times; i++)
{
// 如果不指定全路径,则只检查PATH变量中存储的命令
if((pid = fork())==0) {
printf("in child process 1......\n");
//flag = execvp("./hello", NULL);
//envp变量的用
char *envp[]={"PATH=.", NULL};
flag = execve("/usr/local/test/code/hello", NULL, envp);
if(flag == -1)
printf("exec error!\n");
return 0;
}
else
{
printf("pid is:%d\n", pid);
sprintf(buf, "kill -9 %d", pid);
printf("buf is:%s\n", buf);
sleep(1);
system(buf);
}
}
printf("in parent process ......\n");
return 0;
}
相关文章推荐
- 测试自动化 2011/06/11 - 1 一个最简单的GMock测试程序HelloGMock
- 一个最简单的测试在/dev下生成字符设备的程序
- 一个简单的字符设备驱动程序和测试程序
- 把double 类型数组中的数据倒序排列,并在一个简单的程序中测试该函数
- 一个简单的测试计算机的位数的程序: 不用sizeof
- 一个简单的HTTP并发测试程序
- 基于JUnit3.8的一个简单的测试程序
- OpenCV20(flann一个简单的测试程序)
- Java实训——创建一个窗体程序,实现简单的数学加、减法测试。
- 一个简单的GNU assert用法测试程序
- 一个简单的AVR测试程序
- java例程练习(一个BirthDate类 以及简单的测试程序)
- 一个简单的redis性能测试程序和性能测试结果
- (原创)液晶的一个最简单的测试程序
- 一个最简单的测试在dev/下生成字符设备的程序
- 使用J2SE开发一个测试Xfire的webservice的简单程序
- 一个简单的AVR测试程序
- 安装Thrift并写一个简单的测试程序
- 一个简单精确的asp速度测试程序
- 编写一个函数,返回一个int数组中存储的最大数值,并在一个简单的程序中测试这个函数。(有缺陷)