Threads & Processes
There are kernel threads and user threads
Basically user space thread vs kernel space thread.
Green Threads are the same as User space threads. Kernel Thread is used interchangeably with OS thread.
Os threads are hard to use as the programmer or the code is responsible for preventing data related problems through locks and all.
Green threads are opposite to os threads and are managed by the language (the users of the kernel)
These threads only exist only in language and not in the os. Green threads are much simpler for the programmer, but their performance varies: many languages can’t take advantage of multiple cores.
Difference between process and threads.
great link: https://stackoverflow.com/questions/200469/what-is-the-difference-between-a-process-and-a-thread?rq=1
Threads and processes are independent sequences of execution.
Threads work in same memory space while processes have separate memory spaces.
A process provides resources needed for execution of program. Each process starts with a single thread but can create more threads. Each process has:
- a unique process identifier (pid)
- virtual address space
- executable code
- open handles to system objects
- environment variables
- security context
- minimum and max set sizes
- priority class
- at least one thread
A thread is an entity within a process that can be scheduled for execution. All threads within a process share its virtual address and system resources. Each thread maintains its:
- exception handling
- thread local storage
- a set of structures system will use to protect the thread context until execution
- registers
- stack
Threads share code, data and files but have their own registers and stack
Three ways threads can be implemented:
- userspace thread
- kernel thread
- combination of two
What is needed to implement thread:
The ability to maintain cpu state and ability to maintain multiple stacks.
This can be implemented in userspace as well as kernel
User space thread switching is faster as it wont be trapped in kernel and you can schedule your thread the way you like.
The problem with user space thread is inability to perform blocking io call. why? i think it means you can do it but you dont want to do it because it halts all processes.
Kernel thread have the advantage of doing blocking io and let the os/kernel handle all scheduling related activity
Another approach: using multiple kernel threads each handling multiple user threads.
Blocking vs Non Blocking IO call:
blocking io: thread cannot do anything until io response is returned
non-blocking io: the io is queued straight away and function returns/ thread continues execution
Can a single process run on multiple cores?: find answer in os & hardware architecture
Yes. And that’s for the kernel to take care.
cooperative multi-tasking, coroutines (https://sprksh.github.io/notes/2020/04/18/multiprocessing-multithreading-cooperative-multi-tasking-coroutines-gevent.html)
Best article on GIL: https://opensource.com/article/17/4/grok-gil
But what is runtime and what would python runtime be?