[PDF] [PDF] Threads: POSIX API Pthreads

POSIX and threads ▫ Early on ○ Each OS had its own thread library/API ○ Difficult to write multithreaded programs ▫ Learn a new API with each new OS



Previous PDF Next PDF





[PDF] Interlude: Thread API

This chapter briefly covers the main portions of the thread API Each part program is to create new threads, and thus some kind of thread creation interface  



[PDF] API 8 Round Connections - Tenaris

API RP 5B1: Gauging and inspection of casing, tubing, and line pipe threads API 5B: Specification for Threading, Gauging and Thread Inspection of 



[PDF] API Buttress Connections - Tenaris

and line pipe threads API 5B: Specification for Threading, Gauging and Thread Inspection of Casing, Tubing and Line Pipe Threads 



[PDF] 27 Interlude: Thread API - GitHub Pages

Interlude: Thread API Operating attr: Used to specify any attributes this thread might have ○ Stack int pthread_join(pthread_t thread, (void *)*value_ptr);



[PDF] ASSESSMENT OF API THREAD CONNECTIONS UNDER - CORE

Assessment of API Thread Connections Under Tight Gas Well Conditions ( August 2009) Dwayne Bourne API Buttress Thread Casing and Thread Features



[PDF] Threads: POSIX API Pthreads

POSIX and threads ▫ Early on ○ Each OS had its own thread library/API ○ Difficult to write multithreaded programs ▫ Learn a new API with each new OS



[PDF] Threading and gauging of rotary shouldered thread connection

3) shall not apply to products that have specific requirements in API Spec 7- 1, API Spec 5DP, ISO 10424-1 and ISO 11961, such as tool joints for drill pipe and 



[PDF] Rotary Shouldered Connection Thread Inspection

TDWIN Taper is included in any full JSS Package • Follows API Specification 7-2 , Dec 2008, for Rotary Shouldered Connections and API Specification 5B, Oct



[PDF] PROFILE GAGES, THREAD PROFILE, TP SERIES - Thread Check

These precision gages are ideal for quickly identifying each of the different thread forms named in the API Std 5B and Specification 7 Profile gages can also be 



[PDF] Thread / API

Thread / API M41-120-QNT-001 Inspection of Threads / API Threads on a 3D Coordinate Measuring Machine With the option QUINDOS Thread / API single 

[PDF] thread api in os

[PDF] threats to the cruise industry

[PDF] three degrees below zero meaning

[PDF] three levels of culture hammond

[PDF] three levels of formatting in excel in order

[PDF] three major components of a proper literacy learning environment

[PDF] three phase house wiring diagram

[PDF] three phase house wiring diagram pdf

[PDF] three signs and symptoms of mild to moderate alcohol intoxication might be

[PDF] three signs and symptoms of severe alcohol intoxication might be

[PDF] three tier architecture of mobile computing pdf

[PDF] three tier architecture simplifies applications

[PDF] three tier security checks in governance

[PDF] three types of network

[PDF] three layer model in data communication

Copyright ©: University of Illinois CS 241 Staff

Threads: POSIX API 'Pthreads'

2

Creating a Thread

n When a new thread is created it runs concurrently with the creating thread. n When creating a thread you indicate which function the thread should execute. Copyright ©: University of Illinois CS 241 Staff

Compare: Normal function call (one thread)

Copyright ©: University of Illinois CS 241 Staff processfd(); processfd() { } Calling program Called function Thread of execution

Compare: Threaded function call

Copyright ©: University of Illinois CS 241 Staff pthread_create(); processfd() { }

Creating program Created thread

processfd(); processfd() { }

Calling program Called function

Thread creation Thread of execution

Threads vs. Processes

n Process ¡ fork is expensive (time & memory) ¡ each process has its own virtual addr. space n Thread

¡ Lightweight process ¡ Shared virtual address space ¡ Does not require lots of memory or startup time

Copyright ©: University of Illinois CS 241 Staff 6

Design choices: Processes versus Threads

Copyright ©: University of Illinois CS 241 Staff

Thread-Specific Resources

n Each thread has its own ¡ pthread_t identifier ¡ Stack, Registers state,

Program Counter

n Threads within the same

process can communicate using shared memory ¡ Must be done carefully! ¡ Virtual memory is shared

Copyright ©: University of Illinois CS 241 Staff

Process and Threads

