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: 17 additions & 0 deletions ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
int main(void)
{
// Your code here
int x = 100;
int my_fork = fork();

if (my_fork == 0) {
printf("x child is %d\n", x);
x = 111;
printf("x child is now %d\n", x);
}
if (my_fork > 0 ) {
printf("x parent is %d\n", x);
x = 222;
printf("x parent is now %d\n", x);
}
if (my_fork < 0) {
printf("Error!");
}


return 0;
}
Binary file added ex2/ex2
Binary file not shown.
18 changes: 16 additions & 2 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@

int main(void)
{
// Your code here

// Your code here
int x = 100;
FILE * fp;
fp = fopen("text.txt", "w");
int my_fork = fork();
if (my_fork == 0) {
fprintf(fp, "%s", "Child\n");
printf("Child\n");
fclose(fp);
}
else if (my_fork > 0) {
fprintf(fp, "%s", "Parent\n");
printf("Parent\n");
fclose(fp);
}

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 @@
Parent
Child
Binary file added ex3/ex3
Binary file not shown.
14 changes: 14 additions & 0 deletions ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,19 @@ int main(void)
{
// Your code here

int my_fork = fork();
if(my_fork == 0) {
printf("hello\n");

}
else if(my_fork > 0 ) {
wait(NULL);
printf("goodbye\n");

} else {
printf("Error!");
exit(1);
}

return 0;
}
Binary file added ex4/ex4
Binary file not shown.
13 changes: 12 additions & 1 deletion ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@

int main(void)
{
// Your code here
// Your code here
pid_t pid;
pid = fork();
if (pid < 0) {
printf("Error %u\n", getpid());
} else if (pid == 0) {
printf("Child, pid = %u\n", getpid());
char *args[] = {"/bin/ls", NULL};
execv(args[0], args);
} else {
printf("Parent, pid = %u\n", getpid());
}

return 0;
}
Binary file added ex5/ex5
Binary file not shown.
19 changes: 17 additions & 2 deletions ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ char* msg3 = "hello world #3";

int main(void)
{
// Your code here

char inbuf [MSGSIZE]; // buffer that will hold incoming data being written
int p[2]; // two element array to hold and read write file descriptors

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

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

for (int i = 0; i<3; i++){
//read 16 bytes of data from file descriptor
read(p[0], inbuf, MSGSIZE);
printf("%s\n", inbuf);
}
return 0;
}
Binary file added ex6/ex6
Binary file not shown.
12 changes: 11 additions & 1 deletion ex6/ex6.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ and `clock_gettime()` should work just fine.
int main()
{
// Your code here

int diff;
struct timespec start, end;
int i;
clock_gettime(CLOCK_MONOTONIC, &start);
for (i = 0; i < number_iter; i++)
{
write(fileno(stdout), NULL, 0);
}
clock_gettime(CLOCK_MONOTONIC, &end);
diff = (BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec) / number_iter;
printf("elapsed time = %llu nanoseconds\n", (long long unsigned int)diff);
return 0;
}