开发者

Homework - One Server serves multiple Clients with Semaphores and Shared Memory

Hello everyone here am I with another homework issue. I have to write a Client and a Server program so that the Server (wit开发者_运维技巧h shared memory and semaphores) can communicate with the Client. A client gets data from the stdin sends it to the server, the server sorts it and sends it back. The problem is that the server has to serve multiple clients and I wrote it so it could serve only one. If someone could give me that mind push how to achieve that would be nice.

here are my client :

Client

and server:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <errno.h>

#define SEMKEY1 6666
#define SEMKEY2 7777
#define SHMKEY  8888
#define HSIZE    128

union semun {
  int val;                   /*value for SETVAL*/
  struct semid_ds *buf;      /*buffer for IPC_STAT, IPC_SET*/
  unsigned short int *array; /*array for getall, setall*/
  struct seminfo *__buf;     /*buffer for IPC_INFO*/
};

int P(int semid){
  struct sembuf occupy;
  int res;

  occupy.sem_num  =  0;
  occupy.sem_op   = -1;
  occupy.sem_flg  =  0;
  res = semop(semid, &occupy, 1);
  if(res < 0){
    fprintf(stderr,"P() Failed");
    exit(1);
  }
  return res;  
}

int V(int semid){
  struct sembuf release;
  int res;
  release.sem_num  =  0;
  release.sem_op   =  1;
  release.sem_flg  =  0;

  res = semop(semid, &release, 1);
  if(res < 0){
    fprintf(stderr,"V() failed");
    exit(1);
  }
  return res;  
}

int getSem(int key){
  int semid;
  int errno;

  if((semid = semget(key, 1, 0)) < 0){
    fprintf(stderr, "getSem failed for key %d because %d \n ", key, errno);
    exit(1);    
  }
  return semid;
}

/*STRUCKT die fuer die SHARED MEMORY benutzt wird*/
typedef struct srtelem {
  long elem;
  int flg;
}SMSTRCKT;

int long_comp(const void *a, const void *b){
  const int *ai = (const int *) a;
  const int *bi = (const int *) b;

  return *ai - *bi; 
}

void print_hangar(long *hangar, int i){
  int j;

  for(j = 0; j < i; j++){
    printf("%ld \n", *(hangar+j));
  }
}

int main(){
  int semid1, semid2, shm_id, count;
  int errno, index;
  SMSTRCKT *shmptr;
  long hangar[HSIZE];

  /*Semaphore hollen*/
  semid1 = getSem(SEMKEY1);
  semid2 = getSem(SEMKEY2);
  count = 0;
  index = 0;
  printf("\t**SERVER**\n");

  /*Shared memory anlegen*/
  if((shm_id = shmget(SHMKEY, sizeof(SMSTRCKT), 066)) < 0){
    fprintf(stderr, "SHMGET failed because of %d\n",errno);
    exit(1);  
  }

  /*Shared memory anhaengen*/
  if((shmptr = (SMSTRCKT *) shmat(shm_id, NULL, 0)) == (SMSTRCKT*) -1){
    fprintf(stderr,"SHMAT failed because of %d\n", errno);
    exit(1);    
  }

/*Get date from the Client*/
   while((shmptr->flg) == 1){
      P(semid2);
      if(shmptr->flg != 0){
    printf("elem %d \n ",(int) shmptr->elem);
    *(hangar+count) = shmptr->elem;
    count++;
      }  
      V(semid1);
    }
    qsort(hangar,count, sizeof(long),long_comp);

    /*Send the result to the Client*/
    while(index < count){
      P(semid1);

      shmptr->elem = *(hangar+index);
      /*printf(" elem %ld  index %d\n", shmptr->elem, index);*/
      ++index;

      V(semid2);
    }

    P(semid1);
    shmptr->flg++;
    V(semid2);

  return 0;
}


First, you need to read about semaphores and mutex, they are to use in multi-thread applications, so I suppose that you need to make a server that serve to multiple clients but using threads and so, semaphores.

You need to understand yourself how and why semaphores + mutex works and their differences, later that you will found a lot of examples about servers that works with multi-threads and you will be pointed about how to modify your current server.

Small tutorial: http://www.csc.villanova.edu/~mdamian/threads/posixsem.html

And 2 examples: http://www.minek.com/files/unix_examples/semab.html http://refactormycode.com/codes/1237-using-semaphores-in-c-and-forking-processes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