Process Communication 👍
RMI Client 👍
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class RMIClient {
private UpperCaseServer server;
public RMIClient() {}
public void startClient() throws RemoteException, NotBoundException {
Registry registry = [Link]("localhost", 1199);
server = (UpperCaseServer)[Link]("Server");
}
public String toUpCase(String argument) {
String result = null;
try {
result = [Link](argument);
} catch (RemoteException e) {
[Link]();
throw new RuntimeException("Could not contact server");
}
return result;
}
}
Run Client 👍
package [Link];
import [Link];
import [Link];
import [Link];
public class RunClient {
public static void main(String[] args) throws RemoteException,
NotBoundException {
RMIClient client = new RMIClient();
[Link]();
Scanner in = new Scanner([Link]);
while(true) {
[Link]("Input >");
String line = [Link]();
if([Link]("exit")) break;
String result = null;
try {
result = [Link](line);
[Link]("Result > " + result);
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}
}
Interface 👍 [Link]
package [Link];
import [Link];
import [Link];
public interface UpperCaseServer extends Remote {
String toUpCase(String str) throws RemoteException;
}
Server 👍 [Link]
import [Link];
import [Link];
public class RunServer {
public static void main(String[] args) throws RemoteException,
AlreadyBoundException {
UpperCaseServer server = new ServerImpl();
Registry registry = [Link](1199);
[Link]("Server", server);
[Link]("Server started");
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
public class ServerImpl implements UpperCaseServer {
public ServerImpl() throws RemoteException {
[Link](this, 0);
}
@Override
public String toUpCase(String str) {
return [Link]();
}
}
/**
* Simple program demonstrating shared memory in POSIX systems.
*
* This is the producer process that writes to the shared memory region.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
int main()
{
const int SIZE = 4096;
const char *name = "OS";
const char *message0= "Studying ";
const char *message1= "Operating Systems ";
const char *message2= "Is Fun!";
int shm_fd;
void *ptr;
/* create the shared memory segment */
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
/* configure the size of the shared memory segment */
ftruncate(shm_fd,SIZE);
/* now map the shared memory segment in the address space of the
process */
ptr = mmap(0,SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) {
printf("Map failed\n");
return -1;
}
/**
* Now write to the shared memory region.
*
* Note we must increment the value of ptr after each write.
*/
sprintf(ptr,"%s",message0);
ptr += strlen(message0);
sprintf(ptr,"%s",message1);
ptr += strlen(message1);
sprintf(ptr,"%s",message2);
ptr += strlen(message2);
return 0;
}
Socket Program [Link]
/**
* Client program requesting current date from server.
*
* Figure 3.22
*/
import [Link].*;
import [Link].*;
public class DateClient
{
public static void main(String[] args) {
try {
// this could be changed to an IP name or address other
than the localhost
Socket sock = new Socket("[Link]",6013);
InputStream in = [Link]();
BufferedReader bin = new BufferedReader(new
InputStreamReader(in));
String line;
while( (line = [Link]()) != null)
[Link](line);
[Link]();
}
catch (IOException ioe) {
[Link](ioe);
}
}
}
[Link]
/**
* Time-of-day server listening to port 6013.
*
* Figure 3.21
*
*/
import [Link].*;
import [Link].*;
public class DateServer
{
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(6013);
// now listen for connections
while (true) {
Socket client = [Link]();
// we have a connection
PrintWriter pout = new
PrintWriter([Link](), true);
// write the Date to the socket
[Link](new [Link]().toString());
// close the socket and resume listening for more
connections
[Link]();
}
}
catch (IOException ioe) {
[Link](ioe);
}
}
}
/**
* Example program demonstrating UNIX pipes.
*
* Figures 3.25 & 3.26
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define BUFFER_SIZE 25
#define READ_END 0
#define WRITE_END1
int main(void)
{
char write_msg[BUFFER_SIZE] = "Greetings";
char read_msg[BUFFER_SIZE];
pid_t pid;
int fd[2];
/* create the pipe */
if (pipe(fd) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
/* now fork a child process */
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
if (pid > 0) { /* parent process */
/* close the unused end of the pipe */
close(fd[READ_END]);
/* write to the pipe */
write(fd[WRITE_END], write_msg, strlen(write_msg)+1);
/* close the write end of the pipe */
close(fd[WRITE_END]);
}
else { /* child process */
/* close the unused end of the pipe */
close(fd[WRITE_END]);
/* read from the pipe */
read(fd[READ_END], read_msg, BUFFER_SIZE);
printf("child read %s\n",read_msg);
/* close the write end of the pipe */
close(fd[READ_END]);
}
return 0;
}
[Link]
package osprocess;
import [Link].*;
public class OSProcess
{
public static void main(String[] args) throws IOException {
if ([Link] != 1) {
[Link]("Usage: java OSProcess <command>");
[Link](0);
}
// args[0] is the command that is run in a separate process
ProcessBuilder pb = new ProcessBuilder(args[0]);
Process process = [Link]();
// obtain the input stream
InputStream is = [Link]();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// read the output of the process
String line;
while ( (line = [Link]()) != null)
[Link](line);
[Link]();
}
}
[Link]
package processbuilderdemo;
// important import statements
import [Link];
import [Link];
import [Link];
import [Link];
public class ProcessBuilderDemo
{
// main method
public static void main(String[] argvs) throws IOException
{
ProcessBuilder processBuilder = new ProcessBuilder();
[Link]("[Link]", "/c", "java", "Driver 25");
//[Link](new File("C:\\users"));
[Link](new File("C:\\Users"));
// can also run the java file like this
// [Link]("java", "Hello");
try {
Process process = [Link]();
BufferedReader reader =
new BufferedReader(new
InputStreamReader([Link]()));
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
int exitCode = [Link]();
[Link]("\nExited with error code : " + exitCode);
} catch (IOException e) {
[Link]();
} catch (InterruptedException e) {
[Link]();
}
}
}
Process.C
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ps","ps",NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete\n");
}
return 0;
}