Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
19 changes: 18 additions & 1 deletion ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@

int main(void)
{
// Your code here
int x = 100;

printf("Parent PID: %d, x = %d \n", (int)getpid(), x);

int rc = fork();

if (rc == 0)
{
printf("Fork successful. We are the child process.\n Child PID: %d, Parent PID: %d", getpid(), getppid());
}
else if (rc > 0)
{
printf("We are the parent process.\n Parent PID: %d of Child PID: %d", getpid(), rc, x);
}
else
{
printf(stderr, "Fork failed");
}

return 0;
}
22 changes: 20 additions & 2 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@

int main(void)
{
// Your code here

FILE *fptr;
fptr = fopen("text.txt", "w");

int rc = fork();

if (rc < 0) { // fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) { // child process satisfies this branch
printf("hello, child here (pid: %d) \n", (int) getpid());
} else {
printf("hello, parent here (pid: %d) of child %d\n", (int) getpid(), rc);
}

if (fptr == NULL)
{
printf("Error opening text file!");
exit(1);
}

return 0;
}
17 changes: 16 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@

int main(void)
{
// Your code here
int rc = fork();

if (rc < 0)
{
fprintf(stderr, "Fork failed");
exit(1);
}
else if (rc == 0)
{
printf("Hello from Child %d", (int) getpid());
}
else
{
int wc = waitpid(rc, NULL, 0); //wait or child to terminate, dont care about status, no flags
printf("Goodbye from Parent %d of Child %d", (int) getpid(), rc);
}

return 0;
}