LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2005, 10:05 AM   #1
thinkgeek
LQ Newbie
 
Registered: Jun 2005
Posts: 15

Rep: Reputation: 0
Forking process using python


I have written a script which wil fork a process and the main process gets free form th terminal
.I have to make the child process run in background.

I am using the os.fork() method to fork a process but it is returning a non zero pid.Why?

And how to solve the process.
 
Old 07-02-2005, 10:26 AM   #2
sm1else
Member
 
Registered: Oct 2004
Distribution: At home: Ubuntu, Debian Sarge
Posts: 56

Rep: Reputation: 15
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
 
Old 07-02-2005, 10:35 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
os.fork() Return Values

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.
 
Old 07-02-2005, 12:42 PM   #4
thinkgeek
LQ Newbie
 
Registered: Jun 2005
Posts: 15

Original Poster
Rep: Reputation: 0
Post I tried it but still........

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
 
Old 07-03-2005, 11:36 AM   #5
sm1else
Member
 
Registered: Oct 2004
Distribution: At home: Ubuntu, Debian Sarge
Posts: 56

Rep: Reputation: 15
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.

Google turned up this link that should tell you what you want.
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

By the way, according to http://unix.derkeiler.com/Newsgroups...5-01/0393.html, double forking is only needed if the parent doesnt exit immediately but instead does something else.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie on forking child process and I/O redirection neo_in_matrix Programming 4 09-16-2005 03:05 AM
piping /forking geminigal Programming 3 04-10-2005 09:49 PM
User "list" running process "python" TroelsSmit Linux - Newbie 2 02-22-2005 04:55 AM
Forking is very slow jspenguin Linux - General 2 05-29-2004 02:03 PM
Question on forking a child process brianvdc Programming 2 10-16-2003 04:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration