Skip to content
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

Scott Bren - Processes #383

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ int main(int argc, char *argv[])
```
Here, we're doing the same thing as before, forking a new child process from a parent process. Then, inside that
child process, we're calling `execvp()` with the arguments it needs to run the word count program. Note that `exec`
does not spin up _another_ child process from the child process in which we called it. It transforms the process
that called it into the new program that was passed to `exec`, in this case, `wc`, the word count process. That's
does not spin up _another_ child process from the child process in which we calledbin/lsit. It transforms the process
that called it into the new program that was passed to `exec`, in this case, `wc`,bin/lsthe word count process. That's
why we still had to have the parent process `fork` a new child process.

## `pipe()`
Conceptually, a pipe is a uni-directional channel between two processes that would otherwise be isolated from each other. When a pipe is established between two processes, one process has access to the write end of the pipe, while the other has read access to the other end of the pipe. Thus, if you want two-way communication between two processes, two pipes will have to be created between both processes, one in each direction.
Conceptually, a pipe is a uni-directional channel between two processes that wouldbin/lsotherwise be isolated from each other. When a pipe is established between two processes, one process has access to the write end of the pipe, while the other habin/ls read access to the other end of the pipe. Thus, if you want two-way communication between two processes, two pipes will have to be created between both processes, one in each direction.

Some things to keep in mind when working with pipes is that when a process writes to a pipe, the other process receives the data in FIFO order (which makes sense when you think about the pipe analogy in real life). Additionally, if the process with read access tries to read from the pipe before anything has been written to it, the reading process is suspended until there is some data to read.
```c
Expand Down
Binary file added ex1/ex1
Binary file not shown.
15 changes: 14 additions & 1 deletion ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
#include <stdlib.h>
#include <unistd.h>

int x;

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

printf("%d", x);

int child = fork();

if(child) {
x = 30;
printf("%d", x);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should print something out for the parent.

printf("%d", x);

return 0;
}
Binary file added ex2/ex2
Binary file not shown.
15 changes: 14 additions & 1 deletion ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@

int main(void)
{
// Your code here

FILE * fp;

fp = fopen("text.txt", "w+");

int child = fork();

fprintf(fp, "Testing the parent process.");

if(child) {
fprintf(fp, "Testing the child process.");
}

fclose(fp);

return 0;
}
1 change: 1 addition & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing the parent process.Testing the child process.Testing the parent process.
Binary file added ex3/ex3
Binary file not shown.
10 changes: 9 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@

int main(void)
{
// Your code here

int child = fork();

if(child) {
printf("Hello");
} else {
wait(NULL);
printf("Goodbye");
}

return 0;
}
Binary file added ex4/ex4
Binary file not shown.
10 changes: 9 additions & 1 deletion ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@

int main(void)
{
// Your code here
int child = fork();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice job today scott. Good you're trying the many variants of the exec family


if(child) {

execl("/bin/ls", "ls", 0);
execle("/bin/ls", "ls", 0);
execv("/bin/ls", 0);

}

return 0;
}