Link to home
Start Free TrialLog in
Avatar of julius-krebs
julius-krebs

asked on

Start and kill a process (Linux)

# Tools
C++, GNU, Linux

# Goal
Play an audio file in a c++ application using gstream via a system cmd like this:
$ gst-launch-0.10 playbin uri=file:///home/krebs/test.mp3 > /dev/null 2>&1.
The "kill" function should be used to stop.

# Problem
To kill the gsrteam process a id is reqired and i don't now how to get it. ok, there is a possibility seraching the "/proc/" folder by the process name, but i hope therer is easier solution.
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

You could try this...

Use fork() to have your process spawn a child, fork() will return the pid of the new child. The child process can then start your external command using one of the exec() functions, which will replace your child process in memory but preserve the pid. The idea is that you then signal the child process from the parent, using the pid obtained from fork().

http://www.opengroup.org/onlinepubs/000095399/functions/fork.html
http://www.opengroup.org/onlinepubs/000095399/functions/exec.html

BTW: This is just a suggestion, I have not tested it.
Avatar of julius-krebs
julius-krebs

ASKER

Is there realy no other way to get to the PID?
In the shell its's automaticly diplayed.



krebs@krebs-desktop:~$ gst-launch-0.10 playbin uri=file:///home/krebs/test.mp3 >/dev/null 2>&1 &
[6] 25270

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of veedar
veedar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> kill processes by name
This really should be done with care because it can kill ALL processes of that name (assuming the user has the correct privilidges to kill each processes). It is for this reason I didn't suggest this. The forking of a new process and then calling exec to replace this process is going to be safer. Still, if killall serves your purpose then so be it.
>> Is there realy no other way to get to the PID?
>> In the shell its's automaticly diplayed.

That is because the shell basically forked off the process (that is now running in the background), so it has access the the process id, and can display it to the user for future use.

Note that every process has access to its own process id using getpid :

        http://linux.die.net/man/2/getpid

so, for your process, you can get its process id, and pass it on to wherever you need it.

The process id is the principal identifier of a process, so if you need to get access to a certain process, you need a way to get its process id, either by getting fork's return value if it's a child process, or other means if it's not.
Right, the code to do this (at least a very quick example) is below. As you can see there really is nothing to it. I'd suggest that you take a look at this since the killall idea really isn't the way to go and is quite possible dangerous (for the reasons I state above).
#include <unistd.h>
#include <signal.h>
#include <iostream>
 
int main()
{
        pid_t pid = fork();
 
        if(pid < 0)
        {
                // Error condition
        }
        else
        if(pid > 0)
        {
                // Parent
                std::cout << "Child pid: " << pid << std::endl;
                getchar();
                kill(pid, SIGTERM);
        }
        else
        {
                // Child
                execl("/usr/bin/sleep", "999999");
        }
}

Open in new window

Just an observation : although this question was posted in the C programming zone, the accepted answer was not a C solution, but rather a shell command.

Was this question supposed to be in the C programming zone ?
@ evilrix
Thank you for the detailed answer.

@ Infinity08
I use the command in c, so we have a mix of several languages.

# my solution
For first I will use "killall", later i will make my app more secure with the kind process method.