c - Shared Memory and semaphores for (multiple) client and server game -


i have program little game course in c , has done using shared-memory, semaphores , client/server architecture can handle multiple clients (the exact requirement game 2).

the 2 clients need turns in turns , represented same program (no fork() involved here - both started ./client)

the server has create resources when starts up. main problem regarding semaphores. (the shared memory , game-logic stuff works or isn't difficult implement.)

to decide if server or client has access shared-memory need 1 semaphore. need second 1 decide of clients has access. right?

so got hint done assigning ids clients. shared-memory has 3 additional variables so:

struct game {     int id_needed, client_id, id_ready;     ... // additional stuff needed game logic }; 

as server boots i'm initializing 1 semaphore 0 , other 1 1. when first client appears checks if id still 0 (it's initialized zero)

if so, tries this:

while(my_id == 0) {    if(semaphore_down(semaphore_1) == 0) // check if have access shared mem    {        shared_memory->id_needed = 1;        if(shared_memory->id_ready == 1)        {            my_id = shared_memory->client_id;            (void) printf("debugging: id %d\n", my_id);        }    } } shared_memory->id_needed = 0; 

and in server ...

while(1) {     if(shared_memory->id_needed = 1)     {         (void) printf("debugging: id needed client!");         shared_memory->client_id++;         shared_memory->id_ready = 1;         (void) printf("debbuging: dispatched new id: %d", shared_memory->client_id);     }     // if enough players, start game ... } 

i'm stuck here. server increments id (which logical), i'm stuck resolve problem.

i want clients work alternately on shared-memory , server check values of game etc.

i've never worked semaphores before , documentation or examples find work 1 client , 1 server, never multiple clients.

please enlighten me!

i see 1 strange thing , 2 things mistakes here

  1. i see semaphore_down no semaphore_up in code showed
  2. you assign instead of comparing: if(shared_memory->id_needed = 1)
  3. even if comparison, not right anyway since compiler free optimize out. make variable volatile hint compiler variable can change outside of serial code flow. or better declare atomic.

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -