Skip to content

Commit 5d1b63d

Browse files
committed
Mar 6th
1 parent 16b8ca7 commit 5d1b63d

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

System/Processes/child.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os, sys
2+
3+
print('Hello from child', os.getpid(), sys.argv[1])

System/Processes/fork-count.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os, time
2+
def counter(count):
3+
for i in range(count):
4+
time.sleep(1)
5+
print('[%s] => %s' % (os.getpid(), i))
6+
os._exit(0)
7+
8+
for i in range(5):
9+
pid = os.fork()
10+
if pid != 0:
11+
print('Process %d spawned' % pid)
12+
else:
13+
counter(i)
14+
#os._exit(0)
15+
16+
print('Main process exiting.')

System/Processes/fork-exec.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
3+
parm = 0
4+
while True:
5+
parm += 1
6+
pid = os.fork()
7+
if pid == 0:
8+
os.execlp('python', 'python', 'child.py', str(parm))
9+
assert False, 'error starting program'
10+
else:
11+
print('Child is', pid)
12+
if input() == 'q': break

System/Processes/fork1.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
import os
4+
5+
def child():
6+
print('Hello from child', os.getpid())
7+
os._exit(0)
8+
9+
def parent():
10+
while True:
11+
newpid = os.fork()
12+
if newpid == 0:
13+
child()
14+
else:
15+
print('Hello from parent', os.getpid(), newpid)
16+
if input() == 'q': break
17+
18+
parent()

0 commit comments

Comments
 (0)