FileZillaFTP Server Source Code Analysis: FileZilla is a free and open-source FTP tool. Including FileZilla Client and FileZilla Server. FileZilla Server only provides windows versions. to upload a local website webpage file to the website Server or download a webpage file from the Server, you only need the FileZilla Client Version. FileZilla FTP Server Source Code Analysis
Win32 Synchronization Control Mechanism (Synchronization)
1. Critical Sections (Key domain)
The simplest synchronization mechanism is to create and destroy functions:
- InitializeCriticalSection()
-
- DeleteCriticalSection()
-
After being created, use the following function to implement thread synchronization,
- EnterCritSection()
-
... Code to be synchronized
- LeaveCritSection()
-
That is, the code between EnterCritSection and LeaveCritSection can be processed by only one thread at a time. Note:
The variable of the Critical Sections type is not a core object, that is, there is no handle;
It exists in the memory space of the process, that is, it cannot be used across processes;
It may lead to deadlocks;
2. Mutexes (mutex)
The creation and destruction functions are as follows:
- CreateMutex()
-
- CloseHandle()
-
If Mutex has been created, enable and disable it as follows:
- OpenMutex()
-
- ReleaseMutex()
-
When used, use the wait function to wait for Mutex. Once no thread has this Mutex, this thread will obtain the Mutex. After this thread completes processing, it will call ReleaseMutex () the Mutex can be released, and other threads in the waiting state will compete for the Mutex again. At the same time, only one thread can obtain the Mutex, and other threads that do not compete with the Mutex will be in the blocking state. Common wait functions include:
WaitForSingleObject () // wait for a mutex
WaitForMultipleObjects () // wait for multiple mutex at the same time, or have multiple, or none
Compared with Critical Sections, mutex is a core object, so it is cross-process. That is, multiple processes can use the same mutex, And the CreateMutex () overhead is higher than InitializeCriticalSection () it is much larger.
Compared with mutex, mutex is more heavyweight, slower, but more flexible.
3. Semaphores)
The creation and destruction functions are as follows:
CreateSemaphore ()
CloseHandle ()
The functions used to obtain this Semaphore are also the wait functions WaitForSingleObject (), WaitForMultipleObjects (), and so on.
Using Semaphores means that Semaphores can be owned by multiple threads at the same time, but when CreateSemaphore () is used, it specifies the maximum number of threads that have the Semaphore at the same time, that is, when each thread calls the wait function to obtain Semaphore, the available value in Semaphore is reduced by 1. Once the available value is 0, the thread must wait. After the thread running on Semaphore is finished, call ReleaseSemaphore () to release it.
Unlike Mutexes, the thread that calls ReleaseSemaphore () must be the one that calls wait and obtains the ownership. That is, any thread can call ReleaseSemaphore () at any time () to remove the Semaphore locked by any thread.
In a sense, Mutexes can be considered as a special case of Semaphore, that is, Semaphore that can only be locked by one thread at the same time.
Semaphore is also the core object.
4. Event Objects)
Events is the most flexible in win32, and it is also a core object.
Events indicates that there are two statuses of events: Activation and non-activation. When events are activated, the waiting threads will be awakened.
The creation and destruction functions are as follows:
CreateEvent (), you can specify that the events are manual or automatic during creation. The meaning of manual is that the events state is set by the programmer. The meaning of automation is that after events is converted into an exciting call, it will automatically become inactive immediately.
CloseHandle ()
The functions used to obtain this events are the wait functions WaitForSingleObject (), WaitForMultipleObjects (), and so on.
The following three methods can change the events status:
SetEvent (): Set events to active
ResetEvent (): Set events to non-active
PulseEvent (): Activate events, and then immediately turn high to non-active. If events are manual, all the waiting threads will be awakened. If events are automatic, the "one" Waiting thread will be awakened.
Note: If the event is manual, after SetEvent is called, if ResetEvent is not called, the waiting thread will be continuously awakened, that is, the lpStartAddress method specified when CreateThread is continuously executed.
In addition, the windows system can ensure that the wake-up thread is one after another, that is, some threads are not always awakened, and some threads are starved to death.
- FileZilla FTP Server Source Code Analysis 1)
- FileZilla FTP Server Source Code Analysis 2)
- FileZilla FTP Server Source code analysis 4)
- FileZilla FTP Server Source Code Analysis 5)