Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-

ID: 3872680 • Letter: #

Question

(Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes a string argument from the command line and outputs the string in reversed order You have two constraints: Constraint 1: Each process can output at most one character. If you want to output more than a single character, you must fork off one or more processes in order to do that, and each of the forked processes in turn outputs a single character t 2: Each process can fork-off at most one other process. After the call to program string invert with the command line argument, the output should appear, and no more processes should be running, in addition to the shell. Test your program on any UNIX/LINUX machine, and turn in the source code as part of the written assignment. (The source code should be at most a few lines long.)

Explanation / Answer

#include <stdio.h> /* needed for printf() and fprintf() */

#include <stdlib.h> /* needed for EXIT_FAILURE/EXIT_SUCCESS */

#include <string.h> /* needed for strerror() */

#include <unistd.h> /* needed for fork() and getpid() */

#include <sys/types.h> /* needed for pid_t */

#include <iostream>

using namespace std;

string result;

void print(string s){

pid_t pid;

cout<<"once ";

pid = fork();

cout<<"twice ";

if(pid == 0){

//child

print(s)

exit(0);

}

else if (pid < 0){

}

else{

cout<<result;

wait(NULL);

}

return 0;

}