私はクライアントプログラムでサーバーを実装しています。通信は共有メモリと連動します。共通のリソースへのアクセスを制御するために、私はセマフォを使用しています。クライアントがサーバーに対して新規である場合、サーバーはクライアントのIDを生成します。クライアントはIDを保存し、それ以降のリクエストでこのIDを送信します。 私はサーバーとクライアント間の通信に次のコードを使用しています。このソリューションは、1つのサーバーと1つのクライアントに対してのみ機能します。どのように3つのセマフォを使用するサーバークライアントアーキテクチャの共有メモリ
サーバー:
#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);
サーバが動作するはずです/ 1クライアント、より多くを管理します。私はこれを3番目のセマフォで解決できると思ったが、デッドロックの問題に遭遇しているか、クライアントが他のクライアントの結果を処理している。
第三セマフォと私のソリューションは、次のようになります。
サーバー:
クライアントsem_wait(clients); sem_wait(client); sem_post(server);
:
sem_wait(s1); sem_post(clients); sem_post(server);
がどのように私はこの問題を解決することができますか?