Skip to content

Commit 0b9aa58

Browse files
committed
update concurrency related topic
1 parent 9ad57be commit 0b9aa58

File tree

7 files changed

+170
-46
lines changed

7 files changed

+170
-46
lines changed

Diff for: docs/0index/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ A total of **19** topics, and a total of **373** notes (may not be accurate for
5252
- [Declarative & Functional Programming](computer-and-programming-fundamentals/declarative-functional-programming)
5353
- [Query Language](computer-and-programming-fundamentals/query-language)
5454
- [Logic Programming](computer-and-programming-fundamentals/logic-programming)
55+
- General Concepts
5556
- [Concurrency](computer-and-programming-fundamentals/concurrency)
5657
- Code Execution
5758
- [Compilation](computer-and-programming-fundamentals/compilation)

Diff for: docs/computer-and-programming-fundamentals/15-concurrency/concurrency.md

+68-31
Large diffs are not rendered by default.

Diff for: docs/computer-and-programming-fundamentals/computer-and-programming-fundamentals-intro.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Although the topic seems introductory, it actually assumes certain programming k
3333
- [Declarative & Functional Programming](computer-and-programming-fundamentals/declarative-functional-programming)
3434
- [Query Language](computer-and-programming-fundamentals/query-language)
3535
- [Logic Programming](computer-and-programming-fundamentals/logic-programming)
36+
- General Concepts
3637
- [Concurrency](computer-and-programming-fundamentals/concurrency)
3738
- Code Execution
3839
- [Compilation](computer-and-programming-fundamentals/compilation)

Diff for: docs/operating-system/04-multithreading/multithreading.md

+87-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ description: Multithreading
1212
- **[Multithreading (computer architecture) - Wikipedia](<https://en.wikipedia.org/wiki/Multithreading_(computer_architecture)>)**
1313
- **[Thread (computing) - Wikipedia](<https://en.wikipedia.org/wiki/Thread_(computing)>)**
1414

15-
**Multithreading** is a concept that enable us to use multiple thread to execute tasks.
15+
**Multithreading** is a concept that enable us to use multiple thread to execute tasks. See [concurrency](/computer-and-programming-fundamentals/concurrency) for an introduction to concurrency.
1616

1717
### Thread
1818

19-
Thread is a unit of execution in CPU, it can execute a set of instruction, basically it is a "worker" in a CPU. Thread exist within a [process](/operating-system/process-management#process--thread) and has its own data including thread ID, program counter, a register set, and a stack.
19+
Thread is a unit of execution in CPU, it can execute a set of instruction. Basically, it is a "worker" in a CPU. Thread exist within a [process](/operating-system/process-management#process--thread) and has its own data including thread ID, program counter, a register set, and a stack.
2020

21-
In multithreading, instead of just one thread or one worker in a process, multiple thread is utilized. The benefit is we are not limited to complete a single task at a time, for example, a mobile app can fetch data from remote server while also loading data from local storage. If possible, we can also divide a computationally intensive task into smaller, parallelizable subtasks, and use multiple threads to speeds up the overall execution time.
21+
In multithreading, instead of just one thread or one worker in a process, multiple thread is utilized. The benefit is we are not limited to complete a single task at a time. For example, a mobile app can fetch data from remote server while also loading data from local storage. If possible, we can also divide a computationally intensive task into smaller, parallelizable subtasks, and use multiple threads to speeds up the overall execution time (achieving parallelism).
2222

2323
A thread can also be **blocked**, which means the thread is unable to make progress or continue its execution because it is waiting for a certain event such as I/O results or condition to occur. Blocked thread can be inefficient, as it is unable to perform any useful work.
2424

@@ -27,15 +27,15 @@ Source : https://towardsdatascience.com/multithreading-and-multiprocessing-in-10
2727

2828
The image above shows the illustration of multithreading. Each thread holds different data, but they share the same memory space and resources of the parent process. In contrast, multiprocessing is when we utilize a processor that has several cores. Each core would have their own data and thread that will execute simultaneously.
2929

30-
Utilizing multiple threads is typically more efficient than making multiple process that execute the same tasks. Threads have a smaller memory footprint, require less time for [context switching](/operating-system/process-management#context-switch), and have lower scheduling overhead. Also, separate process means [IPC](/operating-system/inter-process-communication) is required to communicate between processes, whereas thread shares the same memory within a process, thus communication will be easier.
30+
Utilizing multiple threads is typically more efficient than making multiple process that execute the same tasks ([multiprocessing](/computer-and-programming-fundamentals/concurrency#multiprocessing)). Threads have a smaller memory footprint, require less time for [context switching](/operating-system/process-management#context-switch), and have lower scheduling overhead. Also, separate process means [IPC](/operating-system/inter-process-communication) is required to communicate between processes, whereas thread shares the same memory within a process, thus communication will be easier.
3131

3232
### Multithreading Model
3333

3434
#### User & Kernel Thread
3535

3636
There are two types of thread, **user thread** and **kernel thread**.
3737

38-
User threads, also known as **green threads**, are implemented and managed by a thread library or runtime system at the user level, without direct involvement of the operating system kernel. The creation, scheduling, and synchronization of user threads are handled entirely in the user space (memory space where user applications run).
38+
User threads, also known as **green threads**, are implemented and managed by a thread library or runtime system at the user level, without direct involvement of the operating system kernel. The creation, scheduling, utilization, and synchronization of user threads are handled entirely in the user space (memory space where user applications run). They may not be able to access the memory used by kernel.
3939

4040
On the other hand, kernel threads, which is also known as **native threads**, are managed directly by the operating system kernel. Each kernel thread is represented as a separate entity within the operating system and has its own program counter, stack, and thread control block.
4141

@@ -45,7 +45,9 @@ Kernel threads are heavyweight, they require system calls and interaction with t
4545

4646
#### Relationship Model
4747

48-
- **Many-to-One** : This model involves mapping multiple user-level threads to a single kernel-level thread. The thread management and scheduling are performed by a thread library or runtime system at the user level, and the operating system sees only a single thread. This model has efficient management, but may not take full advantage of multiprocessor systems as the execution of multiple threads is handled by a single kernel-level thread.
48+
Relationship model between user and kernel thread describe how they are associated with each other.
49+
50+
- **Many-to-One** : This model involves mapping multiple user-level threads to a single kernel-level thread. The thread management and scheduling are performed by a thread library or runtime system at the user level. From the programming language, we can see and use multiple thread, but under the hood the operating system sees only a single thread. This model has efficient management, but may not take full advantage of multiprocessor systems as the execution of multiple threads is handled by a single kernel-level thread.
4951
- **One-to-One** : In this model, each user-level thread is mapped to a separate kernel-level thread by the operating system. This model provides more concurrency and true parallelism to the kernel-level. However, the overhead of creating and managing kernel-level threads can be higher compared to other models.
5052
- **Many-to-Many** : This model combines the aspect of many-to-one and one-to-one. Many-to-many model consist of many kernel threads and smaller or equal number of user thread. The operating system can create multiple kernel-level threads, while the thread library manages and schedules the user-level threads across the available kernel-level threads.
5153

@@ -58,7 +60,7 @@ Multithreading implementation depends on the programming language used. Threadin
5860

5961
#### Thread Creation & Termination
6062

61-
The thread library provided by programming languages have specific function or method to create and manage threads. For example, in Java, we can create a thread by extending the `Thread` class or implementing the `Runnable` interface and then invoking the `start()` method. In C++, you can use the `std::thread` class or the threading utilities provided by libraries like POSIX threads (`pthread_create()` function).
63+
The thread library provided by programming languages have specific function or method to create and manage threads. For example, in Java, we can create a thread by implementing the `Thread` class or the `Runnable` interface and then invoking the `start()` method. In C++, you can use the `std::thread` class or the threading utilities provided by libraries like POSIX threads (`pthread_create()` function).
6264

6365
When creating thread, we can specify thread attributes such as stack size, thread priority, CPU affinity. After a thread is created, it is assigned a unique identifier called the thread ID.
6466

@@ -69,6 +71,78 @@ Thread can be stopped explicitly using function like `stop()` in Java. Sometimes
6971

7072
It is important to note that thread termination should be handled carefully. For example, a thread may have used some data structure, but when it is not freed before the termination, this can cause [memory leak](/computer-security/other-attack-and-exploit#memory-leak).
7173

74+
##### Example
75+
76+
One reason to implement the `Thread` class is to encapsulate application logic that is intended to be run in separate thread. In a server app, it is expected that the server handle multiple request in parallel. Let's say server is trying to read and write an input.
77+
78+
```javascript
79+
public class ClientHandlerThread extends Thread {
80+
private Socket clientSocket;
81+
82+
public ClientHandlerThread(Socket clientSocket) {
83+
this.clientSocket = clientSocket;
84+
}
85+
86+
public void run() {
87+
try {
88+
// Get the input stream from the client
89+
InputStream inputStream = clientSocket.getInputStream();
90+
91+
// Read the client's request
92+
byte[] buffer = new byte[1024];
93+
int bytesRead = inputStream.read(buffer);
94+
95+
// Process the request (example: convert to uppercase)
96+
String request = new String(buffer, 0, bytesRead);
97+
String response = request.toUpperCase();
98+
99+
// Get the output stream to send the response to the client
100+
OutputStream outputStream = clientSocket.getOutputStream();
101+
102+
// Send the response back to the client
103+
outputStream.write(response.getBytes());
104+
} catch (IOException e) {
105+
e.printStackTrace();
106+
} finally {
107+
// Close the client socket
108+
try {
109+
clientSocket.close();
110+
} catch (IOException e) {
111+
e.printStackTrace();
112+
}
113+
}
114+
}
115+
}
116+
```
117+
118+
This implementation of thread encapsulate the logic of reading, processing, and responding back to user request. We can use it like below.
119+
120+
```javascript
121+
public class Server {
122+
public static void main(String[] args) {
123+
try {
124+
// Create a server socket
125+
ServerSocket serverSocket = new ServerSocket(8080);
126+
System.out.println("Server listening on port 8080");
127+
128+
while (true) {
129+
// Accept client connections
130+
Socket clientSocket = serverSocket.accept();
131+
System.out.println("Client connected: " + clientSocket.getInetAddress());
132+
133+
// Create a new thread to handle the client connection
134+
ClientHandlerThread clientHandlerThread = new ClientHandlerThread(clientSocket);
135+
clientHandlerThread.start();
136+
}
137+
} catch (IOException e) {
138+
e.printStackTrace();
139+
}
140+
}
141+
}
142+
```
143+
144+
For each client connection, we create a new thread through the `ClientHandlerThread` and start it to handle the connection.
145+
72146
#### Thread Execution & Scheduling
73147

74148
In Java, we can start the execution of a thread, by calling the `start()` method on the `Thread` object. The `start()` method internally calls the thread's `run()` method, which contains the code that will be executed by the thread. The JVM manages the execution of threads and ensures that the `run()` method is executed concurrently with other threads.
@@ -82,7 +156,11 @@ Source : https://medium.com/spring-boot/multithreading-in-java-with-examples-25b
82156

83157
#### Thread Communication
84158

85-
Multiple threads exist within the same process, threads communicate using the [IPC mechanism](/operating-system/inter-process-communication). There are two method, the first method is **[shared memory](/operating-system/inter-process-communication#shared-memory)**, where each thread read and write data in the same region of memory. The other method is **[message passing](/operating-system/inter-process-communication#message-passing)**, where they send messages or signals to each other. One thread can send a message to another thread, which then receives and processes the message.
159+
Multiple threads exist within the same process, external threads can communicate using the [IPC mechanism](/operating-system/inter-process-communication). There are two method, the first method is [shared memory](/operating-system/inter-process-communication#shared-memory), where each thread read and write data in the same region of memory. The other method is [message passing](/operating-system/inter-process-communication#message-passing), where they send messages or signals to each other. One thread can send a message to another thread, which then receives and processes the message.
160+
161+
:::note
162+
Although they are for process communication, it may be used to communicate between thread if they are running in separate processes. If threads were to communicate between the same process, typically they would directly access and communicate with each other through shared variables or data structures.
163+
:::
86164

87165
#### Thread Synchronization
88166

@@ -96,7 +174,7 @@ These are fundamental tools used in multithreaded programming to synchronize.
96174

97175
###### Locks / Mutex
98176

99-
**Mutex (mutual exclusion)** is a synchronization primitive that ensure only one thread to access a shared resource. It works by having a lock, a thread that wants to access the resource must acquire the lock first. If the lock is already held by another thread, the requesting thread will be blocked until the lock is released. When the thread that access the resource has finished, then the lock will be released.
177+
**Mutex (mutual exclusion)** is a synchronization primitive that ensure only one thread to access a shared resource. It works by having a lock, a thread that wants to access the resource must _acquire_ the lock first. If the lock is already held by another thread, the requesting thread will be blocked until the lock is _released_. If the thread that access the resource has finished, only then the lock will be released.
100178

101179
The mutex technique can be implemented in the software-level by memory synchronization instructions provided by the hardware architecture.
102180

Diff for: docs/operating-system/08-inter-process-communication/inter-process-communication.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ For example, when a process want to access some data in the address "0x05", the
3636
![Virtual memory](./virtual-memory.png)
3737
Source : https://en.wikipedia.org/wiki/Virtual_memory
3838

39-
The same concept applies for shared memory, where there are several processes each with their own isolated virtual memory. When a process want to share memory, the OS will allocate some shared memory somewhere. After that, the process will attach to it, meaning the process will be given some range of address on their virtual address that it can use to access the shared data. Essentially, the OS maps the shared memory to the virtual memory owned by the process.
39+
The same concept applies to shared memory, where multiple processes each have their own isolated virtual memory. When a process wants to share memory, the operating system allocates some shared memory. Then, the process attaches the shared memory to each process. This means that every process is given a range of addresses on their virtual address space that they can use to access the shared data.
4040

4141
![Shared memory](./shared-memory.png)
4242
Source : https://www.linkedin.com/pulse/interprocess-communicationipc-using-shared-memory-pratik-parvati
@@ -53,16 +53,17 @@ Message passing can be synchronous or asynchronous. In the synchronous model, th
5353

5454
Some example of message passing :
5555

56-
- **Pipes** : Pipes are a form of [inter-process communication (IPC)](/operating-system/inter-process-communication) that allows the output of one process to be used as the input of another process. In a pipe, data flows in a unidirectional manner from the writer process to the reader process. Pipes can be either named or unnamed, with unnamed pipes typically used for communication between related processes (e.g., parent-child processes).
56+
- **Pipes** : Pipes are a form of IPC that allows the output of one process to be used as the input of another process. In a pipe, data flows in a unidirectional manner from the writer process to the reader process. Pipes can be either named or unnamed, with the latter typically used for communication between related processes (e.g., parent-child processes).
5757

58-
- **Sockets** : Sockets are a communication endpoint that enables bidirectional communication between processes over a network. They can be used for IPC within the same machine (domain sockets) or across different machines (network sockets).
58+
![Pipe](./pipe.png)
59+
Source : https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/Pipes.html
5960

61+
- **Sockets** : Sockets are a communication endpoint that enables bidirectional communication between processes over a network. They can be used for IPC within the same machine (domain sockets) or across different machines (network sockets).
6062
- **[Message Queues](/backend-development/message-broker)** : Message queues is where processes exchange messages through a shared [queue](/data-structures-and-algorithms/queue) in the operating system. Each message has a specific format and is placed into the queue by the sending process. The receiving process can then retrieve messages from the queue in a first-in-first-out (FIFO) order.
61-
6263
- **Channels** : Channels is a higher-level concept for message passing. Channels typically provide a set of operations, such as sending and receiving messages, and may incorporate synchronization mechanisms like blocking or non-blocking operations. Channels can be implemented using various underlying mechanisms, including shared memory, pipes, or sockets.
6364

64-
![Message passing](./message-passing.png)
65-
Source : https://beingintelligent.com/difference-between-shared-memory-and-message-passing-process-communication.html
65+
![Message passing](./message-passing.png)
66+
Source : https://beingintelligent.com/difference-between-shared-memory-and-message-passing-process-communication.html
6667

6768
### RPC
6869

49.8 KB
Loading

Diff for: sidebars.js

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ const sidebars = {
109109
"computer-and-programming-fundamentals/logic-programming/logic-programming",
110110
],
111111
},
112+
],
113+
},
114+
{
115+
type: "category",
116+
label: "General Concepts",
117+
items: [
112118
"computer-and-programming-fundamentals/concurrency/concurrency",
113119
],
114120
},

0 commit comments

Comments
 (0)