클라이언트 프로그램이있는 서버를 구현하고 있습니다. 통신은 공유 메모리와 작동합니다. 공통 리소스에 대한 액세스를 제어하기 위해 세마포를 사용합니다. 클라이언트가 서버를 처음 사용하는 경우 서버는 클라이언트에 대한 ID를 생성합니다. 클라이언트는 ID를 저장하고 이후 요청에서이 ID를 보냅니다. 서버와 클라이언트 간의 통신을 위해 다음 코드를 사용하고 있습니다. 이 솔루션은 한 서버와 한 클라이언트에서만 작동합니다.서버 클라이언트 용 세마포어 사용 방법 공유 메모리가있는
서버 :
#include <semaphore.h>
sem_t* server = sem_open(SEM_1, O_CREAT | O_EXCL, 0600, 1);
sem_t* client = sem_open(SEM_2, O_CREAT | O_EXCL, 0600, 0);
//other stuff
while (!want_quit)
{
sem_wait(client)
//get id of client from shared memory or generate id for client
//get the command from the client via shared memory
//process command
//write result back to shared memory
sem_post(server)
}
클라이언트 :
가#include <semaphore.h>
sem_t* s1 = sem_open(SEM_1, 0);
sem_t* s2 = sem_open(SEM_2, 0);
do
{
//wait for the server
sem_wait(s1);
//get result from last command from shared memory
//send new request to server by writing command into shared memory
sem_post(s2);
} while (command from shm != CLOSE);
서버는/작동 두 개 이상의 클라이언트를 관리해야합니다. 나는 세 번째 세마포어로이 문제를 해결할 수 있다고 생각했지만 교착 상태 문제가 발생하거나 클라이언트가 다른 클라이언트의 결과를 처리합니다.
세 번째 세마포를 내 솔루션은 다음과 같습니다
서버 :
클라이언트sem_wait(clients); sem_wait(client); sem_post(server);
:
sem_wait(s1); sem_post(clients); sem_post(server);
가 어떻게이 문제를 해결할 수 있습니까?