gpt4 book ai didi

客户端/服务器打印数组并写回

转载 作者:行者123 更新时间:2023-11-30 15:16:05 26 4
gpt4 key购买 nike

我正在使用 C 和 putty 编写客户端/服务器程序。两个 c 文件位于同一系统上。

我目前在向客户端写回其正在使用的框架以及打印我的框架时遇到问题。它打印出 3 0 9 8,但随后开始打印 13456756 等。

这是我所拥有的:

服务器:

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


main (void)
{
int to_server; // to read from client
int from_server; // to write to client
int finish; // lets me know that client is done
int i,j,k,m,l; // because C needs this defined as int
int numClient;// number of clients
char temp[14];
int page_size = 128;
int pages_left;
int max_frames=10;
int used_frames =0;
int frameUpdate=0;
int freeframe[10] = {3,0,9,8,7,5,1,4,2,6}; //this is the array
int numpage=0;
int frames;
int check;
int option;
int byte;
int getPage;
int getOffset;
int physical_Addr;
int offset;
int req[3];
int again;

struct values{
char privFifo[14];
int memoryreq;
}cinput;


/* Create the fifos and open them */
if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
{
perror("cant create FIFO1");
exit(-1);
}
if((to_server=open("FIFO1", O_RDONLY))<0){
printf("cant open fifo to write");
}

//get number of clients
printf("\nHow many clients?\n");
scanf("%d", &numClient);

for(j =1; j <= numClient; j++){
read(to_server, &cinput, sizeof(cinput));
printf("\n\nFifo_%d \nMemory request %d", &cinput.privFifo, cinput.memoryreq);
req[j-1] = cinput.memoryreq;
if((mkfifo(cinput.privFifo,0666)<0 && errno != EEXIST))
{
perror("cant create privFifo");
exit(-1);
}

if((from_server=open(cinput.privFifo, O_WRONLY)<0)){
printf("cant open fifo to write");
}

// find number of pages need for request
numpage = cinput.memoryreq/page_size;

if((numpage * page_size) < cinput.memoryreq){
numpage++;
}

sleep(1);
printf("\nPages needed %d", numpage);
write(from_server, &numpage, sizeof(numpage));

printf("\n******Main Memory******");
for(m = used_frames; m < numpage; m++){
printf("\n* client: %d\tframe: %d", j, freeframe[m]);
frames = freeframe[m];
write(from_server, &frames, sizeof(frames));
}

used_frames = max_frames - used_frames;
pages_left = max_frames - numpage;

//this is where I try to print out the available frames
printf("\n Frames available:");
for(l = pages_left; l!= 0; l--){
check = max_frames - l;
printf(" %d", freeframe[check]);
max_frames = check;
}
close(from_server);
unlink(cinput.privFifo);
}

printf("\nDONE!!!");
close(to_server);
unlink("FIFO1");

客户:

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

main (void)
{
int to_server; // to write to server
int from_server;
char temp[14]; // server puts string here
int clientID;
//int frames;
int numpage;
int i;

struct values{
char privFifo[14];
int memoryreq;
}cinput;

if((to_server=open("FIFO1", O_WRONLY))<0)
printf("cant open fifo to write\n");

printf("writing data to to_server\n");
printf("Client: Please enter number of memory units: ");
scanf("%d", &cinput.memoryreq);
printf("%d", cinput.memoryreq);
clientID = getpid();
sprintf(cinput.privFifo, "Fifo_%d", getpid());
printf("\nFifo name is %s", &cinput.privFifo);
write(to_server, &cinput, sizeof(cinput));//write client pid and memUnit to server
sleep(2); //give time to send
printf("\nClient: Got the character sent, now waiting for response ");

if ((mkfifo(cinput.privFifo,0666)<0 && errno != EEXIST))
{
perror("cant create FIFO1");
exit(-1);
}
if((from_server=open(cinput.privFifo, O_RDONLY))<0){
printf("cant open fifo to write");
}
read(from_server, &numpage, sizeof(numpage));
printf("\nFrames Occupied %d", numpage);

close(to_server);
close (from_server);
unlink(cinput.privFifo);

printf ("\nall done!\n");

}

非常感谢任何帮助。谢谢。

最佳答案

我强烈怀疑问题出在 used_frames = max_frames - used_frames; 行上。自 used_frames最初为 0,这将其设置为第二次迭代中超出数组末尾的值,因此当您运行 for(m = used_frames; m < numpage; m++) 时,您开始打印超出帧数组末尾的值。 。 (顺便说一句:请正确缩进。)。但可以设置断点并在调试器中运行以确保确定。

关于客户端/服务器打印数组并写回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33243969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com