由于进程和信号量

时间:2016-10-10 19:15:51

标签: c parallel-processing mutex semaphore producer-consumer

我的计划的一般性和功能

NB:您将能够测试我的程序(只有一个文件,包含main功能)。我在这个问题的最后给你完整的代码。

我编写了一个程序,可以用UNIX-Processes来说明生产者 - 消费者算法。生产者创建一些值,例如5,将其写入RAM shared_buffer,然后将后者写入文件test.txt。使用者为此shared_buffer分配文件test.txt的内容,并从RAM缓冲区shared_buffer中获取一些值。

我使用函数将shared_buffer转换为文件,并相互:arrayToFilefileToArray。这两个问题都在这个问题的最后提出。

shared_buffer的大小为1 + 10个案例:第一个包含完整案例的数量(即:5已写入)和10个其他案例可以用5填充,也可以不填充任何内容。 第一种情况对生产者有用,知道在哪里写下一个值(即:在这种情况下)。 该文件当然也有1 + 10个案例。该文件是必需的,因为我使用进程而不是线程(因此不是共享内存)。

shared_buffer的初始化包含在main函数中。 shared_buffer访问(在阅读和书面中)分别包含在消费者的功能和生产者的功能中。这些代码将在本问题的最后部分提供。

访问shared_buffer和整个文件当然是互斥的,并且使用了三个信号量。互斥锁阻止生产者和消费者同时访问它,另外两个信号量用于保证生产者在没有足够的地方时不会尝试放置新元素,并且如果没有任何元素,消费者不会尝试使用元素。嗯,它只是生产者 - 消费者算法。

最后,制作人的流程一直持续到时间结束,消费者的流程也是如此。

这三个信号量的声明和初始化将在本问题的最后提供。

我的问题

只有一个问题:当生产者的流程和消费者流程都运行到时间while(TRUE)时,arrayToFilefileToArray告诉我该文件的开放失败。如果我删除while(TRUE)中的一个或两个,则此错误消失(但因此,我的程序无法完成其工作)。

因此只有在while(TRUE)都被写入时才会出现此问题。

我认为这是因为我没有充分利用互斥锁。但我无法给你更多解释。

来源

代码备受好评。

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/sem.h>
#include <stdlib.h>

#define TRUE 1
#define ERR_NBR_ARGS "ERROR. Argument needed. Use as follows.\n"

int fileToArray(char *, int *, int);
int arrayToFile(char *, int *, int);

void check_if_command_arguments_are_correct(int);
void mutual_exclusion_produce(int, char*, int*, int);
void mutual_exclusion_consume(int, char*, int*, int);

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

    check_if_command_arguments_are_correct(argc);

    // FILE'S PATH
    char* file_path = argv[1];

    // DECLARATION AND INITIALISATION OF THE SHARED RESOURCE
    int shared_buffer_number_of_elements = 10;
    int shared_buffer[shared_buffer_number_of_elements + 1];
    shared_buffer[0] = 0;
    arrayToFile(file_path, shared_buffer, shared_buffer_number_of_elements);

    // FILE'S KEY (used to make processes able to use the same semaphores)
    key_t key = ftok(argv[0], '0');
    if(key == - 1) {
        perror("ftok");
        exit(EXIT_FAILURE);
    }

    // DECLARATION AND INITIALISATION OF THE THREE SEMAPHORES
    int semid = semget(key, 3, IPC_CREAT|IPC_EXCL|0666); // Declaration of the semaphores
    if(semid == -1) {
        perror("semget");
        exit(EXIT_FAILURE);
    }

    int array_semaphores_values[3];
    array_semaphores_values[0] = 1;
    array_semaphores_values[1] = shared_buffer_number_of_elements;
    array_semaphores_values[2] = 0;
    int sem_controller = semctl(semid, 3, SETALL, array_semaphores_values); // Initialisation of the semaphores - 2th parameter is the array's size
    if(sem_controller == -1) {
        perror("semctl");
        exit(EXIT_FAILURE);
    }

    // THE TWO FORKS - CREATION OF BOTH PRODUCER AND CONSUMER
    pid_t producer = fork();
    if(producer == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    } else if(producer == 0) { // The producer process

        mutual_exclusion_produce(semid, file_path, shared_buffer, shared_buffer_number_of_elements);

    } else { // The main process

        pid_t consumer = fork();
        if(consumer == - 1) {
            perror("fork");
            exit(EXIT_FAILURE);
        } else if(consumer == 0) { // The consumer process

            mutual_exclusion_consume(semid, file_path, shared_buffer, shared_buffer_number_of_elements);

        }
    }

    semctl(semid, 0, IPC_RMID, 0); // The semaphores are freed
}

