• C

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.
julius-krebsAsked:
Who is Participating?
 
veedarConnect With a Mentor Commented:
Sounds like a job for killall - kill processes by name:
http://linux.die.net/man/1/killall
0
 
evilrixSenior Software Engineer (Avast)Commented:
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.
0
 
julius-krebsAuthor Commented:
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

0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
 
Infinity08Commented:
>> 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.
0
 
evilrixSenior Software Engineer (Avast)Commented:
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

0
 
Infinity08Commented:
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 ?
0
 
julius-krebsAuthor Commented:
@ 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.