Unix Programming - Daemon problem.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2005 > Daemon problem.





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 Daemon problem.
seema_coma@yahoo.co.in

2005-10-24, 3:47 pm

Hi all,
I am new to Unix programming, i am facing a problem
and here is its explanation in my own words. I have
an application that depends on two daemons called
registry and service_manager. So when main application
starts it invokes registry daemon, and registry daemon
invokes service_manager daemon. Basically main application
depends on these two daemons for the reading and writing
data. It works fine but when i invoke 2 or more main simultaneously
like "linux_i32/main&;linux_i32/main&;linux_i32/main&;" it
starts running multiple instances of the "registry" and
"service_manager"
daemons which causes every instance of the main application to go
haywire.
I have tried to use following work around in both the daemons,
if ((tempFD = open(tempFileName, O_RDWR | O_CREAT | O_EXCL)) < 0){
sleep(1);
exit(0)
}
else{
sleep(1);
close(tempFileName);
remove(tempFileName);
}
So this stops the problem based on the no of fds.But the problem
is, it affects the overall performance by the overhead caused by
sleep(). Is any work around is possible to know whether instance of
daemon is already running ?
Can anyone explain how to do it?
Thanks in advance,
Seema Rao

Pascal Bourguignon

2005-10-24, 3:47 pm

seema_coma@yahoo.co.in writes:

> Hi all,
> I am new to Unix programming, i am facing a problem
> and here is its explanation in my own words. I have
> an application that depends on two daemons called
> registry and service_manager. So when main application
> starts it invokes registry daemon, and registry daemon
> invokes service_manager daemon.


How do you invoke your daemons?

> Basically main application
> depends on these two daemons for the reading and writing
> data. It works fine but when i invoke 2 or more main simultaneously
> like "linux_i32/main&;linux_i32/main&;linux_i32/main&;" it
> starts running multiple instances of the "registry" and
> "service_manager"
> daemons which causes every instance of the main application to go
> haywire.
> I have tried to use following work around in both the daemons,
> if ((tempFD = open(tempFileName, O_RDWR | O_CREAT | O_EXCL)) < 0){
> sleep(1);
> exit(0)
> }
> else{
> sleep(1);
> close(tempFileName);
> remove(tempFileName);
> }
> So this stops the problem based on the no of fds.But the problem
> is, it affects the overall performance by the overhead caused by
> sleep(). Is any work around is possible to know whether instance of
> daemon is already running ?
> Can anyone explain how to do it?


You seem to have a misconception here.

Daemons are program that work alone, detached from any controling
terminal and from other programs. Usually, the react to changes in
the file system. For example, they can periodically read a
configuration file and act according to the instructions in it (like
cron). Or they could watch for new files in a directory and process
them when they find some (like a print spooler). You could create a
daemon that would watch subdirectories in /ftp, and when it'd find a
file such as /ftp/ftp.example.com/file, it would send the file to the
ftp.example.com server and delete it. So you could magically
(demoniacally >:-} ) send files just by creating subdirectories in
/ftp and puting files there.

When you have a client program connecting to a server program, you
don't have a daemon, you have a server.

The question then is the IPC (Inter Process Communication) you'll use.


The client can speak to the server thru:
- shared memory
- message queues
- named pipes
- unix sockets
- tcp/ip sockets
- etc.

The point is that the server program (somewhat like a daemon) will be
already executing, waiting for requests on its IPC channel(s).

The client can know when the server is not executing, merely by trying
to open and write to the IPC channel of the server. If it gets errors,
then it means the server is not there, and the client could
auto-launch the server, and try again.

Let's take for example network sockets. So the client tries to
connect to the host/port where the server resides. If it cannot
connect, then it means the server is not there listening to the port,
so the client could try to launch it. (If the host is localhost, it
can launch the server with fork/exec; if the host is different, it can
launch it with a "ssh host start-server").

In any case, the server will need to implement the exclusive access to
the server IPC. Most often it's done automatically by the server:
mknod(2) for named pipes returns EEXIST if the named pipe already exists.
bind(2) for sockets returns EINVAL if the socket is already bound, etc.


--
__Pascal Bourguignon__ http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink.
Thobias Vakayil

2005-10-24, 3:48 pm

Hi,
use ps command to check the process is already running or not.
Regards,
Thobias
seema_coma@yahoo.co.in wrote:

>Hi all,
>I am new to Unix programming, i am facing a problem
>and here is its explanation in my own words. I have
>an application that depends on two daemons called
>registry and service_manager. So when main application
>starts it invokes registry daemon, and registry daemon
>invokes service_manager daemon. Basically main application
>depends on these two daemons for the reading and writing
>data. It works fine but when i invoke 2 or more main simultaneously
>like "linux_i32/main&;linux_i32/main&;linux_i32/main&;" it
>starts running multiple instances of the "registry" and
>"service_manager"
>daemons which causes every instance of the main application to go
>haywire.
>I have tried to use following work around in both the daemons,
>if ((tempFD = open(tempFileName, O_RDWR | O_CREAT | O_EXCL)) < 0){
> sleep(1);
> exit(0)
>}
>else{
> sleep(1);
> close(tempFileName);
> remove(tempFileName);
>}
>So this stops the problem based on the no of fds.But the problem
>is, it affects the overall performance by the overhead caused by
>sleep(). Is any work around is possible to know whether instance of
>daemon is already running ?
>Can anyone explain how to do it?
>Thanks in advance,
>Seema Rao
>
>
>



--
Thobias Vakayil
Alcatel Development India (ADI)
PH: 2349961/72/86 EXTN :7018
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com