I am currently writing a program to calculate the Lucas Series, Hexagonal Series, and Harmonic Series given a number from an input file. I am using a pipe and shared memory segment to share values across the programs.
I start by creating my pipe
int p[2];
if (pipe(p) < 0)
fprintf(stderr, "Problem opening pipe!\n");
Then I run a child process to read the contents of the input file into my pipe where argv[1] is the name of the input file(maxPrime is used later but not here)
char tmpbuf[10];
char str[10];
sprintf(tmpbuf, "%d", p[1]);
pid = fork();
int maxPrime;
if (pid < 0) fprintf(stderr, "Error running child process\n");
else if (pid == 0) { // child
close(p[0]);
execlp("./Reader", "reader", argv[1], tmpbuf, NULL);
} else {
wait(&status);
close(p[1]);
read(p[0], str, 10);
printf("[Starter][%ld]: contents read from the read end pipe: %d\n", (long)prPid, atoi(str));
n = atoi(str);
}
And finally, here is where I get the Bus error when trying to access a void pointer array called voidarr. This part of the program is where I am forking to the child processes to get the sums of the numbers in their respective series and print them to the standard output.
shms is an array of ints for file descriptors, voidarr is an array of void * for pointing to the shared memory, and pidArray is for storing the process ID of each child process
char *my_array[3] = {"lucas", "hexagonalseries", "harmonicseries"};
char *my_array2[3] = {"./lucas", "./hexagonalseries", "./harmonicseries"};
char *my_array3[3] = {"SHM_lucas", "SHM_hexagonalseries", "SHM_harmonicseries"};
char *my_array4[3] = {"Lucas.c", "Hexagonalseries.c", "Harmonicseries.c"};
for (int i = 0; i < 3; i++) {
if ((shms[i] = shm_open(my_array3[i], O_CREAT | O_RDWR, 0666)) == -1) {
fprintf(stderr, "Failed to open file descriptor!\n");
return 1;
}
if ((voidarr[i] = mmap(NULL, 32, PROT_WRITE, MAP_SHARED, shms[i], 0)) == MAP_FAILED){
fprintf(stderr, "Failed to map!\n");
return 1;
}
pidArray[i] = fork();
if (pidArray[i] < 0) {
fprintf(stderr, "Error running child process %s", my_array[i]);
} else if (pidArray[i] == 0) {
execlp(my_array2[i], my_array4[i], my_array3[i], (char *) voidarr[i], NULL);
} else { // parent
//RECEIVING BUS ERROR ON LINE BELOW
printf("[Starter]: %d and %s]\n", prPid, (char *)voidarr[i]);
shm_unlink(my_array3[i]);
}
}
This is what my output is
[Starter][1106394] : Created Shared memory "SHM_lucas" with FD: 3
[Starter][1106394] : Created Shared memory "SHM_hexagonalseries"
with FD: 5
[Starter][1106394] : Created Shared memory "SHM_harmonicseries"
with FD: 6
[Starter][1106394]: contents read from the read end pipe: 0
Bus error (core dumped)
Additionally, I don't know if this is needed to answer this question but here is my Reader.c
int main (int argc, char *argv[]) {
if (argc != 3) fprintf(stderr, "[Reader]: Wrong number of arguments, usage requires 2, found %d", argc - 1);
int pipe_ref = atoi(argv[2]);
FILE *f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "File failed to open");
return 1;
}
char buf[256];
int run_sum = 0;
int tmp = 0;
printf("Hello from reader");
while (fgets(buf, sizeof(buf), f)) {
tmp = atoi(buf);
run_sum += tmp;
}
sprintf(buf, "%d", run_sum);
write (pipe_ref, buf, sizeof(buf));
return 0;
}
Your problem is that after doing
shm_open, doingfstaton the return value shows anst_sizevalue of 0!Initially, the descriptor from
shm_openhas zero size. It must be explicitly given a size. Note that just specifying 32 as a length tommapdoes not cause the file to be expanded.This must be done with
ftruncate.After the
shm_opencall, you need to do:Here is a patch against your github repo. Even without that, it shows the changes needed:
Here is the output of the modified program: