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
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 rc = fork();

if (rc < 0)
{
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0)
{
// x = 20;
printf("Child x:%d \n", x);
}
else
{
x = 200;
printf("Parent x:%d \n", x);
}
return 0;
}
34 changes: 33 additions & 1 deletion ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@
int main(void)
{
// Your code here

FILE *file;
// int rc = fork(); Having fork here will overwrite th file
file = fopen("text.txt", "w");
if (file == NULL)
{
printf("Could not open text.txt");
exit(1);
}
int fileNum;
fileNum = fileno(file);
printf("Pointer: %d\n", fileNum);

int rc = fork();
if (rc < 0)
{
fprintf(stderr, "fork failed\n");
exit(1);
}
else if (rc == 0)
{
char *txt = "Child.";
fprintf(file, "Child: %s", txt);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good! You can also use fwrite() as an alternative to fprintf()


printf("Child descriptor: %d\n", fileNum);
}
else
{
char *txt = "Parent.";
fprintf(file, "Child %s", txt);
printf("Parent descriptor: %d\n", fileNum);
}

fclose(file);
return 0;
}
16 changes: 15 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
int main(void)
{
// Your code here

int child_process = fork();
if (child_process < 0)
{
fprintf(stderr, "Fork failed\n");
exit(1);
}
else if (child_process == 0)
{
printf("hello.\n");
}
else
{
int wc = waitpid(child_process, NULL, 0);
printf("goodbye.\n");
}
return 0;
}
20 changes: 20 additions & 0 deletions ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@
int main(void)
{
// Your code here
int child = fork();

if (child < 0)
{
printf("\nFork failed\n");
}
else if (child == 0)
{
char *args[2];
args[0] = "/bin/ls";
args[1] = NULL;

execvp(args[0], args);
printf("\nNot found\n");
}
else
{
int wc = waitpid(child, NULL, 0);
printf("\nParent\n");
}

return 0;
}
19 changes: 18 additions & 1 deletion ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ char* msg3 = "hello world #3";
int main(void)
{
// Your code here

char s[MSGSIZE];
int p[2];

if (pipe(p) < 0)
{
fprintf(stderr, "\npipe error\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(p[0], s, MSGSIZE);
printf("%s\n", s);
}
return 0;
}
14 changes: 13 additions & 1 deletion ex6/ex6.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ and `clock_gettime()` should work just fine.
int main()
{
// Your code here

int time = 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);

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

printf("\nAverage time: %d nanoseconds\n", time);
return 0;
}