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

Processes by Kevin Brack #369

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
examples
a.out
43 changes: 42 additions & 1 deletion ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,48 @@

int main(void)
{
// Your code here
printf("BEFORE FORK (pid: %d) \n", (int)getpid());
int x = 10;
int *xp = &x;

int f = fork();
if (f < 0)
{
printf("%s\n", "Fork faliled");
exit(1);
}
else if (f == 0)
{
// we are in the child branch
printf("Greetings from child (pid: %d)\n", f);
printf("Childs x: %i\n", x);
printf("Childs pointer to x: %p\n", xp);
printf("Childs pointer to x value: %i\n", *xp);

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.

x = x + 5;
printf("%s\n", "Child adding 5 to x");
printf("Childs x: %i\n", x);
printf("Childs pointer to x: %p\n", xp);
printf("Childs pointer to x value: %i\n", *xp);
int *npx = &x;
printf("Childs NEW pointer to x: %p\n", npx);
printf("%s\n", "---END CHILD---\n\n");
}
else
{
// we are in the parent branch
printf("Greetings from parent (pid: %d)\n", f);
printf("Parents x: %i\n", x);
printf("Parents pointer to x: %p\n", xp);
printf("Parents pointer to x value: %i\n", *xp);
x = x + 5;
printf("%s\n", "Parent adding 5 to x");
printf("Parents x: %i\n", x);
printf("Parents pointer to x: %p\n", xp);
printf("Parents pointer to x value: %i\n", *xp);
int *npx = &x;
printf("Childs NEW pointer to x: %p\n", npx);
printf("%s\n", "---END PARENT---\n\n");
}

return 0;
}
29 changes: 25 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,28 @@

int main(void)
{
// Your code here

FILE *fp;
fp = fopen("text.txt", "r+");
int f = fork();
if (f < 0)
{
printf("%s\n", "Fork faliled");
exit(1);
}
else if (f == 0)
{
// we are in the child branch
fprintf(fp, "Greetings from child: %d\n", (int)getpid());
}
else
{
// we are in the parent branch
fprintf(fp, "Greetings from parent: %d\n", (int)getpid());
}
fclose(fp);

return 0;
}

// Answer yes they were both able to write
// The parent wrote first the the child
2 changes: 2 additions & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Greetings from parent: 10998
Greetings from child: 10999
18 changes: 17 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@

int main(void)
{
// Your code here
int f = fork();
if (f < 0)
{
printf("%s\n", "Fork faliled");
exit(1);
}
else if (f == 0)
{
// we are in the child branch
printf("%s\n", "hello");
}
else
{
// we are in the parent branch
wait(NULL);
printf("%s\n", "goodbye");
}

return 0;
}
24 changes: 21 additions & 3 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,25 @@

int main(void)
{
// Your code here
int f = fork();
if (f < 0)
{
printf("%s\n", "Fork faliled");
exit(1);
}
else if (f == 0)
{
// we are in the child branch
char *args[] = {"ls", "-a", NULL};
// execvp(args[0], args);
execvp(args[0], args);
}
else
{
// we are in the parent branch
int wc = waitpid(f, NULL, 0);
printf("%s\n", "Child finished, exiting program");
}

return 0;
}
47 changes: 40 additions & 7 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,46 @@

#define MSGSIZE 16

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

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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;
}
18 changes: 15 additions & 3 deletions ex6/ex6.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@ and `clock_gettime()` should work just fine.
*/

#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define number_iter 1000000
#define BILLION 1000000000L

struct timespec start, end;

int main()
{
// Your code here

clock_gettime(CLOCK_MONOTONIC, &start);
for (int i = 0; i < number_iter; i++)
{
write(fileno(stdout), NULL, 0);
}
clock_gettime(CLOCK_MONOTONIC, &end);
double diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
double individual = diff / number_iter;
printf("Average time in nanoseconds: %lf\n", individual);

return 0;
}