FIFO服务器未响应客户端???

时间:2018-10-05 06:45:37

标签: c mkfifo

服务器首先以帧为单位询问客户端数量和初始内存大小。

客户端仅一次与服务器联系!客户端通过直接进入服务器的通用FIFO(我们称为FIFO_to_server)向服务器发送内存请求和作业名称。至少使用3个客户端。

在为所有适合的客户端分配了内存之后,在服务器端显示已分配内存的“映射”。

系统中至少应包含三个客户端。每个客户: 1.提示用户输入作业名称和存储请求。 2.通过所有客户端使用的公共FIFO将每个客户端的名称和内存大小发送到服务器。 3.客户端终止。 4.注意:client pro程序将是运行3次的一个程序,因此,您将打开运行该客户端程序的多个窗口。客户的最小数量必须为3,最大数量可以选择。您将仅使用一次由服务器创建和打开的公共FIFO。 服务器: 1.提示并接收一个整数,该整数确定客户端数量(从服务器上的键盘,这将是至少3个),并且以256字节帧的数量表示总内存。 2.通过公共FIFO接收每个客户端的作业名称和内存请求。 3.充当内存管理器,将页面分配给客户端框架。 4.计算每个客户最终框架中的内部碎片。 5.所有客户端都完成后,服务器将关闭。

即使从客户端成功发送了数据,我的服务器也无法将结果发送回客户端。 到目前为止,这是我的代码:

//client.c
#include <errno.h>
#include <fcntl.h>

struct values{
 char privateFIFO[14];
 char jobName[14];
 int memRequest;
 int fragmentation;

 int page;


 }input;

 int main ()
 {
   int fda;// write to server
   int fdb; // read to server
   char temp[14];
   int clientID;



    clientID = getpid();

    strcpy(input.privateFIFO, "FIFO_"); //get name of private
    sprintf(temp, "%d", clientID);
    strcat(input.privateFIFO, temp);
    printf("\nFIFO name is: %s", input.privateFIFO);

    printf("Enter the name of the job name: ");
    scanf("%s", &input.jobName);
    printf("\nJob name is: %s\n", input.jobName);

    printf("\nEnter the amount of memory memory request: \n");
    printf("The amount MUST NOT be greater than 4096 or less than 0:   ");
    scanf("%d", &input.memRequest);
    printf("Amount of memory requested:  %d\n", input.memRequest);

    while(input.memRequest <= 0 || input.memRequest > 4096) //Send a Error 
     message if the request is invalid
    {
      printf("\nINVALID REQUEST!!!.\n");
       printf("The amount of request MUST NOT be greater than 4096 or less than 0:   ");
    scanf("%d", &input.memRequest);
}


if ((mkfifo(input.privateFIFO,0666)<0 && errno != EEXIST))
{
   perror("cant create FIFO_to_client");
   exit(-1);
}

if((fda=open("commonFIFO", O_WRONLY))<0)
    printf("can't open fifo to write.");

write(fda, &input, sizeof(input));
printf("\nClient: The Job Name and Memory Request have been sent to the Server and Waiting for the response: ");

if((fdb=open(input.privateFIFO, O_RDONLY))<0)
    printf("\n can’t open FIFO to read.");

read(fdb, &input, sizeof(input));
printf("\nClient: recieved response from Server");
printf("\nThe job needes %d number of frames\n", input.page);
printf("\nThe fragmentation is %d\n", input.fragmentation);
close(fda);
close(fdb);
unlink(input.privateFIFO);
return 0;
 }

服务器:

// server.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

struct values{
char privateFIFO[14];
char jobName[14];
int memRequest;
int fragmentation;
int page;

};

int main ()
{
  int fda;  // read from client
  int fdb;  // write to client
  //int finish; // lets me know that client is done

 char temp[14]; // buffer hold a character
 int clientNum;
int i; // loop counter
int j; // loop counter
int holder = 0;
int frameNum[16];
int frames[16];
int fragmentation = 0;
int counter = 0;
int page = 0;

//reads the number of clients

printf("\nAssume that in the main memory contain 16 frames\n");
printf("Each frame has 256 bit\n");
printf("How many clients [Min: 3]: ");
scanf("%d", &clientNum);    

struct values input[clientNum];

for (i = 0; i < 16; i++)
{
    frameNum[i] = 0;
    frames[i] = 256;
}


if ((mkfifo("commonFIFO", 0666)<0 && errno !=EEXIST))
{
    perror("can’t create commonFIFO\n");
    exit(-1);
}

if((fda=open("commonFIFO", O_RDONLY))<0)
    printf("can't open fifo to write.");


for(i=0; i<clientNum; i++) // how many job request form client
{
    /* Create the fifos and open them  */
    printf("hello");
    read(fda, &input[i], sizeof(input));
    printf("\nServer: FIFO name is: %s\n", input[i].privateFIFO);

    holder = input[i].memRequest;

    if (holder <= 256)
    {
        frames[counter] = holder;
        fragmentation = 256 - holder;
        frameNum[counter] = counter + 1;
        page = frameNum[counter];
        counter++;
        printf("\nThe fragmentation is %d\n", fragmentation);
        printf("\nThe memory request %d frame(s) in the main memory", page);
    }
    else
    {
        frames[counter] = 256;
        counter =  holder / 256 + 1;
        fragmentation = 256* counter - holder;
        frameNum[counter] = counter;
        page = frameNum[counter];
        printf("\nThe fragmentation is %d\n", fragmentation);
        printf("\nThe memory request %d frame(s) in the main memory\n", page);
        counter++;
    }
    input[i].page = page;
    input[i].fragmentation = fragmentation;

}
for(i=0; i<clientNum; i++) // how many job request form client
{
if ((fdb=open(input[i].privateFIFO,O_WRONLY))<0)
    printf("\n Can't open to read. ");

write(fdb, &input[i], sizeof(input[i]));

close (fdb);


}
printf("\nI am Done! Closing Server!\n");
close(fda);

unlink("commonFIFO");
return 0;

   }

客户端输出:

-bash-3.2$ ./client2

 FIFO name is: FIFO_3794Enter the name of the job name: job1

 Job name is: job1

 Enter the amount of memory memory request:
 The amount MUST NOT be greater than 4096 or less than 0:   489
 Amount of memory requested:  489

 Client: The Job Name and Memory Request have been sent to the Server and 
 Waiting for the response:

服务器:

-bash-3.2$ ./server2

 Assume that in the main memory contain 16 frames
 Each frame has 256 bit
 How many clients [Min: 3]: 3

 Server: FIFO name is: FIFO_3794

 The fragmentation is 23

 The memory request 2 frames in the main memory

服务器未将最终结果发送回客户端!!!!

0 个答案:

没有答案