Forking is based on the notion of copying programs: when a program calls the fork routine, the
operating system makes a new copy of that program and its process in memory andstarts running that copy in parallel with the original. Some systems don’t really copythe original program (it’s an expensive operation), but the new copy works as if it werea literal copy.
all forked processes run independently and in parallel under the operating system’s control, and
children may continue to run after their parent exits.
os.fork built-in function. Because this function
generates a copy of the calling program, it returns a different value in each copy: zeroin the child process and the process ID of the new child in the parentimport osdef child(): print("hello from child", os.getpid()) os.exit(0)def parent(): while True: newpid=os.fork() if newpid==0: child() else: print("hello from parent",os.getpid(), newid) if input()=='q': breakparent()
*fork cannot work well in typical win; but can work under cygwin