fork + execvp

kgzhw 2012-07-02 04:38:01
我使用了fork + execvp去创建一个新的进程,创建成功,但是使用ps -ef查看进程时发现test进程名变成了[test],我查了一下带方括号的进程是内核进程,不知道怎么建立一个普通的进程。请高手指点!
代码如下:

#include <stdio.h>
#include <unistd.h>

int main()
{
pid_t pid = fork();
if(0 ==pid )
{
execvp("/opt/testdir/test", "");
exit(0);
}
while(1)
{
printf("p pgressrunning\n");
sleep(1);
}
}
...全文
188 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kgzhw 2012-07-04
  • 打赏
  • 举报
回复
已经解决代码如下:
#include <stdio.h>
#include <unistd.h>

int main()
{
pid_t pid = fork();
if(0 ==pid )
{
char *argv[] = {"/opt/testdir/test", NULL}
execve( "/opt/testdir/test", argv, NULL);

exit(0);
}
while(1)
{
printf( "p pgressrunning\n ");
sleep(1);
}
}
kgzhw 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
学习。
都是进程,我感觉没什么区别。
这是cu上的别人的回答:
Under the -f flag, ps tries to determine the current command name and arguments both of which may be changed asynchronously by the process. These are then displayed.……
[/Quote]

估计是我表述错误了,重点是编程实现启动常规进程,而不是ps命令。
happylifer 2012-07-03
  • 打赏
  • 举报
回复
学习。
都是进程,我感觉没什么区别。
这是cu上的别人的回答:
Under the -f flag, ps tries to determine the current command name and arguments both of which may be changed asynchronously by the process. These are then displayed. Failing this, the command name as it would appear without the option -f, is written in square brackets.

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