Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Confidential Enterprise Solutions Group Process and Threads.

Similar presentations


Presentation on theme: "1 Confidential Enterprise Solutions Group Process and Threads."— Presentation transcript:

1 1 Confidential Enterprise Solutions Group Process and Threads

2 2 Confidential Enterprise Solutions Group What is Process? they are generated they have a life each process has just one parent eventually they die they have one or more child ( optional)

3 3 Confidential Enterprise Solutions Group Process States As a process executes, it changes state – new: The process is being created. – running: Instructions are being executed. – waiting: The process is waiting for some event to occur. – ready: The process is waiting to be assigned to a processor. – terminated: The process has finished execution.

4 4 Confidential Enterprise Solutions Group Doubt – Program Vs. Process A process invokes or initiates a program. It is an instance of a program that can be multiple and running the same application. Example:- Notepad is one program and can be opened twice. A process is a sequence of tasks.

5 5 Confidential Enterprise Solutions Group Process Conditions which terminate processes Normal exit (voluntary) Error exit (voluntary) Fatal error (involuntary) Killed by another process (involuntary)

6 6 Confidential Enterprise Solutions Group Threads What is thread? Threads are different parts of human being.

7 7 Confidential Enterprise Solutions Group Threads A thread is a path of execution through a program’s code, plus a set of resources (stack, register state, etc) assigned by the operating system. A thread lives in one and only one process. A process may have one or more threads. Each thread in the process has its own call stack, but shares process code and global data with other threads in the process. Pointers are process specific, so threads can share pointers.

8 8 Confidential Enterprise Solutions Group Process Vs Thread A Process is inert. A process never executes anything; it is simply a container for threads. Threads run in the context of a process. Each process has at least one thread. A thread represents a path of execution that has its own call stack and CPU state. Threads are confined to context of the process that created them. – A thread executes code and manipulates data within its process’s address space. – If two or more threads run in the context of a single process they share a common address space. They can execute the same code and manipulate the same data. – Threads sharing a common process can share kernel object handles because the handles belong to the process, not individual threads.

9 9 Confidential Enterprise Solutions Group Process Vs Thread

10 10 Confidential Enterprise Solutions Group Benefits from Multithreading – Improve application responsiveness E.g. a GUI thread to read from the user and another performing data base access. The GUI thread can respond to user commands, rather then doing them sequentially – Use multiple processors more efficiently – Improve program structure – Use fewer system resources

11 11 Confidential Enterprise Solutions Group Thread Data State which is maintained per thread basis – Thread ID – uniquely identifies a thread within a process – Register state – including PC, stack pointer – Stack – Signal Mask – Priority – Thread-Private storage

12 12 Confidential Enterprise Solutions Group Thread Synchronization Critical Section Mutex Event Semaphore we will have separate session on synchronization

13 13 Confidential Enterprise Solutions Group Hands on Windows Threads Write a program to create thread. Posix Threads Create a thread and print 1-100 in loop

14 14 Confidential Enterprise Solutions Group Windows Thread HANDLE hThrd = (HANDLE)_beginthread(ThreadFunc, 0, &ThreadInfo); ThreadFunc – the function executed by the new thread void _cdecl ThreadFunc(void *pThreadInfo); pThreadInfo – pointer to input parameters for the thread For threads created with _beginthread the thread function, ThreadFunc, must be a global function or static member function of a class. It can not be a non-static member function.

15 15 Confidential Enterprise Solutions Group pThread pthread_t mythread; if( pthread_create(&mythread, NULL, thread_function, NULL)) ThreadFunc – the function executed by the new thread void * thread_function(void *arg) arg– pointer to input parameters for the thread pthread_join(mythread, NULL)

16 16 Confidential Enterprise Solutions Group

17 17 Confidential Enterprise Solutions Group Thread Synchronization Critical Section Mutex Event Semaphore

18 18 Confidential Enterprise Solutions Group Critical Section Used to synchronize between threads in single process Not kernel object

19 19 Confidential Enterprise Solutions Group Mutex Kernel Object Used to synchronize between threads in process and across process Named and Non-Named Mutex Ex: Single Instance of application

20 20 Confidential Enterprise Solutions Group Event Notifies one or more waiting threads that an event has occurred Auto Reset – Notifies one thread Manual Reset – Notifies one or more threads

21 21 Confidential Enterprise Solutions Group Semaphore Maintains a count between zero and some maximum value limiting the number of threads that are simultaneously accessing a shared resource The state of a semaphore object is signaled when its count is greater than zero, and nonsignaled when its count is equal to zero

22 22 Confidential Enterprise Solutions Group Sharing among process Child process can inherit Duplicate handle OpenMutex,OpenEvent…..

23 23 Confidential Enterprise Solutions Group Hands on critical section CRITICAL_SECTION m_cs; InitializeCriticalSection(&m_cs); UINT ThreadOne(LPVOID lParam) { // // Lock the Critical section EnterCriticalSection(&m_cs); // Some Process //Release the Critical section LeaveCriticalSection(&m_cs); return 0; } UINT ThreadTwo(LPVOID lParam) { // Lock the Critical section EnterCriticalSection(&m_cs); // Some Process //Release the Critical section L eaveCriticalSection(&m_cs); // return the thread return 0; }

24 24 Confidential Enterprise Solutions Group Job, Process, Thread & Fiber

25 25 Confidential Enterprise Solutions Group Job, Process, Thread & Fiber Mgmt. API Calls

26 26 Confidential Enterprise Solutions Group A kernel connects the application software to the hardware of a computerapplication software Manages task of computer and hardware Kernel objects are all those objects which are created and maintained by the Win32 kernel, like processes, threads, files, jobs, mailslots, mutexes, events

27 27 Confidential Enterprise Solutions Group

28 28 Confidential Enterprise Solutions Group Inter Process communication Pipe MailSlots Shared memory Sockets File mapping


Download ppt "1 Confidential Enterprise Solutions Group Process and Threads."

Similar presentations


Ads by Google