Create a fan of processes and chain of processes. Ask the user for the number of processes to be created.

CODE: (i) CHAIN PROCESS
#include <stdio.h>
#include<unistd.h>
void main()
{
  pid_t cid=0;
int n,i,a=5,j;
printf("enter numbers of Processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
 cid=fork();
 if(cid!=0)
 {
   printf("%d Process id:%d Child:%d\n",i,getpid(),cid);
   break;
 }
}
}

OUTPUT:

(ii) FAN PROCESS
#include <stdio.h>
#include<unistd.h>
void main()
{
  pid_t cid=0;
int n,i,a=5,j;
printf("enter numbers of Processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
 cid=fork();
 if(cid!=0)
 {
   printf("%d Process id:%d Child:%d\n",i,getpid(),cid);
 }
 else
    break;
}
}
OUTPUT:





Program that will create a new child process using fork() system call.


The fork() System Call:
System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of fork():
  • If fork() returns a negative value, the creation of a child process was unsuccessful.
  • fork() returns a zero to the newly created child process.
  • fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.
Therefore, after the system call to fork(), a simple test can tell which process is the child. Please note that Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<errno.h>
int main(int argc, char *argv[])
{
pid_t pid;
pid=fork();
if (pid == 0) {
printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), getppid());
}
else {
   printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), pid);
}
return 0;
}

Output:





















A Java program for Calculator

Program code:


package UdpServer;

import java.awt.*;
import java.awt.event.*;
 
 
public class calci extends Frame
{
 
public boolean setClear=true;
double number, memValue;
char op;
 
String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." };
String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" };
String memoryButtonText[] = {"MC", "MR", "MS", "M+" };
String specialButtonText[] = {"Backspc", "C", "CE" };
 
MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];
MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];
MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];
MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];
 
Label displayLabel=new Label("0",Label.RIGHT);
Label memLabel=new Label(" ",Label.RIGHT);
 
final int FRAME_WIDTH=325,FRAME_HEIGHT=325;
final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;
final int TOPX=30, TOPY=50;
///////////////////////////
calci(String frameText)//constructor
{
super(frameText);
 
int tempX=TOPX, y=TOPY;
displayLabel.setBounds(tempX,y,240,HEIGHT);
displayLabel.setBackground(Color.BLUE);
displayLabel.setForeground(Color.WHITE);
add(displayLabel);
 
memLabel.setBounds(TOPX,  TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);
add(memLabel);
 
// set Co-ordinates for Memory Buttons
tempX=TOPX;  
y=TOPY+2*(HEIGHT+V_SPACE);
for(int i=0; i<memoryButton.length; i++)
{
memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this);
memoryButton[i].setForeground(Color.RED);
y+=HEIGHT+V_SPACE;
}
 
//set Co-ordinates for Special Buttons
tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);
for(int i=0;i<specialButton.length;i++)
{
specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this);
specialButton[i].setForeground(Color.RED);
tempX=tempX+2*WIDTH+H_SPACE;
}
 
//set Co-ordinates for Digit Buttons
int digitX=TOPX+WIDTH+H_SPACE;
int digitY=TOPY+2*(HEIGHT+V_SPACE);
tempX=digitX;  y=digitY;
for(int i=0;i<digitButton.length;i++)
{
digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this);
digitButton[i].setForeground(Color.BLUE);
tempX+=WIDTH+H_SPACE;
if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}
}
 
//set Co-ordinates for Operator Buttons
int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;
int opsY=digitY;
tempX=opsX;  y=opsY;
for(int i=0;i<operatorButton.length;i++)
{
tempX+=WIDTH+H_SPACE;
operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this);
operatorButton[i].setForeground(Color.RED);
if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;}
}
 
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent ev)
{System.exit(0);}
});
 
setLayout(null);
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setVisible(true);
}
//////////////////////////////////
static String getFormattedText(double temp)
{
String resText=""+temp;
if(resText.lastIndexOf(".0")>0)
    resText=resText.substring(0,resText.length()-2);
return resText;
}
////////////////////////////////////////
public static void main(String []args)
{
new calci("Calculator - JavaTpoint");
}
}
 

 
class MyDigitButton extends Button implements ActionListener
{
calci cl;
 

MyDigitButton(int x,int y, int width,int height,String cap, calci clc)
{
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
}

static boolean isInString(String s, char ch)
{
for(int i=0; i<s.length();i++) if(s.charAt(i)==ch) return true;
return false;
}

public void actionPerformed(ActionEvent ev)
{
String tempText=((MyDigitButton)ev.getSource()).getLabel();
 
if(tempText.equals("."))
{
 if(cl.setClear)  
    {cl.displayLabel.setText("0.");cl.setClear=false;}
 else if(!isInString(cl.displayLabel.getText(),'.'))
    cl.displayLabel.setText(cl.displayLabel.getText()+".");
 return;
}
 
int index=0;
try{
        index=Integer.parseInt(tempText);
    }catch(NumberFormatException e){return;}
 
if (index==0 && cl.displayLabel.getText().equals("0")) return;
 
if(cl.setClear)
            {cl.displayLabel.setText(""+index);cl.setClear=false;}
else
    cl.displayLabel.setText(cl.displayLabel.getText()+index);
}//actionPerformed
}//class defination
 
