LINUX fork写时复制

先分配1G的内存空间,然后fork,睡眠5秒后父进程修改内存的数据,统计一下时间。

#include <iostream>
#include <unistd.h>
#include <chrono>
#include <iomanip>

//父进程工作函数
void parentProcessRun(void* data) {
    while (true) {
        sleep(5);
        auto start = std::chrono::steady_clock::now();
        for (int i = 0; i < 1024 * 1024 * 1024; ++i) {
            int* item = (int*)data;
            item[i] = 333323;
        }
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;
        std::cout<<"parent is "<<diff.count()<<std::endl;
    }

}

//子进程工作函数
void childProcessRun(void* data) {
//    auto item = (int*)data;
    while (true) {
        auto start = std::chrono::steady_clock::now();
        sleep(1);
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;
        std::cout<<"diff is "<<diff.count()<<std::endl;
    }
}


int main() {
    //分配1G的空间
    auto start = std::chrono::steady_clock::now();
    auto mem = new int [1024 * 1024 * 1024];
    for (int i = 0; i < 1024 * 1024 * 1024; ++i) {
        mem[i] = 32;
    }
    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double> diff = end - start;
    std::cout<<std::setw(9)<<"malloc 1G time cost:"<<diff.count()<<std::endl;
    auto start1 = std::chrono::steady_clock::now();
    if ( auto pid = fork(); pid == 0) {
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff1 = end - start1;
        std::cout<<std::setw(9)<<"malloc 1G time cost:"<<diff1.count()<<std::endl;
        childProcessRun(mem);
    } else {
        parentProcessRun(mem);
    }
    return 0;
}

打印的时间:

/home/jaime/code/linux_test/cmake-build-debug/linux_test
malloc 1G time cost:3.24325
malloc 1G time cost:0.0242203
diff is 1.00013
diff is 1.00008
diff is 1.00014
diff is 1.00042
diff is 1.00015
diff is 1.00012
diff is 1.0001
diff is 1.0001
parent is 3.87098
diff is 1.00012
diff is 1.00013
diff is 1.00013
diff is 1.00014
diff is 1.00041
diff is 1.00006
diff is 1.00013
diff is 1.00014
parent is 2.21931
diff is 1.00017
diff is 1.00013
diff is 1.00014
diff is 1.00034
diff is 1.00037
diff is 1.00014
diff is 1.00014
parent is 2.2625
diff is 1.00013
diff is 1.00065
diff is 1.00037
diff is 1.00032
diff is 1.00062
diff is 1.00013
diff is 1.00014
parent is 2.25607
diff is 1.00008
diff is 1.00031
diff is 1.00018
diff is 1.00036
diff is 1.00046
diff is 1.00014
diff is 1.00013
parent is 2.25377
diff is 1.00029
diff is 1.00041

修改子进程的数据:

#include <iostream>
#include <unistd.h>
#include <chrono>
#include <iomanip>

//父进程工作函数
void parentProcessRun(void* data) {
    while (true) {
        auto start = std::chrono::steady_clock::now();
        sleep(1);
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;
        std::cout<<"parentProcessRun is "<<diff.count()<<std::endl;
    }

}

//子进程工作函数
void childProcessRun(void* data) {
//    auto item = (int*)data;
    while (true) {
        sleep(5);
        auto start = std::chrono::steady_clock::now();
        for (int i = 0; i < 1024 * 1024 * 1024; ++i) {
            int* item = (int*)data;
            item[i] = 333323;
        }
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;
        std::cout<<"childProcessRun is "<<diff.count()<<std::endl;
    }
}


int main() {
    //分配1G的空间
    auto start = std::chrono::steady_clock::now();
    auto mem = new int [1024 * 1024 * 1024];
    for (int i = 0; i < 1024 * 1024 * 1024; ++i) {
        mem[i] = 32;
    }
    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double> diff = end - start;
    std::cout<<std::setw(9)<<"malloc 1G time cost:"<<diff.count()<<std::endl;
    auto start1 = std::chrono::steady_clock::now();
    if ( auto pid = fork(); pid == 0) {
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff1 = end - start1;
        std::cout<<std::setw(9)<<"malloc 1G time cost:"<<diff1.count()<<std::endl;
        childProcessRun(mem);
    } else {
        parentProcessRun(mem);
    }
    return 0;
}

