将文件IO初始化为C中的全局变量

问题描述 投票:0回答:1

我想在C的pthread算法中将FILE *in, *out;初始化为全局变量。虽然确实可以编译,但是当我在命令提示符下测试程序时,系统报告程序没有响应并自动停止。

in读取文本文件,out将内容写入另一个文本文件。两种操作都在使用pthread操作的两个单独的函数中执行。在main()中打开和关闭文件。

如果我根本无法将文件IO初始化为全局变量,那么在函数之间共享它们的正确方法是什么?请。

这是我目前拥有的,但是一旦使FILE IO成为全局文件,就会导致错误。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> 
#include "list.h"
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; //declare thread conditions
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; //declare mutex to lock functions
static int sec; //time required by each lift to serve a request
int counter; //number of requests made
static int size; //buffer size (must only change once at beginning)
int length; //Number of values in buffer
static int in; //index of reader
static int out_i; //index of writer
buffer_t A[];
write_t write; //RAM file to store information to write to output
void* lift(void* vargp) //Consumer
{
    pthread_mutex_lock(&lock);
    while (length != size)
    {
        if (length <= 0)
        {
            printf("Signalling\n");
            pthread_cond_signal(&cond1);
        }
        printf("lifting information\n");
        FILE *out = vargp;
        //gather information to print
        if (write.p == 0) //only for when system begins
        {
            write.p = A[out_i].from;
        }
        write.rf = A[out_i].from;
        write.rt = A[out_i].to;
        write.m = (write.p - A[out_i].from) + (A[out_i].to - A[out_i].from);
        write.c = A[out_i].to;
        //Now write the information
        //FILE* out = fopen("sim_output.txt", "a");
        fprintf(out, "Lift-%d Operation\n", write.l);
        fprintf(out, "Previous position: Floor %d\n", write.p);
        fprintf(out, "Request: Floor %d to Floor %d\n", write.rf, write.rt);
        fprintf(out, "Detail operations:\n");
        fprintf(out, "    Go from Floor %d to Floor %d\n", write.p, write.rf);
        fprintf(out, "    Go from Floor %d to Floor %d\n", write.rf, write.rt);
        fprintf(out, "    #movement for this request: %d\n", write.m);
        write.total_r[write.l] = write.total_r[write.l] + 1;
        fprintf(out, "    #request: %d\n", write.total_r[write.l]);
        write.total_m[write.l] = write.total_m[write.l] + 1;
        fprintf(out, "    Total #movement: %d\n", write.total_m[write.l]);
        fprintf(out, "Current Position: Floor %d\n", write.c);
        fprintf(out, "\n");
        write.p = write.c; //for next statement
        printf("%d \n", out_i);
        length++;
        out_i++;
        if (out_i == size)
        {
            out_i = 0;
        }
    }
    pthread_mutex_unlock(&lock);
    return NULL;
}


void *request_t(void *vargp) //producer
{
    pthread_mutex_lock(&lock); //Now only request can operate (mutual exclusion)
    FILE *f, *out = vargp;
    f = fopen("sim_input.txt", "r");
    if (f == NULL)
    {
        printf("Input file empty\n");
        exit(0);
    }
    while(1)
    {
        printf("Requesting information\n");
        if (length < 1)
        {
            printf("Waiting\n");
            pthread_cond_wait(&cond1, &lock);
        }
        //read the input line by line and into the buffer
        fscanf(f, "%d %d\n", &A[in].from, &A[in].to);
        //Print buffer information to sim_output
        fprintf(out, "New Lift Request from Floor %d to Floor %d \n", A[in].from, A[in].to);
        fprintf(out, "Request No %d \n", counter++);
        fprintf(out, "----------------------------\n");
        length--;
        in++;
        if (in == size)
        {
            in = 0;
        }
    }
    fclose(f);
    pthread_mutex_unlock(&lock);
    return NULL;
}
void main(int argc, char *argv[]) // to avoid segmentation fault
{
    //establish values for use (convert argv to int values)
    counter = 1;
    in = 0;
    out_i = 0;
    long arg = strtol(argv[1], NULL, 10);
    size = arg;
    if (size <= 1)
    {
        printf("buffer size too small\n");
        exit(0);
    }
    else
    {
        length = size;
        A[size].from = NULL;
        A[size].to = NULL;
    }
    arg = strtol(argv[2], NULL, 10);
    sec = arg;
    if (sec <= 0)
    {
        printf("insufficient time\n");
        exit(0);
    }
    FILE *out = fopen("sim_output.txt", "w"); //Clear output
    //Create threads
    printf("Creating threads\n");
    pthread_t Lift_R, lift_1, lift_2, lift_3;
    pthread_create(&Lift_R, NULL, request_t, out);
    sleep(sec);
    write.l = 1;
    pthread_create(&lift_1, NULL, lift, out);
    sleep(sec);
    write.l = 2;
    pthread_create(&lift_2, NULL, lift, out);
    sleep(sec);
    write.l = 3;
    pthread_create(&lift_3, NULL, lift, out);
    pthread_join(lift_3, NULL);
    fclose(out);
    //int sum_r = write.total_r[1] + write.total_r[2] + write.total_r[3],
        //sum_t = write.total_m[1] + write.total_m[2] + write.total_m[3];
}

我想将FILE * in,* out初始化为C中pthread算法中的全局变量。虽然确实可以编译,但是当我在命令提示符下测试程序时,系统报告该程序不是...

c file-io pthreads global-variables
1个回答
0
投票

共享文件的正确方法是将代码本身作为参数放入pthread函数中。

© www.soinside.com 2019 - 2024. All rights reserved.