Table of Content
Process Management using wait and exit
Exit() System Call
The exit() function is how a process tells the operating system that it’s finished and ready to be removed. When a process uses exit(), it can also send some final information back to its parent process. The parent can then collect this information using the wait() function.
Wait() System Call
- The wait() function is a way for a parent process to pause and wait for one of its child processes to finish. When a child process ends, the parent process can continue its work.
- A child process can end in different ways: by calling exit(), returning from main, or receiving a signal that tells it to stop.
- The wait() function is useful because it helps coordinate the activities of parent and child processes, ensuring that the parent doesn’t continue until the child has completed its task.
Behavior of Wait()
- When a parent process uses wait(), it will pause until any of its child processes finishes. If a child process ends, wait() tells the parent which child (by its ID number) has finished.
- If more than one child process ends at the same time, wait() will choose one of them randomly to report back. If the parent doesn’t have any child processes when it calls wait(), the function immediately returns with a special signal (-1) to indicate that there are no children to wait for.
WIFEXITED: Wait If EXITED
- This macro checks if the child process terminated normally (i.e., via an
exit()call or a return frommain()).
- This macro checks if the child process terminated normally (i.e., via an
WEXITSTATUS: Wait EXIT STATUS
- This macro extracts the exit status of the child process if
WIFEXITEDis true.
- This macro extracts the exit status of the child process if
Explanation:
If
fork()returns0, the code inside theif (child_pid == 0)block is executed by the child process. The child process prints a message, pauses for 2 seconds usingsleep(2), and then exits with a status of5.If
fork()returns a positive number, the parent process executes the code in theelse if (child_pid > 0)block. The parent waits for the child process to finish usingwait(). Once the child process terminates, the parent checks if it ended normally usingWIFEXITED(status). If true, the parent retrieves and prints the child’s exit status usingWEXITSTATUS(status).If
fork()fails, the code in theelseblock runs, printing an error message and returning1to indicate the failure.
execlp() System Call
- The execlp() function is used to replace the current process with a new program. It’s often used after creating a new process with fork().
- When you use execlp(), it loads a new program into memory, erasing the current program, and starts running the new one.
- This is useful when you want a child process to run a different program than its parent.
Zombies Processes
When a process finishes but its parent hasn’t yet used wait() to acknowledge its end, it becomes a “zombie” process. A zombie is a process that has finished running but still uses a small amount of system resources. The parent process is responsible for “reaping” its zombie children by using wait() to collect their exit information. Once this is done, the operating system can fully remove the child process from its records.
Recent Posts
- Navigating Playinexch feels like flipping through a well-worn travel journal filled with unexpected detours and easy routes
- Test Post Created
- Here2WIN Casino welcome package in 2026: maximize rewards for Canadian players
- Navegar en 1win nunca fue tan sencillo, incluso para quienes arrancan desde cero
- Navegar en 1win nunca fue tan sencillo, incluso para quienes arrancan desde cero
Recent Comments
Archives
- September 2026
- August 2026
- July 2026
- June 2026
- May 2026
- October 2025
- September 2025
- August 2025
- June 2025
- October 2024
- September 2024
- August 2024
- June 2024
- January 2024
- November 2023
- October 2023
- September 2023
- August 2023
- June 2023
- May 2023
- March 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
Categories
- 03_07_PL_AKS
- 08_07_fr_work
- 21
- aks 1 en
- aks 1 pl
- aks 18
- aks 28
- aks 3 en
- aks 31
- Angular
- AWS
- Backend Development
- Betriot
- Big Data
- Blog
- Cloud
- crypto 01
- crypto 05
- crypto 15
- crypto 19 en
- crypto 20
- crypto 20.05
- crypto 21.05
- crypto 29
- crypto 5
- Database
- Deployment
- DevOps
- Docker
- Entertainment
- fi
- Frontend Development
- Game
- GitHub
- Google Cloud Platform
- hu
- Installations
- it
- Java
- JavaScript
- jeux
- KZ 12633
- Linux
- MySQL
- Networking
- News
- Nine Casino
- nl
- no
- NodeJS
- Online casino
- Operating System
- Pin Up Casino
- Post
- public
- Python
- Python Flask
- Report
- Security
- Server
- sport
- SpringBoot
- Subdomain
- sushicasino 13
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
- Παιχνίδια
- Στοιχηματικές
- Блог
Search
Recent Post
Navigating Playinexch feels like flipping through a
- 8 September, 2026
- 5 min read
Test Post Created
- 8 September, 2026
- 1 min read
Here2WIN Casino welcome package in 2026: maximize
- 8 September, 2026
- 5 min read