C系统v信号量未锁定关键区域

时间:2019-05-03 14:58:11

标签: c linux semaphore

我有一个代码,该代码从共享内存中读取一个整数,然后确定子进程中的数字。我正在使用信号量来锁定此关键区域,以便仅在没有其他进程不增加整数的情况下才增加整数。这样我得到一个稳定的整数值。

我为此使用了SYSTEM V Semaphore,但是由于使用该函数没有任何作用,因此我无法使其正常工作。

该程序仅生成100个processeces,其中每个进程将共享内存中的整数增加1。(该共享内存段最初是由另一个程序创建的,请告诉我是否也需要!)

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sem.h>
#include <unistd.h>
#include <sys/wait.h>

#define FIFO_PATH "/tmp/RESULT_FIFO"
#define PROC_NUM 10
#define MAX_ITERATIONS 1
#define SHM_SIZE 1024
#define SHM_KEY 23423

int shmid, *shmdata, semid;
struct sembuf semaphore;

int main() {
    FILE *fp_fifo;
    /* Initialize Semaphore */
    semid = semget (2134L, 0, IPC_PRIVATE);
    if(semid <0) {
        semid = semget (2134, 1, IPC_CREAT | IPC_EXCL | 0666);
        if (semid < 0) {
            perror("semget()");
            exit(EXIT_FAILURE);
        }
    }
    semctl(semid, 0, SETVAL, (int)1);
    shmid = shmget(234234, sizeof(int),  0777);
    if(shmid == -1) {
        perror("shmget()");
        exit(EXIT_FAILURE);
    }
    shmdata = shmat(shmid, 0, 0);
    if(shmdata == -1) {
        perror("shmat()");
        exit(EXIT_FAILURE);
    }
    printf("key is53 %i \n", *shmdata);
    for (int i = 0; i < 100; ++i) {
        int t = fork();
        if(t > 0) {
            /*parent*/
        } else if(t == -1) {
            perror("fork()");
        } else if(t == 0)  {
            /* Child*/
            semaphore.sem_op  = -1;
            semaphore.sem_num = 0;
            semaphore.sem_flg = SEM_UNDO;
            if(semop(semid, &semaphore, 1) == -1) {
                perror("semop()");
                exit(EXIT_FAILURE);
            }

            *shmdata=*shmdata+1;
            semaphore.sem_op  = 1;
            if(semop(semid, &semaphore, 1) == -1) {
                perror("semop()");
                exit(EXIT_FAILURE);
            }
            return EXIT_SUCCESS;
        }
    }
    wait(0);
    printf("key is53 %i \n", *shmdata);

    if((fp_fifo = fopen(FIFO_PATH, "w")) == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }

    fprintf(fp_fifo, "%d", *shmdata);
    shmdata = 0;
    shmdt(shmdata);
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:5)

我发现错误是因为wait()无法正常工作。 信号灯正常工作,但是没有正确使用wait(NULL)。这是我的简单示例的正确版本,该示例如何使用 system V信号量

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sem.h>
#include <unistd.h>
#include <sys/wait.h>

#define FIFO_PATH "/tmp/RESULT_FIFO"
#define PROC_NUM 10
#define MAX_ITERATIONS 1
#define SHM_SIZE 1024
#define SHM_KEY 23423

int shmid, *shmdata, semid;
struct sembuf semaphore;

int main() {
    FILE *fp_fifo;
    /* Initialize Semaphore */
    semid = semget (2134L, 0, IPC_PRIVATE);
    if(semid <0) {
        semid = semget (2134, 1, IPC_CREAT | IPC_EXCL | 0666);
        if (semid < 0) {
            perror("semget()");
            exit(EXIT_FAILURE);
        }
    }
    semctl(semid, 0, SETVAL, (int)1);
    shmid = shmget(234234, sizeof(int),  0777);
    if(shmid == -1) {
        perror("shmget()");
        exit(EXIT_FAILURE);
    }
    shmdata = shmat(shmid, 0, 0);
    if(shmdata == -1) {
        perror("shmat()");
        exit(EXIT_FAILURE);
    }
    printf("key is46 %i \n", *shmdata);
    for (int i = 0; i < 100; ++i) {
        int t = fork();
        if(t > 0) {
            /*parent*/
            wait(NULL);
        } else if(t == -1) {
            perror("fork()");
        } else if(t == 0)  {
            /* Child*/
            semaphore.sem_op  = -1;
            semaphore.sem_num = 0;
            semaphore.sem_flg = SEM_UNDO;
            if(semop(semid, &semaphore, 1) == -1) {
                perror("semop()");
                exit(EXIT_FAILURE);
            }


            *shmdata= *shmdata+1;
           // printf("key is65 %i \n", *shmdata);
            semaphore.sem_op  = 1;
            if(semop(semid, &semaphore, 1) == -1) {
                perror("semop()");
                exit(EXIT_FAILURE);
            }
            return EXIT_SUCCESS;
        }
    }
    printf("key is73 %d \n", *shmdata);

    if((fp_fifo = fopen(FIFO_PATH, "w")) == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }

    fprintf(fp_fifo, "%d", *shmdata);
    shmdata = 0;
    shmdt(shmdata);
    return EXIT_SUCCESS;
}