Sunday, 14 March 2010


Creation of thread/child process:
In java, whenever you start the thread (thread.start()), a new child process i.e. a thread gets created and there multithreading/multiprocessing starts.

This is wrong. It is not a child process. It is a thread. And it is not multiprocessing. It is multithreading. [This was added later.]

To implement it in Java there are 2 options:
1) Extend thread class by your class say myThread.
2) Implement the run method.
3) Create object of myThread.
4) With this object call the start method.

The other option is:
1) Implement the interface runnable
2) Implement the method run()
3) Pass the object of this class to the constructor of Thread class and create the thread class’s object.
4) Call the start method with the object of thread class.

To implement this in C,
Fork() procedure is used.
fork() spawns the child process from parent. The child process has the same attributes as that of parents except process id. Both run concurrently.

Consider an example is as follows –
Say you have an attribute called newProcID in the parent procedure.
newProcID = fork();
fork() creates the child process and returns 2 IDS to 2 processes. Parent process gets the ID of the child process and child process gets the newProcID = 0. As a child process has the same attributes as that of the parent, it also gets the newProcID attribute and hence,

the code in the parent process –
newProcID = fork();
If (newProcID == 0)
Newfunction(); // this part will be executed in child process
childID = newProcID; // this part will be executed in parent process.

Now some difference in java and C implementation by memory point of view:
When we call fork() method in C, it spawns the child process. The address space of parent gets copied. Both child and parent process have identical address spaces but at different address locations. So whatever attributes( variables and their values) parent process has , same the child process has. You can access the attributes of the parent process in the child process.
But in Java, when we call start method of thread, it spawns the child process/thread and then it runs the run method of that thread class. (The class that extends thread class).
Run() method can access attributes of only the class by object of which we invoked start method and hence may not have the parent class’s attributes. It means in Java, it does not copy the whole memory stack for child process. Instead it creates new memory stack. So child cannot access the parent’s attributes.
This is all what I understand from how the implementation goes in both the languages. I’m not sure about what I have written about memory management in Java. Still trying to find the answer.

This is again wrong. Whatever implementation in Java I have talked about is of multithreading. And what I have talked about in C is of multiprocessing. So the difference which I have indicated here is between multithreading and multiprocessing not between what Java provides and what C provides.

No comments: