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 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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ int main(int argc, char *argv[])
```
Here, the `waitpid()` function suspends the parent process until the child process pointed at by `rc` terminates. Thus, we ensure that the parent process only runs after the child process has finished its execution.

## `exec()`
The `exec()` system call is used in order to run a program that is different from the calling program (since `fork` only executes copies of the program that called it).
Let's say we wanted to spin up a child process to execute a word count program. Here's how what a program that does that might look like:
## `exehttps://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdout
The `exhttps://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdout program that is different from the calling program (since `fork` only executes copies of the program that called https://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdout
Let's shttps://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdoutexecute a word count program. Here's how what a program that does that might look like:
```c
// p3.c
// p3.chttps://stackoverflow.com/questions/19099352/get-stdout-content-from-system-call-in-c-without-printing-to-stdout
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Expand Down 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;
}
Binary file added ex5/ex5
Binary file not shown.
25 changes: 25 additions & 0 deletions ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ char* msg3 = "hello world #3";
int main(void)
{
// Your code here
char inbuf[MSGSIZE];
int i;

int pipeEnds[2];
int pipe(pipeEnds);


int child = fork();

if(child) {

write(pipeEnds[1], msg1, MSGSIZE);
write(pipeEnds[1], msg2, MSGSIZE);
write(pipeEnds[1], msg3, MSGSIZE);

} else {

wait(NULL);
for(i = 0; i < 3; i++) {
Copy link

Choose a reason for hiding this comment

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

sweet love that you're paying attention to what's going on.


read(pipeEnds[0], inbuf, MSGSIZE);

}

}

return 0;
}
Binary file added ex6/ex6
Binary file not shown.
21 changes: 21 additions & 0 deletions ex6/ex6.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,30 @@ and `clock_gettime()` should work just fine.
#define number_iter 1000000
#define BILLION 1000000000L

int total = 0;
int diff;
int avg;
int i;
struct timespec start, end;

int main()
{
// Your code here
for(i = 0; i < number_iter; i++) {

clock_gettime(_POSIX_MONOTONIC_CLOCK, &start);
write(STDOUT_FILENO, NULL, 0);
clock_gettime(_POSIX_MONOTONIC_CLOCK, &end);

diff = BILLION * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec);

total = total + diff;

}

avg = (total / number_iter);

printf("%d\n", avg);

return 0;
}
1 change: 1 addition & 0 deletions stretch/src/balance.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4458
Binary file added stretch/src/bankers
Binary file not shown.
33 changes: 32 additions & 1 deletion stretch/src/bankers.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ int get_random_amount(void)

// Return a random number between 0 and 999 inclusive using rand()

int randomAmount = rand() % 999;

return randomAmount;

// ^^^^^^^^^^^^^^^^^^
}

Expand All @@ -116,14 +120,28 @@ int main(int argc, char **argv)
// message to stderr, and exit with status 1:
//
// "usage: bankers numprocesses\n"

if (argc < 2) {

fprintf(stderr, "Usage: bankers numProcesses\n");
exit(1);

}

int num_processes = atol(argv[1]);

// Store the number of processes in this variable:
// How many processes to fork at once
int num_processes = IMPLEMENT ME


// Make sure the number of processes the user specified is more than
// 0 and print an error to stderr if not, then exit with status 2:
//
if (num_processes < 1) {
fprintf(stderr, "Bankers: number of processes must be at least 1");
exit(2);
}

// "bankers: num processes must be greater than 0\n"

// ^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -152,17 +170,30 @@ int main(int argc, char **argv)
// Open the balance file (feel free to call the helper
// functions, above).

int balFile = open_balance_file(BALANCE_FILE);

// Read the current balance

read_balance(balFile, &balance);

// Try to withdraw money
//
// Sample messages to print:
//
// "Withdrew $%d, new balance $%d\n"
// "Only have $%d, can't withdraw $%d\n"

if(amount <= balance) {
balance = balance - amount;
write_balance(balFile, balance);
printf("Withdrew $%d, New Balance: $%d\n", amount, balance);
} else {
printf("Insufficient Funds - Current Available Balance: $%d.\n", balance);
}

// Close the balance file
//^^^^^^^^^^^^^^^^^^^^^^^^^^
close_balance_file(balFile);

// Child process exits
exit(0);
Expand Down