- 论坛徽章:
- 1
|
自己测试了一下发现execlp执行完,后面的语句都不执行了,本人比较菜不知道是什么原因,但是感觉应该和c调用shell到标准输出有关,试想如果execlp用子进程
运行应该能解决,因为system()调用fork()执行shell,所以用system测试了一下,测试成功,好久没写c了(本人c也是初级阶段,见笑)fork的实验本人要搞的话
还需要一段时间,还是等待大侠们测试吧。不过希望具体的原理哪位大侠来总结一下比较好。
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<string.h>
- main()
- {
- int fd,size;
- char template[]="/tmp/template-XXXXXX";
- char s[]="Linux Programmer1!\n";
- char r[]="Linux Programmer2!\n";
- char _Bash[]="cat ";
- char c[strlen(template)+strlen(_Bash)];
- fd=mkstemp(template);
- write(fd,s,sizeof(s));
- write(fd,r,sizeof(r));
- strncat(c,_Bash,strlen(_Bash));
- strncat(c,template,strlen(template));
- system(c);
- if (unlink(template) == 0)
- {
- printf("unlink OK\n");
- }else
- {
- perror("unlink");
- }
- close(fd);
- }
复制代码 |
|