LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-28-2005, 06:51 AM   #1
Jeon, Chang-Min
LQ Newbie
 
Registered: Jul 2005
Location: South Korea
Posts: 9

Rep: Reputation: 0
I want to kill process


my program exectute mencoder program(from MPlayer) by using system function in program.

I want to kill process of mencoder in my program.

ex) int main()
{
// running program
}

void run_mencoder()
{
// system("mencoder");
}

void kill_mencoder()
{
// system("kill -9 ????"); //????->mencoder process ID
}

how to know process ID of mencoder?
 
Old 07-28-2005, 07:40 AM   #2
akudewan
Member
 
Registered: Apr 2004
Location: India
Distribution: Ubuntu
Posts: 364

Rep: Reputation: 31
The command is "pidof" which will be used as:
Code:
 pidof mencoder
 
Old 07-28-2005, 08:16 AM   #3
Jeon, Chang-Min
LQ Newbie
 
Registered: Jul 2005
Location: South Korea
Posts: 9

Original Poster
Rep: Reputation: 0
wow it's amazing

thank you very much your reply~

I have a one more question.

How to get return value (PID) of "pidof" in my program ?
 
Old 07-28-2005, 08:21 AM   #4
seran
Member
 
Registered: Aug 2004
Location: Bangalore
Posts: 64

Rep: Reputation: 16
PID=`pidof xxxx`
echo $PID

replace xxxx with ur process name.
 
Old 07-28-2005, 08:26 AM   #5
Jeon, Chang-Min
LQ Newbie
 
Registered: Jul 2005
Location: South Korea
Posts: 9

Original Poster
Rep: Reputation: 0
sorry, ~~

I want to use "pidof" program in my app by calling system function.

system function can't return value of "pidof"

How to get return value of "pidof" in my application.
 
Old 07-28-2005, 11:58 AM   #6
akudewan
Member
 
Registered: Apr 2004
Location: India
Distribution: Ubuntu
Posts: 364

Rep: Reputation: 31
Why not make it simpler and just do this:
Code:
 killall -9 mencoder
 
Old 08-09-2005, 12:14 AM   #7
seran
Member
 
Registered: Aug 2004
Location: Bangalore
Posts: 64

Rep: Reputation: 16
/proc can be usefull

I had a similar kind of work. If you want to get the process-id of a currently running process, you search thru the /proc directories. Inside this /proc you can see some directories named using numbers, those are all actually process-id's, and symbolic link exe inside each numbered directory will show the process-name.

This link will provide more information: http://www.linuxexposed.com/Articles...em---proc.html

I had written a small program, to get the process id of a process which was created using system(). I thought this will be usefull. The code is here...


#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

unsigned int getProcessID( char *process )
{
int target_link;
DIR *dir_p;
DIR *dir_proc;
struct dirent *dir_entry_p;
struct dirent *dir_proc_p;
char dir_name[35];
char target_name[235];
char exe_link[235];
char process_id[20];
int i;

dir_p = opendir( "/proc/" ); /* Open /proc/ directory */

while( NULL != (dir_entry_p = readdir(dir_p))) /* Reading /proc/ entries */
{
if ( strspn( dir_entry_p->d_name, "0123456789" ) == strlen( dir_entry_p->d_name ) ) /* Checking for numbered directories */
{
strcpy( process_id, dir_entry_p->d_name ); /* Saving the numbered directory i.e process-id */

strcpy( dir_name, "/proc/" );
strcat( dir_name, dir_entry_p->d_name );
strcat( dir_name, "/" ); /* Obtaining the full-path eg: /proc/24657/ */

if ( ( dir_proc = opendir( dir_name ) ) ) /* Open that numbered directory */
{
while ( ( dir_proc_p = readdir( dir_proc ) ) ) /* Reading its entries */
{
if ( strcmp( dir_proc_p->d_name, "exe" ) == 0 ) /* Check for that exe link file */
{
strcat( exe_link, dir_name );
strcat( exe_link, dir_proc_p->d_name ); /* Getting the full-path of that exe link */

/* Getting the target of the exe ie to which binary it points to */
target_link = readlink( exe_link, target_name, sizeof( target_name ) );
target_name[ target_link ] = '\0';

if ( strstr( target_name, process ) != NULL ) /* Searching for process name in the target name */
{
printf( "\n\n%s FOUND --- PROCESS_ID %s \n\n\n", process, process_id );
return atoi(process_id); /* GOT IT: return the process-id */
}

strcpy( exe_link ,"" );
}
}
}
else
printf( "Dir %s opening failed \n", dir_name );
}
}

closedir(dir_p);
return 0;
}


int main()
{
system( "emacs &" );
printf( " %u ", getProcessID( "emacs" ) );
return 0;
}


I was able to use this program successfully. Let me know if there are some other ways.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Process won't kill mrsolo Linux - Software 5 08-03-2007 12:45 PM
cannot kill process (kill -9 does not work) mazer13a Linux - General 1 05-27-2005 02:32 PM
Cannot kill process Zeno McDohl Linux - General 13 04-14-2005 02:31 PM
kill a process for sure spuzzzzzzz Linux - General 1 06-05-2004 04:59 PM
Why can't I kill this process? solking Linux - General 5 10-29-2003 11:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration