CS361 Slides - Week 12
Course Information
Course Title: CS 361: Computer Systems II
Instructor: Prof. Michael S. Kirkpatrick
Week: 12, Spring 2024
Institution: James Madison University (JMU)
Concept Testing
Thread Output Consistency
Code:
void * child (void *args) { int x = 5; printf ("%d\n", ++x); pthread_exit (NULL); }Conclusion: Both threads' outputs are the same (each prints 6) because
xis a local variable.
Process Structure
Process Components:
Stack: Memory used for function calls and local variables.
Data: Static variables and heap memory.
Code: Executable code of the program.
Thread Benefits:
Modularity
Parallel execution
Faster/more flexible than processes
A necessity in modern computing.
Hardware Considerations
Clock Rates:
Inquiry about the clock rate on personal computers.
Discussion of a hypothetical 52 THz CPU.
Intel Pentium Processors
Historical Overview:
The Pentium 4 had notable increases in clock rate and power consumption but less performance enhancement.
The Prescott version faced thermal issues, leading to the discontinuation of the Pentium 4 line.
The Core series adopted a simpler pipeline architecture with lower clock rates but supported multiple processors per chip.
Multithreading Concepts
Types of Threads
POSIX Threads
Green Threads
Fibers
Lightweight Processes:
Examples include JVM threads, kernel threads, etc.
Forking and Thread Creation
Code Example for Forking
Code:
int main (int argc, char* argv[]) { pid_t pid = fork(); if (pid > 0) wait (NULL); /* parent */ else printf ("hello\n"); /* child */ return 0; }Basic Operation: This code shows a process creating a child process using
fork.
Creating and Managing Threads
Thread Creation and Management:
Each thread can be created using:
pthread_create (&thread, NULL, hello, NULL); pthread_join(thread, NULL);Function to be executed by the thread:
void * hello (void *args) { printf("hello\n"); pthread_exit (NULL); }
Comparing Process Creation and Thread Creation
Process ID (PID) vs. Thread ID (TID)
Differences include resource allocation and management overhead.
Thread Function Prototype:
All thread entry functions take and return void*.
Activity: Passing Arguments to Threads
Multi-Argument Handling
Same Type: Simple handling in arrays.
Different Types: Use structures to encapsulate multiple types.
Return Structures: Allocated memory structures or pointers.
Example of Argument Passing
Addition Function:
void *add(void * _args) { int *args = (int*) _args; int x = args[0]; int y = args[1]; pthread_exit(x + y); }
Structs in Thread Arguments
Implementation Example
Structure for Passing Data:
typedef struct name_dob { char *name; char *dob; } bio_t;Allocation advice: Always ensure allocated data persists beyond thread lifetime.
Potential Issues in Threads
Returning Local Variables: Dangerous to return local stack variables that may go out of scope.
Sample Problematic Code:
void * child (void *args) { int x = 5; pthread_exit(&x); }Addressing Shared Variables: Make sure thread interactions on shared resources are well-controlled to avoid data races.
Issues arise if threads may concurrently modify shared variables without synchronizations.
Final Activity
Reflection on Threads in Assembly Language: Understanding how threading interacts at the assembly level can significantly aid in debugging multi-threaded applications.