Presentation is loading. Please wait.

Presentation is loading. Please wait.

V 1.0 OE-NIK HP 1 Advanced Programming Fundamentals of parallel execution Processes Threads.

Similar presentations


Presentation on theme: "V 1.0 OE-NIK HP 1 Advanced Programming Fundamentals of parallel execution Processes Threads."— Presentation transcript:

1 V 1.0 OE-NIK HP 1 Advanced Programming Fundamentals of parallel execution Processes Threads

2 V 1.0 OE-NIK HP 2 Advanced Programming Fundamentals of parallel execution Processes Threads

3 V 1.0 Fundamentals of parallel execution The Neumann-based computers are executing programs in a purely sequential way To execute multiple programs simultaneously –More than one CPUs / cores –Hyperthreading –Time sharing Number of concurrent operations > number of execution units Separation of “concurrently” executing programs –Processes: high-level separation One program (or a set of resources used by a program task) Separated memory area When faults, only that process is damaged (not the others, nor the OS) No simple way for IntraProcess Communication –Threads: low-level separation Parallelism inside the processes Partially shared memory area for threads in one process 3 OE-NIK HP P 1 process T 11 thread T 12 thread T 13 thread

4 V 1.0 Time sharing P 1 process Running state Ready or Standby Save state (PCB 2 ) Restore state (PCB 1 ) Save state (PCB 1 ) Restore state (PCB 2 ) Interrupt / system call Running state Ready or Standby P 2 process Interrupt / system call Original image © 2000-2005 David A. Solomon and Mark Russinovich 4 OE-NIK HP this figure is generic (DOS/Unix/Windows) and overly simplified; in Windows the base unit of the parallel execution is the thread

5 V 1.0.NET processes, threads, AppDomain In the.NET framework the processes are equivalents with the OS processes –System.Diagnostics.Process, System.Diagnostics.ProcessStartInfo The.NET threads are (currently) equivalent with the OS threads –It is not limited to this – one OS thread could execute multiple.NET threads In.NET there is an additional level from in-process parallelism, the „application domain” for multiple processes –The managed programs are supervised by the framework, so they cannot affect each other negatively –Thus, there is no need for truly executing some as physically separate processes –Due to speed and memory requirements, it can be advantageous (the creation and maintaining of processes takes up many CPU and memory resources) –Not in our schedule 5 OE-NIK HP

6 V 1.0 OE-NIK HP 6 Advanced Programming Fundamentals of parallel execution Processes Threads

7 V 1.0 Processes (fragment) System.Diagnostics.Process class Methods Start() CloseMainWindow()Only with GUI processes Kill() GetCurrentProcess() GetProcesses()List of all processes WaitForExit() Properties StartInfoThe assigned ProcessStartInfo instance PriorityClass EnableRaisingEvents HasExited ExitCode, ExitTime StandardInput, StandardOutput Streams Events Exited 7 OE-NIK HP

8 V 1.0 Processes (fragment) System.Diagnostics.ProcessStartInfo class Properties FileNameExecutable (on its own or using its associated program) Arguments, WorkingDirectory Domain, UserName, Password RedirectStandardInput, RedirectStandardOutput Redirect the specified stream? ErrorDialogTrue/false, if the execution fails UseShellExecute Use the OS shell execute method (Advantage: more clever execution, independent execution. Disadvantage: cannot read output) Verb Examples of verbs are "Edit", "Open", "OpenAsReadOnly", "Print", and "Printto" WindowStyleInitial window size (minimized, maximized) 8 OE-NIK HP

9 V 1.0 Example: new process using System; using System.Diagnostics; class Program { static void Main() { Process newProcess = new Process(); newProcess.StartInfo = new ProcessStartInfo("hello.exe", "Joe"); newProcess.StartInfo.ErrorDialog = true; newProcess.StartInfo.UseShellExecute = false; newProcess.StartInfo.RedirectStandardOutput = true; newProcess.Start(); newProcess.WaitForExit(); Console.WriteLine("The messages from the new process:"); Console.Write(newProcess.StandardOutput.ReadToEnd()); Console.ReadLine(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 9 OE-NIK HP

10 V 1.0 Exercises Create a console application that executes an arbitrary number of traceroute (tracert) processes so the individual calls do not block the main application Use the netstat command’s output to list the currently opened network communication channels for an arbitrary process 10 OE-NIK HP

11 V 1.0 OE-NIK HP 11 Advanced Programming Fundamentals of parallel execution Processes Threads

12 V 1.0 Single threading / Multi threading? Multi-threading is essential for the responsiveness of the program / OS, but... It is expensive (depending on the OS) Memory allocated for each thread: –Stack, thread context (stb.) Wasted CPU time: –Management of the time sharing Ideally: number of active threads = number of cores –Context changes  Increased number of “cache miss” events There are lots of context changes, lots of cache miss events... –Every managed DLL is notified when creating/destroying a thread Dll_Main(DLL_THREAD_ATTACH), Dll_Main(DLL_THREAD_DETACH) calls powerpnt.exe 147 dll!!!!... VS 2012: 183 dll... Still: Thread overhead < process overhead, especially in Windows OS OE-NIK HP 12

13 V 1.0 Possibilities for multi-threaded processing Asynchronous method call (not in our schedule) –We can call delegates and special methods supporting this pattern Thread class –Low-level management of threads –Simple (simply “execute this method in another thread”), but requires lots of accuracy and planning (Concurrency? Return values?) ThreadPool class (not in our schedule) –There is a set/pool of threads waiting for usage –No need for individually creating/destroying threads –Simple and very efficient, but lacks functionality (e.g. thread identification) BackgroundWorker class (not in our schedule) –Typically used to separate the UI thread from long operations –Very limited functionality TPL / Task class –High-level thread management, unified API with high functionality TPL / Parallel class –Data-parallel execution, Parallel loops 13 OE-NIK HP

14 V 1.0 Threads (fragment) System.Threading.Thread class Methods Start() Suspend(), Resume() Abort()Rarely used GetHashCode()Usable for identification Sleep()Wait a given timespan Join()Wait for a thread to finish Properties CurrentCulture, CurrentUICulture IsBackground The execution of a program is finished when the last foreground thread is finished (the background threads are aborted) IsThreadPoolThread ManagedThreadID Name Priority ThreadState 14 OE-NIK HP

15 V 1.0 System.Threading.Thread example using System; using System.Threading; class Program { static void Main(string[] args) { Console.WriteLine("Now we create a thread"); Console.WriteLine("Main thread ({0})", Thread.CurrentThread.GetHashCode()); Thread newThread = new Thread(ThreadMethod); newThread.Name = "NewThread"; newThread.Start(); newThread.Join(); } static void ThreadMethod() { Console.WriteLine("{0} (ID: {1})", Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode()); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 15 OE-NIK HP

16 V 1.0 Exercise Create a console application that is capable of measuring the speed of a webpage throughout 10 seconds by executing System.Net.WebClient().DownloadString(url) calls in separate threads Create a console application that is capable of concurrently downloading several RSS feeds and looking for user-specified keywords in them. 16 OE-NIK HP


Download ppt "V 1.0 OE-NIK HP 1 Advanced Programming Fundamentals of parallel execution Processes Threads."

Similar presentations


Ads by Google