Monday 27 August 2012

Share Folder between Mac OS and Ubuntu in VirtualBox

Last night, I want to share the unp(UNIX network programming) source code between my host Mac OS and Ubuntu in the Virtual Box.
So I summarize some steps:

 Mac OS : Mountain Lion
 Virtual Box: 4.1.20
 Ubuntu: 12.04

1.      setup the share folder in the Mac OS.
         Choose in the VirtualBox's men:  settings -> Shared Folders.
        

2.  Add the new shared folder button
     Choose the Folder Path e.g /User/reg/xxxx
     and You should give the name for it, which will be used in the mount in the Ubuntu
     Select Make permanent  -  if you would like to persist this shared folder definition





3.Now work in the Ubuntu in the Virtual Box
 
 a.   Create the folder to be shared folder mount the Mac OS Shared folder.
   e.g  ~/share
 

  b.   get you id
       
       id
    
  c. Mount the shared folder
     
    sudo mount -t vboxsf -o uid=USER_ID Mac_OS_Shared_Folder_Name Ubuntu_Shared_Name 

  
   e.g.


    sudo mount -t vboxsf -o uid=1000 share ~/share 
 
 IF you meant any issues about vboxsf 
 e.g.
    mount.xboxsf failed no such device

make sure you installed the Virtual Box's Guest Additions
  
The steps about install Guest Additions

   Choose the Virtual Box VM
   Devices ----> Install Guest Additions

   Now the Virtual Box will help you install it,
   if not, you can enter the directory in the Ubuntu  /media/VBOXADDITIONS_xxx
   run the VBoxLinuxAdditions.run


  Now make sure the vboxsf module is right
  No error to run the modprobe vboxsf

4. Now you can use the shared folder.



Others about the  UNP
if you run the ./daytimecocli 127.0.0.1

connect error: Connection refused

you May not start the daytime server

1.    Install the xinetd
       apt-get install xinetd

 2.  modify the /etc/xinetd.d/daytime,
      change the two disable yes -------> disable no

3. Now you can /etc/init.d/daytime restart











Wednesday 11 July 2012

Core Data Brief introduction

What's the Core Data?

Suppose you have the object named "People", it has two variables: name which is the string type, age which is the int type.

class People
{
      public string name;
      public int age;
}

Now I want to these people to persistent, so I use a "place" to store my people.
 No matter which will do these steps:

1.  define the people class.
2.  make one "place" to store it. (xml, binary, sqlite as you like)
3.  make the relation the data from the file with the people class. It can help you map the data to people, and map the people to data.

and so on.....

For the developer, maybe you only care the object, which you can delete, create, update, you don't care how it persistence, the style of it store, how it map to you object...
you only want to change the object, it auto changes in the persistence, you create the object, it auto insert the data in the persistence, you delete the object, it auto delete the data in persistence.

So the Core data help you.
Core Data describes data with a high level data model expressed in terms of entities and their relationships plus fetch request that retrieve entities meeting specific criteria.
Code can retrieve and manipulate this data on a purely object level without having to worry about the details of storage and retrieval.




Let's use this.
1.
First we will create the Data Model to store our "object"





 Choose the Data Model, and name it as you like.


2.
Then we will has the *.xcdatamodel in our project, its home of our object.




And it has three components:  ENTITIES, FETCH REQUESTS, CONFIGURATIONS.

The ENTITY is model that mapped our purely object.
So let add our variable to the Entity. First Add the Entity,Find the Add Entity at the bottom left of the window and click it. A new Entity will appear in the list of entities in the lefthand table, change its name.

 Then you click + button in the Attribute section and edit the Attribute and Type values:




Then we finish the persistence of our data, now we should make our object to connect to these entity. We used the NSManagedObject to do this.



Now we can finish our preparatory work. we want to use these object, we must follow Core Data stack.


Don't be afraid!
Let me show what stack do.

First: We must find our entity. Yep, it's in the file (*.xcdatamodel file that created).
So we use the Managed Object Model.

A managed object model that describes the entities in the stores.

NSManagedObjectModel * managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

Then: There is another component to connect your object to the persistence data, this is the Persistent Store Coordinator.

A persistent store coordinator that aggregates all the stores, and context used it to persistence the object graph, and retrieve the entity information.



 NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

it initializes with model, it must know the entity you want to store and the entity's information.

Next: We will find a place to store your entity by your type. so you must tell persistent object store coordinator, you have a store.

A persistent object store that maps between records in the store and objects in your application.

    NSString *path = [self storePath];
    NSURL *storeURL = [NSURL fileURLWithPath:path];
   
    NSError *error = nil;
   
    if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        [NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
    }

We give a path to store our entity, and the type for our persistence.


Final:  We must use something to manipulate our object,  to connect with the coordinator.
A managed object context that provides a scratch pad for managed objects. 

    NSManagedObejctContext* context = [[NSManagedObjectContext alloc] init];
    [context setPersistentStoreCoordinator:psc];


