Fedora Linux Support Community & Resources Center
  #1  
Old 20th May 2009, 06:00 PM
monk.pt Offline
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.
Reply With Quote
  #2  
Old 20th May 2009, 06:10 PM
brunson Offline
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.
__________________
Registered Linux User #4837
411th in line to get sued by Micro$oft
Quote:
Basically, to learn Unix you learn to understand and apply a small set of key ideas and achieve expertise by expanding both the set of ideas and your ability to apply them - Paul Murphy
Reply With Quote
  #3  
Old 20th May 2009, 06:29 PM
monk.pt Offline
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 ?
Reply With Quote
  #4  
Old 20th May 2009, 09:14 PM
brunson Offline
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.
__________________
Registered Linux User #4837
411th in line to get sued by Micro$oft
Quote:
Basically, to learn Unix you learn to understand and apply a small set of key ideas and achieve expertise by expanding both the set of ideas and your ability to apply them - Paul Murphy
Reply With Quote
  #5  
Old 26th May 2009, 07:35 AM
stevea's Avatar
stevea Offline
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
Reply With Quote
  #6  
Old 26th May 2009, 06:21 PM
brunson Offline
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.
__________________
Registered Linux User #4837
411th in line to get sued by Micro$oft
Quote:
Basically, to learn Unix you learn to understand and apply a small set of key ideas and achieve expertise by expanding both the set of ideas and your ability to apply them - Paul Murphy
Reply With Quote
Reply

Tags
kill, process, run, time

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to kill 99% CPU X process ? sunset Using Fedora 9 1st June 2006 12:23 PM
How do I kill a process? Sunnz Using Fedora 8 14th July 2005 03:16 AM
how to kill a process embassy Using Fedora 5 7th December 2004 01:26 PM


Current GMT-time: 18:24 (Thursday, 23-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat