Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISP – 4 th Recitation Times System Errors Threads Waits Code examples.

Similar presentations


Presentation on theme: "ISP – 4 th Recitation Times System Errors Threads Waits Code examples."— Presentation transcript:

1 ISP – 4 th Recitation Times System Errors Threads Waits Code examples

2 GetProcessTimes() Retrieves time statistics of a process: Syntax : BOOL GetProcessTimes( HANDLE hProcess, FILETIME *CreationTime, FILETIME *ExitTime, FILETIME *KernelTime, FILETIME *UserTime); FILETIME contains a 64-bit value representing the number of 100- nanosecond intervals since January 1, 1601 (UTC).

3 FileTimeToSystemTime() Converts FILETIME to SYSTEMTIME Syntax : BOOL FileTimeToSystemTime( FILETIME *FileTime, SYSTEMTIME *SystemTime ); FILETIME contains a 64-bit value representing the number of 100- nanosecond intervals since January 1, 1601 (UTC). SYSTEMTIME contains fields of year, month, day, hour, min, etc.

4 SystemTimeToTzSpecificLocalTime() Convert time from UTC to a different time zone. Syntax : BOOL SystemTimeToTzSpecificLocalTime ( TIME_ZONE_INFORMATION *TimeZone, SYSTEMTIME *UniversalTime, SYSTEMTIME *LocalTime ); TIME_ZONE_INFORMATION contains time zone of interest. It can be NULL if the currently used time zone is needed.

5 Thread Wikipedia definition: Threads are a way for a program to split itself into two or more simultaneously (or pseudo- simultaneously) running tasks. A thread is contained inside a process and different threads in the same process share some resources while different processes do not. Switching between threads of the same process requires less context switching.

6 Creating a Thread Creating a new thread Syntax : HANDLE CreateThread( SECURITY_ATTRIBUTES *ThreadAttributes, SIZE_T dwStackSize, THREAD_START_ROUTINE *StartAddress, VOID *Parameter, DWORD dwCreationFlags, DWORD *ThreadId );

7 Creating a Thread Using CreateThread(): Example : tHandle=CreateThread( NULL,// default security attributes 0,// use default stack size (LPTHREAD_START_ROUTINE) searchDB, // thread function &args,// argument to thread function 0,// use default creation flags &threadID// returns the thread identifier ); RED = Not too relevant for our purposes, can keep it like in this example Green = Interesting stuff we have to understand Note : CreateThread() returns the handle to the thread directly.

8 WaitForSingleObject() - Reminder WaitForSingleObject() is used for waiting for an object to become “signaled”. Syntax : DWORD WaitForSingleObject( HANDLE hThread, DWORD dwMilliseconds ); Each object has a signaled and non signaled state and for each object the meaning is slightly different and depends on its function. For threads, “Signaled” means that the thread has ended.

9 WaitForMultipleObjects() WaitForMultipleObjects() is used for waiting for several object to become “signaled”. Syntax : DWORD WaitForMultipleObjects( DWORD nCount, HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds ); nCount is a the number of processes we’re waiting for. lpHandles is an array of handles to objects. bWaitAll indicates whether we want to wait for all objects to become signaled or the first one.

10 WaitForMultipleObjects() dwMilliseconds is a timeout period (in ms): 0 means that WaitForSingleObject() is used for polling the status. INFINITE means that our code is waiting for the object to become signaled, even infinitely. Other values are used as a timeout. If the object is not signaled then wait until signaled or timeout occurs. Return values : 0 – The object is in a signaled state. 102h – Timeout has elapsed, the object is nonsignaled. Note: WaitForMultipleObjects() recieves an array of handles. Handles can be handles to all kinds of objects.

11 TerminateThread() Brutally closing a thread. Syntax : BOOL TerminateThread( HANDLE hThread, DWORD uExitCode ); Returns 0 if failed, nonzero if successful NOTE : There are far less good reasons to brutally terminate threads than there are to processes!

12 GetExitCodeThread() Retrieves the exit code of a Thread. Syntax: BOOL GetExitCodeThread( HANDLE hObject, DWORD *lpExitCode ); Note: If the thread is still active, a value of STILL_ACTIVE (259) is returned.

13 GetLastError() Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code. Syntax: DWORD GetLastError(void); Always call GetLastError() immediately after a relevant function returned with an error. List of all error codes (over 10,000) : http://msdn.microsoft.com/en-us/library/ms681381.aspx http://msdn.microsoft.com/en-us/library/ms681381.aspx


Download ppt "ISP – 4 th Recitation Times System Errors Threads Waits Code examples."

Similar presentations


Ads by Google