n Each process can include many threads n All threads of a process share:

¡ Process ID ¡ Virtual Memory (program code and global data) ¡ Open file/socket descriptors ¡ Semaphores ¡ Signal handlers ¡ Working environment (current directory, user ID,

etc.) Copyright ©: University of Illinois CS 241 Staff

Threads and address space

From: https://computing.llnl.gov/tutorials/pthreads/images/thread.gif

Process Creation vs. Thread Creation

n http://www.llnl.gov/computing/tutorials/pthreads.

n Timings reflect 50,000 process/thread. n Creations, were performed with the time utility, and units are

in seconds, no optimization flags. Copyright ©: University of Illinois CS 241 Staff

Platform fork() pthread_create() real user sys real user sys AMD 2.3 GHz Opteron (16 cpus) 12.5 1.0 12.5 1.2 0.2 1.3 AMD 2.4 GHz Opteron (8 cpus) 17.6 2.2 15.7 1.4 0.3 1.3 IBM 4.0 GHz POWER6 (8 cpus) 9.5 0.6 8.8 1.6 0.1 0.4 IBM 1.9 GHz POWER5 p5-575 (8 cpus) 64.2 30.7 27.6 1.7 0.6 1.1 IBM 1.5 GHz POWER4 (8 cpus) 104.5 48.6 47.2 2.1 1.0 1.5 INTEL 2.4 GHz Xeon (2 cpus) 54.9 1.5 20.8 1.6 0.7 0.9 INTEL 1.4 GHz Itanium2 (4 cpus) 54.5 1.1 22.2 2.0 1.2 0.6

POSIX and threads

n Early on ¡ Each OS had its own thread library/API ¡ Difficult to write multithreaded programs n Learn a new API with each new OS n Modify code with each port to a new OS n So

¡ POSIX (IEEE 1003.1c-1995) provided a

standard known as pthreads Copyright ©: University of Illinois CS 241 Staff 12

Pthread Operations

