• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Killing process using C language

 
karthik deepak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to write code to kill a process using C language.
I should write C code and use it in java using JNI
Is there an API in C that kills a process given its name or something like pid in unix?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to use JNI or C. You can use Runtime.exec or ProcessBuilder to launch whatever Unix utility you prefer to handle this (most likely ps and kill).
 
karthik deepak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to write code to kill a process on a Windows(Win 2000 server, Win 2003 server) and unix systems.

1) For unix systems i wrote a shell script which did the job ->

procID=`ps -e | grep $1 | grep -v grep | awk '{print $1}'`
kill -9 $procID

But i was asked to integrate this into java code and creat an APi, how do i do that.

2) I was given a specification saying that we cant kill a process on server cleanly using java, so write native code in C and intgrate into java using JNI.
But how to kill a process using C?
Is there a way to kill it cleanly in java?
will destroy() method kill a process cleanly?

HELP ME OUT PLEASE...

Im a fresher into the company, so facing an uphill task understanding things..
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#1 - by using Runtime.exec or ProcessBuilder

For Windows, if you want to use JNI -for which there is no need- then you need to find a C/C++ API that can terminate processes. Some rudimentary code for that is mentioned in http://www.coderanch.com/t/385840/Java-General-intermediate/java/Relaunch-program-if-it-crashes
You can also use Windows' tasklist utility (for which the Runtime.exec/ProcessBuilder approach should also work).
 
Abhinav Trivedi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthik deepak wrote:I need to write code to kill a process using C language.
I should write C code and use it in java using JNI
Is there an API in C that kills a process given its name or something like pid in unix?



Actually you can do it with taskkill command that default shipps with windows. You can check the syntax. And in C language and if you are using borland editor import dos.h and use int86 interrupt handler to kill a process. There is also the unixtodos function. Hope you will get the idea.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic