2017-10-29 172 views
3

多个客户端正在向服务器发送其作业名称和内存请求。服务器充当内存管理器并为尽可能多的客户端分配内存,使用分页作为内存分配方案。我正在使用FIFO进行客户端 - 服务器通信。尝试在模拟的操作系统内存管理器中显示分配内存的“映射”

我遇到的问题是所有的客户端都经过处理后,我想在服务器端显示分配内存的映射。换句话说,我想展示什么已分配到什么客户端

下面是我的服务器应用程序的一部分。我还附上了一些可能有助于理解问题的输出。一切都按预期工作,直到程序结束(打印出分配给每个客户端的帧;最后的对于环路server.c)。 clientsAllocation数组是保存每个客户端的私有FIFO名称的数组。我试图将分配的帧的索引(在assignedFrames数组中)指定为客户端的privateFIFOName。我不知道为什么这不起作用。预先感谢您的任何帮助。

任何答案都应该是可移植的。我必须能够使用cygwin-gcc编译器在UNIX机器上运行此代码。我正在测试Windows上的代码,因为它是我的主要机器。我使用PuTTY连接到我的大学的UNIX机器,每更换一次代码,并确保代码也在那里运行。

server.c

...//include statements 

#define FRAMESIZE 256 
#define MAX_LENGTH_FIFO_NAME 16 
#define MAX_LENGTH_JOB_NAME 32 
#define MAX_LENGTH_MESSAGE 256 

int main(void) 
{ 
    int numOfClients = 0; //total number of clients this server will process 
    int totalNumOfFrames = 0; //total number of frames in memory 
    int frames = 0; //copy of numOfFrames used to allocate frames for client 

    //Struct to recieve from client 
    struct 
    { 
     char jobName[MAX_LENGTH_JOB_NAME]; 
     char privateFIFOName[MAX_LENGTH_FIFO_NAME]; 
     int memoryRequest; 
    }input; 

    //Struct to send to client containing the calculated frames and the fragmentation 
    struct 
    { 
     char message[MAX_LENGTH_MESSAGE]; 
     int fragmentation; 
     int totalNumOfFrames; 
     int frameNumbers[totalNumOfFrames]; 
    }output; 

    ... //getting input from user and doing error checking 

    int allocatedFrames[totalNumOfFrames]; //an array of "flags" that will keep track whether a frame is allocated or not 
    char* clientsAllocation[totalNumOfFrames]; //an array to keep track of what frames are allocated to what client 

    memset(allocatedFrames, 0, sizeof(allocatedFrames)); //make sure all values in the array are set to 0 to prevent random values 
    memset(clientsAllocation, 0, sizeof(clientsAllocation)); 

    int i = 0; 
    int j = 0; 

    for (i; i < numOfClients; i++) 
    { 
     if (input.memoryRequest >= FRAMESIZE && input.memoryRequest <= memoryLeft) 
     { 
      ... 
      frames = 0; 

      if (framesLeft >= numOfFrames) 
      { 
       j = 0; 
       while (frames < numOfFrames) 
       { 
        for (j; j < totalNumOfFrames; j++) 
        { 
         if (allocatedFrames[j] == 0) //if the value at j is 0, then this is an empty frame and can be allocated 
         { 
          allocatedFrames[j] = 1; //switch the value to 1 in both arrays 
          output.frameNumbers[j] = 1; 
          clientsAllocation[j] = input.privateFIFOName; //keep track of what frames this client was allocated 
          printf("%d: %s\n", j, clientsAllocation[j]); 
          printf("SERVER:> Frame Allocated: %d\n", j); 
          break; //breaks out of this 'for' loop which should only be run as many times as there are frames to be allocated 
         } 
        } 
        frames++; //increment the temporary frames variable to keep track of how many times to run the for loop 
       } 

       ... //calculations on framesLeft and memoryLeft 
      } 
      //if it is not a valid request, (i.e. requesting more frames than are available) 
      else 
      { 
       ... //some error printing 
      } 
     } 
     else if (...)... //range checking but the code is very similar to above 
    } 

    i = 0; 
    for (i; i < totalNumOfFrames; i++) 
    { 
     printf("%d: %s\n", i, clientsAllocation[i]); 
    } 
    printf("\n\n"); 

    return 0; 
} 

Output

+1

这是一个非常不相关的代码。请阅读[帮助页面](http://stackoverflow.com/help),尤其是名为[“我可以询问什么主题?”](http://stackoverflow.com/help/on-topic)和[“我应该避免问什么类型的问题?”](http://stackoverflow.com/help/dont-ask)。还请[参观](http://stackoverflow.com/tour)和[阅读关于如何提出好问题](http:// stackoverflow。COM /帮助/如何对问)。最后,请学习如何创建一个[** Minimal **,Complete和Verifiable示例](http://stackoverflow.com/help/mcve)。 –

+0

@Ehan,编写功能超过20行的程序员有一个特别的地狱。你似乎在里面。 – jwdonahue

+0

@Someprogrammerdude,谢谢你的提示。我编辑了我的问题,并拿出了许多不相关的代码。我希望更容易理解我遇到的问题是什么。 – Ethan

回答

1

你总是分配相同的缓存解决您的客户端名称的阵列目前使用的条目。

clientsAllocation[j] = input.privateFIFOName; 

通过在输入结构中提供char数组的名称/标识符来实现。
它被视为指向char的指针,并被分配给clientsAllocation中所有使用的条目。

所以在最后,在打印时,所有的指针指向的char
在同一个阵列,你会得到相同的名单,被写入到缓冲区中的最后一个。

为了避免这种情况,您可以为每个名称指定一些内存,并通过字符串复制填充当前名称。

当然,你应该在某个时候释放那些分配的内存。
对于简单的演示程序,打印后释放似乎是合适的。

+0

谢谢你的回答。在阅读C教科书中关于指针和操作字符串的章节后,我意识到发生了什么。我感谢帮助,并指引我朝着正确的方向前进! :) – Ethan