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

Is An Operating System an Process ?
Virtual Memory,Page size,Maximum Virtual Address
Whats the relationship between file system block size and disk space wasted per file
Page fault when trying to access VESA LFB with paging enabled
ROS Operating system services ? How?
Where do driver developers get hardware specifications?
What is the difference between standalone and desktop application?
Memory mapped IO - who maps the addresses to the physical address space?
Round Robin Scheduling with arrival time and priority level
What's the best language to start write OS
How exactly does a program talk to a device driver?
What is a multi-rate non preemptive OS?
Finding TLB size with a program?
Calculating Size of a Page Table
What is the exact definition of 'process preemption'?
Why can't compiled machine code (EXE, PE, APP) work on all platforms?

Categories

HOME
android-espresso
class
freeradius
jms
paypal-ipn
braintree
ant
ios-simulator
nsis
drag-and-drop
ag-grid
tweepy
mutation-testing
phpstorm-2017.1
xbap
smartgwt
mailgun
squid
max-msp-jitter
heap-dump
pywinauto
boomi
nullreferenceexception
scheduled-tasks
raml
android-custom-view
web-analytics
maven-surefire-plugin
nurbs
managed-c++
ruby-daemons
pyspark-sql
sparkle
javax.persistence
docx4j
nest
custom-keyboard
markov-chains
apptentive
orchardcms-1.8
des
keyboard-layout
djcelery
macports
right-click
grails3.2.0
classnotfoundexception
distributed-caching
dagger
elasticsearch-aggregation
linq-to-excel
liteide
rightnow-crm
.net-micro-framework
libreoffice-writer
karabiner
popen
nivo-slider
test-data
case-when
business-rules
prefetch
multi-touch
e
msbuild-4.0
openseadragon
android-4.2-jelly-bean
eclemma
frame-grab
mercurial-extension
openmrs
vim-plugin
enunciate
google-maps-api-2
openshift-cartridge
ambiguity
vdm-sl
mobilefirst-server
phpredis
ocmockito
python-curses
microsoft-expression-web
angular-ui-select
full-text-indexing
accessory
prism.js
fileshare
usb-flash-drive
armv6
facebook-game-groups
frontbase
yii-booster
chartfx
examine
cyrillic
pageheap
java.lang.class
acpi
type-safety
navigationcontroller
fbml
snackjs
active-record-query
bass
raw-data
iphone-sdk-4.3
aspbutton
source-code-protection
eai

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App