1

This code performs the following: Reads the contents of the "read" text file and writes it into the shared memory space.The code was working until yesterday but the same code shows segmentation fault today. Can you help me figure out where I'd made a mistake?

#include<sys/ipc.h>
#define NULL 0
#include<sys/shm.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
#include<sys/wait.h> 
#include<ctype.h>
#include<fcntl.h>
#include<stdio_ext.h>

int main()
{

char *a;
char name[20];
int id,n;
char buf[50];
int fd;
fd=open("read",O_RDONLY);   
int s1=read(fd,&buf,50);

id=shmget(200,50,IPC_CREAT);

    a=shmat(id,NULL,0);
    strcpy(a,buf);
    wait(NULL);
    shmdt(a);

shmctl(id,IPC_RMID,NULL);
return 0;

 }
3
  • 4
    You will have more robust code and find it much easier to debug if you do error handling by validating the return values of all the function calls. For example, if open returns -1 the program should print an error with perror and then exit. Commented Feb 15, 2020 at 10:24
  • 1
    Are you sure that the data read from your file contains a terminating null byte? If not, your strcpy will overrun. Wouldn't it be safer to use memcpy instead? Commented Feb 15, 2020 at 13:39
  • 1
    Also, what's the point of wait(NULL)? Your process has no children at this point, so it's just going to return -1 immediately (which you never check). What is this line meant to accomplish? Commented Feb 15, 2020 at 13:42

1 Answer 1

1

You have to check the return value of every function you used in your code. The segmentation fault occurred when you call strcpy(a,buf);. If you check the value of a it does not have a valid memory address, that is because you never checked the value returned from shmat() call and you need to carefully check the parameters that this function is taking(man page).

Below, I commented where the seg fault occurs:

int main()
{
  //char name[20]; // never used
  int id; 

  char *a;
  char buf[50];
  int fd;
  fd=open("read",O_RDONLY); // check return value
  //make sure you provide the correct path for your "read" text file
  int restlt = read(fd,&buf,50); // check return value


  key_t mem_key = ftok(".", 'a');
  id = shmget(mem_key, 50, IPC_CREAT | 0666);
  //id = shmget(200,50,IPC_CREAT); //check the return value
  if (id < 0) {
      printf("Error occured during shmget() call\n");
      exit(1);
  }
  a = shmat(id,NULL,0); //check the return value
  if ((int) a == -1) {
       printf("Error occured during shmat() call\n");
       exit(1);
  }
  printf("Value of pointer a = %p \n", a);
  strcpy(a,buf); // better to use strncpy(a, buf, n);

  printf("Value of a[0]  = %c \n", *a);

  wait(NULL);
  shmdt(a);
  shmctl(id,IPC_RMID,NULL);

  return 0;

 }

Check out this link: http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/shm/shmat.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.