30 lines
1.4 KiB
Plaintext
30 lines
1.4 KiB
Plaintext
Assignment name: process_termination
|
|
Expected files: process_termination.c
|
|
Allowed functions: memset, printf, malloc, free, write, fork, kill, exit, waitpid,
|
|
sem_open, sem_close, sem_post, sem_wait, sem_unlink,
|
|
usleep, gettimeofday
|
|
-------------------------------------------------------------------------------
|
|
|
|
Create a program that demonstrates process termination and cleanup:
|
|
1. Launch 5 child processes that run in a loop (each iteration takes 500ms)
|
|
2. Each child has a random "death time" between 2-5 seconds
|
|
3. Parent process monitors all children using waitpid() with WNOHANG
|
|
4. When a child "dies" (exits), parent detects it and kills all remaining children using kill()
|
|
5. Parent prints which child died first and performs cleanup
|
|
|
|
Output format:
|
|
- "Child <id> (PID: <pid>): Iteration <n>"
|
|
- "Child <id> (PID: <pid>): Dying after <time>ms"
|
|
- "Parent: Child <id> (PID: <pid>) has died!"
|
|
- "Parent: Terminating remaining children..."
|
|
- "Parent: Child <id> (PID: <pid>) terminated"
|
|
- "Parent: All processes cleaned up"
|
|
|
|
Usage: `./process_termination`
|
|
|
|
Hint: This simulates what happens in philosophers_bonus when one philosopher dies.
|
|
Use waitpid() with WNOHANG to check if a child has exited without blocking.
|
|
kill(pid, SIGTERM) or kill(pid, SIGKILL) terminates a process.
|
|
Proper cleanup is critical - all child processes must be terminated.
|
|
This is why philosophers_bonus needs a monitoring mechanism in the parent.
|