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);
}

Recommended Answers

All 4 Replies

can anyone help me??

how come no one help me?????

why does no one help you?

Dead thread is dead. Leave dead thread dead.

On the subject of how to go about doing this there's a few different ways:
A. Use pipes.
B. Use sockets.
C. Use environment variables.
D. Use shared memory.

Also, the OP didn't realize the word would be passed as a parameter on the command-line instead of reading in with a prompt.
Admittedly, this is one of the few times where I've read an entire first post instead of skimming it. (I've gotten in trouble for that.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.