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
int x = 100;
int child = fork();
if (child < 0)
{
fprintf(stderr, "Nope fork failed.\n");
exit(1);
}
else if (child == 0)
{
printf("child start: %i \n", x);
x = 50;
printf("child end: %i \n", x);
}
else
{
printf("parent start: %i \n", x);
x = 60;
printf("parent end: %i \n", x);
}

return 0;
}
Binary file added ex2/ex2
Binary file not shown.
36 changes: 32 additions & 4 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory
// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor
// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory
// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor
// returned by `fopen()`? What happens when they are written to the file concurrently?

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
// Your code here

// Your code here
FILE *txt = fopen("./text.txt", "w");
int child = fork();

if (child < 0)
{
fprintf(stderr, "Nope. fork failed");
exit(1);
}
else if (child == 0)
{
char *str1 = "The truth is out there\n";
fwrite(str1, sizeof(char), strlen(str1), txt);
// txt = fopen("text.txt", "a");
// fprintf(txt, "The truth is out there\n" );
// fclose(txt);
printf("child\n");
}
else
{
// txt = fopen("text.txt", "a");
// fprintf(txt, "Shut up Mulder\n");
// fclose(txt);
char *str2 = "Shut up Mulder\n";
fwrite(str2, sizeof(char), strlen(str2), txt);
printf("parent\n");
}
fclose(txt);

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 @@
Shut up Mulder
The truth is out there
Binary file added ex3/ex3
Binary file not shown.
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 child = fork();
if (child < 0)
{
printf("Nope. Fork failed\n");
exit(1);
}
else if (child == 0)
{
printf("Knock knock\n");
}
else
{
waitpid(child, NULL, 0);
printf(" Who's there?\n");
}

return 0;
}
Binary file added ex4/ex4
Binary file not shown.
24 changes: 20 additions & 4 deletions ex4/ex4.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Write a program that calls `fork()` and then calls some form of `exec()`
// to run the program `/bin/ls`. Try a few variants of `exec()`, such as
// `execl()`, `execle()`, `execv()`, and others. Why do you think there
// to run the program `/bin/ls`. Try a few variants of `exec()`, such as
// `execl()`, `execle()`, `execv()`, and others. Why do you think there
// are so many variants of the same basic call?

#include <stdio.h>
Expand All @@ -10,7 +10,23 @@

int main(void)
{
// Your code here

// Your code here
int rc = fork();
if (rc < 0)
{
printf("Nope. Fork failed\n");
exit(1);
}
else if (rc == 0)
{
printf("hey that worked! (pid: %d)\n", getpid());
int run_it = execl("/bin/ls", "ls", "-1", NULL);
printf("run_it %d \n", run_it);
}
else
{
waitpid(rc, NULL, 0);;
printf("parent\n");
}
return 0;
}
Binary file added ex5/ex5
Binary file not shown.
43 changes: 39 additions & 4 deletions ex5/ex5.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Write a program that forks a child and creates a shared pipe
// between the parent and child processes. Have the child write
// the three messages to the parent and have the parent print out
// the messages.
// between the parent and child processes. Have the child write
// the three messages to the parent and have the parent print out
// the messages.

#include <stdio.h>
#include <unistd.h>
Expand All @@ -17,6 +17,41 @@ char* msg3 = "hello world #3";
int main(void)
{
// Your code here

char buf[MSGSIZE];
int p[2];
if (pipe(p) < 0)
{
fprintf(stderr, "Nope. Pipe failed\n");
exit(1);
}
int rc = fork();

if (rc < 0)
{
printf("Nope. Fork failed\n");
exit(2);
}
else if (rc == 0)
{
printf("child writing\n");

write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);

printf("child done\n");
}
else
{
waitpid(rc, NULL, 0);;
close(p[1]);
printf("parent\n");

while (read(p[0], buf, MSGSIZE) > 0)
{
printf("%s\n", buf);
}
printf("parent done\n");
}
return 0;
}
Binary file added ex6/ex6
Binary file not shown.
32 changes: 25 additions & 7 deletions ex6/ex6.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
Write a program that will find the average time it takes for your computer
to make a system call an empty write to stdout. Your program should use the
`clock_gettime` procedure to time how long a single system call takes. It'll
do this for one million iterations, and then find the average of all of those
to make a system call an empty write to stdout. Your program should use the
`clock_gettime` procedure to time how long a single system call takes. It'll
do this for one million iterations, and then find the average of all of those
iterations in nanoseconds.
For some helpful documentation and examples of using time-related system calls,
visit this site: https://www.cs.rutgers.edu/~pxk/416/notes/c-tutorials/gettime.html
While the linked site does say that `clock_gettime()` does not work on OSX, this
turns out to only be the case for OSX versions < 10.12. Anything later than that
and `clock_gettime()` should work just fine.
While the linked site does say that `clock_gettime()` does not work on OSX, this
turns out to only be the case for OSX versions < 10.12. Anything later than that
and `clock_gettime()` should work just fine.
*/

#include <stdio.h>
Expand All @@ -21,6 +21,24 @@ and `clock_gettime()` should work just fine.
int main()
{
// Your code here


long sum = 0;
long time;
double avg;
struct timespec start, end;

for (int i = 0; i < number_iter; i++)
{
clock_gettime(CLOCK_MONOTONIC, &start);
write(fileno(stdout), NULL, 0);
clock_gettime(CLOCK_MONOTONIC, &end);

time = BILLION * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec);
sum += time;
}

avg = sum / (float) number_iter;
printf("Average is %f ns \n", avg);

return 0;
}