We general communicate with NSManagedObjectContext.
We can Insert the New entity to the store.

    People *e = [NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:context];
   
    NSError *error = nil;
    [context save:&error];

query the entity in the store

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"People" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
 
 NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
if (mutableFetchResults == nil) 
}
 

delete the entity in the store
 
NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row]; 
[managedObjectContext deleteObject:eventToDelete]; 
 
 
 We can get More information from apple website, I just make a brief introducation.
 Hope you like. 
 
 Reference Link:
 
 wiki: http://en.wikipedia.org/wiki/Core_Data 

 apple: http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/CoreData/cdProgrammingGuide.html







Monday 25 June 2012

State Pattern

What's the State Pattern?

In the Design Pattern:
Allow an object to alter its behavior when its internal state changes. The will appear to change to its class.



File:State Design Pattern UML Class Diagram.svg 



From this UML image, all the request() function calls the state.handle(), the Context don't care the which state is, just call the state.handle(). IF you not use the state pattern, you maybe implement the request() like

     switch():
           case ConcreteStateA:
                  dosomething();
           case ConcreteStateB:
                  dosomething();
when have more than one functions which implement must based on state choose, so I have the many switch case, one day you want to remove one state or add one state, you must modify all the functions which has this state choose. That is very crazy.

For example, the Mars Rovers problem:
the mars rover at the mars surface(like Cartesian Coordinate(x,y)), it has two the actions, turn left and move, and the four direction it faced, south , north, east, west.

so image the original place is (1, 5, N) means the mars rover at
coordinate (1, 5), face the North direction.
when we give the turn left command, it will become (1, 5, W), (very useful Hint from my geography teacher, In the map, up->north, down->south, left->west, right->east ).

when we give the move command, it will become (0, 5, W), the x -1, move the negative direction of x.

So You maybe have the idea for it about state pattern.

Class Rover
{
        State s;
        int x;
        int y;
        turnleft(Rover r){
             s.handleturnleft(this);     // the turnleft function call state handleturnleft function.
        }
        move(Rover r){
              s.handlemove(this);      // the move function call state handlemove function.
         }
        setState(State s){
               this.s = s;
         }
}

interface State
{
       handleturnleft(Rover r);

      handlemove(Rover r);
}

and we have the four the state inherit from State.   Give the NorthState implemention.

class NorthState : State
{
       handleturnleft(Rover r){
               r.setState(new WestState());
       }

      handlemove(Rover r){
             r. y++
      };

}


Mar Rover at (1, 5, N), and send the turn left command what happend?


the Rover->turnleft().

and the Rover's state change from NorthState to WestState.

We don't have the State choose at turnleft(, these hide in the state class,  the NorthState
knows the it turn left, will become the WestState, and It move, make the Rover.y + 1. We have no conditional choose in the function, it's very distinct.

You maybe know where use the State pattern

1.  An object's behavior depends on its state, and it must change its behavior run-time depending on that state.

2.  Operations have large, multiparty conditional statements that depend on the object's state.

The State pattern puts each branch of the conditional in a separate class. This lets you treat the object's the object's state as an object in its own right that vary independently from other objects. 

State pattern puts all behavior associated with a particular state into one object. Because all state specific code lives in a State subclass, new states and transitions can be added easily by defined new subclass. The State class is more easy to share between other classes.


Reference links:

1. Design Pattern Book (GOF)
2. http://en.wikipedia.org/wiki/State_pattern









Thursday 31 May 2012

Linux Kernel Process


 what is the process?
A process is an instance of a computer program that is being executed. But the Processes are more than just the executing program code( often called the text section in Unix, the program has some section with in it(data section, text section, BSS section...)). Because the process also can include the set of resources such ass open files and pending signals, internal kernel data, one or more threads of execution.

Now we can see the Your process in the OS:

At Linux, you may run the command ps,
ps aux

USER             PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
reg              637   5.6 14.9  3266892 623324   ??  S     9:55上午   1:40.51 /Applications/VirtualBox.app/Contents/MacOS/../Resources/VirtualBoxVM.app/Contents




Above this, we can the process has the ID named (PID).

Create the process example.

In Linux, the fork() system calls  make the a process created by duplicating an existing one.
The process that calls fork() is the parent, whereas the new process is the child. The parent resumes execution and the child starts execution at the same place: where the call to fork() returns. The fork() system call returns from the kernel twice: once in the parent process and again in the newborn child process.

#include <unistd.h>
#include <sys/types>
#include <stdio.h>


int main()
{
  pid_t pid;
  printf("The parent calls fork!\n");
  pid = fork();
 
  if(pid < 0)
      printf("error in fork!\n")
   else if(pid == 0)
      printf("The child process, process id is %d\n", getpid());
   else
     printf("The parent process, process id is %d\n", getpid());
  return 0;
}



