



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Lab; Class: INTRO TO COMPUTER NETWORKS; Subject: Computer Science; University: University of Alabama - Huntsville; Term: Unknown 1989;
Typology: Lab Reports
1 / 6
This page cannot be seen from the preview
Don't miss anything!
Threads in Windows NT/Windows 95 1.0 Introduction Windows is a preemptive, multi-tasking, multithreaded operating system. This is much more flexible than the old MS-DOS "operating system" (there is a question as to whether MS-DOS was truly an operating system), which allowed only a single task at a time. It is also somewhat more flexible, in regard to tasking, than traditional UNIX, which is a preemptive multi-tasking system, but which is not a multi-threaded operating system. A "thread" is different from a "process." A multi-tasking operating system employs processes, but does not employ threads. A multi-threaded operating system must employ both processes (it is inherently multi-tasking as well as multi-threaded) and threads. A "process" in a multi-tasking system is given a time slice of CPU time by the operating system. When that time slice expires (the time slice is measured by a quantum timer), then the operating system preempts the process, and allows the next process to run. Normally, each process in a multi-tasking operating system is given it's own data storage space, which is normally organized as part of the system stack. A process can be broken down itself into threads. The breaking of a process into threads is normally performed under application programmer control (the application programmer is the programmer who wrote the process). A thread is not just a smaller process. The primary differences between a process and a thread are: threads share the memory space of a single process (although each thread is assigned its own section of the memory space). the processor overhead of switching the CPU from one thread to another thread is typically lower than for switching the CPU from one process to another process a thread will typically receive a smaller time slice of CPU time than a process, since a thread normally receives part of the time slice assigned to its parent process Figure 1 describes the differences between processes versus threads. Quantum Quantum Processes Threads Process 1 Process 2 Figure 1. Processes versus Threads
2.0 Description of Thread Creation The purpose of this lab is for you to learn about Windows NT/Windows 95 threads. During this lab, you will implement a Windows NT process/program that includes threads. Listing 1 is a thread program that creates three threads. As you run this program, each thread will repeat "Here I am in Thread #n" 50 times. A parameter is passed into each thread. The threads whose parameter is an even number will sleep longer than threads whose parameter is an odd number. Exercise: Try modifying the time slept, and watch the different running characteristics of the threads. In Listing 1, there are some constructs that will probably be unfamiliar to you. Let's discuss these constructs. The CreateThread routine creates a thread with appropriate security settings, passes the thread some parameters, and gives the thread a unique identifier. The WaitForMultipleObjects routine keeps the process from completing until all three threads have completed. 2.1 A Detailed Description of Thread Creation The following is a more detailed look at the CreateThread and WaitForMultipleObjects routines: The constructs below are from the include files in Visual C++ version 5.0. Here is the definition ( from winbase.h , which is included in windows.h ) of the CreateThread routine: WINBASEAPI HANDLE WINAPI CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId ); where:
Finally, the threadID location receives a special identifying number that is assigned by the CreateThread routine. Here is the definition ( from winbase.h , which is included in windows.h ) of the WaitForMultipleObjects routine: WINBASEAPI DWORD WINAPI WaitForMultipleObjects( DWORD nCount, CONST HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds ); Here, nCount is the number of objects in the object array lpHandles (which is declared in pointer notation rather than in array notation. Remember that the base address of an array in C can be accessed as a pointer). The bWaitAll variable is a Boolean (can hold the values true or false). If true, the function waits for all events to occurotherwise it waits for only any one of the events to occur. The dwMilliseconds variable is the maximum time the WaitForMultipleObjects routine is to wait, in milliseconds.
#include <windows.h> #include <stdlib.h> #include <iostream.h> void myThread(DWORD *params) { DWORD i; i=0; while (i < 50) { cout << "Here I am in thread #"<< *params << "\n"; if (i%2 == 0) Sleep(100); else Sleep(10); i++; } GlobalFree(params); } void main(void) { HANDLE myHandles[3]; //array of three handles DWORD threadID; DWORD *params; DWORD count; for (count=0; count<3; count++) { // allocate memory for a "params" structure params=(DWORD *) GlobalAlloc(GPTR, sizeof(DWORD)); *params = count; // create a thread and pass it the pointer // to its "params" struct myHandles[count]=CreateThread(0, 0, (LPTHREAD_START_ROUTINE) myThread, params, 0, &threadID); } // wait for all threads to finish execution WaitForMultipleObjects(3, myHandles, TRUE, INFINITE); } Listing 1. Thread Program Providing 3 threads.