打印的数据:

/home/jaime/code/linux_test/cmake-build-debug/linux_test
malloc 1G time cost:3.33296
malloc 1G time cost:0.0251366
parentProcessRun is 1.00013
parentProcessRun is 1.0003
parentProcessRun is 1.00008
parentProcessRun is 1.00043
parentProcessRun is 1.00009
parentProcessRun is 1.00006
parentProcessRun is 1.0001
parentProcessRun is 1.0001
childProcessRun is 3.99467
parentProcessRun is 1.0004
parentProcessRun is 1.00012
parentProcessRun is 1.00013
parentProcessRun is 1.00008
parentProcessRun is 1.0004
parentProcessRun is 1.00012
parentProcessRun is 1.00016
parentProcessRun is 1.00013
childProcessRun is 2.2702
parentProcessRun is 1.00025
parentProcessRun is 1.00011
parentProcessRun is 1.00013
parentProcessRun is 1.00014
parentProcessRun is 1.00031
parentProcessRun is 1.00013
parentProcessRun is 1.0001
childProcessRun is 2.28965
parentProcessRun is 1.00013
parentProcessRun is 1.00014
parentProcessRun is 1.00009

父子进程同时修改内存数据:

#include <iostream>
#include <unistd.h>
#include <chrono>
#include <iomanip>

//父进程工作函数
void parentProcessRun(void* data) {
    while (true) {
        sleep(1);
        auto start = std::chrono::steady_clock::now();
        for (int i = 0; i < 1024 * 1024 * 1024; ++i) {
            int* item = (int*)data;
            item[i] = 333323;
        }
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;
        std::cout<<"childProcessRun is "<<diff.count()<<std::endl;
    }
}

//子进程工作函数
void childProcessRun(void* data) {
//    auto item = (int*)data;
    while (true) {
        sleep(1);
        auto start = std::chrono::steady_clock::now();
        for (int i = 0; i < 1024 * 1024 * 1024; ++i) {
            int* item = (int*)data;
            item[i] = 333323;
        }
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;
        std::cout<<"childProcessRun is "<<diff.count()<<std::endl;
    }
}


int main() {
    //分配1G的空间
    auto start = std::chrono::steady_clock::now();
    auto mem = new int [1024 * 1024 * 1024];
    for (int i = 0; i < 1024 * 1024 * 1024; ++i) {
        mem[i] = 32;
    }
    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double> diff = end - start;
    std::cout<<std::setw(9)<<"malloc 1G time cost:"<<diff.count()<<std::endl;
    auto start1 = std::chrono::steady_clock::now();
    if ( auto pid = fork(); pid == 0) {
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff1 = end - start1;
        std::cout<<std::setw(9)<<"malloc 1G time cost:"<<diff1.count()<<std::endl;
        childProcessRun(mem);
    } else {
        parentProcessRun(mem);
    }
    return 0;
}

打印的数据:

/home/jaime/code/linux_test/cmake-build-debug/linux_test
malloc 1G time cost:3.26486
malloc 1G time cost:0.0240646
childProcessRun is 4.31179
childProcessRun is 4.31189
childProcessRun is 2.35453
childProcessRun is 2.37635
childProcessRun is 2.38194
childProcessRun is 2.37046
childProcessRun is 2.40167
childProcessRun is 2.39633
childProcessRun is 2.37682
childProcessRun is 2.37693

从打印的数据得到,fork的时候并不会拷贝数据副本,当父子进程修改数据的时候,修改了内存的进程会拷贝一份副本出来,如果高并发频繁修改数据有可能会阻塞进程。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值