-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Processes by Kevin Brack #369
base: master
Are you sure you want to change the base?
Changes from all commits
f85e7e6
bbb8893
a7783f7
6d6fe03
4d7716e
a447184
686aa6c
8768a27
d74362f
e6cee20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.DS_Store | ||
examples | ||
a.out |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Greetings from parent: 10998 | ||
Greetings from child: 10999 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// Write a program that forks a child and creates a shared pipe | ||
// between the parent and child processes. Have the child write | ||
// the three messages to the parent and have the parent print out | ||
// the messages. | ||
// between the parent and child processes. Have the child write | ||
// the three messages to the parent and have the parent print out | ||
// the messages. | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> | ||
|
@@ -10,13 +10,46 @@ | |
|
||
#define MSGSIZE 16 | ||
|
||
char* msg1 = "hello world #1"; | ||
char* msg2 = "hello world #2"; | ||
char* msg3 = "hello world #3"; | ||
char *msg1 = "hello world #1"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you changed the spacing here, good. It's funny how something as small as spacing can give you clues about who the author of the code was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh. I love to use Prettier and format-on-save. As a highly visual learner it gives me a holistic impression of what good code should look like in each language. |
||
char *msg2 = "hello world #2"; | ||
char *msg3 = "hello world #3"; | ||
|
||
int main(void) | ||
{ | ||
// Your code here | ||
|
||
char inbuf[MSGSIZE]; | ||
int p[2]; | ||
if (pipe(p) < 0) | ||
{ | ||
fprintf(stderr, "pipe failde\n"); | ||
exit(1); | ||
} | ||
int f = fork(); | ||
if (f < 0) | ||
{ | ||
printf("%s\n", "Fork faliled"); | ||
exit(1); | ||
} | ||
else if (f == 0) | ||
{ | ||
// we are in the child branch | ||
close(p[0]); // close the read branch | ||
write(p[1], msg1, MSGSIZE); | ||
write(p[1], msg2, MSGSIZE); | ||
write(p[1], msg3, MSGSIZE); | ||
close(p[1]); // close the write branch | ||
} | ||
else | ||
{ | ||
// we are in the parent branch | ||
close(p[1]); // close the write branch | ||
wait(NULL); | ||
for (int i = 0; i < 3; i++) | ||
{ | ||
read(p[0], inbuf, MSGSIZE); | ||
printf("%s\n", inbuf); | ||
} | ||
close(p[0]); // close the read branch | ||
} | ||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I like how you experiment with pointers here.