Subscribe by Email


Showing posts with label Multi-threading. Show all posts
Showing posts with label Multi-threading. Show all posts

Thursday, May 9, 2013

What is a thread? What is meant by multi-threading?


About Thread

- A thread is a smaller instance of a process i.e., a sequence of instructions and an operating system scheduler can manage it independently. 
- A thread is sometimes also called as the light weight process. 
- The way these threads and processes are implemented is different for different operating systems. 
- But in majority of the cases threads are contained within a process. 
- The same process can have more than one thread. 
- These threads have to share the resources including memory while different processes may not share these. 
- In simple words, we can say that the instructions or code and the context or the values of the process are shared by its constituting threads. 

In this article we focus on threads and multi-threading.

- Multi-threading is a task of multiprocessor systems.
- But even single processor systems can do it by time division multiplexing just like the multitasking. 
- In TDM context switch occurs between the many threads. 
- This happens many times and so it seems to the user that a number of processors are being executed concurrently.
- However, in multiprocessor systems concurrency can be truly achieved since every processor processes one thread and so many threads are executed simultaneously. 
- Both multiprocessor and time threading is supported by most of the modern operating systems with help from the process scheduler. 
- The threads can only be manipulated through a system and this all is facilitated by the kernel of the operating system. 
- This is why some implementations have been named as the kernel thread. 
- An example of kernel thread is the LWP or the lightweight process and it shares same state as well as info. 
- Some programs even use user space threads when threading with the help of signals and timers etc. 
- These programs perform a kind of ad hoc time slicing. 
- Some may take threads and processes to be the same but there is a considerable difference between the two:
  1. Processes are independent whereas the threads are a subset of the processes.
  2. More state information is contained in threads whereas the same process state, resources and memory are shared by all the threads contained in that process.
  3. Address spaces of different processes are different whereas the same address space is also shared by the threads.
  4. IPC or the inter–process communication is the only medium for the processes to communicate with each other.
  5. The threads within the same process are switched with context switch which is faster than that of the processes.

Features of Multi-threading

- Multi-threading is now among the widespread programming models. 
- The major characteristic feature of this model is that multiple threads can execute within the same process context. 
- Even though the resources of the process are shared by the threads, they execute independently. 
- The most widespread application of this model is in parallel computing.
- Full advantage of this technology can be taken only when it is applied to a multiprocessor system or a distributed system. 
- This is so because the program threads have a natural tendency to heed to the true concurrent execution. 
- But in these cases, necessary precautions must be taken for avoiding the race conditions and other undesirable behavior. 
- For the correct manipulation of data thread synchronization is also important. - Mutually exclusive operations are another requirement of the threads for preventing the simultaneous modification of the common data. 
- If these primitives are used carelessly, then it can lead the system to a deadlock. 
- Another feature of multi-threading is that it is always responsive to I/P. 
- This can be contrasted with the single threaded applications where if one block occurs, the whole program freezes.


Wednesday, April 24, 2013

What is multi-tasking, multi-programming and multi-threading?


When it comes to computing, there are 3 important tasks that are inter-related concepts namely multi-programming, multitasking and multi-threading. 

What is Multitasking?

- This has actually emerged out of the need of multitasking since while the system performed one task a lot of time was wasted. 
- As their needs grew,people wanted the computer to perform many tasks at the same time. Multi-tasking is what we call it. 
- Here, multiple tasks or processes are carried out simultaneously.
- The common processing resources i.e., the main memory and the CPU are shared by these processes. 
- If the system has only one CPU to work with, then it can only run one task at a time. 
- Such systems seek to multi-task by scheduling all the processes required to be carried out. 
- It runs one task and the other one waits in the pipeline.
The CPU is reassigned to all the tasks turn by turn and this is termed as a context switch. 
- When this happens very frequently, it gives an illusion that the processes are being executed in parallel. 
- There are other systems called multi-processor machines which have more than one CPU and can perform a number of tasks greater than the number of CPUs. 
- There are a number of scheduling strategies that might be adopted by the operating systems and they are:
Ø  Multi – programming
Ø  Time – sharing
Ø  Real – time systems

What is Multi-Programming?

- Earlier we had very slow peripheral devices and therefore the CPU time was a luxury and so expensive. 
- Whenever a program was being executed for accessing a peripheral, the CPU was to keep waiting for the peripheral to finish with processing the data. 
- It is very inefficient. 
- Then came the concept of multi–programming which was a very good solution. 
-  When the program reached the waiting status, its context was stored in the memory and the CPU was given some other program to execute. 
- This processing continued till all the processes at hand were completed. 
- Later,developments such as VMT or virtual machine technology and virtual memory greatly increased the efficiency of the multi – programming systems. 
With these two technologies the programs were able to make use of the OS and the memory resources just as they were being used by the currently executing programs. 
- However, there is one drawback with multi–programming which is that is does not guarantees that all programs will be executed in a timely manner. 
- But then also it was of a great help in processing multiple batches of programs.

What is Multi-threading?

 
- With multi–tasking a great improvement was seen in the throughput of the computer systems. 
- So programmers found themselves implementing programs in sets of cooperating processes.
- Here, all the processes were assigned different tasks like one would take input, other one would process it and a third one would write the output to the display. 
- But for this, there was a requirement of tools that allowed an efficient exchange of the data.
- Threads were an outcome of the idea that the processes can be made to cooperate efficiently if their memory space is shared.
- Therefore, threads can be defined as the processing running in a memory context that is same for all. 
- These threads are said to be light – weight since there is no need for a change of memory context for switching between them. 
- The scheduling followed here is of the preemptively. 


