kill a process which run out of time


 
Thread Tools Search this Thread
Top Forums Programming kill a process which run out of time
# 1  
Old 12-16-2008
kill a process which run out of time

hello everybody!!
i want ur help! it is urgent!!

...
pid=fork();
if(pid==0)
{
execl(a program);
exit(1);}
else if (pid>0)
{
timer(5); //(command 1)timer is a function that count up to 5sec
if(kill(pid,0)==0)kill(pid,9);//(command 2)
wait(&status);
....
}
else
perror("error");
....
The purpose of command1 and command2 is, if the execution of child process
does not finish in 5sec(timer(5)), i want to kill it (kill(pid,9)).
Can anybody pleasee help why this thing does not work properly!!!
THANX in advance!:-)
# 2  
Old 12-16-2008
please check whether kill (pid, 9) was really successfully called.
# 3  
Old 12-16-2008
Quote:
Originally Posted by ivhb
please check whether kill (pid, 9) was really successfully called.
the problem i found is that the command if(kill(pid,0)==0) is always true either if the child process finishs on time (before timer ends up) or if the process does not finish on time. ANybody help?

thanx

Last edited by nicos; 12-17-2008 at 01:21 PM..
# 4  
Old 01-06-2009
Quote:
Originally Posted by nicos
hello everybody!!
i want ur help! it is urgent!!

...
pid=fork();
if(pid==0)
{
execl(a program);
exit(1);}
else if (pid>0)
{
timer(5); //(command 1)timer is a function that count up to 5sec
if(kill(pid,0)==0)kill(pid,9);//(command 2)
wait(&status);
....
}
else
perror("error");
....
The purpose of command1 and command2 is, if the execution of child process
does not finish in 5sec(timer(5)), i want to kill it (kill(pid,9)).
Can anybody pleasee help why this thing does not work properly!!!
THANX in advance!:-)
It's because before calling wait() to wait child exits, the child won't actually disappear in the system, i.e. if a child exits, but the parent does not immediately call wait() or waitpid() to handle the SIGCHLD from its child process, the exited child process would keep as a zombie in the system, until the parent exits or the parent call wait() or waitpid(). And kill -SIGNAL to a zombie will always succeed, so kill() returns 0.
You can try the following code zombie.c:
Quote:
include<stdio.h>
#include<unistd.h>
#include<sys/types.h>

int main()
{
pid_t pid;

pid=fork();

if(pid==0) {
printf("child process, sleep 5s and exit!");
sleep(5);
exit(0);
}
else if(pid>0) {
printf("parent process, sleep 10s, then kill and wait!");
sleep(10);
printf("parent process, kill!");
if(kill(pid, 0)==0) {
printf("child still alive!");
kill(pid, 9);
}
waitpid(pid, NULL);
exit(0);
}
}
you will see after 5s, child exits, and before parent kill it and wait it, by "pf -ef | grep zombie", a <defunct> process could be found.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To run 5 commands at the same time with process from a list

I have many command is list in the variable lists,each command will run a very long time, so I want to run 5 commands at the same time with process till it complete run all the command, lists="aa bb cc dd xx gg blabla zz ......." ( a very long list) can some one point me the codes? ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. Shell Programming and Scripting

Help with kill a specific process after certain running time

Hi, Do anybody experience to write a bash script in order to kill a specific process (java) after certain time of running? eg. java java.jar task_run.txt I will run a java program (java.jar) which will run a long list of process (task_run.txt) one by one. I plan to terminate the java... (5 Replies)
Discussion started by: perl_beginner
5 Replies

3. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

4. UNIX for Advanced & Expert Users

Kill a process after a certain amount of time

I would like to kill a process after a certain amount of time. Can I please get some ideas on how to do this? (9 Replies)
Discussion started by: cokedude
9 Replies

5. AIX

kill process that run more then 1 day

hi all i need a script which will find all the processes witht the name of xxx and kill all those processes that runs for more than 1 day. Regards (3 Replies)
Discussion started by: Elii
3 Replies

6. UNIX for Dummies Questions & Answers

Run process with nohup every certain time

Hi, I need execute a script every 30 minutes. As might be done without using cron Thx. (6 Replies)
Discussion started by: pepeli30
6 Replies

7. Shell Programming and Scripting

how to run multiple process at the same time

Hello guys, Look what im doing: I need to run a process from a SERVER1 to SERVER2, SERVER3 and SERVER4. The shell of the process is in each SERVER (2 to 4) So from SERVER1 i do: for i in SERVER2 SERVER3 SERVER4 do rsh $i ' ./process.sh ' done The problem is: each process.sh... (2 Replies)
Discussion started by: lestat_ecuador
2 Replies

8. Shell Programming and Scripting

how to run process in certain date and time

hi all! i want to run a process in certain date and hour (like feb 2007 ,hour 3 p.m) how shell i write it my script call cs-update-pr another question :as the script running, will i see it as process ?ho does it run background? and if not - how can i define to him to run background? thanks... (3 Replies)
Discussion started by: naamas03
3 Replies

9. UNIX for Dummies Questions & Answers

How ti kill a process which is consuming so much time

There is a process which is consuming too much time.. how to find that process and kill it. (3 Replies)
Discussion started by: shreenivas
3 Replies

10. Programming

Process Run time information

Hello, I am working on Sun Solaris 5.7. I am trying to read the running time of a process through a C program. One way I am reading it is by using the command ps -<pid> -f The other way is from the struct psinfo_t which is there under /proc/pid/psinfo. However, the two times are... (1 Reply)
Discussion started by: hmurali
1 Replies
Login or Register to Ask a Question