thread and process
- smallest sequence of programmed instructions that can be managed independently by a scheduler.
- Has its own registers, PC. SP.
- Instance of a computer program that is being executed
- A process can have one or more threads
- Run programs on one or more CPUs
- Multi-threading (shared memory)
- Multi-processing (Independent-memory)
Shared memory, so variable can be accessed by both threads.
However, when multithreads write (non-atomic instruction) a shared variable, inconsistency could happen.
CPU may process instructions randomly.
a += 1;//non-atomic
Note: only use lock when going to the critical section, becuase multithreading is much faster.
- shared variable
- semaphore, mutex, lock
The pid returned by the fork() will not be 0 (0 as child process)
pid is the child process, should be 2+2;
pid is the parent process, should be 2+1;
- shared memory