'Whenever I run this code, it says Segmentation Fault! Does anyone have any suggestions?

So the task is to adapt the strategy to develop two applications so when the client sends messages with shared memory, the server application can log the message to log file. You should append the new message to the log file with a new line at end of the file, so the log file will grow as more messages are logged into it. the starting code is in the logger.zip file. You need to use additional variables, file pointers, and logarithms needed to make the program work. code:

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
        
int main(void) {
        
    size_t bytesWritten = 0;
    int   my_offset = 0;
    char  *text1Ý="Data for file 1.";
    char  *text2Ý="Data for file 2.";
    int fd1,fd2;
    int PageSize;
    void *address;
    void *address2;
    fd1 = open("/reading.txt",
               (O_CREAT | O_TRUNC | O_RDWR),
               (S_IRWXU | S_IRWXG | S_IRWXO) );
    if ( fd1 < 0 )
        perror("open() error");
    else {
        bytesWritten = write(fd1, text1Ý, strlen(text1Ý));
        if ( bytesWritten != strlen(text1Ý) ) {
            perror("write() error");
            int closeRC = close(fd1);
            return -1;
        }
        
        fd2 = open("/writing.txt",
                   (O_CREAT | O_TRUNC | O_RDWR),
                   (S_IRWXU | S_IRWXG | S_IRWXO) );
        if (fd2 < 0 )
            perror("open() error");
        else {
            bytesWritten = write(fd2, text2Ý, strlen(text2Ý));
            if ( bytesWritten != strlen(text2Ý) )
                perror("write() error");
        
            PageSize = (int)sysconf(_SC_PAGESIZE);
            if ( PageSize < 0) {
                perror("sysconf() error");
            }
            else {
        
                off_t lastoffset = lseek( fd1, PageSize-1, SEEK_SET);
                if (lastoffset < 0 ) {
                    perror("lseek() error");
                }
                else {
                    bytesWritten = write(fd1, " ", 1);
        
                    lastoffset = lseek( fd2, PageSize-1, SEEK_SET);
        
                    bytesWritten = write(fd2, " ", 1);
        
                    int len;
        
                    my_offset = 0;
                    len = PageSize;
                    address = mmap(NULL,
                                   len,
                                   PROT_READ,
                                   MAP_SHARED,
                                   fd1,
                                   my_offset );
                    if ( address != MAP_FAILED ) {
                        address2 = mmap( ((char*)address)+PageSize,
                                         len,
                                         PROT_READ,
                                         MAP_SHARED | MAP_FIXED, fd2,
                                         my_offset );
                        if ( address2 != MAP_FAILED ) {
        
                            printf("\n%s",address);
        
                            printf("\n%s",address2);
                        }
                        else {
                            perror("mmap() error=");
                        }
                    }
                    else {
                        perror("munmap() error=");
                    }
        
                    if ( munmap(address, 2*PageSize) < 0) {
                        perror("munmap() error");
                    }
                    else;
        
                }
            }
            close(fd2);
            unlink( "/tmp/mmaptest2");
        }
        close(fd1);
        unlink( "/tmp/mmaptest1");
    }
}



Solution 1:[1]

address and address2 don't have a null terminator so you need to specify the length when printing.

e.g. printf("%.*s", length, string);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fool