commit e359896da026a29b412d1f05e299cdd7042f6187 Author: Akemi Izuko Date: Tue Sep 24 13:34:24 2024 -0600 Add lab 1 diff --git a/lab1/demos/chdir.c b/lab1/demos/chdir.c new file mode 100644 index 0000000..354b6ef --- /dev/null +++ b/lab1/demos/chdir.c @@ -0,0 +1,14 @@ +#include +#include +#include + +int main(void) { + char s[PATH_MAX]; + + // printing current working directory + printf("%s\n", getcwd(s, 100)); + // using the command + chdir(".."); + // printing current working directory after chdir is executed + printf("%s\n", getcwd(s, 100)); +} diff --git a/lab1/demos/execve.c b/lab1/demos/execve.c new file mode 100644 index 0000000..23f1fef --- /dev/null +++ b/lab1/demos/execve.c @@ -0,0 +1,10 @@ +#include +#include + +int main(void) { + char *args[] = {"fork", NULL}; + + execve("fork", args, NULL); + + printf("End of execve\n"); +} diff --git a/lab1/demos/exit1.c b/lab1/demos/exit1.c new file mode 100644 index 0000000..013eb7e --- /dev/null +++ b/lab1/demos/exit1.c @@ -0,0 +1,8 @@ +#include +#include + +int main(void) { + printf("start of program\n"); + _exit(3); + printf("End of program\n"); +} diff --git a/lab1/demos/exit2.c b/lab1/demos/exit2.c new file mode 100644 index 0000000..0b9c834 --- /dev/null +++ b/lab1/demos/exit2.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int main(void) { + for (int i = 0; i < 5; i++) { + if (fork() == 0) { + printf("Child %d with pid %d\n", i, getpid()); + _exit(0); + } + } + printf("hello\n"); +} diff --git a/lab1/demos/fork.c b/lab1/demos/fork.c new file mode 100644 index 0000000..f4597c5 --- /dev/null +++ b/lab1/demos/fork.c @@ -0,0 +1,9 @@ +#include +#include + +int main(void) { + fork(); // 2 + fork(); // 4 + fork(); // 8 + printf("hello\n"); +} diff --git a/lab1/demos/kill.c b/lab1/demos/kill.c new file mode 100644 index 0000000..dbde575 --- /dev/null +++ b/lab1/demos/kill.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +int main(void) { + int pid = fork(); + + if (pid == 0) { + printf("Child with pid %d\n", getpid()); + while (1) { + printf("*"); + } + } else { + sleep(1); + kill(pid, SIGKILL); + printf("\nParent killed pid %d with signal %d\n", pid, SIGKILL); + } +} diff --git a/lab1/demos/leak.c b/lab1/demos/leak.c new file mode 100644 index 0000000..fb08d15 --- /dev/null +++ b/lab1/demos/leak.c @@ -0,0 +1,6 @@ +#include + +int main(void) { + int *p = malloc(64); + (void) p; +} diff --git a/lab1/demos/timing.c b/lab1/demos/timing.c new file mode 100644 index 0000000..55a2ec1 --- /dev/null +++ b/lab1/demos/timing.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +int main(void) { + struct tms start_times; + struct tms cease_times; + + times(&start_times); + + int pid = fork(); + + if (pid == 0) { + printf("This is a child process!\n"); + + for (int i = 0; i < 300000; i += 1) { + printf("Got a number %d\n", i); + } + _exit(1); + } + + waitpid(pid, NULL, 0); + times(&cease_times); + + printf(" user time: %lf\n", (double) (cease_times.tms_utime - start_times.tms_utime) / sysconf(_SC_CLK_TCK)); + printf("system time: %lf\n", (double) (cease_times.tms_stime - start_times.tms_stime) / sysconf(_SC_CLK_TCK)); + printf(" user time: %lf\n", (double) (cease_times.tms_cutime - start_times.tms_cutime) / sysconf(_SC_CLK_TCK)); + printf("system time: %lf\n", (double) (cease_times.tms_cstime - start_times.tms_cstime) / sysconf(_SC_CLK_TCK)); +} diff --git a/lab1/demos/wait.c b/lab1/demos/wait.c new file mode 100644 index 0000000..c075882 --- /dev/null +++ b/lab1/demos/wait.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +int main(void) { + for (int i = 0; i < 5; i++) { + int f = fork(); + + if (f == 0) { + printf("Child %d with pid %d\n", i, getpid()); + _exit(0); + } + } + + while (wait(NULL) > 0); // This waits until all children are terminated + // while (waitpid(-1, NULL, 0) > 0); // Same effect + + printf("parent\n"); + return 0; +} diff --git a/lab1/zombies.c b/lab1/zombies.c new file mode 100644 index 0000000..4c11232 --- /dev/null +++ b/lab1/zombies.c @@ -0,0 +1,36 @@ +// Write a program that creates a zombie, and then call system() from stdlib.h +// (for executing a command from within a program) to execute the ps(1) command +// to verify that the process is a zombie. +#include +#include +#include +#include + +void create_zombie() { + int pid = fork(); + + if (pid == 0) { + sleep(100000); + } else { + _exit(0); + } +} + +void get_ps(int pid) { + char spid[1000]; + sprintf(spid, "ps aux | awk '$2 == %d { print }'", pid); + system(spid); +} + + +int main(void) { + int pid = fork(); + + if (pid == 0) { + create_zombie(); + } else { + get_ps(pid); + } + + return -1; +}