Good evening, I need to create a program that will ask for 3 segment names, each segment will ask for a value. my total memory is 2000 units. I need to use structs and fifos to communicate between a client (which will get the input from the user) and a server (which will process the total units). the client will send back the beginning and ending addresses for each segment, as well as any leftover memory.
my issue once I run is that I'm getting a segmentation fault. I can't really see where I might be overwriting the memory or if I'm resetting the memory at some point. any observations or suggestions are welcome, thanks.
client code:
#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 response\n");
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");
}
server code:
#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");
}
Not sure it is the only issue, but your memset is probably causing the segmentation fault.
You have defined:
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)
} ;
total size of probsiz is sizeof(int * 11)
Now lets look at this line for example:
memset(numbs.leftov, 0, sizeof(struct probsiz)); //setting up memory for the first segment
You are telling the computer to fill 0's starting at numbs.leftov to the length of the entire struct.
Hope I helped