As an addition to abbemans post, be aware that your program will only be able to kill processes started by the user who runs the application. If you want the ability to kill any process you will need to have your killer app run as setuid root so you need to refer to man pages for getuid and setuid.
Hi abbeman,
thanks for your post. But what happens if i want to start a process in my c program and at the same time needs to monitor it? Meaning, i need to be able to get the pid of the process.
fork does not let me start a certain process right?
I infer from the last line in your last posting that what you actually meant in the beginning was that you wanted the ability to start a second program from your new program and then the ability to kill that program.
We inferred that you wanted to fork a process which is why the answers were given as were.
Unfortunately if you do want to start a second, unrelated, program from your program you have to use either system() or execv() calls and none of these return the pid of the started program (because there is no logical relationship between the two programs as far as the operating system is concerned).
You would have to somehow get the PID by reading the list of all processes open - this could get messy. A stop-gap method that springs to mind is for your calling application to launch the second application and then launch a script that performs an ps -ef to get a list of all running processes, greps that output for the new program (by name) ,uses awk to extract the pid number and then writes that pid to a file which can then be read by your application. It's a bit of a lash-up and has some timing issues but it should give you a head start - others may have something more elegant to suggest.
The thing which i don't want to do is to use system commands to invoke other applications coz you've sort of lose control over them. Also, i do not want to have a lot of temp files to read in the pid and status of the processes.
Hopefully, somebody can come up with another way to do it.
I might be missing something obvious here but I can't see an easy way of doing this - you say you don't want to use the system() command to start your second application as you lose control over the second application - this is true but as far as I am aware the only way you will be able to start one application from another is either a system() call or an execv() or similar call - in either case you lose control of the second application. In addition the system() call will not return control to your application until the second application terminates - a bit of a drawback presumably. execv() and it relations will over-write your current application with the new application so this is also a bit of a problem.
Generally what is done in these cases is to fork from you application then have the forked child call system() or exec() to spawn the new application but again you lose control of the second application. You can kill off the forked child as the parent knows its PID but the application that the forked process spawned then becomes a zombie as no one is waiting for it to terminate. This doesn;t solve your problem.
I think that you have misunderstood what execv does. Execv, and it's sibblings do not spawn new processes to run the program that you tell them to run, they replaces the calling program with the one you want to run. This means that if you do the following:
Your program will _never_ reach the printf-line. So to do what you want, you normally fork (wich does return the pid of the child to the parent) and if you are the child you exec the program you want. You now have two processes, the first one running your original program and a second one running the program that you want, plus you have the pid to the second program.
Just as stated in the man-pages for exec and fork.
From man 2 exec:
"The exec family of functions replaces the current process image with a new process image."
From man 2 fork:
"On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution."
So if you do the following:
int pid;
if ((pid = fork) == 0) {
/* I am child!! */
execv("another_program", NULL);
printf("This row will never be executed!!\n"
} else {
/* I am parent, kill the newly spawned child. */
sleep(5); /* wait for five seconds */
kill(pid, SIGINT);
}
you will actually start a new process, both the parent and child process will continue on the exact same instruction in two exact copies of code, one will get 0 from fork (the child) and the other (parent) will get the pid of the newly created child.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.