How to realize multithreading cp replication by LINUX
This article mainly shows you "how LINUX realizes multi-thread cp replication", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how LINUX realizes multi-thread cp replication".
The implications of this question are limited because generally when copying files, the actual bottleneck comes from I/O, and no matter how many threads are opened, it's not actually much faster, but to practice multithreaded programming,
Here is a C++ code implementation, code attached at the end.
In fact, it is to divide a file into multiple fragments and open multiple threads for simultaneous replication. If the parallelism specified by the user is greater than the actual CPU core number of the server, the program will automatically downgrade the parallelism to CPU core number. If the file is less than
100M is always parallel to 1.
root@bogon:/home/gaopeng/mmm# ./ parcp log.log log10.log 2
set parallel:2
Your cpu core is:4
real parallel:2
Will Create 2 Threads
140677902710528:0:174522367:3:4
140677894317824:174522368:349044736:3:4
Copy Thread:140677902710528 work 25%
Copy Thread:140677894317824 work 25%
Copy Thread:140677902710528 work 50%
Copy Thread:140677902710528 work 75%
Copy Thread:140677902710528 work 100%
Copy Thread:140677902710528 work Ok!!
Copy Thread:140677894317824 work 50%
Copy Thread:140677894317824 work 75%
Copy Thread:140677894317824 work Ok!!
md5 validation after replication is complete
root@bogon:/home/gaopeng/mmm# md5sum log.log
f64acc21f7187a865938b340b3eda198 log.log
root@bogon:/home/gaopeng/mmm# md5sum log10.log
f64acc21f7187a865938b340b3eda198 log10.log
It can be seen that the verification is passed
The code is as follows:
Click here to fold or open
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_BUFFER 65536
using namespace std;
pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
class thread_info
{
private:
uint64_t start_pos;
uint64_t end_pos;
int fdr;
int fdw;
public:
pthread_t t_id;
static int do_id;
public:
thread_info()
{
start_pos = 0;
end_pos = 0;
fdr = 0;
fdw = 0;
t_id = 0;
}
void set_start(uint64_t a)
{
start_pos = a;
}
void set_end(uint64_t a)
{
end_pos = a;
}
void set_fd(int a,int b)
{
fdr = a;
fdw = b;
}
uint64_t get_start(void)
{
return start_pos;
}
uint64_t get_stop(void)
{
return end_pos;
}
int get_fdr(void)
{
return fdr;
}
int get_fdw(void)
{
return fdw;
}
void print(void)
{
cout