
Aero T.
asked 07/23/21Write a C program with the parent process and a child process communicating with each other using a pipe.
Write a C program with the parent process and a child process communicating with each other using a pipe. The child process will execute the shell command provided by the user via command line arguments. The result of executing this shell command is passed to the parent process using a pipe. The parent process will write the result into a file called result.txt and acknowledge the user on the screen with the shell command and the total number of bytes in the result. For simplicity, the shell command contains only the command name, no argument.
1 Expert Answer

Patrick B. answered 07/24/21
Math and computer tutor/teacher
import java.net.*;
import java.io.*;
class AeroServer
{
public static void main(String args[])
{
Socket socket=null;
long pid=0;
try
{
ServerSocket serverSocket = new ServerSocket(80);
while (true)
{
socket = serverSocket.accept();
new AeroServerThread(socket,(++pid)%Long.MAX_VALUE).start();
} //while
}
catch(IOException ex)
{
System.out.println(" server socket exception error occurs !");
}
}
}
//==========================================================
import java.lang.Thread;
import java.net.*;
import java.io.*;
class AeroServerThread extends Thread implements Runnable
{
protected Socket socket;
protected long pid;
protected InputStream inputStream;
protected OutputStream outputStream;
public AeroServerThread(Socket socket, long pID) { this.socket = socket; pid=pID; }
protected byte[] ProcessRequest(String cmdReqStr)
{
System.out.println("server:>" + cmdReqStr);
return(cmdReqStr.getBytes());
}
public void start()
{
try
{
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
while (true)
{
int numBytes=-1; //polls
while (numBytes<1) { numBytes = inputStream.available(); }
byte inbuff[] = new byte[numBytes];
inputStream.read(inbuff);
String cmdReqStr = new String(inbuff);
if (cmdReqStr.compareToIgnoreCase("BYE")!=0)
{
byte outbuff[] = ProcessRequest(cmdReqStr);
outputStream.write(outbuff);
}
else
{
inputStream.close();
outputStream.close();
socket.close();
break;
} //command
} //while
}//try
catch (IOException ex)
{
System.out.println("Server thread exception occurs ");
return;
}
}
}
//****************************************************************************************************//
import java.net.*;
import java.io.*;
import java.util.Scanner;
class AeroClient
{
public void Go()
{
try
{
Scanner scanner = new Scanner(System.in);
Socket socket = new Socket("127.0.0.1",80);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
while (true)
{
System.out.print(" input :>");
String strbuff = scanner.nextLine();
outputStream.write(strbuff.getBytes());
int numBytes=-1;
while ( (numBytes = inputStream.available())<1) { }
byte inbuff[] = new byte[numBytes];
inputStream.read(inbuff);
System.out.println("client :>"+new String(inbuff));
if (strbuff.compareToIgnoreCase("BYE")==0) { break; }
} //while
socket.close();
} //try
catch (Exception ex)
{
}
}
public static void main(String args[])
{
new AeroClient().Go();
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
I'm interested, but only have cygwin unix emulator and Dev C++, and a shabby one at best, which do not support this type of IPC. The best I can offer is IPC via sockets using Java.07/23/21