here is the problem:
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. Here comes
the twist: 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. 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.
and i really dunno how to use fork to make this work, should I recursive call the function to make this work? or by creating more child processes? I am really new to this fork function and have no idea how to really apply it in this situation, so please help me!!! thank you!
#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\n";
pid = fork();
cout<<"twice\n";
if(pid == 0){
//child
print(s)
exit(0);
}
else if (pid < 0){
//eror
}
else{
cout<<result;
wait(NULL);
}
return 0;
}
int main(){
string s;
cout<<"enter string you want to invert\n";
cin>>s
print(s);
}