Wednesday, March 28, 2012

Using the example program material and thread

class TestSinkronisasi {
private java.util.Random random = new java.util.Random();
public void callMe(String data) {
System.out.print("[");
67
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print(data);
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("]");
}
}
class ThreadBaru extends Thread {
private String data;
private TestSinkronisasi obj;
public ThreadBaru(TestSinkronisasi obj,String data) {
this.obj = obj;
this.data = data;
start();
}
public void run() {
obj.callMe(data);
}
}
class DemoThread {
public static void main(String[] args) {
TestSinkronisasi obj = new TestSinkronisasi();
ThreadBaru thread1 = new ThreadBaru(obj,"Superman");
ThreadBaru thread2 = new ThreadBaru(obj,"Batman");
ThreadBaru thread3 = new ThreadBaru(obj,"Spiderman");
//wait until all the child thread finishes
try{
68
thread1.join();
thread2.join();
thread3.join();
}catch(InterruptedException e) {
System.out.println("The main thread is interrupted " + e);
}
}
}

Processes and Threads in Windows

The flavor of processes and threads in Windows differs somewhat from that presented earlier in this chapter-Windows threats a process as a unit for resource allocation, and uses a thread as a unit for concurrency. Accordingly, a Windows process does not operate by itself; it must have at least one thread inside it.. The OS maintains a handle table for each process in which it stores handles to resources. A process inherits some resource handles from its parent and my open new resources when needed; the OS adds their handles to the handle table when an open succeeds.

Windows uses three control blocks to manage a process. Each process has an executive process block. It contains fields that store the process id, memory management information, address of the handle table, a kernel process block for the process and a process environment block. the kernel process block contains scheduling information for threads of the process, such as the processor affinity for the process, the state of the process and pointers to the kernel thread blocks of its threads.

The executive process block and the kernel process block are situated in the system address space. The process environment block is situated in the user address space because it contains information that is used by the loader to load the code to be executed, and by the head manager.

The control blocks employed to manage a thread contain information about its operation, and about the process containing it. The executive thread block contains a kernel thread block, a pointer to the executive process block of the process that contains the thread and impersonation information for the thread. The kernel thread block contains information about the kernel stack of the thread and the thread-local storage, scheduling information for the thread, and a pointer to its thread environment block, which contains its id and information aobout its synchronization requirements.

Windows support the notion of a job as a method of managing a group of processes. A job is represented by a job object, which contains information such as handles to processes in it, the job-wide CPU time limit, per process CPU time limit, job scheduling class which sets the time slice for the processes of the job, processor affinity for processes of the job, and their priority class. A process can be a member of only one job; all processes created by it automatically belong to the same job.

Process creation The Windows executive provides a general model of process and thread creation in which the process that issues a create call for a process or thread is not necessarily its parent. The create call takes a handle to the parent of the new process or thread as a parameter. This service is used by a server process to perform impersonation. It creates a thread in a client process so that it can access resources with the client's privileges.

The semantics of process creation depend on the environment subsystem used by an application process. In the Win/32 and OS/2 operating environments, a process has one thread in it when it is created; it is not so in other environments supported by the Windows OS. Hence process creation is actually handled by an environment subsystem DLL that is linked to an applicaton process. After creation, it passes the id of the new process or thread to the environment subsystem process so that it can manage the new process or thread appropriately.

Creation of a child process by an application process in the Win/32 environment proceeds as follows: The environment subsystem DLL linked to the application process makes a syste call to create a new process. The call is handled by the executive. It creates a process object, initializes it by loading the image of the code to be executed, and returns a handle to the process object. The environment subsystem DLL makes a second system call to create a thread, and in the new process and returns its handle. 

The DLL now sends a massage to the environment subsystem process, passing it the process and thrad handles, and the id of their parent process. The environment subsystem process enters the process handle in the table of proceesses that currently exist in the environtment and enters the thread handle in the scheduling data structures. Control now returns to the applications process.