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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Processes and System Calls

init....

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.

## Objective
Expand Down
Binary file added ex1/ex1
Binary file not shown.
22 changes: 22 additions & 0 deletions ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@
int main(void)
{
// Your code here
// init x
int x = 100;
printf("Hello there, before we call fork(), this is our processID is: (pid: %d) \n", (int)getpid());
printf("This is the parents value for x: %d \n", x);
int rc = fork();

if (rc < 0)
{
printf("Forking failed \n");
}
else if (rc == 0)
{

printf("Hello there, if rc==0, this is our child processID is: (pid: %d) \n", (int)getpid());
printf("This is the childs value for x if rc==0: %d \n", x);
}
else
{
int x = 200;
printf("hello, if rc > 0, this is parent here (pid: %d) of child %d\n", (int)getpid(), rc);
printf("This is the childs value for x if rc > 0: %d \n", x);
}

return 0;
}
Binary file added ex2/ex2
Binary file not shown.
25 changes: 25 additions & 0 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@
int main(void)
{
// Your code here
FILE *fp;

fp = fopen("text.txt", "w+");
printf("Before forking pid: %d\n", (int)getpid());

fprintf(fp, "%s %s %s %s %d\n", "File", "write", "before", "fork", (int)getpid());

pid_t pid = fork();

if (pid < 0) //fork failed
{
exit(1);
}
else if (pid == 0) //child process
{
printf("AFTER forking CHILD pid: %d\n", (int)getpid());
fprintf(fp, "%s %s %d\n", "Child-File", "write", (int)getpid());
}
else
{
printf("AFTER forking PARENT pid: %d\n", (int)getpid());
fprintf(fp, "%s %s %d\n", "Parent-File", "write", (int)getpid());
}
fclose(fp);


return 0;
}
4 changes: 4 additions & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
File write before fork 4563
Parent-File write 4563
File write before fork 4563
Child-File write 4564
Binary file added ex3/ex3
Binary file not shown.
17 changes: 17 additions & 0 deletions ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
int main(void)
{
// Your code here
printf("Before forking pid: %d\n", (int)getpid());

pid_t pid = fork();

if (pid < 0)
{
exit(1);
}
else if (pid == 0)
{
printf("AFTER forking CHILD says Hello pid: %d\n", (int)getpid());
}
else
{
waitpid(pid, NULL, 0);
printf("AFTER forking PARENT says Goodbye pid: %d\n", (int)getpid());
}

return 0;
}