make
/ make all
make clean
run
Default value (50,000) used if no args provided.
lab2
lab2 4000
A vagrant virtual-box running Ubuntu was used to compile and run the code using a Makefile while remaining on the Windows host machine.
IDE: Intellij Clion 2020.2.4
OS: Windows 10 Ubuntu 18.04
Compiler: g++ clang
Tools: Vagrant 2.2.10 Virtual Box 6.1
A program illustrating the use of inter-process communication using pipes and polling two separate forked child processes. Each child calculates the decimal places of a mathematical constant (pi or e). Each individual number is piped to the parent; the parent then writes the number to the appropriate files (pi.dat or e.dat). Progress is intermittently displayed by the parent process printing output to console.
- Two pipes are created (one for each child process) for inter-process communication using the
pipe()
function. - A
struct pollfd
is initiated with the rules forpoll()
using the pipe file descriptors. - Two files are opened and each is associated with a file descriptor.
- A fan of child processes is created using the system call
fork()
.
Parent code
- Two
std::map
s are created to hold a count of how many times a certain number is received. After every 1000 digits received from a child, the distribution of the numbers is printed using the child's map. - A
while()
loop is used to repeatedly poll, i.e. to check if input from either of the two child processes is available.The poll()
timeout argument is used to break out of this loop. The return value ofpoll()
is checked for errors. - If
poll()
returns ready events from either of the children's file descriptors and no errors were set, the parent attempts toread()
from the pipe and, if successful, performs awrite()
of the character to the appropriate file. Additionally, for the purposes of the distribution output, each child's count variable andstd::map
is updated.
Child code
Each child simply calls their respective calculator, the results of which are piped to the parent.