Created
June 2, 2017 05:21
-
-
Save shekkbuilder/d5868f1e8db7252901b3af705e41622c to your computer and use it in GitHub Desktop.
copy on write
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/sysinfo.h> | |
void printMemStat(){ | |
struct sysinfo si; | |
sysinfo(&si); | |
printf("===\n"); | |
printf("Total: %llu\n", si.totalram); | |
printf("Free: %llu\n", si.freeram); | |
} | |
int main(){ | |
long len = 200000000; | |
long *array = malloc(len*sizeof(long)); | |
long i = 0; | |
for(; i<len; i++){ | |
array[i] = i; | |
} | |
printMemStat(); | |
if(fork()==0){ | |
/*child*/ | |
printMemStat(); | |
i = 0; | |
for(; i<len/2; i++){ | |
array[i] = i+1; | |
} | |
printMemStat(); | |
i = 0; | |
for(; i<len; i++){ | |
array[i] = i+1; | |
} | |
printMemStat(); | |
}else{ | |
/*parent*/ | |
int times=10; | |
while(times-- > 0){ | |
sleep(1); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment