
Understanding CPU Processes, Threads, and Multi-Core Technology
Explore the intricacies of CPU execution, multitasking, process vs. thread management, and the role of the operating system in scheduling tasks efficiently. Learn how multi-core CPUs and multi-threading enhance performance and resource utilization in computing systems.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Whats a CPU do? Runs programs So what s a program? A set of instructions A process is an instance of a program An active program Instructions being executed Each process is independent
How does a CPU execute code? One instruction at a time For a single-core CPU Add Multiply Store data Etc Fine when PCs ran one program at a time How do they run lots of programs?
Multi-Tasking CPU switches between active processes Very rapidly Gives the illusion of parallel execution Still exactly one instruction at a time Program A Program B Program A Program C Add Multiply Store Divide Subtract Shift left Store Multiply Branch Add
The OS is in Charge Your operating system schedules processes Determines when each runs And for how long You have almost no control over this! Context Switch: Swapping between processes Storing/restoring process state & memory Processes are waiting while not running Basically paused
Multi-Core CPUs? OS distributes processes Among available cores But a simple process = one core How can one process do two things? Write code to divide up instructions So that OS can schedule tasks Multi-threading!
Process vs. Thread Process Thread Process is heavy weight or resource intensive. Thread is light weight, taking lesser resources than a process. Process switching needs interaction with operating system. Thread switching does not need to interact with operating system. In multiple processing environments, each process executes the same code but has its own memory and file resources. All threads can share same set of open files, child processes. If one process is blocked, then no other process can execute until the first process is unblocked. While one thread is blocked and waiting, a second thread in the same task can run. Multiple processes without using threads use more resources. Multiple threaded processes use fewer resources. In multiple processes each process operates independently of the others. One thread can read, write or change another thread's data. e.g. > test.exe > ./test
Threads Schedulable units of processing Sub-processes Or lightweight processes Each thread is separate Has its own stack Can communicate with other threads Every process begins as a single thread
Thread States Threads can be in one of several states: Started Ready to go, not running yet Running Executing code Suspended Waiting to resume Sleeping Waiting a set amount of time Blocked Waiting on an operation Stopped Finished running
Why Use Threads? Allows for intra-process parallelism One process can do multiple things Loading screens One thread loads (slow & blocking) Another displays progress (60 fps) Background Processing One thread runs the UI Another thread does work (processing files, data, )
Threads in Code Create a thread Give it a method to begin with This will start the thread Thread ends when its method ends Once done, threads can not be restarted
Threads in C C didn t have threads built into the language, unlike newer language like Java, C# However, a standard library pthread is available Has to be linked in explicitly e.g. gcc -o threader main.o -lpthread
Creating a thread #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); On success, pthread_create() returns 0; on error, it returns an error number NOTE: - Have to pass a pointer to the function to be run as a thread - Have to pass a pthread_t struct that gets filled in
Creating a thread #include <pthread.h> void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! Thread ID, %d\n", tid); pthread_exit(NULL); } Int main() { pthread_t threads[NUM_THREADS]; //Create the thread int rc; rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i); int i;
Sharing Resources Threads may want to access resources Variables, static data, etc. Potential for lots of errors here! Out of sync Threads execute unpredictably Contention - Multiple threads need the same thing Deadlocks Multiple threads waiting for each other to finish (block forever)
Thinking with Threads Think about the following line: printf( Result: [%d\n] ,(a * b + c) ); How many operations occur? Multiply Add Concatenate string w/ number Concatenate string w/ string Actually write it out (with newline)
Thread Execution Order printf( Result: [%d\n] ,(a * b + c) ); The thread could be paused at any point And any other thread could run Completely up to the OS What if variable a changes and is printed After the multiply above But before the line above finishes printing?
Fixing Thread Errors Could spend multiple semesters on threading And fixing threading errors Just scratching the surface here Big take-aways: Threads are unpredictable Non-thread-safe code is easy to write Try to be as fool proof as possible
Thread Control Several examples of controlling threads: Sleep Yield Join
Sleep sleep(int n); This will suspend the thread for n seconds Thread sleeps for at least that long Thread may not become active immediately And context switch takes time Not a precise timing mechanism!
Join Blocks current thread Until specified thread object is done Often used to wait for several threads to finish ret = pthread_join(threads[t],(void**)&retval);//Takes a void **
Yield pthread_yield () This function allows a thread to give up control of a processor so that another thread can have the opportunity to run.
References https://www.codeproject.com/articles/13557/creating-threads-using- the-createthread-api pthread_join() Wait for a thread to end - IBM Documentation https://www.microsoft.com/msj/0799/win32/win320799.aspx