Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.

Slides:



Advertisements
Similar presentations
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads.
Advertisements

1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Threads By Dr. Yingwu Zhu.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads Changes by MA Doman 2013.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
1 Threads. 2 Processes versus Threads 3 Why Threads? Processes do not share resources very well Why? Process context switching cost is very high Why?
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Pthreads: A shared memory programming model
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
POSIX THREADS. What is a thread? Multiple stands of execution in a single program are called threads. In other words, a thread is a sequence of control.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
Threads Threads.
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 4: Threads.
Principles of Operating Systems Lecture 8
Multithreading Tutorial
Concurrent Servers Topics Limitations of iterative servers
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Thread Programming.
Realizing Concurrency using the thread model
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Shared Memory Programming with Pthreads
Presentation transcript:

Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004

Threads Thread: an execution within a process A multithreaded process consists of many co-existing executions Separate: –CPU state, stack Shared: –Everything else Text, data, heap, environment

Threading Models - 1 Kernel (1-1) –All threads are first class objects in the kernel –Scheduling in and by the kernel –Utilizes multi-processors efficiently –Syscalls do not block the other threads –High overhead for large number of threads

Theading Models - 2 User Space (N-1) –Single kernel process, multiple user threads –Low kernel overhead – threads are cheap –Scheduling is determined by the process –Syscalls block the whole process (and all the threads) –No efficiency on multi-processors

Threading Models - 3 Hybrid (M-on-N) –User both kernel threads and user threads –More complicated to implement Requires changes to libraries Scheduling is complicated User space libraries must be synchronized with kernel version

State of the Art Linux –Pre 2.6: LinuxThreads (1-1 with extras) –2.6 on: NPTL – Native POSIX Thread Library (1-1) Windows –Threads, but not POSIX (1-1)

How to Compile #include gcc myprog.c –o myprog –l pthread

thread creation int pthread_create(pthread_t *thread, pthread_attr_t *attr, void* (*start_routine)(void*), void *arg); Create a thread and run the start_routine attr is usually NULL, don’t mess with it

Example #include int val = 0; void *thread(void *vargp) { val = (int)vargp; } int main() { int i; pthread_t tid; pthread_create(&tid, NULL, thread, (void *)42); pthread_join(tid, NULL); printf("%d\n",val); }

sched_yield #include int sched_yield (void); Yield the processor to another thread Useful on uni-processor

Who am I pthread_t pthread_self(void) Uses: –Debugging –Data structures indexed by thread pthread_equal: compare two pthread_t

Relationships Marriage: –pthread_join – I will wait for you forever Good bye –pthread_exit – I am going away now Death –pthread_cancel – please die Divorce: –pthread_detach – never talk to me again

pthread_join int pthread_join(pthread t, void *data) Wait until the thread exits and return the exit data. This call blocks! Performs a detach after the join succeeds Return values: –0: successful completion –EINVAL: thread is not joinable –ESRCH: no such thread –EDEADLK: a deadlock was detected, or thread specifies the calling thread

pthread_exit void pthread_exit(void *data) Stops execution of this thread Return data to anyone trying to join this thread Don’t call from the main thread, use exit()

Example #include void *thread(void *vargp) { pthread_exit((void*)42); } int main() { int i; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, (void **)&i); printf("%d\n",i); }

pthread_cancel Die you int pthread_cancel(pthread_t thread) return values: –0: ok –EINVAL: thread is invalid –ESRCH: no such thread

pthread_cancel Three phases –Post a cancel request –Deliver to the target thread at the next cancellation point –Call cleanup routines and die

pthread_detach I never want to see this thread again. int pthread_detach(pthread_t thread) Return values: –0 - ok –EINVAL – thread is not joinable –ESRCH – no such thread On some systems, exit does not cause the program to exit until all non-detached threads are finished

Review: Exiting threads Four options –Exit from start routine –Call pthread_exit –Call exit –Killed by pthread_cancel

Review pthread_create pthread_join pthread_detach pthread_exit pthread_cancel pthread_self pthread_equal sched_yield