Unix Programming - Problem in Child process

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2005 > Problem in Child process





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Problem in Child process
seema_coma@yahoo.co.in

2005-10-29, 7:47 am

Hi All I am new to unix programming. I have a
problem, I am using fork and in the child
process I am doing some what like this,

if ((pid = fork()) == 0) {
execvp(path, regdArgv);/*execute "my executable"*/
}

which will invoke an executable. But i just
want to do some thing like this,
if ((pid = fork()) == 0) {
if (/my executable is already running*/){
exit(0);
}
else{
execvp(path, regdArgv);
}

}

Can some body explain how to CHECK that executable
is already running ?

Thanks in Advance,
Seema Rao

Jordan Abel

2005-10-29, 7:47 am

On 2005-10-29, seema_coma@yahoo.co.in <seema_coma@yahoo.co.in> wrote:
> Hi All I am new to unix programming. I have a
> problem, I am using fork and in the child
> process I am doing some what like this,
>
> if ((pid = fork()) == 0) {
> execvp(path, regdArgv);/*execute "my executable"*/
>}
>
> which will invoke an executable. But i just
> want to do some thing like this,
> if ((pid = fork()) == 0) {
> if (/my executable is already running*/){
> exit(0);
> }
> else{
> execvp(path, regdArgv);
> }
>
>}
>
> Can some body explain how to CHECK that executable
> is already running ?
>
> Thanks in Advance,
> Seema Rao
>


Well, other than the obvious question of why you want to do this
[what if someone wants to run multiple instances of the program],
i'd suggest using a pidfile
seema_coma@yahoo.co.in

2005-10-29, 7:47 am

I just want to stop running multiple instance of the program.Would you
please give me a hint on how to use pidfile

Jordan Abel

2005-10-29, 7:47 am

On 2005-10-29, seema_coma@yahoo.co.in <seema_coma@yahoo.co.in> wrote:
> I just want to stop running multiple instance of the program.Would you
> please give me a hint on how to use pidfile


Can you explain _why_ you want that?
seema_coma@yahoo.co.in

2005-10-29, 7:47 am

Basically my executable again forks and executes a system wide daemon.
So if i run multiple instance of the code i am mentioning like,
linux_i32/somepad&;linux_i32/somepad&;linux_i32/somepad&
It will invoke multiple instances of daemon and even my Linux Box goes
out of memory and hangs. Thats why!!

Jordan Abel

2005-10-29, 7:47 am

NNTP-Posting-Host: tark-b-149.resnet.purdue.edu
X-Trace: mailhub227.itcs.purdue.edu 1130587043 31836 128.211.221.149 (29 Oct 2005 11:57:23 GMT)
X-Complaints-To: news@purdue.edu
NNTP-Posting-Date: Sat, 29 Oct 2005 11:57:23 +0000 (UTC)
User-Agent: slrn/0.9.8.1 (FreeBSD)
Xref: number1.nntp.dca.giganews.com comp.unix.programmer:162197

On 2005-10-29, seema_coma@yahoo.co.in <seema_coma@yahoo.co.in> wrote:
> Basically my executable again forks and executes a system wide daemon.
> So if i run multiple instance of the code i am mentioning like,
> linux_i32/somepad&;linux_i32/somepad&;linux_i32/somepad&
> It will invoke multiple instances of daemon and even my Linux Box goes
> out of memory and hangs. Thats why!!


This should be handled from the initscript [shell script in
/etc/init.d or /etc/rc.d] that invokes your system daemon - not in a
binary executable.
seema_coma@yahoo.co.in

2005-10-29, 7:47 am

We use this Daemon as System Daemon and some time Daemon for single
user. So how do I stop multiple instance in that case?

Jordan Abel

2005-10-29, 5:51 pm

On 2005-10-29, seema_coma@yahoo.co.in <seema_coma@yahoo.co.in> wrote:
> We use this Daemon as System Daemon and some time Daemon for single
> user. So how do I stop multiple instance in that case?


Redesign your program - what kind of daemon is it?
joe@invalid.address

2005-10-29, 5:51 pm

seema_coma@yahoo.co.in writes:

> Hi All I am new to unix programming. I have a
> problem, I am using fork and in the child
> process I am doing some what like this,
>
> if ((pid = fork()) == 0) {
> execvp(path, regdArgv);/*execute "my executable"*/
> }
>
> which will invoke an executable. But i just
> want to do some thing like this,
> if ((pid = fork()) == 0) {
> if (/my executable is already running*/){
> exit(0);
> }
> else{
> execvp(path, regdArgv);
> }
>
> }
>
> Can some body explain how to CHECK that executable
> is already running ?


Have the executable create a file somewhere (on a *local* file
system), and get a lock on it. It can simply be a zero length file,
the important thing is that it's locked.

When the program starts up, have it try to get a lock on the file. If
it can't get a lock, then an instance of the program is already
running.

Joe
--
Gort, klatu barada nikto
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com