/********************************************/
 
class MyOperatorButton extends Button implements ActionListener
{
calci cl;
 
MyOperatorButton(int x,int y, int width,int height,String cap, calci clc)
{
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
}
 
public void actionPerformed(ActionEvent ev)
{
String opText=((MyOperatorButton)ev.getSource()).getLabel();
 
cl.setClear=true;
double temp=Double.parseDouble(cl.displayLabel.getText());
 
if(opText.equals("1/x"))
    {
    try
        {double tempd=1/(double)temp;
        cl.displayLabel.setText(calci.getFormattedText(tempd));}
    catch(ArithmeticException excp)
                        {cl.displayLabel.setText("Divide by 0.");}
    return;
    }
if(opText.equals("sqrt"))
    {
    try
        {double tempd=Math.sqrt(temp);
        cl.displayLabel.setText(calci.getFormattedText(tempd));}
            catch(ArithmeticException excp)
                    {cl.displayLabel.setText("Divide by 0.");}
    return;
    }
if(!opText.equals("="))
    {
    cl.number=temp;
    cl.op=opText.charAt(0);
    return;
    }
// process = button pressed
switch(cl.op)
{
case '+':
    temp+=cl.number;break;
case '-':
    temp=cl.number-temp;break;
case '*':
    temp*=cl.number;break;
case '%':
    try{temp=cl.number%temp;}
    catch(ArithmeticException excp)
        {cl.displayLabel.setText("Divide by 0."); return;}
    break;
case '/':
    try{temp=cl.number/temp;}
        catch(ArithmeticException excp)
                {cl.displayLabel.setText("Divide by 0."); return;}
    break;
}//switch
 
cl.displayLabel.setText(calci.getFormattedText(temp));
//cl.number=temp;
}//actionPerformed
}//class
 
/****************************************/
 
class MyMemoryButton extends Button implements ActionListener
{
calci cl;
 

MyMemoryButton(int x,int y, int width,int height,String cap, calci clc)
{
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
}

public void actionPerformed(ActionEvent ev)
{
char memop=((MyMemoryButton)ev.getSource()).getLabel().charAt(1);
 
cl.setClear=true;
double temp=Double.parseDouble(cl.displayLabel.getText());
 
switch(memop)
{
case 'C':  
    cl.memLabel.setText(" ");cl.memValue=0.0;break;
case 'R':  
    cl.displayLabel.setText(calci.getFormattedText(cl.memValue));break;
case 'S':
    cl.memValue=0.0;
case '+':  
    cl.memValue+=Double.parseDouble(cl.displayLabel.getText());
    if(cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0")  )
        cl.memLabel.setText(" ");
    else  
        cl.memLabel.setText("M");    
    break;
}//switch
}//actionPerformed
}//class
 
/*****************************************/
 
class MySpecialButton extends Button implements ActionListener
{
calci cl;
 
MySpecialButton(int x,int y, int width,int height,String cap, calci clc)
{
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
}
//////////////////////
static String backSpace(String s)
{
String Res="";
for(int i=0; i<s.length()-1; i++) Res+=s.charAt(i);
return Res;
}
 
//////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent ev)
{
String opText=((MySpecialButton)ev.getSource()).getLabel();
//check for backspace button
if(opText.equals("Backspc"))
{
String tempText=backSpace(cl.displayLabel.getText());
if(tempText.equals(""))  
    cl.displayLabel.setText("0");
else  
    cl.displayLabel.setText(tempText);
return;
}
//check for "C" button i.e. Reset
if(opText.equals("C"))  
{
cl.number=0.0; cl.op=' '; cl.memValue=0.0;
cl.memLabel.setText(" ");
}
 
//it must be CE button pressed
cl.displayLabel.setText("0");cl.setClear=true;
}//actionPerformed
}//


Output




JAVA TCP program to create a simple Calculator.

Client Side Code:

package serverclient1;
import java.util.*;
import java.io.*;
import java.net.*;

public class ClientCalci {
    public static void main(String arg[]){
        try{
                             Socket s=new Socket( "localhost",1234);
                             Scanner sc=new Scanner(System.in);
                             DataOutputStream dos=new DataOutputStream(s.getOutputStream());
                             DataInputStream din=new DataInputStream(s.getInputStream());
                             System.out.print("Enter your choice: ");
                             int c=sc.nextInt();
                             dos.write(c);
                             System.out.println("Enter Two Number : ");
                             int a=sc.nextInt();
                             dos.write(a);
                             int b=sc.nextInt();
                             dos.write(b);
                             int res=din.readInt();
                             System.out.println("Result is:" +res);
                           
}
                        catch(Exception e){}
    }

}


Server Side Code :

package serverclient1;
    
    
import java.io.*;
import java.lang.*;
import java.net.*;

public class ServerCalci{
public static void main(String arg[]){
try{
int a,b,c,d;
int ch;
ServerSocket ss = new ServerSocket(1234);
while(true){
Socket s = ss.accept();
BufferedReader bin=new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
 System.out.print("1. Addition\n");
             System.out.print("2. Subtraction\n");
             System.out.print("3. Multiplication\n");
            System.out.print("4. Division\n");
             System.out.print("5. Exit\n\n");
ch=bin.read();
switch(ch)
            {
                    case 1 : 
                    a = bin.read();
                    b = bin.read();
                    c = a + b;
                    System.out.print("Result = " + c);
dout.writeInt(c);  
                  break;
                case 2 : 
                    a = bin.read();
                    b = bin.read();
                    c = a - b;
                    System.out.print("Result = " +c);
dout.writeInt(c); 
break;
                case 3 : 
                    a = bin.read();
                    b = bin.read();
                    c = a * b;
                    System.out.print("Result = " +c);

dout.writeInt(c); 
                    break;
                case 4 : 
                    a = bin.read();
                    b = bin.read();
                    c = a / b;
                    System.out.print("Result = " +c);
dout.writeInt(c);                     
break;
                case 5 : System.exit(0);
                    break;
                default : System.out.print("Wrong Choice!!!");
                    break;
            }

}
}
catch(Exception e){}
}
}


Output

For Addition

For Subtraction

 
 


For Multiplication
 
 


RECHARGE YOUR PHONE FOR FREE............

Now this is quite interesting to recharge for free. there are different and many ways to get free recharge.This will compensate your monthly mobile expenses .


