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.
17 changes: 16 additions & 1 deletion ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@
// (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process?
// What happens to the variable when both the child and parent change the value of x?

// Answer: At the start the child process has the same value (100).
// When changed the variable has two values one on the main thread and one on the forked thread.

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

int main(void)
{
// Your code here

int x = 100;

if (x == 100){
int new_process = fork();
if (new_process){
printf("CP has same value as MP: %d\n", x);
printf("Child process will now change the val: ");
x = 10;
}

}
printf("From main process the value is left the same: %d\n", x);

return 0;
}
Binary file added ex2/ex2
Binary file not shown.
17 changes: 16 additions & 1 deletion ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@
// 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?

// Answer: Yes they can both access the file descriptor. The file is written to 3 times.

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

int main(void)
{
// Your code here
FILE *fp;

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

fprintf(fp, "%s %d\n", "We are in", getpid());

int new_process = fork();

if (new_process)
{
fprintf(fp, "%s %d\n", "We are in", getpid());
}

fclose(fp);

return 0;
}
3 changes: 3 additions & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We are in 43508
We are in 43508
We are in 43508
Binary file added ex3/ex3
Binary file not shown.
12 changes: 11 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <strings.h>

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

// Return value of fork()
// On success, the PID of the child process is returned in the parent, and 0 is returned in the child.

if (new_process){
printf("hello from child\n");
} else {
printf("goodbye from main\n");
}

return 0;
}
5 changes: 5 additions & 0 deletions ex3/tempCodeRunnerFile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

int new_process = fork();
if (new_process){
printf("%s from child\n", hello);
}
Binary file added ex4/ex4
Binary file not shown.
12 changes: 11 additions & 1 deletion ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@

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

// Return value of fork()
// On success, the PID of the child process is returned in the parent, and 0 is returned in the child.

if (new_process){
// printf("hello from child\n");
execl("/bin/ls", "ls", NULL);
} else {
// printf("goodbye from main\n");
}

return 0;
}
Binary file added ex5/ex5
Binary file not shown.
29 changes: 28 additions & 1 deletion ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,34 @@ char* msg3 = "hello world #3";

int main(void)
{
// Your code here
char buf[MSGSIZE];
int p[2]; // a two-element array to hold the read and write file descriptors that are used by the pipe

// establish our pipe, passing it the p array so that it gets populated by the read and write file descriptors
if (pipe(p) < 0) {
fprintf(stderr, "pipe failed\n");
exit(1);
}else {
int new_process = fork();

if (new_process){
printf("hello from child\n");
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
printf("goodbye from child\n");

} else {
printf("hello from parent\n");
for (int i = 0; i < 3; i++)
{
// read 16 bytes of data from the read file descriptor
read(p[0], buf, MSGSIZE);
printf("\t%s\n", buf);
}
printf("goodbye from parent\n");
}
}

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 @@ -20,7 +20,15 @@ and `clock_gettime()` should work just fine.

int main()
{
// Your code here

struct timespec start, end;
/* now re-do this and measure CPU time */
/* the time spent sleeping will not count (but there is a bit of overhead */
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); /* mark start time */
printf( "" ); /* write to stdout */
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); /* mark the end time */

uint64_t diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed process CPU time = %llu nanoseconds\n", (long long unsigned int) diff);

return 0;
}