Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@

int main(void)
{
// Your code here
int x = 100;

printf("pre-fork: parent pid %d, x = %d \n", (int)getpid(), x);

int rc = fork();

if (rc < 0)
{
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0)
{
printf("post-fork: child pid %d, x = %d \n", (int)getpid(), x);

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 structured this. It's very detailed.

}
else
{
printf("post-fork: parent pid %d of child %d, x = %d\n", (int)getpid(), rc, x);
}

return 0;
}
28 changes: 24 additions & 4 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory
// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor
// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory
// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor
// returned by `fopen()`? What happens when they are written to the file concurrently?

#include <stdio.h>
Expand All @@ -8,7 +8,27 @@

int main(void)
{
// Your code here

FILE *file = fopen("text.txt", "r+");

fprintf(file, "pre-fork text\n");

int rc = fork();

if (rc < 0)
{
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0)
{
printf("post-fork: child pid %d\n", (int)getpid());
fprintf(file, "child text\n");
}
else
{
printf("post-fork: parent pid %d of child %d\n", (int)getpid(), rc);
fprintf(file, "parent text\n");
}

return 0;
}
21 changes: 20 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,26 @@

int main(void)
{
// Your code here
printf("pre-fork: pid %d\n", (int)getpid());

int rc = fork();

if (rc < 0)
{
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0)
{
printf("post-fork: child pid %d\n", (int)getpid());
printf("hello\n");
}
else
{
int wc = waitpid(rc, NULL, 0);
printf("post-fork: parent pid %d of child %d\n", (int)getpid(), rc);
printf("goodbye\n");
}

return 0;
}
27 changes: 23 additions & 4 deletions ex4/ex4.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Write a program that calls `fork()` and then calls some form of `exec()`
// to run the program `/bin/ls`. Try a few variants of `exec()`, such as
// `execl()`, `execle()`, `execv()`, and others. Why do you think there
// to run the program `/bin/ls`. Try a few variants of `exec()`, such as
// `execl()`, `execle()`, `execv()`, and others. Why do you think there
// are so many variants of the same basic call?

#include <stdio.h>
Expand All @@ -10,7 +10,26 @@

int main(void)
{
// Your code here
printf("pre-fork: pid %d\n", (int)getpid());

int rc = fork();

if (rc < 0)
{
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0)
{
printf("post-fork: child pid %d\n", (int)getpid());

execl("/bin/ls", "ls", NULL);
}
else
{
int wc = waitpid(rc, NULL, 0);
printf("post-fork: parent pid %d of child %d\n", (int)getpid(), rc);
}

return 0;
}
}
53 changes: 45 additions & 8 deletions ex5/ex5.c
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>
Expand All @@ -10,13 +10,50 @@

#define MSGSIZE 16

char* msg1 = "hello world #1";
char* msg2 = "hello world #2";
char* msg3 = "hello world #3";
char *msg1 = "hello world #1";
char *msg2 = "hello world #2";
char *msg3 = "hello world #3";

int main(void)
{
// Your code here

char inbuff[MSGSIZE];
int p[2];

if (pipe(p) < 0)
{
printf("Pipe failed\n");
exit(1);
}

printf("pre-fork: pid %d\n", (int)getpid());

int rc = fork();

if (rc < 0)
{
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0)
{
printf("post-fork: child pid %d\n", (int)getpid());

write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
}
else
{
int wc = waitpid(rc, NULL, 0);
printf("post-fork: parent pid %d of child %d\n", (int)getpid(), rc);

for (int i = 0; i < 3; i++)
{
read(p[0], inbuff, MSGSIZE);

printf("% s\n", inbuff);
}
}

return 0;
}