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;
}
openreturns-1the program should print an error withperrorand then exit.strcpywill overrun. Wouldn't it be safer to usememcpyinstead?wait(NULL)? Your process has no children at this point, so it's just going to return-1immediately (which you never check). What is this line meant to accomplish?