And the result is:
The parent calls fork!
The parent process, process id is 2377
The child process, process id is 2378
 
For the one process program, it's  amazing to run the if and the else, why it shows this?
Because All the resources owned by the parent are duplicated and the copy is given to the child.
So from the pid = fork() line, the child run the same code with the parent, so "The child process, process id is 2378" printed, the "The parent calls fork!" didn't print, the child process exec from the fork function.



What's in the process?



In the kernel stores the process use the process descriptor of  struct task_struct, which defined in the <linux/sched>.

With the process descriptor now dynamically created via the slab allocator, a new struct thread_info, was created that lives at the bottom of the stack(for stacks that grows down).
The thread_info structure is defined on x86 in <asm/thraed_info.h>.

thread_info->task is the process struct /* main task structure */.

Use the thread_info created at the bottom feature, the linux use it to store current process, because in the x86, the registers is so few to waste to store the current process address indivisibly.

static inline struct thread_info *current_thread_info(void)
{
        return (struct thread_info *)
                 (current_stack_pointer & ~(THREAD_SIZE - 1));
}

#ifdef CONFIG_4KSTACKS
#define THREAD_ORDER    0
#else
#define THREAD_ORDER    1
#endif
#define THREAD_SIZE     (PAGE_SIZE << THREAD_ORDER)
 
IF the  CONFIG_4KSTACKS defined means the STACK_SIZE is 4kb, ELSE

STACK_SIZE is 8kb.

current_stack_pointer is
register unsigned long current_stack_pointer asm("esp") __used;
means esp address .
So the 
(current_stack_pointer & ~(THREAD_SIZE - 1))   means get the current stack pointer address,
i.e. 0x01511fff, we assume the  THREAD_SIZE is 8kb, ~(THREAD_SIZE - 1) = 0xfffffe000, so we can
get the bottom of the stack is 0x0151e000. No matter stack pointer address changed, as long 
as it in the stack, it & ~(THREAD_SIZE - 1)  always get the bottom address of the stack 0x0151e000.
 
  
The Process State:

