CLASS :FY-MCA.

BLOG WRITTEN BY:

1.   Mayur Shinde (59).                    2. Rohit Sharma (52).

3.   Abhishek Tiwari (68).                4.  Rutuja Taywade (67).

5.   Ayushi Roy (53).

                            THREAD LIBRARIES IN                                   OPERATING SYSTEM

First of all we will understand what is Process and Thread?

When a program begins to run, we refer to it as a process. Previously, a computer could only support one task or software at a time. However, in today's computer, numerous processes or programs can execute at the same time. Even a single program can be linked to a large number of other processes.It is called process that happens when a program begins to run at a given moment in time.

Now try to understand what are the threads?

A thread is the basic unit of a running process or a basic unit of CPU use. Each program may be linked to a number of processes, each of which may contain a number of threads. As a result, in the previous system, each process had only one thread. However, a single process now contains numerous threads or execution units.

Thread comprises of 

1.   A thread ID.

2.   A program counter.

3.   A register set.

4.   A stack.

Apart from that, it shares its code section, data section, and other operating system resources, such as open files and signals, with other threads in the same process. This is how thread is constructed.

A single control thread exists in a traditional/heavyweight procedure. A process with several control threads may do various tasks at the same time. It is now visible how important thread is and how using threads increases a system's efficiency.

Using the diagrams below, let's try to grasp the concept of thread.

On the right side of the diagram is the single threaded process, often known as a classic or heavy weight process. The entire block is treated as a process, with a single thread, a code section, data section, file section, registers, and stack all belonging to this process. Because this process only has one thread, it can only complete one task at a time. 



Fig 1. Single Thread process & Multi-threaded process

In comparison, here's a left-side diagram of a multi-threaded process; the entire block represents a multi-threaded process, thus it's a single process with multiple threads over here.

In this diagram having three thread in this process and each thread have their own stack, registers and on the top the code section, data, files belonging to this process are shared by these thread. This process may accomplish several jobs at once since each thread will complete a different task at a time. By the above diagram and concept understand Multi-threaded process is more efficient as compared to Single-threaded process and it make computation faster, efficient.

Most of the system that use today follow the Multi-threaded Process

Thread contains types of thread libraries. The thread libraries below is illustrate  with example

Thread Libraries are used to provide programmers an API for creating, managing thread and also can use already created thread. For example in C, Java language wants to use any string function which is in Hydro files. Thread is nothing but an interface regarding How to work on the thread and create. Thread libraries can be implemented in one of two ways.

The first strategy is to provide a user-space library with no kernel support, implying that any thread library will be managed in user space .User space is where the library's code and data structures depart .This implies that executing a library function results in a user-space local function rather than a system call. For example, a function can be developed in C programming or any other programming language, and it can also be found in libraries and activated by a header file in a program.


Fig 2.Thread Implementation

The second method is to write a kernel-level library that exists in kernel space and assists the operating system. In this instance, the library's code and data structure are located in kernel space. Invoking a function in the library's API often results in a kernel system call. A system call is an application mode function that requests service from the operating system. System calls will switch the thread from user to kernel mode, perform the system call handler, and then return to user mode. 
Thread libraries are divided into three categories:

Fig 3.Type of Thread Libraries

1. POSIX Thread:

  • Pthread is another name for POSIX thread. Pthread is an API for creating and synchronizing threads. Pthread can be used to offer user- or kernel-library.
  • The Pthread extension is a POSIX standard expansion. Pthread comes pre-installed on Solaris, Linux, Mac OS X, Tru64, and Windows (with the help of freeware). Data can be declared globally, making it easy for threads to exchange it.
  • Before proceeding, one thread should wait for the other to reconnect. Pthread knows when to start running because of the runner () function. At the start of a program, a single control thread is started in the main loop ().
  • main () generates a second thread after some start and passes control to the runner () method.. Both threads share the global data. 

  • The pthread.h header file must be included in all Pthreads program.
  • Let understand the Pthread library with help of example.

Fig 4. Program Example of Pthread

So the functions used in program i.e. Pthread library methods pthread create (&thread,NULL,sum array,(void*)NULL), pthread join(thread, NULL) are used to new and manage threads.

Work flow of Pthread

