家庭作业 - 一台服务器通过信号量和共享内存为多个客户端提供服务

发布于 2024-10-14 02:19:24 字数 3106 浏览 3 评论 0原文

大家好,我还有另一个作业问题。我必须编写一个客户端和一个服务器程序,以便服务器(具有共享内存和信号量)可以与客户端进行通信。客户端从标准输入获取数据并将其发送到服务器,服务器对其进行排序并将其发送回。问题是服务器必须为多个客户端提供服务,而我编写了它,因此它只能为一个客户端提供服务。如果有人能给我启发如何实现这一目标,那就太好了。

这是我的客户端:

客户端

和服务器:

#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;
}

Hello everyone here am I with another homework issue. I have to write a Client and a Server program so that the Server (with 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-10-21 02:19:24

首先,您需要阅读有关信号量和互斥量的信息,它们将在多线程应用程序中使用,因此我认为您需要创建一个为多个客户端提供服务但使用线程等信号量的服务器。

您需要了解自己如何以及为什么信号量+互斥体工作以及它们的差异,稍后您会发现很多有关使用多线程的服务器的示例,并且您将被指导如何修改当前的服务器。

小教程:
http://www.csc.villanova.edu/~mdamian/threads/posixsem .html

和 2 个示例:
http://www.minek.com/files/unix_examples/semab.html
http://refactormycode.com/codes/1237-using-semaphores-in-c-and-forking-processes

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文