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.
19 changes: 19 additions & 0 deletions ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
int main(void)
{
// Your code here
printf("hello world!\n");
int x = 101;
int fp = fork();
if (fp < 0)
{ // fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (fp == 0)
{ // child process satisfies this branch
printf("x is: %d \n", x);
x = 102;
}
else
{
printf("hello, parent here (pid: %d) of child %d\n", (int)getpid(), fp);
}



return 0;
}
Binary file added ex2/ex2
Binary file not shown.
19 changes: 19 additions & 0 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
int main(void)
{
// Your code here
FILE *file;
file = fopen("text.txt", "r+");
int fp = fork();
if (fp < 0)
{ // fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (fp == 0)
{ // child process satisfies this branch
//fprintf(file, "%s");
fprintf(file, "Hello from child: %d\n", (int)getpid());
}
else
{
fprintf(file, "Hello from parent: %d\n", (int)getpid());
}

fclose(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 @@
Hello from parent: 54
Hello from child: 55
15 changes: 15 additions & 0 deletions ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
int main(void)
{
// Your code here
int fp = fork();
if (fp < 0)
{ // fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (fp == 0)
{ // child process satisfies this branch
printf("hello\n");
}
else
{
int wc = waitpid(fp, NULL, 0);
printf("goodbye\n");
}

return 0;
}