void mutual_exclusion_produce(int semid, char* file_path, int* buffer, int size) {
    /* The producer does the following :
     * 1. It decrements the free cases semaphore ;
     * 2. It decrements the mutex ;
     * 3. It writes the buffer
     * 4. It increments the mutex ;
     * 5. It increments the full cases semaphore ;
     * */

    while(TRUE) {
        // DECREMENTS FREE CASES SEMAPHORE AND DECREMENTS MUTEX
        struct sembuf operation_decrement_free_cases;
        operation_decrement_free_cases.sem_num = 2;
        operation_decrement_free_cases.sem_op = -1;
        operation_decrement_free_cases.sem_flg = 0;

        struct sembuf operation_decrement_mutex;
        operation_decrement_mutex.sem_num = 0;
        operation_decrement_mutex.sem_op = -1;
        operation_decrement_mutex.sem_flg = 0;

        semop(semid, &operation_decrement_free_cases, 0);
        semop(semid, &operation_decrement_mutex, 0);

        // WRITES THE BUFFER INTO THE FILE
        buffer[++buffer[0]] = 5;
        arrayToFile(file_path, buffer, size);

        // INCREMENTS THE MUTEX AND INCREMENTS THE FULL CASES SEMAPHORE
        struct sembuf operation_increment_full_cases;
        operation_decrement_free_cases.sem_num = 1;
        operation_decrement_free_cases.sem_op = +1;
        operation_decrement_free_cases.sem_flg = 0;

        struct sembuf operation_increment_mutex;
        operation_decrement_mutex.sem_num = 0;
        operation_decrement_mutex.sem_op = +1;
        operation_decrement_mutex.sem_flg = 0;

        semop(semid, &operation_increment_mutex, 0);
        semop(semid, &operation_increment_full_cases, 0);
    }
}

void mutual_exclusion_consume(int semid, char* file_path, int* buffer, int size) {
    /*
     * The consumer does the following :
     * 1. It decrements the full cases semaphore ;
     * 2. It decrements the mutex ;
     * 3. It reads the buffer ;
     * 4. It increments the mutex ;
     * 5. It increments the free cases semaphore ;
     * */

    while(TRUE) {
        // DECREMENTS FULL CASES SEMAPHORE AND DECREMENTS MUTEX
        struct sembuf operation_decrement_full_cases;
        operation_decrement_full_cases.sem_num = 1;
        operation_decrement_full_cases.sem_op = -1;
        operation_decrement_full_cases.sem_flg = 0;

        struct sembuf operation_decrement_mutex;
        operation_decrement_mutex.sem_num = 0;
        operation_decrement_mutex.sem_op = -1;
        operation_decrement_mutex.sem_flg = 0;

        semop(semid, &operation_decrement_full_cases, 0);
        semop(semid, &operation_decrement_mutex, 0);

        // READS THE FILE AND PUT THE CONTENTS INTO THE BUFFER
        fileToArray(file_path, buffer, size);

        // INCREMENTS THE MUTEX AND INCREMENTS THE FREE CASES SEMAPHORE
        struct sembuf operation_increment_free_cases;
        operation_decrement_full_cases.sem_num = 2;
        operation_decrement_full_cases.sem_op = +1;
        operation_decrement_full_cases.sem_flg = 0;

        struct sembuf operation_increment_mutex;
        operation_decrement_mutex.sem_num = 0;
        operation_decrement_mutex.sem_op = +1;
        operation_decrement_mutex.sem_flg = 0;

        semop(semid, &operation_increment_mutex, 0);
        semop(semid, &operation_increment_free_cases, 0);
    }
}

void check_if_command_arguments_are_correct(int argc) {
    if(argc != 2) {
        fprintf(stderr, ERR_NBR_ARGS);
        fprintf(stderr, "program_command <file_buffer>\n");
        exit(EXIT_FAILURE);
    }
}

int fileToArray(char *pathname, int *tab, int size) {
    int cible;
    if ( (cible  = open(pathname,O_RDONLY)) < 0){
        fprintf(stderr,"fileToArray - impossible to open the file\n");
        return -1;
    }

    if (read(cible,tab,(size+1) * sizeof(int)) !=(size+1) * sizeof(int)) {
        fprintf(stderr,"fileToArray - impossible to read the file\n");
        return -1;
    }
    close(cible);
    return 0;
}

int arrayToFile(char *pathname, int *tab, int size) {
    int cible;

    if ( (cible  = open(pathname,O_WRONLY|O_CREAT|O_TRUNC,0666)) < 0){
        fprintf(stderr,"arrayToFile - impossible to open the file\n");
        return -1;
    }

    if (write(cible,tab,(size+1) * sizeof(int)) !=(size+1) * sizeof(int)) {
        fprintf(stderr,"arrayToFile - impossible to write the file\n");
        return -1;
    }
    close(cible);
    return 0;
}

0 个答案:

没有答案