The state field of the process descriptor describes the current condition of the process. 
Each process on the system is in exactly one of five different states.This
value is represented by one of five flags:
 
 1. Task_Running. (The process is running)
 2. Task_Interruptable (The process is sleeping, it wait the signal to wake up)
 3. Task_Uninterruptable (The process is sleeping, it wait for the kernel function not the signal
, so the signal can't wake up it)
 4. __Task_Stopped (The process is not runnable, and not eligible to run. )
 5. __Task_Traced (it's for the debug.)

Kernel code often needs to change a process’s state.The preferred mechanism is using
set_task_state(task, state);

The process creation:
In the Unix system, you can use the fork() function to create the process, and you will create 
the child process of current process, has own PID, PPID, and some inherited from the parent process.
  
All resources owned by the parent are duplicated and the copy is given to the child.
This approach is naive and inefficient in that it copies much data that might otherwise be shared.Worse still, if the new process were to immediately
execute a new image, all that copying would go to waste. 
So we use the copy-on-write, it means when the child process created, it just get the pointer
to point the parent's data address, not copy the these data, until the child really use it(Write these data,
, read not cause the copy.)  So we delay the copy until the write, improve the process creation.

 
 
Thread:
 
Why the Thread produced?
  1. as times go on, people want to process can work more parallel things, we need them
    shared the memory address, and have the same resources. 
     
     2.  the thread is light weight than the process, it is quicker , easy to create, destroy. In many system,
          the thread creation time is 10-100 quicker than the process creation.
 
    3.  the thread is easy to communicate with other thread, than the process. the process can 
          not enter others address at most time.

Threads are a popular modern programming abstraction.They provide multiple threads of
execution within the same program in a shared memory address space.They can also
share open files and other resources.Threads enable concurrent programming and, on multiple
processor systems, true parallelism. 

Threads like the process shared its address with others processes, so in Linux, thread struct  is process struct,
The thread create clone:
 
clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
 
The process create clone:
 
clone(SIGCHLD, 0); 
 
 
when the thread has more things created than the process creation,
  •  FILES: file descriptor 
  •   FS: filesystem resources
  • VM: address space
the process create the own FS, FILES, VM, not clone from the parent



 
 
 
 

Monday 21 May 2012

The Java singleton pattern thread safe

what's the Singleton pattern?

the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This useful when exactly one object is needed to coordinate action across the system.  (Singleton_pattern)

So How can we implement it?

For the simple implement:


public class Singleton {
        private static Singleton _instance = null;
 
        private Singleton() {   }
 
        public static Singleton getInstance() {
                if (_instance == null) {
                        _instance = new Singleton();
                }
                return _instance;
        }
 


if every time invoke the Singleton.getInstance() method, we always get the _instance in the all system, but this implementation has the problems? It's not the thread safe..
why?




So let show you:
when the TA(Thread A) enter getInstance(), because of _instance == null, So the _instance = new Singleton(), But when the TB(Thread B) also enter the getInstance() method at the same time, it also check the _instance == null (it's the null, because the TB and TA at the same time), so the _instance was created again.
The singleton pattern must be carefully constructed in multi-thread applications.

And I will provide the Java solutions for the thread safe:

1. Eager initialization

public class Singleton {
    private static Singleton _instance = new Singleton();
 
    private Singleton() {}
 
    public static Singleton getInstance() {
        return _instance;
    }
}
 
 

Why this is thread safe?
Because the _instance is the static variable, and Java guarantees that the initialization will be run before the code is accessed by ANY class.
Popular point that the _instance is constructed at the building time, so when the caller invoke getInstance() method, the _instance just at here, return is OK. NO multi-Thread problem.
But it has the "cost", if the cost of creating the instance is not too large in terms of time/resources, you can do, IF not, you may want to switch to lazy initialization.


2. Lazy initialization

why called the lazy? Because the instance will be created at the needed time, not like the Eager initialization, the instance created at building even if you not call the getInstance().
 The Lazy initialization will avoid above situation(creating the instance is not too large in terms of time/resources)

public class Singleton {
        private static Singleton _instance = null;
 
        private Singleton() {   }
 
        public static synchronized Singleton getInstance() {
                if (_instance == null) {
                        _instance = new Singleton();
                }
                return _instance;
        }
}

Because the getInstance() is the synchronized, So anytime only one thread can enter this function. But it also has a problem: For the thread unsafe situation, just the first creation.
at if _instance != null, we are not needed to lock this function, if we add the synchronized in the method, we lock this function anytime.

So the double-checked locking is coming. it is a software design pattern used to reduce the overhead of acquiring a lock at the first testing the locking criterion without actually acquiring the lock.

So maybe you redesign this function:


public class Singleton {
        private static Singleton _instance = null;
 
        private Singleton() {   }
 
        public static Singleton getInstance() { 
              if(_instance == null){
                    synchronized(this) {
                     if (_instance == null) {
                           _instance = new Singleton();
                     }

               return _instance;
        }
}




why the Double-check happened? Because when the Thread acquired the lock, but the another has done the initialization of the instance. So the Double-check happened.

But the subtle problems happened.

1. Thread A enter this function, notice the _instance is not initialized, so it obtains the lock and begins to initialize the _instance.

2.Due to the semantics of some programming languages, the code generated by the compiler is allowed to update the shared variable to point to partially constructed object.
 Popular point that Before A as finished performing the initialization, the thread B enter this function, it check _instance != null(because java call the constructor, it will updated the _instance when the memory allocate, but it not call the construct function, just has the space. ) SO if thread B use the instance, maybe the program will crash.

the problem has been fixed, the volatile keyword now ensure that multiple threads handle the singleton instance correctly.

ublic class Singleton {
        private static volatile Singleton _instance = null;
 
        private Singleton() {   }
 
        public static Singleton getInstance() { 
              if(_instance == null){
                    synchronized(this) {
                     if (_instance == null) {
                           _instance = new Singleton();
                     }

               return _instance;
        }
}


3. Initialization-on-demand holder idiom
 First let us see the implemention

public class Singleton {
        // Private constructor prevents instantiation from other classes
        private Singleton() { }
 
        /**
        * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
        * or the first access to SingletonHolder.INSTANCE, not before.
        */
        private static class SingletonHolder { 
                public static final Singleton instance = new Singleton();
        }
 
        public static Singleton getInstance() {
                return SingletonHolder.instance;
        }
}

You may look this familiar, it looks like the eager initialization, but how this avoid the construct the large object.
Because it takes advantage of language guarantees about class initialization, so java don't construct the static inner class SingletonHodler at the building time. So the construction happen in the getInstance() function.
Since the class initialization phase is guaranteed by the JLS to be serial, and the initialization phase writes the static variable INSTANCE in a serial operation, so the JVM ensure SingletonHolder.instance is serial, is thread safe.


4. The Enum way

public enum Singleton {
        INSTANCE;
        public void execute (String arg) {
                //... perform operation here ...
        }
}

the second edition of his book "Effective Java" Joshua Bloch claims that "a single-element enum type is the best way to implement a singleton"[10] for any Java that supports enums. The use of an enum is very easy to implement and has no drawbacks regarding serializable objects, which have to be circumvented in the other ways.
 This approach implements the singleton by taking advantage of Java's guarantee that any enum value is instantiated only once in a Java program.Since Java enum values are globally accessible, so is the singleton. The drawback is that the enum type is somewhat inflexible. for examole, it dose not allow lazy initialization.

References Links: