请教:怎么反复执行某个进程,反复杀掉

充电宝111 2009-07-08 05:39:33
要求执行某个进程或程序,然后杀死它,再执行,再杀死,小弟整了一天了,都快晕了。。。
到底应该怎么做?最好有个伪代码,小弟先谢了
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/wait.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <signal.h>
8 #include <time.h>
9
10 void handler()
11 {
12 exit(0);
13 }
14
22
23 int main()
24 {
25 pid_t pid;
26 int status;
27 srand(time(NULL));
28 int t;
29
30
31 if((pid=fork())<0)
32 printf("error in fork!");
33 else
34 if(pid==0){
35 (void) execle("./test",NULL , NULL, NULL); //这里不返回!!!!!!!!!
36 printf("I am the child process, my process ID is %d\n",getpid());
37 }
38 else{
39 t = rand()%10;
40 sleep(t);
41 kill(pid, SIGTERM);
42 wait(&status);
43 printf("killed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
44 printf("I am the parent process, my process ID is %d\n",getpid());
45 }
46 printf("i am the end\n");
47 exit(0);
48 return 0;
49 }

...全文
109 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
once_and_again 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mosaic 的回复:]
引用 5 楼 ddcien 的回复:
“加个死循环”
这个要谨慎吧,进程号用完了,会死机的吧???


在死循环里面起进程,再杀进程,应该没问题啊
[/Quote]

看 设置了,网上 有 一句话 在shell下 ,把linux整崩溃呢。
mosaic 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ddcien 的回复:]
“加个死循环”
这个要谨慎吧,进程号用完了,会死机的吧???
[/Quote]

在死循环里面起进程,再杀进程,应该没问题啊
ddcien 2009-07-23
  • 打赏
  • 举报
回复
“加个死循环”
这个要谨慎吧,进程号用完了,会死机的吧???
bainan 2009-07-12
  • 打赏
  • 举报
回复
你的代码确实只会执行一次

加个死循环
steptodream 2009-07-09
  • 打赏
  • 举报
回复
发了两个版面 再回一次 你的代码只会执行一次
加一个死循环
while(1){

}
  • 打赏
  • 举报
回复
描述下问题啊,你只是fork了一个进程,做了一些操作后就全部exit了。
yhf365 2009-07-08
  • 打赏
  • 举报
回复
这是在折腾什么?
不明白...

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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