 |
 |
 |
 |
| Programming & Packaging A place to discuss programming and packaging. |

20th May 2009, 06:00 PM
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 2

|
|
|
Run process from C++ and kill it after some time
Hello,
Is there any way i can run a process in c/c++ and then kill it if it exceeds the time of x seconds ? X may be an integer or not.
|

20th May 2009, 06:10 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Location: Westminster, Colorado
Posts: 2,304

|
|
|
You would have to fork/exec the process and save the child process id, then set a signal handler for SIGCHILD, the parent program would then sleep for x seconds and if it hasn't received the signal before the time expires, kill() the child process using it's PID.
|

20th May 2009, 06:29 PM
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 2

|
|
|
Well, how can i add a signal handler, and how can i send a signal to the process ? If the process dies by any signal, i have to return "Process killed by signal x", but how can i do this ?
|

20th May 2009, 09:14 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Location: Westminster, Colorado
Posts: 2,304

|
|
|
That is basic system programming. I've given you all the keywords you need, start googling.
|

26th May 2009, 07:35 AM
|
 |
Registered User
|
|
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,302

|
|
I'll suggest a simpler approach. The way to signal your process after some period of time is to use the "alarm" or setitimer" syscall. See "man 2 alarm", "man 2 setitimer". You will receive a SIGALRM signal, for example (but read the man page). No fork or other process is needed. If you need to print a msg or something, then just set up ia signal handler before you call setitimer.
Quote:
void
mysigalarm(int sig)
{
printf("Got SIGALRM !!!\n");
exit(42);
}
...
main()
{
...
signal(SIGALRM, (sighandler_t) mysigalrm);
...
setitimer(ITIMER_REAL, ...);
...
}
|
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
|

26th May 2009, 06:21 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Location: Westminster, Colorado
Posts: 2,304

|
|
|
Oh, I misread the original post. I thought he wanted to spawn an external process from the C++ program.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 18:24 (Thursday, 23-05-2013)
|
|
 |
 |
 |
 |
|
|