CREATING CHILD PROCESS USING FORK

CREATING CHILD PROCESS USING FORK IN UNIX PROGRAMS

#include
#include
#include
#include
#include
main()
{
int pid,m=0;
pid=fork();
if(pid==0)
{

printf("%d\n",m);
printf("CHILD PROCESS STARTED :");
printf("The child process ID 1: %d\n",getpid());
printf("The parent of child process ID 2: %d \n",getppid());
}
else
{
sleep(5);
printf("%d\n",m);
printf("PARENT PROCESS STARTED :");
printf("The parent process ID 3:%d \n",getpid());
printf("The Parent of Parent process ID 4: %d \n",getppid());
}
}

Program to find File types

FINDING FILE TYPES USING UNIX PROGRAMS

#include
#include
#include
#include
#include
main()
{
char fname[30];
int i,ch;
struct stat buf;
do
{
printf("\n Enter the filename: \n");
scanf("%s",fname);
i=lstat(fname,&buf);

if(i==0)
{

if(S_ISREG(buf.st_mode))
printf("\n It is a Regular File");
if(S_ISDIR(buf.st_mode))
printf("\n It is a Directory File");
if(S_ISCHR(buf.st_mode))
printf("\n It is a Character File");
if (S_ISBLK(buf.st_mode))
printf("\n It is a Block File");
if(S_ISFIFO(buf.st_mode))
printf("\n It is a Fifo File");
if(S_ISLNK(buf.st_mode))
printf("\n It is a Link File");
if(S_ISSOCK(buf.st_mode))
printf("\n It is a Socket File");

}

printf(“DO YOU WANT TO CONTINUE:\n”);
scanf(“%d”,&ch);
}
while(ch!=0);
}

FILE OPERATIONS USING UNIX PROGRAMMING

FILE OPERATIONS USING UNIX PROGRAMMING

#include
#include
#include
#include
#include

main()
{
int fd,ch,n,len,p;
char name[20],str[50],buf[40];
do
{
printf("Menu\n");
printf("1.Create a file\n");
printf("2.Open a file\n");
printf("3.Writing to a file\n");
printf("4.Reading from a file\n");
printf("5.Exit\n");
printf("Enter the choice[1-4]:\n");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("Enter the filename:\n");
getchar();
gets(name);
fd=creat(name,S_IRWXU);
printf("File Created\n");
close(fd);
break;
case 2:
printf("Enter the filename to open:\n");
getchar();
gets(name);
fd=open(name,O_RDWR);
printf("%d",fd);
if(fd!=3)
{
fd=open(name,O_CREAT O_RDWR);
printf("File is created\n");
}
else
printf("File Already Exist\n");
close(fd);
break;
case 3:
printf("Enter the filename to Write:\n");
getchar();
gets(name);
fd=open(name, O_RDWR);
printf("X=%d",fd);
if (fd==3)
{
printf("File Already Exist\n");
printf("Are you want to Append or overwrite:\n");
printf("0-Append\n");
printf("1-Overwrite\n");
printf("Enter your Choice[1-0]:");
scanf("%d",&ch);
if(ch==0)
lseek(fd,0,SEEK_END);
else
lseek(fd,0,SEEK_SET);
}
else
{
fd=open(name,O_CREAT O_RDWR,S_IRWXU);
printf("New File is created\n");
}
printf("Enter the String to Write\n");
scanf("%s",buf);
len=strlen(buf);
write(fd,buf,len);
close(fd);
break;
case 4:
printf("Enter the filename to read the content:\n");
getchar();
gets(name);
fd=open(name,O_RDWR);
if(fd==3)
{
printf("already exist!\n");
printf("From where you want to read :\n 0-Starting position \n (1-n)-other postion :\n");
scanf("%d",&p);
lseek(fd,p,SEEK_SET);
n=read(fd,buf,10);
buf[n]='\0';
printf("The content of the file is...%s\n",buf);
}
else
{
fd=open(name,O_CREAT O_RDWR,S_IRWXU);
printf("Newly Created No data to Read!");
}
close(fd);
break;
}
}
while(ch<=4);
}