Sunday, April 21, 2013

What is a virtual memory?


- Virtual Memory is a memory management technique that is a compulsory requirement for the multi-tasking kernels. 
- With this technique, the architecture of a computer can be virtualized to different types of computer data storage such as disk drive storage and RAM i.e., the random access memory. 
- With this, programmers do not have to worry about designing applications that will suit this kind of storage. 
- The programs can be designed keeping in consideration only one kind of memory i.e., the virtual memory. 
- This memory behaves just like the usual memory but more than that. 
- It offers a direct as well as contiguous memory space for various operations. - Some of us might think that the programming the software might get difficult with the virtual memory. But this is not so.
- Instead the task becomes easy because the fragmentation of the main physical memory is hidden. 
- For achieving this, the burden of the management of the memory hierarchy is delegated to the kernel.
- This has another added advantage which is that the need for handling of overlays in an explicit way via program is eliminated.
- The need for the relocation of a program code or accessing the memory is obviated via relative addressing. 
- This lets the process to be executed in its own dedicated space. 
- The concept of the virtual memory in a more generalized form is called the memory virtualization.
- The modern computer architecture cannot do without the virtual memory. 
The only requirement for implementing the virtual memory is the hardware support that is provided through the memory management unit that is in-built in CPU. 
- For increasing the performance of these virtual memory implementations hardware support can be employed by the virtual machines and emulators. 
Computer systems with old operating systems such as DOS in mainframes  do not possess any functionality of the virtual memory. 
- The first computer that featured the virtual memory was the Apple Lisa that was designed in the year of 1980. 
- It appears that with the use of virtual memory as if every program has a sole access to it. 
- However, there were some older operating systems that had single address space Oss. 
- These operating systems used to process tasks in a single space. 
- This space is consisted of the virtual memory. 
- Very consistent response times are a requirement of the special purpose computer systems such as the embedded systems. 
- These systems do not prefer to use the virtual memory as it may decrease the determinism. 
- The unpredictable traps producing unwanted jitter while carrying out the I/O operations might be triggered by the virtual memory systems. 
- This happens because the cost of the embedded hardware is kept low. 
- The operations are included in the software rather than including them in the hardware. 
- This technique is termed as the bit banging. 
- The older programs needed to have logic for the management of both primary and secondary memory. 
- One such logic was that of the overlaying. 
- Therefore, virtual memory was introduced as a method for extending the primary memory and make this extension easy for the programmers.  
- In order to allow multi–tasking and multi–programming, the memory in the early systems was divided between many programs. 

Implementation of the virtual memory saw many problems. One among those problems was of the dynamic address translation that was difficult to be implemented and  quite expensive also. 


Saturday, April 20, 2013

Explain the concepts of threads and processes in operating system?


Threads and processes are an important part of the operating systems that have features of multi–tasking and parallel programming. These come under the sole concept of ‘scheduling’. Let us try to understand these concepts with the help of an analogy.

- Consider the process to be a house and threads are its occupants. 
- Then, process is like a container having many attributes. 
- These attributes can be compared to that of a house such as number of rooms, floor space and so on. 
- Despite having so many attributes, this house is a passive thing which means it can’t perform anything on its own. 
- The active elements in this situation are the occupants of the home i.e., the threads. 
- The various attributes of the house are actually used by them. 
- Since you too live in a house you must have got an idea how it actually works and behaves. 
- You do whatever you like in the house if only you are there. 
- What if another person starts living with you? You just can’t do anything you want to do. 
- You cannot use the washroom without making sure that the other person is not there. 
- This can be related to multi – threading. 
- Just as a part of estate is occupied by the house, an amount of memory is occupied by the process. 
- Just as the occupants are allowed to freely access anything in the house, similarly the occupied memory is utilized by the threads that are a part of that process i.e., the access to memory is common. 
- If one process allocates some memory, it can be accessed by all other threads also. 
- If such a thing is happening, it has to be made sure that from all the threads, the access to the memory is synchronized. 
- If it cannot be synchronized, then it becomes clear that the memory has been allocated specifically to a thread. 
- But in actual, things are a lot more complicated because at some point of time everything has to be shared. 
- If one thread wants to use some resource that is already under use by some other thread, than that thread has to follow the concept of mutual exclusion. 
An object known as the mutex is used by the thread for achieving exclusive access to that resource. 
- Mutex can be compared to a door lock. 
- Once a thread locks this, no other thread can use that resource until the mutex is again unlocked by that thread. 
- Mutex is one resource that a thread uses. 
- Now, suppose there are many threads waiting to use the resource when mutex is unlocked, the question that arises now is that who will be next one to use the resource. 
- This problem can be solved by either deciding on the basis of length of wait or on basis of priority. 
- Suppose there is a location that can be accessed by more than one threads simultaneously.
- You want to have only a limited number of threads using that memory location at any given point of time. 
- This problem cannot be solved by mutex but with another resource called semaphore. 
- Semaphore with a count of 1 is the resource that can only be used by one thread at a time. 
- In semaphore of greater count more threads can access it simultaneously.  
- It just depends up on how you characterize or set the lock.


Facebook activity