[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Start and kill a process (Linux)

Posted on 2009-04-18
8
Medium Priority
?
1,464 Views
Last Modified: 2012-05-06
# 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.
0
Comment
Question by:julius-krebs
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24175161
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
 

Author Comment

by:julius-krebs
ID: 24175316
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
 
LVL 15

Accepted Solution

by:
veedar earned 1000 total points
ID: 24175339
Sounds like a job for killall - kill processes by name:
http://linux.die.net/man/1/killall
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 40

Expert Comment

by:evilrix
ID: 24175528
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24175588
>> 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
 
LVL 40

Expert Comment

by:evilrix
ID: 24175595
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24175611
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
 

Author Comment

by:julius-krebs
ID: 24193016
@ 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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question