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
Binary file added ex1/ex1
Binary file not shown.
23 changes: 22 additions & 1 deletion ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@
int main(void)
{
// Your code here
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(stderr, "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.
62 changes: 62 additions & 0 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,72 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
// Your code here
FILE *file;
printf("Hello there, before we call fork(), this is our processID is: (pid: %d) \n", (int)getpid());
file = fopen("text.txt", "a+");
while (1)
{
int c = fgetc(file);
if (feof(file))
{
break;
}
printf("%c", c);
}

int rc = fork();

if (rc < 0)
{
printf(stderr, "\nForking failed \n");
exit(1);
}
else if (rc == 0)
{
printf("\nWe are now attempting to wait for child process to finish. . . \n");
file = fopen("text.txt", "a+");
printf("\nHello there, if rc==0, this is our child processID is: (pid: %d) \n", (int)getpid());
// printf("This is the childs value for file (opening a file): %d \n", file);
file = fopen("text.txt", "a+");
fprintf(file, "%s %s %s %s %s %s %s", "\nWe", "are", "writing", "in", "the", "child", "process\n");
fclose(file);
file = fopen("text.txt", "a+");
while (1)
{
int c = fgetc(file);
if (feof(file))
{
break;
}
printf("%c", c);
}
fclose(file);
}
else
{
// int wc = waitpid(rc, NULL, 0);
printf("\nOur child has now finished executing, we are back in Parent process with PID: (%d)\n", (int)getpid());
file = fopen("text.txt", "a+");
fprintf(file, "%s %s %s %s %s %s %s", "\nWe", "are", "writing", "after", "the", "child", "process\n");
fclose(file);
// printf("This is the childs value for file (opening a file): %d \n", file);
file = fopen("text.txt", "a+");
while (1)
{
int c = fgetc(file);
if (feof(file))
{
break;
}
printf("%c", c);
}
fclose(file);
}

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 @@

We are writing after the child process

We are writing in the child process
Binary file added ex3/ex3
Binary file not shown.
20 changes: 19 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@
int main(void)
{
// Your code here
int rc = fork();

if (rc < 0)
{
printf(stderr, "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("Goodbye\n");
}
else
{
int wc = waitpid(rc, NULL, 0);
printf("Hello there, if rc==0, this is our Parent processID is: (pid: %d) \n", (int)getpid());
printf("Hello\n");
}

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

int main(void)
{
// Your code here
// Your code here
int status;
// 2 strings, one is /bin/ls and the other is to finish with a NULL
char *args[2];

args[0] = "/bin/ls";
args[1] = NULL;
int y = fork();

if (y == 0) {
execv(args[0], args);

} else {

wait(&status);
}

return 0;
}