c - 使用20个线程写入文件

标签 c

这里的想法是创建一个要写入的文件。我正在尝试创建 10 个线程,并让它们每个打印到文件 10 次。使用信号量阻止多个线程同时写入文件。但我有错误。代码如下:

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

#define FNAME   "fisier.txt"
#define MAX_STRING_LEN  80
#define NUMBER_OF_THREADS 10

FILE *fp;
sem_t mutex;
int counter;

FILE *makeTextFile(char *fname, char mode){
   FILE *localFP;
   localFP = fopen(fname, &mode);
 return (localFP);
}

void *print_message(void *tid){
    int i;
    for (i = 0; i < 10; i++){
        sem_wait(&mutex);
        fp = fopen(FNAME, "a");
        fprintf(fp, "Thread %d is running.\n", tid);
        fclose(fp);
    sem_post(&mutex);
    printf ( "Thread %d has finished.\n", tid);
    }
}

int threads(){
    const char *fName = "fisier.txt";
    int status;
    pthread_t threads[NUMBER_OF_THREADS];
    fp = makeTextFile(FNAME, 'w');
    fprintf(fp, "Process ID: %ld\n", (long)getpid());
    fclose(fp);

    int i;
    for (i =0; i < NUMBER_OF_THREADS; i++){
        status = pthread_create(&threads[i], NULL, &print_message, (void *)i);
        if (status != 0){
            printf("pthread_create returned error code %d\n", status);
            exit(-1);
        }
     }
    }

int main() {
    threads();  

    return 0;
}

警告:

probl2.c: In function ‘print_message’:
probl2.c:27:21: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘void *’ [-Wformat=]
         fprintf(fp, "Thread %d is running.\n", tid);
                     ^
probl2.c:30:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘void *’ [-Wformat=]
     printf ( "Thread %d has finished.\n", tid);
              ^
probl2.c: In function ‘threads’:
probl2.c:44:68: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   status = pthread_create(&threads[i], NULL, &print_message, (void *)i);

将其写入文件:进程 ID:10568 我想写 怎么解决呢?

最佳答案

我注意到的一些事情:

您没有初始化信号量。使用 sem_init 来执行此操作(完成后使用 sem_destroy)。

您没有加入您的话题。程序将退出而不等待线程完成。您可以在循环中使用pthread_join来确保所有线程都已完成。

这是线程函数的更新版本。在生产代码中,我将检查我添加的函数的返回值。

void threads(){
    const char *fName = "fisier.txt";
    int status;
    pthread_t threads[NUMBER_OF_THREADS];
    fp = makeTextFile(FNAME, 'w');
    fprintf(fp, "Process ID: %ld\n", (long)getpid());
    fclose(fp);

    sem_init(&mutex,0,1);
    int i;

    for (i =0; i < NUMBER_OF_THREADS; i++){
        status = pthread_create(&threads[i], NULL, &print_message, (void*)i);
        if (status != 0){
            printf("pthread_create returned error code %d\n", status);
            exit(-1);
        }
     }

     void* value = NULL;
     for (i = 0; i < NUMBER_OF_THREADS; i++) {
         pthread_join(threads[i], &value);
     }
     sem_destroy(&mutex);
}

关于c - 使用20个线程写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42933236/

相关文章:

c - 如何使用 open read 逐行读取文件?

无法计算出此 C 程序中使用函数计算总和和平均值的输出

为参数 `type name' 指定的 C 函数指针错误存储类

c - 想要使用 GSTREAMER 设置捕捉视频的时间

c - NOT false 和 true 的区别

c - 传递一个参数数量不确定的函数,并使用可变参数调用它

c - 链表维护根指针地址,以便在构建后读取它

c - 如何将字符串参数的指针传递给函数?

c - 多管道 C 程序不起作用

将数组表示法转换为指针表示法