如何实现两个C程序使用的全局POSIX信号量?

How to implement global POSIX semaphore to be used by two C programs?

我有两个 C 文件:a.c(master)和 b.c(follower)。
我想使用信号量在主从之间创建同步。
据我所知,我需要一个全局 POSIX 信号量才能完成这项工作。
如何仅使用 a.c(主控)中的信号量在 b.c(跟随者)文件中实现此功能?

a.c 文件(主文件)实现的信号量:

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

sem_t x,y;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount = 0, writercount = 0;

void* reader(void* param)
{
    if (writercount > 0) {
        sem_wait(&x);
        readercount++;
        if (readercount == 1) {
            sem_wait(&y);
        }
        sem_post(&x);
        printf("%d reader is inside\n",readercount);

        //read all the files in the directory

        sleep(3);
        sem_wait(&x);
        readercount--;
        if (readercount == 0) {
            sem_post(&y);
        }
        sem_post(&x);
        printf("%d Reader is leaving\n",readercount+1);
    } else {
        printf("Nothing to view\n");
    }
    return NULL;
}

void* writer(void* param)
{
    printf("Master is trying to upload\n");
    sem_wait(&y);
    printf("Master is uploading\n");

    //create file in a directory

    sem_post(&y);
    writercount++;
    printf("Master is leaving\n");
    return NULL;
}

int main()
{
    int a,i = 0,b;
    sem_init(&x,0,1);
    sem_init(&y,0,1);

    while (1) {
        printf("Enter 1 to View  / 2 to Upload / 3 to Exit:");
        scanf("%d",&b);
        if (b == 1) {
            pthread_create(&readerthreads[i],NULL,reader,NULL);
        } else if (b == 2) {
            pthread_create(&writerthreads[i],NULL,writer,NULL);
        } else {
            exit(0);
        }
        pthread_join(writerthreads[i],NULL);
        pthread_join(readerthreads[i],NULL);
        i++;
    }

    return 0;
}

b.c 文件中的信号量(跟随者):

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

sem_t x,y;
pthread_t tid;
pthread_t readerthreads[100];
int readercount = 0, writercount = 0;

void* reader(void* param)
{
    if (writercount > 0) {
        sem_wait(&x);
        readercount++;
        if (readercount == 1) {
            sem_wait(&y);
        }
        sem_post(&x);
        printf("%d Follower is inside\n",readercount);
        //read all the files in the directory
        sleep(3);
        sem_wait(&x);
        readercount--;
        if (readercount == 0) {
            sem_post(&y);
        }
        sem_post(&x);
        printf("%d Follower is leaving\n",readercount+1);
    } else {
        printf("Nothing to view\n");
    }
    return NULL;
}

int main()
{
    int a,i = 0,b;
    sem_init(&x,0,1);
    sem_init(&y,0,1);

    while (1) {
        printf("Enter 1 to View  / 3 to Exit:");
        scanf("%d",&b);
        if (b == 1) {
            pthread_create(&readerthreads[i],NULL,reader,NULL);
        } else {
            exit(0);
        }
        pthread_join(readerthreads[i],NULL);
        i++;
    }

}

有两个不同的进程,您必须使用命名信号量,如下所述:

所以你的代码可以像

一样编辑

a.c

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>

sem_t *x,*y;
pthread_t tid;
pthread_t writerthreads[100],readerthreads[100];
int readercount = 0, writercount = 0;

void* reader(void* param)
{
    if (writercount > 0) {
        sem_wait(x);
        readercount++;
        if (readercount == 1) {
            sem_wait(y);
        }
        sem_post(x);
        printf("%d reader is inside\n",readercount);

        //read all the files in the directory

        sleep(3);
        sem_wait(x);
        readercount--;
        if (readercount == 0) {
            sem_post(y);
        }
        sem_post(x);
        printf("%d Reader is leaving\n",readercount+1);
    } else {
        printf("Nothing to view\n");
    }
    return NULL;
}

void* writer(void* param)
{
    printf("Master is trying to upload\n");
    sem_wait(y);
    printf("Master is uploading\n");

    //create file in a directory

    sem_post(y);
    writercount++;
    printf("Master is leaving\n");
    return NULL;
}

int main()
{
    int a,i = 0,b;
    x = sem_open("/semaphore_x", O_CREAT | O_EXCL, S_IRWXU | S_IRWXG  | S_IRWXO, 1);
    y = sem_open("/semaphore_y", O_CREAT | O_EXCL, S_IRWXU | S_IRWXG  | S_IRWXO, 1);
    
    if( x == NULL || y == NULL) {
        printf("Unable to open named semaphores\n");
        exit(-1);
    }

    while (1) {
        printf("Enter 1 to View  / 2 to Upload / 3 to Exit:");
        scanf("%d",&b);
        if (b == 1) {
            pthread_create(&readerthreads[i],NULL,reader,NULL);
        } else if (b == 2) {
            pthread_create(&writerthreads[i],NULL,writer,NULL);
        } else {
            sem_close(x);
            sem_close(y);
            sem_unlink("/semaphore_x");
            sem_unlink("/semaphore_y");
            exit(0);
        }
        pthread_join(writerthreads[i],NULL);
        pthread_join(readerthreads[i],NULL);
        i++;
    }

    return 0;
}

b.c

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>

sem_t *x,*y;
pthread_t tid;
pthread_t readerthreads[100];
int readercount = 0, writercount = 0;

void* reader(void* param)
{
    if (writercount > 0) {
        sem_wait(x);
        readercount++;
        if (readercount == 1) {
            sem_wait(y);
        }
        sem_post(x);
        printf("%d Follower is inside\n",readercount);
        //read all the files in the directory
        sleep(3);
        sem_wait(x);
        readercount--;
        if (readercount == 0) {
            sem_post(y);
        }
        sem_post(x);
        printf("%d Follower is leaving\n",readercount+1);
    } else {
        printf("Nothing to view\n");
    }
    return NULL;
}

int main()
{
    int a,i = 0,b;
    x = sem_open("/semaphore_x", O_RDWR);
    y = sem_open("/semaphore_y", O_RDWR);

    if( x == NULL || y == NULL) {
        printf("Unable to open named semaphores\n");
        exit(-1);
    }

    while (1) {
        printf("Enter 1 to View  / 3 to Exit:");
        scanf("%d",&b);
        if (b == 1) {
            pthread_create(&readerthreads[i],NULL,reader,NULL);
        } else {
            sem_close(x);
            sem_close(y);
            exit(0);
        }
        pthread_join(readerthreads[i],NULL);
        i++;
    }

}