The variable part of the array 'a' and 'sum' is the global data that will be shared by all threads in the application
1.   The program start with the main().
2.   Then it executes the pthread_t  thread, this command would generate the thread id that would be created.
3.   The pthread create (&thread, NULL, sum array, (void*)NULL) command would then new a thread.The first of the four arguments is thread id, which indicates that the newly formed thread is linked to the thread id specified in step[2],with the second parameter, it accepts NULL as the default thread argument, with the third argument, pass the name of the function from which the thread should start execution, in this case sum array, and with the last argument, pass to start routine. NULL is passed as no argument, and it is passed as a void pointer.
4.   Now we have two thread the initial thread main() and next thread executing sum_array().For now, the execution of main() thread is put on hold as it calls the function pthread_join(thread[i],NULL) this function transfers the control to the thread with thread id ‘thread’ i.e the thread created to execute sum_array() function.
5.   The outcome of the summation procedure is returned by the sum array() method.  

2. Win32 Thread:

  • Win32 Thread is libraries, provide only kernel-level library on Window System. Win32 threads are available on Microsoft Window operating system. For example, Win92 available on Windows10. 
  • In all operating system default thread is there called Win32 thread. The kernel and user level threads can be created and managed using Win32 threads.
  • In numerous ways, the Win32 thread library is identical to Pthread. When using the Win32 thread, you must use the Win32 API to include the window's header file. In the same way that global variables are utilized in Pthreads, global variables can share threads in the Win32 thread library. 
  • Threads are generated in the Win32 API using the CreateThread () function, which is supplied a list of thread properties. This method receives a collection of thread attributes, similar to Pthreads. 
  • Security information, stack size, and a flag that can be set to indicate if the thread should start in a suspended state are among these attributes.
  • Remember how in the Pthread program, the pthread join() command had the parent thread wait for the child thread? In the Win32 API, we utilise the WaitForSingleObject () function to do the same thing, which forces the creating thread to stall until the child thread has finished. 
  • So here, the functions CreateThread(), WaitForSingleObject(), CloseHandle() are the function in Win32 thread library which   are used to create and manage threads.

Fig 5. Program Example of Win32 Thread


    Work Flow of  Win32Thread:

1.   The main() function begins the program's execution.The statement HANDLE hThread creates a pointer ‘hThread’ that will be used to access the Win32 library functions.

2.   The DWORD threadID statement indicates the thread id that will be established later in this program The statement  hThread = CreateThread (NULL, 0, ThreadFun1, NULL, 0, &threadID) creates a thread and gives its reference to hThread. 

3.   The CreateThread() function accept six arguments where Null is for default security attributes, 0 is the default stack size, ThreadFun1 is the separate thread function, NULL is input data parameter to thread Function,  0 is default flag creation and threadID is the id of the thread that is to be associated to the ThreadFun1 thread function.

4.    The next it print statement and once it gets complete the thread is terminated with CloseHandle(hThread) function. Finally, the output is displayed.


3. Java Thread:

·   Since Java generally runs on JVM. Threads are the fundamental model execution in Java program. The implementation of threads is based upon whatever operating system and hardware the JVM is running on i.e Pthread or Win32 depending on system.

·  Threads are created unintentionally while writing a short or large program. Threads can be created and managed using the Java language and API.

·  Each Java application contains at least one thread. A simple Java program with just the main () function operates as a single thread in the JVM.

·   Java thread execute on top of operating system. Java thread API is typically implement using a thread library available on the host system.

·      In a Java application, there are two methods for creating threads.

·      One solution is to build a new class inherited from the thread class and override the run () method.Another, more widely used method is to define a class that implements the runnable interface, as shown below:


Fig 6. Define Interface Runnable

·   When to construct a Thread object, the start () method is used to create a new thread.

·   Because Java is an object-oriented programming language, it has no concept of global data. When two or more threads must share data, the sharing is accomplished by passing a reference to the share object to the appropriate thread. The parent thread in Java uses join () method wait for child threads to finish before processing.

Fig 7. Program Example Java Thread

In the Java program above there were two threads one executes the main () and other executes the run ().

The three thread libraries listed above are useful for creating and managing threads. Threads in the Win32 and PThread thread libraries can readily communicate data because it can be declared globally. In case of java program is not there.

Advantages of Thread :

1.  Responsiveness – Interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user.

2. Resource sharing – The benefit of sharing code and data is that allows an application to have several different thread of activity within the same address space.

3.  Economy – It is expensive to allocate memory and resources for the development of processes. It is more cost effective to construct and context-switch threads since threads share resources with which they are associated.

4. Utilization of multiprocessor architecture –Multi-threading can be greatly increased in a multiprocessor environment, where each thread may be running in parallel on a different process.

 

Thank you for Reading...

Comments

Post a Comment