Commit 4c224831 authored by Sander van Veen's avatar Sander van Veen

Added example input and improved fishbones.

parent 7f7b1fe8
f a b f c d P e g P x x q
...@@ -67,17 +67,16 @@ static void gup(FILE * log1, FILE * log2, int pipefd[2], int cpid) ...@@ -67,17 +67,16 @@ static void gup(FILE * log1, FILE * log2, int pipefd[2], int cpid)
fclose(log3); fclose(log3);
} }
#define STRING_LENGTH (512) #define STRING_LENGTH (512)
int int main(int argc, char ** argv)
main(void)
{ {
FILE * log1; FILE * log1;
FILE * log2; FILE * log2;
char buf; char buf;
int kiddoCount = 0; int kids = 0;
int pipefd[2]; int pipefd[2];
log1 = fopen("child.log1", "wt+"); log1 = fopen("child.log1", "wt+");
...@@ -89,8 +88,6 @@ main(void) ...@@ -89,8 +88,6 @@ main(void)
// create the pipe somewhere around here (why?). // create the pipe somewhere around here (why?).
// remember to test for success!! // remember to test for success!!
printf("while..\n");
if( pipe(pipefd) == -1 ) if( pipe(pipefd) == -1 )
{ {
perror("pipe"); perror("pipe");
...@@ -99,7 +96,6 @@ main(void) ...@@ -99,7 +96,6 @@ main(void)
pid_t cpid; pid_t cpid;
while( (buf = getchar()) ) while( (buf = getchar()) )
{ {
// You can now choose to use only the first character in the // You can now choose to use only the first character in the
...@@ -109,6 +105,7 @@ main(void) ...@@ -109,6 +105,7 @@ main(void)
// and write it into the pipe (unless you have to call fork) // and write it into the pipe (unless you have to call fork)
printf("%c", buf); printf("%c", buf);
fflush(stdout);
switch( buf ) switch( buf )
{ {
...@@ -118,7 +115,7 @@ main(void) ...@@ -118,7 +115,7 @@ main(void)
case 'f': case 'f':
kiddoCount++; kids++;
// When you fork, be sure to print the process id of the child // When you fork, be sure to print the process id of the child
// (if the fork succeeds) and an error message otherwise // (if the fork succeeds) and an error message otherwise
...@@ -134,25 +131,19 @@ main(void) ...@@ -134,25 +131,19 @@ main(void)
if( cpid == 0 ) if( cpid == 0 )
{ {
// Child reads from pipe u1 // Child reads from pipe u1
gup(log1, log2, pipefd, kids);
gup(log1, log2, pipefd, kiddoCount);
_exit(EXIT_SUCCESS); _exit(EXIT_SUCCESS);
} }
else else
{ {
/* Parent writes argv[1] to pipe */ /* Parent writes to pipe */
//write(pipefd[1], buf, len);
// Close unused read end // Close unused read end
close(pipefd[0]); close(pipefd[0]);
//if( write(pipefd[1], argv[1], strlen(argv[1])) == -1 )
//{
// fprintf(stderr, "write error: %s\n", strerror(errno));
// exit(EXIT_FAILURE);
//}
close(pipefd[1]); /* Reader will see EOF */ close(pipefd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */ wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment