Name: Pavan Kumar Srinivasulu
Student ID: 07700005991
Email: [email protected]
This C code demonstrates the creation of 9 child processes using the fork()
system call in a Unix-like environment. Here's a breakdown of how the code works:
-
Header Files: Includes necessary header files like
stdio.h
,sys/wait.h
, andunistd.h
. -
Main Function:
- Prints the PID of the original process (parent process).
- Declares a variable
pid
of typepid_t
. - Performs two
fork()
calls consecutively:- The first
fork()
creates two processes, resulting in one parent and one child process. - The second
fork()
is immediately after the first one, resulting in each of the two processes creating an additional child process. This leads to a total of four processes (two parent processes and two child processes).
- The first
- Enters an
if
block that checks if the process ID (pid
) is equal to0
(indicating it's a child process):- Inside this block, two more
fork()
calls are executed, resulting in the two child processes from the previous step creating additional child processes. This leads to a total of ten processes (two initial parents + two initial children + six additional child processes).
- Inside this block, two more
-
Output: Prints the PID of each process (
getpid()
) and its parent's PID (getppid()
). It should display information for all ten processes created during the execution. -
Wait for Child Processes: Uses
wait(NULL)
in awhile
loop to wait for each child process to terminate before the program itself terminates (wait()
returns-1
when no child processes are left to wait for).
In summary, this code uses nested fork()
calls to generate multiple child processes, controlling the process count to ensure no parent process generates more than two child processes, as specified in the assignment. The code then displays the PIDs of all created processes and their parent processes.
- Verify if
g++
is Installed: Open a terminal and enterg++ --version
. If installed, it will display the installed version. If not, you'll need to install the GNU Compiler Collection (GCC) to compile C++ code.
- Ubuntu/Debian: Use
apt-get
:sudo apt-get update sudo apt-get install g++
- CentOS/Fedora: Use
yum
:sudo yum update sudo yum install gcc-c++
- Use a text editor (e.g.,
nano
,vim
,gedit
,code
) to write your C++ code. Save it with the.cpp
extension. Here,create_nine_processes.cpp
.
-
Open Terminal: Navigate to the directory containing your
create_nine_processes.cpp
file. -
Compile using g++:
g++ -o output_file_name source_file_name.cpp
Replace
output_file_name
with a desired name for the compiled executable andsource_file_name.cpp
with your C++ file name.here,
g++ -o output_hw1 create_nine_processes.cpp
-
Execute the Compiled Program:
./output_file_name
Replace
output_file_name
with the name specified during compilation.here,
./output_hw1
- You should now see the output of your C++ program in the terminal.
- Ensure the C++ file is saved with the
.cpp
extension. - Make sure you have necessary permissions to read, write, and execute files in the directory.
- Resolve any errors or warnings that occur during compilation before running the executable.
- Debug any issues that arise during execution, considering the terminal output and any error messages.