process and thread
A program = MS Word, Power Point
A state that program runs = Process
For example, when we open Chrome, we have 1 window. It is Process.
Also, When we print the Word file, printing is Process.
Program is stored in memory. Once OS executes Program, Process is working on memory.
CPU gives Drive Resource to Process. Process only work when they have Resource.
The system resource is CPU time, memory storage, and address space.
When program starts, memory makes address sapce for process.
In address space, there are 3 segments.
- code segment : code storage (read only)
- data segment : data (global variables) storage (read & write)
- stack segment : functions & local variables (read & write)
Local variables should separate from Global variables.
Even though they have same name, they can access to the different level.
When they are in the same memory, they can mess them up.
Each process has at least one thread.
Each process has independent address space, they do not share variable and data type.
To communicate each other, they use IPC (inter-process communication).
The communication between process is called “context switching”.
At t0 time, Process 1 is handling the task.
At t1 time, Process 1 hands over their memory to Process 2.
Process 2 remove their memory and download Process 1 memory.
Thread
A Process consists of several Threads.
they share the resources (heap, code, data) and address space.
Each threads have their own register and stack, but they can read and write heap memory.
- Stack : A state that functions run
- Register : the logs of command’s activation, they store last task for context switching.
Java thread
Java thread is quite similar to general threads, JVM(java virtual machine) is OS in java.
Java has no process, only threads. Java threads are code blocks “scheduled” by JVM.
Also, JVM manages
- How many threads exist
- The thread’s location in memory
- Thread statement
- Priority of threads
So, developers write thread codes by using java thread, and request to activates codes to JVM.
Multi-processing
One program consists of many processes.
One issue from one process doesn’t affect other processes.
However, during context switching, it creates heavy works (such as cache menory reset).
Because process get assigned each independent memory space, they don’t have share memory.
So, they should reset every data to share other’s cache data by context switching.
Multi-threading
One program consists of many threads.
Threads do not need many time for sharing, so, context switching is fast, because it shares
every memory except for stack space.
However, it needs very sophisticated design, and debug is difficult.
One issue from one thread affects other threads.
Multi-thread does not need many processes for tasks.
By doing that, we can save time and resource for context switching.