POSIX function description SWKUHDGBFUHDWH create a thread SWKUHDGBGHWDFK set thread to release resources SWKUHDGBHTXDO test two thread IDs for equality SWKUHDGBH[LW exit a thread without exiting process SWKUHDGBNLOO send a signal to a thread SWKUHDGBMRLQ wait for a thread SWKUHDGBVHOI find out own thread ID

Copyright ©: University of Illinois CS 241 Staff

Creating a Thread

int pthread_create (pthread_t* tid, pthread_attr_t* attr, void*(child_main)(void*), void* arg); n creates a new posix thread n Parameters:

¡ tid:

n Unique thread identifier returned from call

¡ attr:

n Attributes structure used to define new thread n Use NULL for default values

¡ child_main:

n Main routine for child thread n Takes a pointer (void*), returns a pointer (void*)

¡ arg:

n Argument pointer passed to child thread Copyright ©: University of Illinois CS 241 Staff

Creating a Thread

n pthread_create() takes a pointer to a function as one of its arguments ¡ child_main is called with the argument specified by arg ¡ child_main can only have one parameter of type void * ¡ Complex parameters can be passed by creating a structure

and passing the address of the structure

¡ The structure can't be a local variable ¡ By default, a new thread is created in a joinable state

n Thread ID ¡ pthread_t pthread_self(void); ¡ Returns ID of executing thread Copyright ©: University of Illinois CS 241 Staff 15

Exiting a thread

n Question:

¡ If a thread calls exit(), what about other

threads in the same process? n When does a multithreaded process terminate? Copyright ©: University of Illinois CS 241 Staff 16

Exiting a thread

n Question: ¡ If a thread calls exit(), what about other threads in the same process? n A multithreaded process terminates when:

¡ one of its threads calls exit ¡ it returns from main() ¡ it receives a termination signal ¡ all threads have called pthread_exit

n In any of these cases, all threads of the process terminate. Copyright ©: University of Illinois CS 241 Staff

Terminating Threads: pthread_exit()

void pthread_exit(void * retval); n Terminate the calling thread n Makes the value retval available to any successful join with the terminating thread n Returns

¡ pthread_exit() cannot return to its caller

n Parameters

¡ retval:

n Pointer to data returned to joining thread n Pass a pointer to heap not to the stack n Note ¡ If main() exits by calling pthread_exit() before its threads, the other threads continue to execute. Otherwise, they will be terminated when main() finishes. Copyright ©: University of Illinois CS 241 Staff

Detaching Threads: pthread_detach()

int pthread_detach(pthread_t thread);

n Thread resources can be reclaimed on termination n Return results of a detached thread are unneeded

n Returns ¡ 0 on success ¡ Error code on failure n Parameters

¡ thread:

n Target thread identifier n Notes ¡ pthread_detach() can be used to explicitly detach a thread even though it was created as joinable

¡ There is no converse routine

Copyright ©: University of Illinois CS 241 Staff

Detached Threads

Copyright ©: University of Illinois CS 241 Staff

Master Thread Worker Thread Worker Thread

pthread_create() pthread_exit()

Worker Thread

pthread_exit() pthread_exit()

Waiting for Threads: pthread_join()

int pthread_join(pthread_t thread, void** retval); n Suspends execution of the calling thread until the target thread terminates, unless the target thread has already terminated. n Returns ¡ 0 on success ¡ Error code on failure n Parameters

¡ thread:

n Target thread identifier

¡ retval:

n The pointer passed to pthread_exit() by the terminating thread is made available in the location referenced by retval Copyright ©: University of Illinois CS 241 Staff

Joined Threads

Copyright ©: University of Illinois CS 241 Staff

Master Thread Worker Thread

pthread_create() pthread_join() pthread_exit() suspends calling thread, retrieves void* retval

Example 1

int x = 0;

char *p; void *thread(void *th){ x = x + 10; strcat(p, "Hello from thread!"); printf("thread: my x is %d. Bye from thread!\n", x); pthread_exit((void *) p+5); } int main() { pthread_t tid; char *p_char; p_char = p = malloc(25 * sizeof(char)); // data allocated on heap strcpy (p, "main-thread:"); pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, (void **) &p_char); printf("%s\n", p_char); printf("main: my x is %d; Bye from main!\n", x); }

Copyright ©: University of Illinois CS 241 Staff Necessary includes: #include #include #include #include

Example 2

int x = 0;

char *p; void *thread(void *th){ x = x + 10; strcat(p, "Hello from thread!"); printf("thread: my x is %d. Bye from thread!\n", x); pthread_exit((void *) p+5); } int main() { pthread_t tid; char *p_char; p_char = p = malloc(25 * sizeof(char)); // data allocated on heap strcpy (p, "main-thread:"); pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, (void **) &p_char); printf("%s\n", p_char); printf("main: my x is %d; Bye from main!\n", x); }

Copyright ©: University of Illinois CS 241 Staff Necessary includes: #include #include #include #include

What is the output?

Valid outputs for example 2 (non-deterministic)

Output #1

--------------------------------------- main-thread:Hello from thread! thread: my x is 10. Bye from thread! main: my x is 10; Bye from main!

Output #2

--------------------------------------- main-thread: thread: my x is 10. Bye from thread! main: my x is 10; Bye from main!

Output #3

--------------------------------------- main-thread: main: my x is 10; Bye from main! Copyright ©: University of Illinois CS 241 Staff pthread Error Handling n pthread functions do not follow the usual Unix conventions

¡ Similarity

n Returns 0 on success

¡ Differences

n Returns error code on failure n Does not set errno

¡ What about errno?

n Each thread has its own n errno is thread-local; setting it in one thread does not affect its value in any other thread. Copyright ©: University of Illinois CS 241 Staff 26

Threads vs processes

n Threads are similar to concurrent processes ¡ Pros: thread creation is faster; data sharing among threads is fast and easy ¡ Cons: application is less robust; data sharing requires synchronization to avoid race conditions n If a thread misbehaves, it can corrupt data of other threads within same process n If a thread crashes, the entire process crashes Copyright ©: University of Illinois CS 241 Staff

Threads vs. Processes

Copyright ©: University of Illinois CS 241 Staff

Property Processes created with fork Threads of a process Ordinary function calls variables Get copies of all variables Share global variables Share global variables IDs Get new process IDs Share the same process ID but have unique thread ID Share the same process ID (and thread ID) Data/control Must communicate explicitly, e.g., use pipes, shared memory, msg. passing. May communicate with return value or carefully shared variables May communicate with return value or shared variables Parallelism (one CPU) Concurrent Concurrent Sequential Parallelism (multiple CPUs) May be executed simultaneously May be executed simultaneously Sequential

quotesdbs_dbs12.pdfusesText_18