  1. Free ATM
    • First of all, install Free ATM app from Google Play-store.
    • Now login with your valid credentials and using this code: 3AFV89 you will get instant Rs.20 in your account.
    • Explore and install given apps you like and get reward for it.
    • The minimum recharge amount is Rs.40.
    • Enjoy and share it with your friends.

      2. mCent
  • This is most frequently use app in India. The steps to install this app are:
  • Click here to install mcent.
  • Login with your credentials and explore.
  • Install apps and earn money easily.
  • Minimum recharge amount is Rs.10.

      3. Taskbucks
  • This app is every fast processing for the rewards points so that you redeem it fast.The steps to install this app are:
  • Click here to install Taskbucks .
  • Login with your credentials and explore.
  • Install apps and earn money easily.
 


Easy way to make Money

There are many different ways to earn money online.Almost everybody in today’s world want to earn money online. There are thousand of ways to earn cash by doing online work.One of the way is to install different android apps and fulfill their respective tasks and earn money. Another way way is to Use the Google Ad sense which is an effective way to earn money monthly nearly about(2000-4000) every month according to the visitors in your blog or website.
Today, I'am going tell you about 100% success full approved way;

  1. Install App Make Money from Google Play.
  2. Login with your valid details and with this referral code : 30693J 
  3. You will get 100 credit points .
  4. Now just explore the app and earn money by install another app.
  5. When you reach the 5000 credit points you can payout the credits with $500.
  6. Enjoy with earning and share this with your friends. 


Dynamic Method Dispatch



Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.
·       By restating an important principle: a super class reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. Here is how. When an overridden method is called through a super class reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time. When different types of objects are referred to, different versions of an overridden method will be called.
·       In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed.
·        Here is an example that illustrates dynamic method dispatch:

// Dynamic Method Dispatch
 class A {
  void callme(){
   System.out.println("Inside A's callme method");
   }
 } 
class B extends A {// override callme()
  void callme() {
   System.out.println("Inside B's callme method");
  }
 } 
class C extends A {  // override callme()
  void callme() {
  System.out.println("Inside C's callme method");
   }
 } 
class Dispatch {
  public static void main(String args[]) {
   A a = new A(); // object of type A
   B b = new B(); // object of type B
   C c = new C(); // object of type C 
  A r; // obtain a reference of type A
   r = a; // r refers to an A object
   r.callme(); // calls A's version of callme
   r = b; // r refers to a B object
   r.callme(); // calls B's version of callme
   r = c; // r refers to a C object
   r.callme(); // calls C's version of callme
   }

 }

   OUTPUT


How to crack Aptitude Question In GATE and many Competitive Examination

As you know the aptitude in the competitive examination is quite for more challenging to clear it. But believe me it is the easy stuff t...