#include #include #include #include #include ///////////////////////////////////////////////////// // Decrements the Environment Variable HIPPO, // Checks the return values, and prints the output ///////////////////////////////////////////////////// void DecrementHIPPO(char* name, int decr) { char cmvar[50]; char* pmvar = cmvar; pmvar = getenv("HIPPO"); if(pmvar == NULL) perror("Failed to get evironment variable"); int tmpHippo=atoi(pmvar); tmpHippo -= decr; char mvar[50]; sprintf(mvar, "%d", tmpHippo); if(setenv("HIPPO", mvar, 1) != 0) perror("Failed to set environment variable"); printf("%s:\tHIPPO=%d\n", name, tmpHippo); fflush(0); } //////////////////////////////////////////////////// // Displays the hostname, userid, and other misc // system information relative to the session /////////////////////////////////////////////////// void ShowSystemInfo() { char hostname[255]; // Display the hostname if(gethostname(hostname, 255) != 0) perror("Failed to get the hostname."); else printf("Hostname: %s\n", hostname); // Display the userid char userid[255]; cuserid(userid); if(userid[0] == '\0') perror("Failed to get the userid."); else printf("UserID: %s\n", userid); // Display the current time time_t currentTime; if(!time(¤tTime)) perror("Failed to get systemtime."); else // Note: ctime() adds a linefeed at the end of the string printf("Time: %s", ctime(¤tTime)); // Display the working directory char currentDir[255]; if(!getcwd(currentDir, 255)) perror("Failed to get working directory."); else printf("WorkingDir: %s\n", currentDir); } main() { //////////////////////////////////////////////////////// // Parent Process Setup /////////////////////////////////////////////////////// // Getpid() never fails, so we don't need to check it. pid_t pid1 = getpid(); printf("\nParent Process Info:\n"); printf("ProcessID: %d\n", pid1); ShowSystemInfo(); if(putenv("HIPPO=11") != 0) perror("Failed to initialize environment variable."); ///////////////////////////////////////////////////// // Create the child processes ///////////////////////////////////////////////////// int pid, status1, status2; // Create the first process (parent skips this if) if((pid=fork()) == 0) { int pidC1 = getpid(); int ppidC1 = getppid(); printf("C1 started. ID: %d, ParentID: %d\n", pidC1, ppidC1); fflush(0); sleep(1); //HIPPO=11 DecrementHIPPO("C1", 2); //HIPPO=9 sleep(4); DecrementHIPPO("C1", 3); //HIPPO=6 sleep(7); DecrementHIPPO("C1", 3); //HIPPO=3 sleep(10); // Change the working directory of C1 char currentDirOld[255]; if(!getcwd(currentDirOld, 255)) perror("Failed to get old C1 working directory."); if(chdir("..") != 0) perror("C1 Failed to change directory."); char currentDirNew[255]; if(!getcwd(currentDirNew, 255)) perror("Failed to get new C1 working directory."); else { printf("Current (C1) working directory changed from:\n%s to %s \n", currentDirOld, currentDirNew); } printf("Listing contents of the new C1 working directory:\n"); fflush(0); execl("/bin/ls", "-l", (char *) 0); exit(1); } // Create the second process (parent skips this if) else if((pid=fork()) == 0) { int pidC2 = getpid(); int ppidC2 = getppid(); printf("C2 started. ID: %d, ParentID: %d\n", pidC2, ppidC2); fflush(0); sleep(2); //HIPPO=11 DecrementHIPPO("C2", 3); //HIPPO=8 sleep(5); DecrementHIPPO("C2", 3); //HIPPO=5 sleep(8); DecrementHIPPO("C2", 3); //HIPPO=2 sleep(11); // Display the working directory char currentDir[255]; if(!getcwd(currentDir, 255)) perror("Failed to get working directory."); else printf("C2 WorkingDir: %s\n", currentDir); DecrementHIPPO("C2", -1); printf("\n"); fflush(0); exit(2); } ////////////////////////////////////////////// // Only the parent process executes this code ////////////////////////////////////////////// //HIPPO=11 DecrementHIPPO("P1", 1); //HIPPO=10 sleep(3); DecrementHIPPO("P1", 3); //HIPPO=7 sleep(6); DecrementHIPPO("P1", 3); //HIPPO=4 sleep(9); DecrementHIPPO("P1", 3); //HIPPO=1 wait(&status1); wait(&status2); printf("Both children have exited.\nFinal Value: "); fflush(0); DecrementHIPPO("P1", -1); }