C:将使用 tmpfile() 函数创建的临时文件与多线程合并

标签 c multithreading merge pthreads

我有 n 个文件,我的程序需要使用线程和临时文件将所有文件的内容合并到一个文件中(必须使用 tmpfile())。创建线程时,它必须将 2 个文件合并到临时文件 (temp1) 中,然后另一个线程将接下来的 2 个文件合并到另一个临时文件 (temp2) 中,依此类推,然后在下一个级别中,另一个线程应该将 temp1 与 temp2 合并到另一个临时文件。

alt text

我正在考虑创建一个文件名数组,将其作为参数传递给 pthread_create ,并且该函数也应该返回修改后的数组,但我不知道如何获取临时文件名。将会是这样的:

int main(int argc, char *argv[]){

int n = argc -1;

char *files_arr[n];

pthread_t threads[n-1];

}

    for (int i=0; i < argc; i++)

    {
       pthread_create (&threads[i], NULL, temp_merge, (void *) &files_arr);
    }
}//end main

void *temp_merge (void *arg){

    char *myarray[];
    myarray = (char *) arg;
    FILE *f1, *f2, *tf;
    tf = tmpfile();

    //code to merge f1 and f2 into tf, f1 and f2 could be temp files created before

     pthread_exit((void*) myarray); //Do I lose the temp file using pthread_exit? 
}

问题是:如何访问之前在上一个线程中使用 tmp() 打开的临时文件来生成新的临时文件?

最佳答案

您能否使用tmpnam()而不是tmpfile(),并根据需要手动打开/关闭/删除文件?

关于C:将使用 tmpfile() 函数创建的临时文件与多线程合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4069825/

相关文章:

Java线程,线程完成后如何释放资源

ios - IOS中文件夹的并行复制

svn - Tortoisesvn 跳过记录合并信息

algorithm - 合并辅助过程的复杂性

c - 将双指针数组的大小加倍?

c - 分配给 double 据类型的位数

c - 如何在 C 中实现位集

c++ - 使用 linux 功能读取 block 的不连续运行

multithreading - 在等待线程完成时显示 progressDialog 2 秒

R 合并数据帧,允许不精确的 ID 匹配(例如,附加字符 1234 匹配 ab1234)