Create subprocess (two distinct processes)

M

Muffinman

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
 
M

Mark Bluemel

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.
 
J

Jorgen Grahn

... 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
 
G

glen herrmannsfeldt

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
 
J

Johann Klammer

Muffinman said:
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.
 
J

Jorgen Grahn

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
 
H

Hans Vlems

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
 
M

Muffinman

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
 
J

Jorgen Grahn

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top