I am trying to share a file between parent and child processes. Parent sends the file via a pipe and child write that lines into shared memory so that parent can read and print out the file via shared memory. However, I am getting segmentation fault: 11. Besides, I did something similar like the code below, but that time I could not get the correct content and even I was getting different results at each call.
I am not sure about increasing the pointer part. But, it is better to have an extra eye on the code.
Edit: I corrected char* to char[] and segmentation fault is now gone. However, I get different results at each run, some extra characters are seen in output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#define SHM_NAME_1 "Child 1"
int main(){
pid_t pid;
FILE *file;
char *infile = "in.txt";
pid = fork();
if(pid < 0){
fprintf(stderr, "Fork failed\n");
return 1;
}
if(pid > 0){ // parent
file = fopen(infile, "r");
if(file == 0){
fprintf(stderr, "File failed\n");
return 1;
}
// close read end of pipe
mknod("FIFO", S_IFIFO | 0666, 0);
int fd = open("FIFO", O_WRONLY);
char str[300];
while(fgets(str, sizeof(str), file) > 0)
{
// write all lines of file
write(fd, str, strlen(str));
}
// close file and pipe
close(fd);
fclose(file);
// wait for child to write to shared memory
wait(NULL);
// open shared segment
int shm_first = shm_open(SHM_NAME_1, O_RDONLY, 0666);
if (shm_first == -1) {
fprintf(stderr, "Failed: Shared Memory 1");
exit(-1);
}
// create memory pointer
void *ptr = mmap(0,4096, PROT_READ, MAP_SHARED, shm_first, 0);
if (ptr == MAP_FAILED) {
printf("Map failed 1\n");
return -1;
}
// print out result and unlibk shared segment
fprintf(stdout, "Normal input: \n%s\n", ptr);
shm_unlink(SHM_NAME_1);
} else { // child
// create the shared segment for the first time
int shm_child_1 = shm_open(SHM_NAME_1, O_CREAT | O_RDWR, 0666);
// configure the size of the shared memory segment
ftruncate(shm_child_1,4096);
// map the pointer to the segment
void *ptr_child_1 = mmap(0,4096, PROT_READ | PROT_WRITE, MAP_SHARED, shm_child_1, 0);
if (ptr_child_1 == MAP_FAILED)
{
printf("Map failed in first child\n");
return -1;
}
mknod("FIFO", S_IFIFO | 0666, 0);
int fd = open("FIFO", O_RDONLY);
int num;
char s[300];
while((num = read(fd, s, sizeof(s)))> 0)
{
sprintf(ptr_child_1, "%s", s);
ptr_child_1 += num;
}
close(fd);
exit(0);
}
return 0;
}
One quick observation.
In the following code, you have a char pointer that is not initialized to point to anything. Which causes fgets to copy what it reads from file
to some arbitrary place in memory.
char *str;
while(fgets(str, 100, file) > 0)
Now that the buffer issues are resolved, there is also an issue with the braces in the following expression
while((num = read(fd, s, sizeof(s)) > 0))
num
is going to be 1 or 0 rather than the number of bytes read or 0 for eof. This should be
while((num = read(fd, s, sizeof(s))) > 0)
Once you have the number of bytes read, you need to zero terminate the buffer. Because you are using sprintf
which expects the argument for %s
to be a zero terminated string.
while((num = read(fd, s, sizeof(s)))> 0)
{
s[num] = '\0'; // Terminate the string to the number of bytes read
sprintf(ptr_child_1, "%s", s);
ptr_child_1 += num;
}