multithreading - What is the correct approach to write multiple small pieces to a temp file in c, in multithreads? -
i simulating multithreads file downloading. strategy in each thread receive small file pieces( each file piece has piece_length , piece_size , start_writing_pos )
and each thread writes same buffer. how realize ? have worry collisions ?
//=================== follow ============//
so write small demo follows:
#include <stdio.h> int main(){ char* tempfilepath = "./testing"; file *fp; fp = fopen(tempfilepath,"w+");//w+: reading , writing fseek( fp, 9, seek_set);//starting in 10-th bytes fwrite("----------",sizeof(char), 10, fp); fclose(fp); }
and before execution let content in "./testing" "xxxxxxxxxxxxxxxxxxx", after above "^@^@^@^@^@^@^@^@^@----------" wonder problem ....
do torrent clients do. create file final size having extension .part
. allocate non-overlapping parts of file each thread, shall have own file-descriptors. collisions avoided. rename final name when finished.
Comments
Post a Comment