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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- https://github.com/LambdaSchool/Processes/pull/379 -->
# Processes and System Calls

Now that we've talked a bit about how processes and system calls work at a high level, it's time to apply these concepts by doing some exercises related to process creation and making system calls. We'll be utilizing the `fork()`, `exec()`, `wait()`, and `pipe()` system calls in order to create processes and even have them pass messages to each other.
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 @@ -9,6 +9,19 @@
int main(void)
{
// Your code here

int x = 100;
int rc = fork();
// if fork fails return value is -1
if(rc < 0){
// 'fprintf' is like 'printf' except you add (where, how, what)
fprintf(stderr, "failing forks\n");
exit(1);
} else if (rc == 0) {
// successful child fork will return a 0, so this would select the child process
printf("This is a child pid: %d\n", (int) getpid());
} else {
printf("This is the parent pid: %d parent to child pid: %d\n", (int) getpid(), rc);
}
// printf("%d", x);
return 0;
}
Binary file added ex2/ex2
Binary file not shown.
25 changes: 24 additions & 1 deletion ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
int main(void)
{
// Your code here

FILE *ptr_file;
char buf[1000];
int rc = fork();

ptr_file = fopen("./text.txt", "r+");
if(!ptr_file) {
return 1;
}

while (fgets(buf,1000, ptr_file) != NULL)
if(rc < 0) {
fprintf(stderr, "unable to fork\n");
exit(1);
} else if (rc == 0) { // this is the child fork
fputs("This has been added from the child process\n", ptr_file);
printf("This is from the child: %s\n", buf);

} else { // this is the parent
fputs("This has been added from the parent process\n", ptr_file);
printf("This is from the Parent: %s\n", buf);
}

fclose(ptr_file);

return 0;
}
2 changes: 2 additions & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
this is the text fileThis has been added from the parent process
This has been added from the child process
Binary file added ex3/ex3
Binary file not shown.
10 changes: 10 additions & 0 deletions ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
int main(void)
{
// Your code here
int rc = fork();

if (rc < 0) {
fprintf(stderr, "failing forking\n");
} else if (rc == 0) {
printf("hello\n");
} else {
int wc = waitpid(rc, NULL, 0);
printf("goodbye\n");
}

return 0;
}
11 changes: 10 additions & 1 deletion ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@

int main(void)
{
// Your code here
// Your code here
int rc = fork();
if (rc < 0) {
fprintf(stderr, "forking fail\n");
exit(1);
} else if (rc == 0) {
// child
} else {
// parent
}

return 0;
}
11 changes: 10 additions & 1 deletion ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ char* msg3 = "hello world #3";
int main(void)
{
// Your code here

int rc = fork();

if (rc < 0) {
fprintf(stderr, "fail fail fail\n");
exit(1);
} else if (rc == 0) {
// this is the child
} else {
// this is the parent
}
return 0;
}