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.
21 changes: 21 additions & 0 deletions ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,26 @@ int main(void)
{
// Your code here

int x = 100;

int rc = fork();

if (rc < 0) {
printf("Fork failed...\n");
} else if (rc == 0) {
// child returns 0 to fork...
printf("The value of x in child is %d.\n", x);

x = 101;

} else {
// parent returns child's PID -> getpid()
printf("The value of x in parent is %d.\n", x);

x = 102;
}

printf("The final value of x is %d\n", x);

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

fp = fopen("text.txt", "r+");

int rc = fork();

if (rc < 0) {
printf("Fork failed...\n");
} else if (rc == 0) {
fprintf(fp, "This is the child.\n");
} else {
fprintf(fp, "This is the parent.\n");
}

while(1) {

c = fgetc(fp);

if (feof(fp)) {
break;
}
printf("%c", c);
}
printf("\n");

fclose(fp);

return 0;
}
6 changes: 6 additions & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This is the parent.
This is the child.
This is the child.
This is the child.
This is the child.
This is the child.
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) {
printf("Fork failed...\n");
} else if (rc == 0) {
printf("hello\n");
} else {
waitpid(rc, NULL, 0);
printf("goodbye\n");
}

return 0;
}
Binary file added ex4/ex4
Binary file not shown.
20 changes: 18 additions & 2 deletions ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>

int main(void)
int main(int argc, char *argv[])
{
// Your code here
// Your code here
int rc = fork();


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

char *my_args[3];

my_args[0] = strdup("/bin/ls");
my_args[1] = strdup("ex4.c");
my_args[1] = NULL;

execvp(my_args[0], my_args);
}

return 0;
}
Binary file added ex5/ex5
Binary file not shown.
34 changes: 33 additions & 1 deletion ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,38 @@ char* msg3 = "hello world #3";
int main(void)
{
// Your code here
char inbuf[MSGSIZE];
int p[2];

if (pipe(p) < 0) {
fprintf(stderr, "pipe failed...\n");
exit(1);
}

int rc = fork();

if (rc < 0) {
printf("fork failed...\n");
} else if (rc == 0) {
close(p[0]);
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
close(p[1]);
} else {

waitpid(rc, NULL, 0);

close(p[1]);

for (int i = 0; i < 3; i++) {

read(p[0], inbuf, MSGSIZE);
printf("%s\n", inbuf);
}

close(p[0]);
}

return 0;
}
}
Binary file added ex5/exercise5
Binary file not shown.