LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Run mpg123: system("mpg123"); How to know what PID it gets? And a Emacs highlightin q (http://www.linuxquestions.org/questions/programming-9/run-mpg123-system-mpg123-%3B-how-to-know-what-pid-it-gets-and-a-emacs-highlightin-q-288546/)

kornerr 02-10-2005 10:51 AM

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.

derekp 02-10-2005 11:18 AM

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.

Hko 02-10-2005 12:08 PM

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.

kornerr 02-11-2005 02:04 AM

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.

Hko 02-11-2005 07:05 AM

Quote:

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;
}


kornerr 02-11-2005 10:17 AM

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!!!

koldun 02-14-2005 12:23 AM

Hey. Is your name Kostya? How are you? How is your family?

kornerr 02-14-2005 09:35 AM

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!!!:)

koldun 02-15-2005 03:15 AM

Darov. Oshibsya znachit. A ty umeesh rabotat v QT? smozgesh mne po
moch?

kornerr 02-15-2005 09:44 AM

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!:cool:
__________________________
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)

kornerr 08-06-2005 10:17 AM

Another Emacs Highlighting q.

With ~/.emacs shown in the 3rd post Emacs doesn't hightlights function names everywhere I use them, only when definition.
How to do it everywhere?

When declaring a variable, variable is highlighted in ~orange color, but again only when declaration/definition.
How to do it everywhere?

Thanks.


All times are GMT -5. The time now is 07:40 AM.