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.
10 changes: 10 additions & 0 deletions ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
int main(void)
{
// Your code here
int x = 100;
if(fork() == 0){
x = 30;
printf("Child %i\n", x);
}else{
x = 20;
printf("Parent %i\n", x);
}

return 0;
}

//A: the variable will be unique to each instance when the variable value is changed
Binary file added ex2/ex2
Binary file not shown.
15 changes: 14 additions & 1 deletion ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
int main(void)
{
// Your code here

FILE * file;
file = fopen("text.txt", "r+");

if (fork() == 0){
fprintf(file, "%s", "child!");
printf("child: %d\n", fileno(file));
}else{
fprintf(file, "%s", "parent");
printf("parent %d\n", fileno(file));
}
fclose(file);
return 0;
}


// A: Yes the child and the parent can access the file descriptor returned by fopen and they both get written in!
1 change: 1 addition & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parentchild!
Binary file added ex3/ex3
Binary file not shown.
8 changes: 7 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
int main(void)
{
// Your code here

int temp = fork();
wait(NULL);
if(temp == 0){
printf("Child -> Hello \n");
}else{
printf("Parent -> Goodbye \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 temp = fork();
if(temp == 0){
char *args[2];
args[0] = "bin/ls";
args[1] = NULL;
execvp(args[0], args);
printf("Child process\n");

}else if(temp < 0){
printf("fork failed\n");
}
else
{
int wc = waitpid(temp, NULL, 0);
printf("Completed child process\n");
}
return 0;
}
Binary file added ex5/ex5
Binary file not shown.
21 changes: 20 additions & 1 deletion ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ char* msg3 = "hello world #3";
int main(void)
{
// Your code here
char buffer[MSGSIZE];
int p[2];
if(pipe(p) < 0){
fprintf(stderr, "error error error\n");
exit(1);
}

int temp = fork();
if(temp == 0){
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
}
else{
for (int i = 0; i < 3; i++){
read(p[0], buffer, MSGSIZE);
printf("%s\n", buffer);
}
}

return 0;
}
}
Binary file added ex6/ex6
Binary file not shown.
12 changes: 10 additions & 2 deletions ex6/ex6.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ and `clock_gettime()` should work just fine.
int main()
{
// Your code here

int timed = 0;
for(int i = 0; i < number_iter; i++){
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
write(fileno(stdout), NULL, 0);
clock_gettime(CLOCK_MONOTONIC, &end);
timed += BILLION * (end.tv_sec - start.tv_sec) + end.tv_sec - start.tv_sec;
}
printf("%d\n", timed / number_iter);
return 0;
}
}