Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
I am working with Obj C and Cocoa framework.
I am working on an application which has UI that interacts with a background process (Both are written by me). When application is started, background process is also started. Here, I want to put a check that if background process is already running (by any chance or if UI is crashed) it must be killed and new process must be started.
What is the way to know whether a particular process is running or not and how to kill it programatically.
 

ChrisA

macrumors G5
Jan 5, 2006
12,581
1,697
Redondo Beach, California
I am working with Obj C and Cocoa framework.
I am working on an application which has UI that interacts with a background process (Both are written by me). When application is started, background process is also started. Here, I want to put a check that if background process is already running (by any chance or if UI is crashed) it must be killed and new process must be started.
What is the way to know whether a particular process is running or not and how to kill it programatically.

Read the man pages for kill(2) sigaction(2) and signal(3)
THese are all pportable and work on any unix-like system

But if your process creates the others then it will get a signal when they terminate so you can avoid the checking and avoid the lag time between when it dies and when you check. You would know the process ID of the other tasks.
 

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
Killing a process programatically

I wrote an application in Obj C and Cocoa framework.
Before starting my application, I want to make sure that some processes (my other C++ process) are not running. If running, I have to terminate using some programming inside my application itself.
I don't want to NSTask to use "ps -ea | grep................" and kill the process.
Is there any way to do it by programming Obj C and Cocoa.
--Satyam.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Merging with your existing thread

Snow Leopard added a new NSRunningApplication class which has a forceTerminate and terminate method, if you don't need backwards compatibility.
 

ChrisA

macrumors G5
Jan 5, 2006
12,581
1,697
Redondo Beach, California
The "old way" that has been used for 25+ years on most Unix systems is then you start a process that you later might need to kill you also create a file in /var/programname/ that uses the process ID as the file name. Then later you loop over those files killing the processes (see man page kill(2)) and deleting (unlinking) the files. It is simple enough that this is many times done in a shell script. This is portable and works for C/C++ process that operate outside of Apple's APIs
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.