LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-10-2005, 11:51 AM   #1
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Rep: Reputation: 35
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.
 
Old 02-10-2005, 12:18 PM   #2
derekp
LQ Newbie
 
Registered: Nov 2003
Location: Dublin
Distribution: Slackware9.1
Posts: 28

Rep: Reputation: 15
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.
 
Old 02-10-2005, 01:08 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.

Last edited by Hko; 02-10-2005 at 01:16 PM.
 
Old 02-11-2005, 03:04 AM   #4
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
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.
 
Old 02-11-2005, 08:05 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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;
}

Last edited by Hko; 02-11-2005 at 08:21 AM.
 
Old 02-11-2005, 11:17 AM   #6
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
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!!!
 
Old 02-14-2005, 01:23 AM   #7
koldun
LQ Newbie
 
Registered: Feb 2005
Location: Russia, Tyumen
Posts: 20

Rep: Reputation: 0
Hey. Is your name Kostya? How are you? How is your family?
 
Old 02-14-2005, 10:35 AM   #8
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
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!!!
 
Old 02-15-2005, 04:15 AM   #9
koldun
LQ Newbie
 
Registered: Feb 2005
Location: Russia, Tyumen
Posts: 20

Rep: Reputation: 0
Darov. Oshibsya znachit. A ty umeesh rabotat v QT? smozgesh mne po
moch?
 
Old 02-15-2005, 10:44 AM   #10
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
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)

Last edited by kornerr; 02-15-2005 at 10:55 AM.
 
Old 08-06-2005, 11:17 AM   #11
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
"startx" crashes entire system after xorgconfig is run sether Arch 7 11-16-2004 03:11 PM
system("top") in a C program giving problems when the C prg is run by cron rags2k Programming 1 09-02-2004 04:25 PM
bash equivalence of tcsh "alias em "emacs \!:1 &""? rgiggs Slackware 3 07-29-2004 03:07 AM
"X-MS" cant open because "x-Multimedia System" cant access files at "smb&qu ponchy5 Linux - Networking 0 03-30-2004 12:18 AM


All times are GMT -5. The time now is 01:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration