Run mpg123: system("mpg123"); How to know what PID it gets? And a Emacs highlightin q
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Run mpg123: system("mpg123"); How to know what PID it gets? And a Emacs highlightin q
I run 'mpg123' as
Code:
system("mpg123");
How to know what PID it gets? And a q about Emacs C++ highlighting
1) I just want to "make" something like xmms under console, using 'mpg123'. I.e. I just want to make an app that'll be able to choose what song to play from a playlist. I want to be able to stop playing whenever I want, too. May be you can advise another mean?
2) And I want to know how to force Emacs to highlight reserved cpp words, funcs, etc.
Thanks.
Don't know how you would implement your player,
but to get syntax highlighting in emacs, I just open a file, hit "F10 o s" - though theres probably a switch for it on the command line.
Re: Run mpg123: system("mpg123"); How to know what PID it gets? And a Emacs highlight
Quote:
Originally posted by kornerr
I run 'mpg123' as
Code:
system("mpg123");
How to know what PID it gets?
Instead of system(), use the combination of fork() and one of the functions in the exec()-family (see "man exec" to pick your choice).
fork() duplicates you running program to another (child-) process. In the original process (the parent) fork() it will return the PID of the new process (the child process).
Call exec..("mpg123", ...) in the child process (you'll know this because fork() returns 0 there). This will replace the program running in the child process with mpg123.
Quote:
2) And I want to know how to force Emacs to highlight reserved cpp words, funcs, etc.
I have put this in my ~/.emacs file:
Code:
(setq font-lock-maximum-decoration
'((c-mode . 3)
(c++-mode . 3)
(java-mode . 3)
(jde-mode . 3)
(t . 3))
)
(global-font-lock-mode t)
(show-paren-mode t)
(column-number-mode t)
(defun my-c-mode-common-hook ()
(c-set-style "k&r")
(setq tab-width 4
;; this will make sure spaces are used instead of tabs
indent-tabs-mode nil)
(setq c-basic-offset 4)
(c-set-offset 'case-label '4)
(c-set-offset 'comment-intro '0)
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
After you do that restart emacs.
If you only want the syntax-highlighting part, use only the first part up until "(show-paren-mode t)" or "(column-number-mode t)". The rest can be useful while coding C/C++ in emacs as well. They are set my preferences, but you can off course use them and change them to your liking.
Thanks, Hko and derekp. My Emacs now highlights what it should to. Actually I didn't understand how to use fork() and exec(), but I didn't watch man yet. I think I should read "Thinking in C++" farther (I'm only on ch3, vol1). Thanks again.
_________________________________
Thanks for book, Hko, - it's the best book about programming I've ever seen.
Originally posted by kornerr
Actually I didn't understand how to use fork() and exec(), but I didn't watch man yet:).
OK, here's a way to do that:
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLEN 200
#define PLAYER "mpg123"
static sig_atomic_t player_finished = 0;
void signalhandler(int sig)
{
/* Do as little as possible in here!
* So just setting global atomic variable.
*/
player_finished = 1;
}
int main()
{
int i, n, childexitcode;
pid_t pid;
char filename[MAXLEN];
struct sigaction sa;
/* Endless loop asking for mp3' and play them */
for (;;) {
printf("Please enter mp3 file name: ");
fgets(filename, MAXLEN, stdin);
/* Remove newline from the end */
n = strlen(filename) - 1;
if (filename[n] == '\n') filename[n] = '\0';
pid = fork();
if (pid == 0) { /* Running child process here */
/* Replace program running in child process with mpg123 */
puts(filename);
execlp(PLAYER, PLAYER, filename, NULL);
fprintf(stderr, "Error starting player: \"%s\"\n", PLAYER);
}
/*
* We're in the parent process here.
*/
/* Register signal function to act when child process finishes. */
sa.sa_handler = signalhandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGCHLD, &sa, NULL);
/* Printing PID of mpg123 each second to demonstrate this runs
* concurrently with mpg123.
*/
for (i = 0; player_finished == 0; ++i) { /* loop until child stops */
sleep(1);
printf("mpg123 running in PID %d for %d seconds now.\n", pid, i);
}
/* Cleanup dead child process, otherwise it would stay as around
* as a zombie process.
*/
wait(&childexitcode);
printf("\nPlayer finished with exit code %d.\n", childexitcode);
} /* Loop again, asking for next mp3 file */
return 0;
}
MAN!!! You shouldn't worry about me so much, but THANK YOU VERY MUCH!!! Now I really understood that I have to read further (or farther... my English is very bad). Thanks!!!
Hello, koldun. My name is Slim Shady!!! Heh, just a joke. My name is Misha from... KEMEROVO! WoW, wow! Cold region So mnoy vse OK! I s sem'ei vse normalek! Kakimi sud'bami zdes'? Tak daleko ot Rodini? Do you know Andruha from Ukraine, Odessa? I've seen him here once. Was nice to meet you! By the way... use Slackware!!!
Hi again, koldun.
Nu, odin tutorial sdelal. Pitalsya vtoroy... no zabil. My cpp knowledge are very POOR. So I decided to read the best book about programming in cpp. Hko gave me an excellent link. And helped me with PID. Then I have a willing to try myself in OpenGL, SDL or smth similar. I like only 2-3 games, but they R the coolest (I think). I want smth better for Linux only, to f*** M$, but it's in the far... far future. If it will be... These Excellent games are: Gothic I and II, and, of course, Morrowind! So now I want to ask you: if you know cpp... you shouldn't worry about understanding QT. I know that here are people who will help you almost in any kind of situations. Just work in QT and ask interesting questions - they'll be glad to help you. They'll even answer stupid questions (this is my type of questions... still). Here's excellent community. So probably I will ask you rather than YOU will ask me Here's the link: www.bruceeckel.com The book is called 'Thinking in C++' Great book!
By the way: what distro do you use???
nice to hear Russian here and all who helps me, too!
__________________________
And by another way: people here are very good, but I've already heard willings (from moderators, too) to talk about the topic. So if you want to talk to me in Russian, you can always mail me. Bye. (and don't forget to rate my s**tty useless site I'll updated it in about a week. I'll put there my Slackware propaganda... that'll be hot enough)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.