Hey all. I don't want to be one of those that sounds like HW but they didn't say it was so I'll be honest, this is HW but I feel like I've done enough that it's reasonable to ask for a little help here. This is a snippet from a bigger assignment where we're writing a suite that communicates using a bunch of IPC methods and I've finished most of them. The code below is what I've written for the sockets section:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define BUFFER_SIZE 25
#define READ_END 0
#define WRITE_END 1
int main(void)
{
char write_msg[BUFFER_SIZE] = "Greetings";
char read_msg[BUFFER_SIZE];
pid_t pid;
int fd[2];
/** create the pipe */
if (pipe(fd) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
//printf("fd[0]=%d fd[1]=%d\n", fd[0], fd[1]);
/** now fork a child process */
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
if (pid > 0) { /* parent process */
/* close the unused end of the pipe */
close(fd[READ_END]);
/* write to the pipe */
write(fd[WRITE_END], write_msg, strlen(write_msg)+1);
/* close the write end of the pipe */
close(fd[WRITE_END]);
}
else { /* child process */
/* close the unused end of the pipe */
close(fd[WRITE_END]);
/* read from the pipe */
read(fd[READ_END], read_msg, BUFFER_SIZE);
printf("child read from parent: [%s]\n",read_msg);
/* close the write end of the pipe */
close(fd[READ_END]);
}
return 0;
}
The current code compiles and runs fine but all I've been able to get it to do is get the child to read from the parent. What I'd like to have it do is fork the child and then simply have the child read from parent(which i've done) and have the child reply to the parent if it gets the message it is expecting.
So, how do I make this child check the incoming message(the part I'm having the most trouble with) against a phrase and then reply to the parent with its own phrase?
I hope I've done enough work on the program that no one minds me asking for some hw help but if it's not cool just let me know and I'll drop the thread.
Namaste,
-Ray-