SAMPLE JAVA BEAN PROGRAM

SAMPLE JAVA BEAN PROGRAM TO DRAW DIFFERENT SHAPES

/* */

import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.applet.*;
import java.applet.Applet;
//import java.io.Serrializable;

public class gkbean extends Applet
{

public void init()
{
setSize(400,400);
setBackground(Color.white);
repaint();
}

public void paint(Graphics g)
{
g.drawRect(50,50,100,100);
g.setColor(Color.blue);
g.fillRect(150,150,200,200);
g.setColor(Color.red);
g.drawLine(200,200,300,300);
}
}

FILE OPERATIONS USING RMI

FILE CONCATENATION USING RMI

PROGRAM:

//Interface

import java.rmi.*;
public interface FileIn extends Remote
{
public String copyFile1(String Name)throws Exception;
public String copyFile2(String Name)throws Exception;
}

//Implementation

import java.rmi.*;
import java.rmi.server.*;
import java.io.*;

public class FileIm extends UnicastRemoteObject implements FileIn
{
public File f1,f2;
public FileReader f3,f4;
public FileIm() throws Exception
{ }

public String copyFile1(String fileName) throws Exception
{
f1=new File(fileName);
f3=new FileReader(f1);
int x;
String res="";
while((x=f3.read())!=-1)
{
res=res+(char)x;
}
f3.close();
return res;
}
public String copyFile2(String fileName) throws Exception
{
f2=new File(fileName);
f4=new FileReader(f2);
int y;
String res1="";
while((y=f4.read())!=-1)
{
res1=res1+(char)y;
}
f4.close();
return res1;
}
}

//Client

import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class FileCli
{
public File f5;
public FileWriter f6;
public FileCli() throws Exception
{
f5=new File("prince.txt");
f6=new FileWriter(f5);
String s1=" ";
String s2= " ";
String s=" ";
FileIn inter1=(FileIn)Naming.lookup("rmi://172.16.1.45/mca1");

FileIn inter2=(FileIn)Naming.lookup("rmi://172.16.1.45/mca2"); s=inter1.copyFile1("King.txt"); s1=inter2.copyFile2("Queen.txt"); s2=s+s1;
f6.write(s2,0,s2.length());
f6.close();
}
public static void main(String[]arg) throws Exception
{
FileCli Object=new FileCli();
}
}







//Server

import java.rmi.*;
import java.rmi.server.*;
public class FileSer
{
public FileSer() throws Exception
{
FileIm Obj1=new FileIm();
FileIm Obj2=new FileIm();
Naming.rebind("mca1",Obj1);
Naming.rebind("mca2",Obj2);
System.out.println("Server Starts...");
}
public static void main(String[]arg) throws Exception
{
FileSer Object=new FileSer();
}
}

Addition of Two numbers using RMI

The program for addition of two numbers using the RMI concept in Java

// interface
import java.rmi.*;

public interface RmiExample extends Remote
{
public int add(int a ,int b) throws RemoteException;
}


//implementation

import java.rmi.*;
import java.rmi.server.*;
public class RmiImpl extends UnicastRemoteObject implements RmiExample
{
public RmiImpl() throws RemoteException
{}
public int add(int a,int b) throws RemoteException
{
return a+b;
}
}


//client

import java.rmi.*;
import java.rmi.server.*;
public class Client
{
public static void main(String[]arg) throws Exception
{
RmiExample r=(RmiExample)Naming.lookup("rmi://172.16.1.45/mca");
System.out.println("The Addition of a+b"+r.add(5,5));
}
}






//server

import java.rmi.*;
import java.rmi.server.*;
public class Server
{
public static void main(String[]arg) throws RemoteException
{
RmiImpl Obj=new RmiImpl();
try
{
Naming.rebind("mca",Obj);
}
catch(Exception e){}

System.out.println("Server Started...");
}
}