ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I dont know much about python but assuming that os.fork() is the same as the fork() call in most other languages then it is doing what it is supposed to.
When a program calls fork() from that moment on there are two processes in identical states. The only difference is that in one process (the original, or 'parent' process) fork() returned a positive process id, and in the other process (the new, or 'child' process) it returned 0. You then use an 'if' statement to send the two processes down different code-paths. If -1 is returned then there was an error and no new process was created.
So in pseudo-code:
Code:
child_process = fork()
If child_process equals 0 then
print "This is the child"
Else If child_process not equals -1 then
print "This is the parent, and my child is " + child_process
Else
print "Something went wrong"
End If
When you fork a process, it literally gets split in two. All memory, etc. is copied into a second process image, which begins execution exactly after the fork occurred. As a result, when you write code for a program that uses a fork, you have to write out all the code for both processes in the same place. In all cases I know of, calling fork() returns 0 to the child process, and the pid of the child to the parent process (in C, it also returns -1 on error). Here's a little bit of example code for demonstration:
Code:
pid = os.fork()
if pid == 0:
# this is the child
do_something()
_exit(0)
else
# this is the parent
os.waitpid(pid, 0)
do_something_else()
The first part of the if statement represents the child process. The second part is for the parent. Note that the last line of the child's section is _exit(). This is because we want to clean up all of the stuff we did in the child (file descriptors, etc.) and (I think) send SIGCHLD to the parent to let it know we're done. You could also use an exec() statement to the same end, only exec() changes your program into another (say, "ls" for example) and then exits the process. In the parent's code, on the other hand, we call waitpid() on the child's pid. This means, don't proceed with execution until the child's done. This prevents the parent from finishing and orphaning the child process. If this would happen, on Linux at least, the child would get passed upwards, and its parent would become the parent of the first process. If that last part was confusing, don't worry about it. All you really need to know you should be able to get from the example code here.
I tried as you said but it is still not working. Below is the code and output.
I want to do doubble forking to make it independent of any terminal and
independently in background.
Can anybody help it out or give me some interesting links for creating
daemons in Unix using python.
__________________________________________________
Code:
import os,sys,signal
pid=os.fork()
if pid==0:
print "First child forked"
print "This is the child"
os.setsid()
signal.signal(signal.SIGHUP,signal.SIG_IGN)
pid=os.fork()
if (pid==0):
print "Second child forked"
#os.chdir("/")
#os.umask(0)
else:
print "null"
elif (pid!=-1):
print "This is the parent, and my child is:", pid
else:
print "Something went wrong"
_________________________________________________
Output
First child forked
This is the child
Second child forked
null
This is the parent, and my child is: 5568
Sorry, Im a bit confused here. In what way exactly is it not working? Looking at your code and your output it is definitely forking correctly (although I notice that in the second fork you are not testing for the error return code.
I suspect that you are getting confused by the fact that all the output is appearing on the terminal. When you fork the processes, the new process is exactly the same as the parent. The only difference is the return value from the fork() function. This means that the input and output filehandles are also the same as they were in the parent (so output will still go to the terminal). If you want the output to appear somewhere else such as a logfile then you have to use an alternative method such as appending to a file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.