Python child process in while True loop blocks parent

Barry Scott barry at barrys-emacs.org
Mon Dec 6 13:37:01 EST 2021



> On 6 Dec 2021, at 17:09, Jen Kris via Python-list <python-list at python.org> wrote:
> 
> I can't find any support for your comment that "Fork creates a new
> process and therefore also a new thread."  From the Linux man pages https://www.man7.org/linux/man-pages/man2/fork.2.html, "The child process is created with a single thread—the one that called fork()." 

You just quoted the evidence!

All new processes on unix (may all OS) only ever have one thread when they start.
The thread-id of the first thread is the same as the process-id and referred to as the main thread.

> 
> I have a one-core one-thread instance at Digital Ocean available running Ubuntu 18.04.  I can fork and create a new process on it, but it doesn't create a new thread because it doesn't have one available. 


By that logic it can only run one process...

It has one hardware core that support one hardware thread.
Linux can create as many software threads as it likes.

> You may also want to see "Forking vs Threading" (https://www.geekride.com/fork-forking-vs-threading-thread-linux-kernel), "Fork vs Thread" (https://medium.com/obscure-system/fork-vs-thread-38e09ec099e2), and "Linux process and thread" (https://zliu.org/post/linux-process-and-thread) ("This means that to create a normal process fork() is used that further calls clone() with appropriate arguments while to create a thread or LWP, a function from pthread library calls clone() with relvant flags. So, the main difference is generated by using different flags that can be passed to clone() funciton(to be exact, it is a system call"). 
> 
> You may be confused by the fact that threads are called light-weight processes. 

No Peter and I are not confused.

> 
> Or maybe I'm confused :)

Yes you are confused.

> 
> If you have other information, please let me know.  Thanks. 

Please get the book I recommended, or another that covers systems programming on unix, and have a read.

Barry

> 
> Jen
> 
> 
> Dec 5, 2021, 18:08 by hjp-python at hjp.at:
> 
>> On 2021-12-06 00:51:13 +0100, Jen Kris via Python-list wrote:
>> 
>>> The C program creates two threads (using pthreads), one for itself and
>>> one for the child process.  On creation, the second pthread is pointed
>>> to a C program that calls fork-execv to run the Python program.  That
>>> way Python runs on a separate thread. 
>>> 
>> 
>> I think you have the relationship between processes and threads
>> backwards. A process consists of one or more threads. Fork creates a new
>> process and therefore also a new thread.
>> 
>> hp
>> 
>> -- 
>> _  | Peter J. Holzer    | Story must make more sense than reality.
>> |_|_) |                    |
>> | |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
>> __/   | http://www.hjp.at/ |       challenge!"
>> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list