Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process execution & kill

Status
Not open for further replies.

Damora

Programmer
Nov 27, 2001
3
ES
Hello everybody!
My problem is that I must execute an executable file. This one may hang - as it is a compiled Pascal program that might contain an infinite loop - , so I must limit its execution time to , let us say, 10 seconds. After that time, the process must be killed.
I am almost sure that "alarm(10)" allows me to count 10 seconds, and using "signal(...)" I can receive a signal when the time limit is exceeded. But I do not really know how to execute the file (I have tried with "system(./name_of_the_file)" and the exec family of functions, but I cannot stop execution) and I do not know how to handle the signal. The process goes on and does not stop.
Thanks.
 
You should use exec family, and get its pid.
You'll need to use a fork before that. Here's an example:
Code:
void main(){
int pid;,dummy

pid=fork();
switch(pid){
  case -1: //Error on fork call
    break;
  case 0: //Child
    //execute your program using exec call
    break;
  default: //Father
    sleep(10);
    kill(pid, SIGKILL);
    wait(&dummy);
    break;
  }
}
 
Thank you very much, it works!!!
I am afraid my search into de Unix C manual has been unuseful, and I would have been lost for years. Had it not been for you...
Thanks again!!!
 
Sorry, I thought it worked but I have realized that it dose not. Your solution only sleeps the process (stops it for 10 seconds), but what I really want is the process to be executed "in the background" and kill it after the mentioned 10 seconds. That is why I told that alarm() might be the solution, but I cannot find any example that works like I want.
 
Uh? Father process is the one stopped, not the executed one. So, father process must do another work separately, is that what you mean?
The executed process (Pascal executable) isn't stopped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top