试图显示"地图"模拟OS内存管理器中分配的内存

时间:2017-10-29 04:17:24

标签: c memory memory-management mkfifo

多个客户端正在将其作业名称及其内存请求发送到服务器。服务器充当内存管理器,并使用分页作为其内存分配方案为尽可能多的客户端分配内存。我正在使用FIFO进行客户端 - 服务器通信。

我遇到的问题是在处理完所有客户端之后,我想在服务器端显示已分配内存的映射。换句话说,我想将 框架分配给 客户端

以下是我的服务器应用程序的一部分。我还附加了一些可能有助于理解问题的输出。一切都按预期工作,直到程序结束(打印出分配给每个客户端的帧; server.c 上的 循环>)。 clientsAllocation数组是保存每个客户端的私有FIFO名称的数组。我试图将分配帧的索引(在allocatedFrames数组中)分配给客户端的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 个答案:

答案 0 :(得分:1)

您始终将相同的缓冲区地址分配给您的客户端名称数组的当前使用的条目。

clientsAllocation

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

所以最后,在打印时,所有指针都指向var Item = <div>Some Text</div> <div>Another div</div> 的同一个数组 你得到一个相同名字的列表,是写入缓冲区的最后一个。

为避免这种情况,你可以为每个名称使用malloc()一些内存,并通过字符串复制将其填入当前名称。

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