operating-system


How thread is preserved in os fork?


I want to check whether thread will be preserved during os.fork.
This is what the thread is started before os.fork.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import multiprocessing
from threading import Timer
import time
i = [0]
def work():
while True:
time.sleep(1)
i[0] += 1
print i[0]
print multiprocessing.current_process()
if __name__ == '__main__':
task = Timer(1, work)
task.daemon = True
task.start()
time.sleep(2)
print 'pre fork'
if os.fork():
# parent process
else:
# child process
multiprocessing.current_process().name = "Secondary"
time.sleep(2)
print 'post fork'
time.sleep(1000)
The output of this program:
pre fork
1
<_MainProcess(MainProcess, started)>
2
<_MainProcess(MainProcess, started)>
post fork
post fork
3
<_MainProcess(MainProcess, started)>
4
<_MainProcess(MainProcess, started)>
5
<_MainProcess(MainProcess, started)>
6
<_MainProcess(MainProcess, started)>
7
<_MainProcess(MainProcess, started)>
==========================================================================
This is what the thread is started after os.fork.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import multiprocessing
from threading import Timer
import time
i = [0]
def work():
while True:
time.sleep(1)
i[0] += 1
print i[0]
print multiprocessing.current_process()
if __name__ == '__main__':
task = Timer(1, work)
task.daemon = True
time.sleep(2)
print 'pre fork'
if os.fork():
# parent process
task.start()
else:
# sub process
multiprocessing.current_process().name = "Secondary"
task.start()
time.sleep(2)
print 'post fork'
time.sleep(1000)
The output is like this:
pre fork
post fork
post fork
1
1
<_MainProcess(Secondary, started)>
<_MainProcess(MainProcess, started)>
2
2
<_MainProcess(MainProcess, started)>
<_MainProcess(Secondary, started)>
Summary
It seems that when we start the thread after os.fork then every thing will be ok and both the parent and child process will run the thread.
When we start the thread before os.fork, then according to the output, it seems that the thread is not come up with os.fork, i.e., it is not executed in the child process.
The question is how should this happen and what is the reason for it.
Suddenly, i come up with an explanation and the answer is No, thread will no be preserved during os.fork.
os.fork only do a copy-on-write.
parent and child process both have the same code.
However, if we start a thread before os.fork, then there no start logic to child process.
So, if we want a thread exist in child process then we can only start it after we have os.fork it.
:)
Note: This has some similar with gunicorn --preload argument.

Related Links

OS Booting Issue
Apply Password to Folder in Directory D:\myFolder
BSD for the NXT
How locks are implemented on multiple cores
boot loader's size
A question about Device drivers & Kernel
What are the factors by which virtual memory is limited?
Hardware supported OS functions
Multicores and mulithreads
default sound for javascript alert() box
What is an Operating System (with respect to embedded systems)? [closed]
Why isn't every OS real-time?
Does disabling interrupt automatically disable CPU scheduling?
Which OS will be the best subsitute for the Microsoft Windows XP/7 [closed]
about hardware drivers in protected mode
Time-Stamp Counter Restriction

Categories

HOME
webview
user-interface
subdomain
command-prompt
stacktrace.js
code-formatting
whmcs
celery
portable-class-library
nsis
amp
ontology
facebook-android-sdk
angular-meteor
cocotb
stack-overflow
match
fullcalendar-scheduler
feature-extraction
dhcp
corona
asp.net-core-1.0
pygobject
google-sites
adminlte
android-custom-view
bsd
pst
conda
uislider
websphere-portal
ava
background-color
sap-lumira
django-import-export
ibm-connections
openbr
audit
monitor
nest
spinner
cordys-opentext
android-alarms
oauth2
xcglogger
nothing
karabiner
gestures
visual-format-language
aurelia-fetch-client
gnome-terminal
stateless-session-bean
predicates
mechanicalturk
business-rules
hmmlearn
openerp-6
bpms
deviare
java-2d
openquery
seccomp
xcode8-beta4
qpixmap
maximize
apple-configurator
jeditable
verisign
odesk
boost-proto
wlanapi
postal-code
ngcordova
roxygen2
artisan
quercus
azure-xplat-cli
scala-swing
utf8-decode
oracle-adf-mobile
incognito-mode
sqlbindparameter
air-native-extension
principalcontext
apportable
rikulo
delphi-xe4
aero
donut-chart
xmlwriter
cyrillic
ckeditor.net
ip-geolocation
system-information
mkannotation
expression-web
filelock
representation
sql-parametrized-query
digiflow
outlook-2007-addin
facebook-fbml
stretchblt

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App