CSS 503
Lab Work 1: Process Management
Professor: Munehiro Fukuda
Lab work date: See the syllabus
#include <iostream> // cout, cerr #include <queue> // stl queue #include <sys/types.h> // fork, wait #include <sys/wait.h> // wait #include <unistd.h> // fork using namespace std; queue<int> pids; // stores a list of processses from the parent to a leaf process void recursive_creation( int leaves ) { if ( leaves > 1 ) { pids.push( getpid( ) ); // fork a left child // fork a right child // wait for one of the children // wait for the other children exit ( 0 ); } else { // I'm a leaf process while( pids.size( ) > 0 ) { // print out a list of my ascendants cout << pids.front( ) << " "; pids.pop( ); } cout << getpid( ) << endl; // print out myself exit( 0 ); } } int main( int argc, char* argv[] ) { // validate arguments if ( argc != 2 ) { cerr << "usage: lab1 #leaves" << endl; return -1; } int leaves = atoi( argv[1] ); if ( leaves < 1 ) { cerr << "usage: lab1 #leaves" << endl; cerr << "where #leaves >= 1" << endl; return -1; } recursive_creation( leaves ); return 0; }The main program receives #leaves of a tree that it should create as follows:
css503@uw1-320-18 lab1]$ ./lab1 1 869 [css503@uw1-320-18 lab1]$ ./lab1 2 870 871 870 872 [css503@uw1-320-18 lab1]$ ./lab1 3 873 874 875 873 877 873 874 876 [css503@uw1-320-18 lab1]$ ./lab1 4 878 879 880 878 879 881 878 882 883 878 882 884 [css503@uw1-320-18 lab1]$ ./lab1 5 885 886 887 888 885 886 887 890 885 889 891 885 886 893 885 889 892 [css503@uw1-320-18 lab1]$Complete the recursive_creation() function so that the program runs as specified above.