Creating daemon in program

580 views
Skip to first unread message

Ashish

unread,
Mar 2, 2012, 3:17:01 AM3/2/12
to golan...@googlegroups.com
Can we create daemons in go ,as i need to fork on each time this command ./binFile start is run and stop on ./binFile stop will stop all threads.
I had a look at os.StartProcess() and exec package  but will need to write a program two programs two achive task, one to handle the commands and other my main program which is not preferable.
Is there any way we can easily fork like the one provided in C fork().

Using syscall.RawSyscall() is giving me Epoll unexpected fd=7
pollServer: unexpected wakeup for fd=7 mode=w and is hanging my child process reason maybe accessing the fd which was created in parent process.



 
 

Ian Lance Taylor

unread,
Mar 2, 2012, 11:41:36 AM3/2/12
to Ashish, golan...@googlegroups.com
Ashish <ashis...@raweng.com> writes:

> Can we create daemons in go ,as i need to fork on each time this command
> ./binFile start is run and stop on ./binFile stop will stop all threads.
> I had a look at os.StartProcess() and exec package but will need to write
> a program two programs two achive task, one to handle the commands and
> other my main program which is not preferable.
> Is there any way we can easily fork like the one provided in C fork().

http://code.google.com/p/go/issues/detail?id=227

In general it's hard to fork a program that uses threads, because there
is no simple way to ensure that all the threads are in a consistent
state.


> Using syscall.RawSyscall() is giving me Epoll unexpected fd=7
> pollServer: unexpected wakeup for fd=7 mode=w and is hanging my child
> process reason maybe accessing the fd which was created in parent process.

Note that Go already uses epoll internally. You should never use
RawSyscall for a system call that can block. You must use Syscall
instead.

Ian

Ashish

unread,
Mar 5, 2012, 12:52:15 AM3/5/12
to golang-nuts
Go is internally doing process of forking as it gives us interface to
start a process and run it using exec package so there should be a
signal which we have to set before forking or some way to handle this
which i am not able to dig it out.
I will be working on this and inform you if i succed
It is not even working with Syscall funtion.

Ian Lance Taylor

unread,
Mar 5, 2012, 1:45:37 AM3/5/12
to Ashish, golang-nuts
Ashish <ashis...@raweng.com> writes:

> Go is internally doing process of forking as it gives us interface to
> start a process and run it using exec package so there should be a
> signal which we have to set before forking or some way to handle this
> which i am not able to dig it out.

I don't know what it is that you are trying to handle. If this is about
the general issue of creating a daemon process, I'll again mention
http://code.google.com/p/go/issues/detail?id=227 .

Forking to exec a program is a special case. It doesn't matter whether
threads are in a consistent state if you are about to call exec anyhow.
You just have to be very careful about the code that runs in the child
process after the fork but before the exec.

Ian

Reply all
Reply to author
Forward
0 new messages