I'm a beginner on C and I have to do an exercise using fork and pipes
I have to create a father process which fill in a pipe (with keyboard: stdin) and a child process which have to read the pipe and print it on the screen.
But the execution of this code bring me a "segmentation fault core dump" I have already programmed on Java and this is the equivalent of a "NullpoinerException" right ? I'm looking for some help, because i'm on this problem since 2 hours Thank you
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
char* p;
char* buffer;
int fd[2];
pid_t pid = fork();
pipe(fd);
//Cas du fils consommateur (Lire dans la pipe, et afficher le contenu a l'ecran )
if (pid == 0)
{
close(fd[1]);
int a;
do
{
a = read(fd[0], buffer, 512);
//printf("consomateur: %s", buffer);
} while (a != 0);
//Cas du père producteur (remplir la pipe par saisie au clavier)
}
else
{
close(fd[0]);
char *adString;
printf("Enter data \n");
do
{
adString = fgets(buffer, 20, stdin); // on recupere dans un buffer les donneés lues
write(fd[1], buffer, 512); // on met dans la pipe ce qu'on a mis dans le buffer
} while (strcmp(adString, "") != 0);
}
return 0;
}