晚上好,我需要创建一个程序,将要求3段名称,每个段将要求一个值。我的总记忆是2000单元。我需要使用结构体和fifo在客户端(它将从用户获得输入)和服务器(它将处理总单元)之间进行通信。客户端将发送回每个段的起始地址和结束地址,以及任何剩余的内存。
我的问题一旦我运行是,我得到一个分割错误。我真的不知道我可能在哪里覆盖了内存或者我是否在某个时候重置了内存。欢迎提出任何意见或建议,谢谢。
客户机代码:#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>
// creating the struct for the program
struct probsiz{
int seg1 [1]; //segment 1 size
int seg2 [1]; //segment 2 size
int seg3 [1]; //segment 3 size
int segsum [7]; //sum for all the segments
int leftov [1]; //memory leftovers
} ;
struct probseg {
char segment[64]; //segments
};
main (void)
{
struct probsiz numbs; //defining the structures
struct probseg names;
int fda; // to write to character server
int fdb; // to read response from character server
//setting up the memory
memset(numbs.seg1, 0, sizeof(struct probsiz)); //setting up memory for the first segment
memset(numbs.seg2, 0, sizeof(struct probsiz)); //setting up memory for the second segment
memset(numbs.seg3, 0, sizeof(struct probsiz)); //setting up memory for the third segment
memset(numbs.segsum, 0, sizeof(struct probsiz));// setting up memory for the sum of all segments
memset(numbs.leftov, 0, sizeof(struct probsiz)); //setting up memory for the first segment
memset(names.segment, 0, sizeof (struct probseg));// setting up memory for the segments
//reading the requested memory and segment name from the user
printf("Client: Please enter requested memory 1: ");
scanf("%d", &numbs.seg1[0]);
while (numbs.seg1 <=0){
printf("Client: please enter a valid request: ");
scanf("%d", &numbs.seg1[0]);
}
printf("Client: Please enter segment 1 name: ");
scanf("%s", names.segment[0]);
printf("Client: Please enter requested memory 2: ");
scanf("%d", &numbs.seg2[0]);
while (numbs.seg1 <=0){
printf("Client: please enter a valid request: ");
scanf("%d", &numbs.seg2[0]);
}
printf("Client: Please enter segment 2 name: ");
scanf("%s", names.segment[1]);
printf("Client: Please enter requested memory 3: ");
scanf("%d", &numbs.seg3[0]);
while (numbs.seg3 <=0){
printf("Client: please enter a valid request: ");
scanf("%d", &numbs.seg3[0]);
}
printf("Client: Please enter segment 3 name: ");
scanf("%s", names.segment[2]);
//send and write into the fifos
printf("nClient: Got the sizes sent now waiting for server's responsen");
write(fda, &numbs, sizeof(struct probsiz));
write(fda, &names, sizeof(struct probseg));
//read from the fifos
read(fdb, &numbs, sizeof(struct probsiz));
if (numbs.leftov[0] >=0) {
printf("nClient: address for segment 1 is: %d - %d", numbs.segsum[0], numbs.segsum[1]);
printf("nClient: address for segment 2 is: %d - %d", numbs.segsum[2], numbs.segsum[3]);
printf("nClient: address for segment 3 is: %d - %d", numbs.segsum[4], numbs.segsum[5]);
printf("nClient: leftover memory is: %d", numbs.leftov[0]);
printf("nall done!");
}
else
{
printf("nClient: segment size is over the capacity, please try again");
printf("nall done!n");
}
//this closes the fifos
close(fda);
close(fdb);
printf ("nall done!n");
}
服务器代码:#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>
// creating the struct for the program
struct probsiz{
int seg1 [1]; //segment 1 size
int seg2 [1]; //segment 2 size
int seg3 [1]; //segment 3 size
int segsum [1]; //sum for all the segments
int leftov [1]; //memory leftovers
} ;
struct probseg {
char segment[64]; //for the segments
};
main (void)
{
struct probsiz numbs; // structure definitions
struct probseg names;
int fda; // to read from client char
int fdb; // to write to client char
int finish; // lets me know that client is done
int i; // because C needs this defined as int
int m=2000; //total memory units
int mtot; //memory total from the sum of all the segments
//setting up the memory
memset(numbs.seg1, 0, sizeof(struct probsiz)); //setting up memory for the first segment
memset(numbs.seg2, 0, sizeof(struct probsiz)); //setting up memory for the second segment
memset(numbs.seg3, 0, sizeof(struct probsiz)); //setting up memory for the third segment
memset(numbs.segsum, 0, sizeof(struct probsiz));// setting up memory for the sum of all segments
memset(numbs.leftov, 0, sizeof(struct probsiz)); //setting up memory for the first segment
memset(names.segment, 0, sizeof (struct probseg));// setting up memory for the segments
/* Create the fifos and open them */
if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
{
perror("cant create FIFO1");
exit(-1);
}
if ((mkfifo("FIFO2",0666)<0 && errno != EEXIST))
{
perror("cant create FIFO2");
exit(-1);
}
if((fda=open("FIFO1", O_RDONLY))<0)
printf("cant open fifo to write");
if((fdb=open("FIFO2", O_WRONLY))<0)
printf("cant open fifo to read");
read(fda, &numbs, sizeof(struct probsiz)); //read the sizes
read(fda, &names, sizeof(struct probseg)); //read the segments
//printing out the characters on the server side to validate the data in
strcpy(names.segment, names.segment);
mtot=numbs.seg1[0]+numbs.seg2[0]+numbs.seg3[0];
numbs.leftov[0]=m-mtot;
printf("Server: just got segment 1: %s", names.segment[0]);
printf("Server: just got segment 1 size: %d", numbs.seg1[0]);
printf("Server: just got segment 2: %s", names.segment[0]);
printf("Server: just got segment 2 size: %d", numbs.seg2[0]);
printf("Server: just got segment 3: %s", names.segment[0]);
printf("Server: just got segment 3 size: %d", numbs.seg3[0]);
//calculation of memory addresses
numbs.segsum[0]=0;
numbs.segsum[1]= numbs.seg1[0]-1;
numbs.segsum[2]= numbs.seg1[0];
numbs.segsum[3]= numbs.segsum[2]+numbs.seg2[0]-1;
numbs.segsum[4]= numbs.segsum[3]+1;
numbs.segsum[5]= numbs.segsum[4]+numbs.seg3[0];
numbs.segsum[6]=0;
write(fdb, &numbs, sizeof(struct probsiz));
if (numbs.leftov[0] >=0) {
printf("nClient: address for segment 1 is: %d - %d", numbs.segsum[0], numbs.segsum[1]);
printf("nClient: address for segment 2 is: %d - %d", numbs.segsum[2], numbs.segsum[3]);
printf("nClient: address for segment 3 is: %d - %d", numbs.segsum[4], numbs.segsum[5]);
printf("nClient: leftover memory is: %d", numbs.leftov[0]);
printf("nall done!");
}
else
{
printf("nClient: segment size is over the capacity, please try again");
printf("nall done!n");
}
if(finish == 1)
printf("nServer: This says I am ready to close ");
close(fda);
close(fdb);
unlink("FIFO1");
unlink("FIFO2");
}
不确定这是唯一的问题,但您的memset可能会导致分段错误。
您已经定义了:
struct probsiz{
int seg1 [1]; //segment 1 size size = sizeof(int)
int seg2 [1]; //segment 2 size size = sizeof(int)
int seg3 [1]; //segment 3 size size = sizeof(int)
int segsum [7]; //sum for all the segments size = sizeof(int*7)
int leftov [1]; //memory leftovers size = sizeof(int)
} ;
probsiz的总大小为sizeof(int * 11)
现在让我们看看这一行,例如:
memset(numbs.leftov, 0, sizeof(struct probsiz)); //setting up memory for the first segment
你在告诉计算机从nums开始填充0。剩余到整个结构体的长度
希望我有所帮助