Last active
October 6, 2025 21:49
-
-
Save MuntashirAkon/ec908f3ca819cfef01b813b74c76f6e4 to your computer and use it in GitHub Desktop.
For CS 153 course at UCR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "types.h" | |
| #include "user.h" | |
| #define WNOHANG 1 | |
| int main(int argc, char *argv[]) { | |
| int getParent(void); | |
| int exitWait(void); | |
| int waitPid(void); | |
| int waitNothing(void); | |
| printf(1, "\nlab#1\n"); | |
| if (atoi(argv[1]) == 2) | |
| waitPid(); | |
| else if (atoi(argv[1]) == 1) | |
| exitWait(); | |
| else if (atoi(argv[1]) == 3) | |
| waitNothing(); | |
| // End of test | |
| exit(0); | |
| return 0; | |
| } | |
| int waitNothing(void) { | |
| int ret, exit_status = -1; | |
| ret = wait(&exit_status); | |
| printf(1, "%d %d\n", ret, exit_status); | |
| return 0; | |
| } | |
| int exitWait(void) { | |
| int pid, ret_pid, exit_status; | |
| int i; | |
| // use this part to test exit(int status) and wait(int* status) | |
| // printf(1, "\n Parts a & b) testing exit(int status) and wait(int* | |
| // status):\n"); | |
| for (i = 0; i < 2; i++) { | |
| pid = fork(); | |
| if (pid == 0) { // only the child executed this code | |
| if (i == 0) { | |
| printf(1, "%d %d\n", getpid(), 0); | |
| exit(0); | |
| } else { | |
| printf(1, "%d %d\n", getpid(), -1); | |
| exit(-1); | |
| } | |
| } else if (pid > 0) { // only the parent executes this code | |
| ret_pid = wait(&exit_status); | |
| printf(1, "%d+%d\n", ret_pid, exit_status); | |
| } else { // something went wrong with fork system call | |
| printf(2, "\nError using fork\n"); | |
| exit(-1); | |
| } | |
| } | |
| return 0; | |
| } | |
| int waitPid(void) { | |
| int ret_pid, exit_status; | |
| int i; | |
| int pid_a[5] = {0, 0, 0, 0, 0}; | |
| // use this part to test wait(int pid, int* status, int options) | |
| // printf(1, "\n Part c) testing waitpid(int pid, int* status, int | |
| // options):\n"); | |
| for (i = 0; i < 5; i++) { | |
| pid_a[i] = fork(); | |
| if (pid_a[i] == 0) { // only the child executed this code | |
| printf(1, "%d %d\n", getpid(), getpid() + 4); | |
| exit(getpid() + 4); | |
| } | |
| } | |
| sleep(5); | |
| printf(1, "%d\n", pid_a[3]); | |
| ret_pid = waitpid(pid_a[3], &exit_status, 0); | |
| printf(1, "%d+%d+%d\n", ret_pid, exit_status, pid_a[3] + 4); | |
| sleep(5); | |
| printf(1, "%d\n", pid_a[1]); | |
| ret_pid = waitpid(pid_a[1], &exit_status, 0); | |
| printf(1, "%d+%d+%d\n", ret_pid, exit_status, pid_a[1] + 4); | |
| sleep(5); | |
| printf(1, "%d\n", pid_a[2]); | |
| ret_pid = waitpid(pid_a[2], &exit_status, 0); | |
| printf(1, "%d+%d+%d\n", ret_pid, exit_status, pid_a[2] + 4); | |
| sleep(5); | |
| printf(1, "%d\n", pid_a[0]); | |
| ret_pid = waitpid(pid_a[0], &exit_status, 0); | |
| printf(1, "%d+%d+%d\n", ret_pid, exit_status, pid_a[0] + 4); | |
| sleep(5); | |
| printf(1, "%d\n", pid_a[4]); | |
| ret_pid = waitpid(pid_a[4], &exit_status, 0); | |
| printf(1, "%d+%d+%d\n", ret_pid, exit_status, pid_a[4] + 4); | |
| ret_pid = waitpid(9999, &exit_status, 0); | |
| printf(1, "%d\n", ret_pid); | |
| // printf(1, "\n This is the parent: Child# %d has exited with status | |
| // %d\n",ret_pid, exit_status); | |
| ret_pid = waitpid(9999, (int *)0xffffffff, 0); | |
| printf(1, "%d\n", ret_pid); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment