我有两个C文件:a.c
(主人)和b.c
(追随者)。
我想使用一个信号量在master和follower之间创建一个同步。
根据我所学到的,我需要一个全局POSIX信号量来完成这项工作。
我如何在b.c
(follower)文件中仅使用a.c
(master)中的信号量来实现这一点?
a.c
文件(master)的信号量实现:
#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 insiden",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 leavingn",readercount+1);
} else {
printf("Nothing to viewn");
}
return NULL;
}
void* writer(void* param)
{
printf("Master is trying to uploadn");
sem_wait(&y);
printf("Master is uploadingn");
//create file in a directory
sem_post(&y);
writercount++;
printf("Master is leavingn");
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
文件(follower)中的信号量:
#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 insiden",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 leavingn",readercount+1);
} else {
printf("Nothing to viewn");
}
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++;
}
}
有两个不同的进程,您必须使用命名信号量,如下所示:
在多个进程间共享POSIX信号量
所以你的代码可以像
这样编辑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 insiden",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 leavingn",readercount+1);
} else {
printf("Nothing to viewn");
}
return NULL;
}
void* writer(void* param)
{
printf("Master is trying to uploadn");
sem_wait(y);
printf("Master is uploadingn");
//create file in a directory
sem_post(y);
writercount++;
printf("Master is leavingn");
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 semaphoresn");
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 insiden",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 leavingn",readercount+1);
} else {
printf("Nothing to viewn");
}
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 semaphoresn");
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++;
}
}