Create subprocess (two distinct processes)

Discussion in 'C Programming' started by Muffinman, Nov 28, 2012.

  1. Muffinman

    Muffinman Guest

    Hello all,

    I have two C programs which need each other. They both do some realtime
    tasks (listening to message queue and Alsa mixer events) and to keep
    that realtime I've got them in two separate applications. Now I could
    create some bash script to execute both in the proper order, but I would
    rather do that in C, to keep it a bit more user friendly. The order of
    tasks is as follows:

    App1: Read configuration file -> open serial port -> open message queue
    (creates queue) -> listen to message queue (receives messages from both
    C and PHP scripts). -> if message received translate and send to serial
    port -> listen to message queue, etc.

    After the message queue is opened above I would like to open a second
    application which does the following:

    App2: Open message queue (may not create queue) -> listen for Alsa mixer
    events -> event is formatted properly and send to message queue ->
    listen for Alsa mixer events, etc.

    How do I execute this App2 at the proper moment? When searching online I
    only get to forking the process. However, the applications don't share
    any code, so that would be combining two different apps in one for the
    sake of having just one file (is that what forking is about?). In
    addition I would have to split the parent and child at the beginning of
    main() and send e.g. a SIGUSR1 from App1 to App2 when it has opened its
    message queue to tell App2 it may continue to execute its code (and
    prevent it from opening the queue while it does not yet exist).

    All I need is App2 to open at the proper moment and for App1 to have the
    pid of App2 so it can send a SIGTERM and have a proper shutdown when needed.

    I hope I've made my point clear. Can someone tell me how I can approach
    this task?

    Thanks in advance, Maarten
    Muffinman, Nov 28, 2012
    #1
    1. Advertising

  2. Muffinman

    Mark Bluemel Guest

    On 28/11/2012 13:11, Muffinman wrote:
    > Hello all,
    >
    > I have two C programs which need each other. They both do some realtime
    > tasks (listening to message queue and Alsa mixer events) and to keep
    > that realtime I've got them in two separate applications. Now I could
    > create some bash script to execute both in the proper order, but I would
    > rather do that in C, to keep it a bit more user friendly. The order of
    > tasks is as follows:
    >
    > App1: Read configuration file -> open serial port -> open message queue
    > (creates queue) -> listen to message queue (receives messages from both
    > C and PHP scripts). -> if message received translate and send to serial
    > port -> listen to message queue, etc.
    >
    > After the message queue is opened above I would like to open a second
    > application which does the following:
    >
    > App2: Open message queue (may not create queue) -> listen for Alsa mixer
    > events -> event is formatted properly and send to message queue ->
    > listen for Alsa mixer events, etc.
    >
    > How do I execute this App2 at the proper moment? When searching online I
    > only get to forking the process. However, the applications don't share
    > any code, so that would be combining two different apps in one for the
    > sake of having just one file (is that what forking is about?). In
    > addition I would have to split the parent and child at the beginning of
    > main() and send e.g. a SIGUSR1 from App1 to App2 when it has opened its
    > message queue to tell App2 it may continue to execute its code (and
    > prevent it from opening the queue while it does not yet exist).
    >
    > All I need is App2 to open at the proper moment and for App1 to have the
    > pid of App2 so it can send a SIGTERM and have a proper shutdown when needed.
    >
    > I hope I've made my point clear. Can someone tell me how I can approach
    > this task?


    Given that as far as I remember, the C language doesn't have much to say
    about separate processes, you're probably looking at platform-specific
    issues and may do better on a platform-specific newsgroup, such as
    comp.unix.programmer. Your reference to forking suggests Unix/Linux.

    If you are working on a unix-like system, I suggest you get hold of W
    Richard Stevens' book "Advanced Programming in the Unix Environment",
    which covers this subject extremely well.
    Mark Bluemel, Nov 28, 2012
    #2
    1. Advertising

  3. Muffinman

    Jorgen Grahn Guest

    On Wed, 2012-11-28, Muffinman wrote:
    > ... Now I could
    > create some bash script to execute both in the proper order, but I would
    > rather do that in C, to keep it a bit more user friendly.


    I know this is c.l.c., but I have to mention it anyway: a shell script
    is in no way less user-friendly than a C program. In Unix, a shell
    script does not have to be just a temporary, fragile hack, if that is
    what you're thinking.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 28, 2012
    #3
  4. Jorgen Grahn <> wrote:
    > On Wed, 2012-11-28, Muffinman wrote:


    >> ... Now I could
    >> create some bash script to execute both in the proper order, but I would
    >> rather do that in C, to keep it a bit more user friendly.


    > I know this is c.l.c., but I have to mention it anyway: a shell script
    > is in no way less user-friendly than a C program. In Unix, a shell
    > script does not have to be just a temporary, fragile hack, if that is
    > what you're thinking.


    And since this is comp.lang.c, isn't the whole reason for csh
    (and successors) to make a shell more C like for C programmers?

    -- glen
    glen herrmannsfeldt, Nov 28, 2012
    #4
  5. Muffinman wrote:
    > Hello all,
    >
    > I have two C programs which need each other. They both do some realtime
    > tasks (listening to message queue and Alsa mixer events) and to keep
    > that realtime I've got them in two separate applications. Now I could
    > create some bash script to execute both in the proper order, but I would
    > rather do that in C, to keep it a bit more user friendly. The order of
    > tasks is as follows:
    >
    > App1: Read configuration file -> open serial port -> open message queue
    > (creates queue) -> listen to message queue (receives messages from both
    > C and PHP scripts). -> if message received translate and send to serial
    > port -> listen to message queue, etc.
    >
    > After the message queue is opened above I would like to open a second
    > application which does the following:
    >
    > App2: Open message queue (may not create queue) -> listen for Alsa mixer
    > events -> event is formatted properly and send to message queue ->
    > listen for Alsa mixer events, etc.
    >
    > How do I execute this App2 at the proper moment? When searching online I
    > only get to forking the process. However, the applications don't share
    > any code, so that would be combining two different apps in one for the
    > sake of having just one file (is that what forking is about?).In

    Forking alone, yes. But typically you would do an exec as the next step
    which loads the other program(the one you actually want to run). AFAIK
    this is a historic thing in UNIX and has been done like this from very
    early on. That's all I know about it.

    > addition I would have to split the parent and child at the beginning of
    > main() and send e.g. a SIGUSR1 from App1 to App2 when it has opened its
    > message queue to tell App2 it may continue to execute its code (and
    > prevent it from opening the queue while it does not yet exist).
    >
    > All I need is App2 to open at the proper moment and for App1 to have the
    > pid of App2 so it can send a SIGTERM and have a proper shutdown when needed.
    >
    > I hope I've made my point clear. Can someone tell me how I can approach
    > this task?
    >
    > Thanks in advance, Maarten
    Johann Klammer, Nov 29, 2012
    #5
  6. Muffinman

    Jorgen Grahn Guest

    On Wed, 2012-11-28, glen herrmannsfeldt wrote:
    > Jorgen Grahn <> wrote:
    >> On Wed, 2012-11-28, Muffinman wrote:

    >
    >>> ... Now I could
    >>> create some bash script to execute both in the proper order, but I would
    >>> rather do that in C, to keep it a bit more user friendly.

    >
    >> I know this is c.l.c., but I have to mention it anyway: a shell script
    >> is in no way less user-friendly than a C program. In Unix, a shell
    >> script does not have to be just a temporary, fragile hack, if that is
    >> what you're thinking.

    >
    > And since this is comp.lang.c, isn't the whole reason for csh
    > (and successors) to make a shell more C like for C programmers?


    According to legend, at least. Note though that most people
    agree that csh was a failure as a programming language.
    As far as I can tell it's not very C-like, either.

    I happily mix C with shell scripts, but then I use /bin/sh or bash.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 30, 2012
    #6
  7. Muffinman

    Hans Vlems Guest

    On 28 nov, 14:11, Muffinman <news**> wrote:
    > Hello all,
    >
    > I have two C programs which need each other. They both do some realtime
    > tasks (listening to message queue and Alsa mixer events) and to keep
    > that realtime I've got them in two separate applications. Now I could
    > create some bash script to execute both in the proper order, but I would
    > rather do that in C, to keep it a bit more user friendly. The order of
    > tasks is as follows:
    >
    > App1: Read configuration file -> open serial port -> open message queue
    > (creates queue) -> listen to message queue (receives messages from both
    > C and PHP scripts). -> if message received translate and send to serial
    > port -> listen to message queue, etc.
    >
    > After the message queue is opened above I would like to open a second
    > application which does the following:
    >
    > App2: Open message queue (may not create queue) -> listen for Alsa mixer
    > events -> event is formatted properly and send to message queue ->
    > listen for Alsa mixer events, etc.
    >
    > How do I execute this App2 at the proper moment? When searching online I
    > only get to forking the process. However, the applications don't share
    > any code, so that would be combining two different apps in one for the
    > sake of having just one file (is that what forking is about?). In
    > addition I would have to split the parent and child at the beginning of
    > main() and send e.g. a SIGUSR1 from App1 to App2 when it has opened its
    > message queue to tell App2 it may continue to execute its code (and
    > prevent it from opening the queue while it does not yet exist).
    >
    > All I need is App2 to open at the proper moment and for App1 to have the
    > pid of App2 so it can send a SIGTERM and have a proper shutdown when needed.
    >
    > I hope I've made my point clear. Can someone tell me how I can approach
    > this task?
    >
    > Thanks in advance, Maarten


    Maarten,
    would the system() function (found in stdlib.h) do what you want?
    Hans
    Hans Vlems, Dec 1, 2012
    #7
  8. Muffinman

    Muffinman Guest

    On 28-11-12 22:43, Jorgen Grahn wrote:

    > I know this is c.l.c., but I have to mention it anyway: a shell script
    > is in no way less user-friendly than a C program. In Unix, a shell
    > script does not have to be just a temporary, fragile hack, if that is
    > what you're thinking.
    >
    > /Jorgen
    >


    That's true. I probably need a shell script anyways to start the program
    at boot. However, I do need the pid of app2 available to app1. I could
    do this with a shell script (I think), but then a user could manipulate
    it (though I wonder why anyone would). Also, since both apps are
    daemonized I'm not sure I can give feedback to shell script telling one
    app did start fine and giving go for the second one.

    Anyway, the book mentioned by Mark is quite helpful. With a bit better
    understanding of forking, daemonizing (and using exec as mentioned by
    Johann), etc. I think this is the way to get what I want, and have a
    proper daemon as well (I learned).

    @Hans: I think it is quite hard to get the pid of a process called from
    system(). I think forking is more efficient.

    Thanks all, Maarten
    Muffinman, Dec 5, 2012
    #8
  9. Muffinman

    Jorgen Grahn Guest

    On Wed, 2012-12-05, Muffinman wrote:
    > On 28-11-12 22:43, Jorgen Grahn wrote:
    >
    >> I know this is c.l.c., but I have to mention it anyway: a shell script
    >> is in no way less user-friendly than a C program. In Unix, a shell
    >> script does not have to be just a temporary, fragile hack, if that is
    >> what you're thinking.

    >
    > That's true. I probably need [...]. I could
    > do this with a shell script (I think), but then a user could manipulate
    > it (though I wonder why anyone would).


    Trying to protect a program from its user is very difficult (and tends
    to upset some user). If you're not sure you have to worry about this,
    don't worry!

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Dec 5, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,089
    David Schwartz
    Dec 9, 2003
  2. johkar
    Replies:
    2
    Views:
    1,369
    johkar
    Dec 4, 2009
  3. Hicham Mouline
    Replies:
    1
    Views:
    383
    Kai-Uwe Bux
    Apr 11, 2010
  4. Marco Alting

    SELECT DISTINCT from two tables

    Marco Alting, Jul 30, 2003, in forum: ASP General
    Replies:
    4
    Views:
    158
    Phill. W
    Jul 31, 2003
  5. Marc Heiler
    Replies:
    1
    Views:
    163
    Robert Klemme
    May 24, 2009
Loading...

Share This Page