Tutorial 4.

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
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.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
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.
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
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.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Threads A thread is an alternative model of program execution
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science,
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
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.
COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Lesson One – Creating a thread
Realizing Concurrency using the thread model
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University.
Threads in C Caryl Rahn.
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Linux Processes & Threads
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
CSE 333 – Section 9 Threads.
Realizing Concurrency using the thread model
Lecture 8: POSIX Threads
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Tutorial 3 Tutorial 3.
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 9: POSIX Threads Cont.
Realizing Concurrency using Posix Threads (pthreads)
Lecture 8: POSIX Threads
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

Tutorial 4

In this tutorial session we’ll see Threads.

Threads

Process Processes contain information about program resources and program execution state, including: Process ID, process group ID, user ID, and group ID Program code (text) Registers Stack (for temporary data) Heap (for dynamic memory allocations) Shared libraries

UNIX Process

Threads In the UNIX environment a thread: Exists within a process and uses the process resources Has its own independent flow of control . Duplicates only the essential resources it needs. May share the process resources with other threads that act equally independently (and dependently). Dies if the parent process dies - or something similar

Cont. This independent flow of control is accomplished because a thread maintains its own: Program Counter Stack Space Register Set (i.e. space to store register values when not on the CPU) Priority (used for scheduling of CPU time). Thread ID

UNIX Thread

Pthreads POSIX Threads: is a POSIX standard for threads. The standard defines an API for creating and manipulating threads. Pthreads : Libraries implementing the POSIX Threads standard. Defined as a set of C language programming types and procedure calls, implemented with a pthread.h header file. Pthreads are most commonly used on Unix-like systems, but Microsoft Windows implementations also exist. POSIX (IPA: /ˈpɒzɪks/) or "Portable Operating System Interface" is the collective name of a family of related standards specified by the IEEE to define the application programming interface (API) for software compatible with variants of the Unix operating system, although the standard can apply to any operating system.

Creating threads Initially, your main() program comprises a single, default thread. All other threads must be explicitly created by the programmer.

Creating threads (cont.) int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void*), void *arg); Description: pthread_create() creates a new thread and makes it executable. This routine can be called any number of times from anywhere within your code.

Creating threads (cont.) pthread_create() arguments: thread: An identifier for the new thread returned by the subroutine. attr: An attribute object that may be used to set thread attributes. You can specify a thread attributes object, or NULL for the default values. Thread attributes includes stack size and scheduling information. start_routine: the C routine that the thread will execute once it is created. arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed.

Creating threads (cont.) Return value: If successful, the pthread_create() routine shall return zero otherwise, an error number shall be returned to indicate the error.

Terminating threads There are several ways in which a Pthread may be terminated: The thread returns from its starting routine. The thread makes a call to the pthread_exit() subroutine. The thread is canceled by another thread via the pthread_cancel routine. The entire process is terminated due to a call to either the exec or exit subroutines.

Joining Threads int pthread_join(pthread_t thread, void ** thread_return); Description: The pthread_join() function shall suspend execution of the calling thread until the target thread terminates. Return value: If successful, the pthread_join() function shall return zero; otherwise, an error number shall be returned to indicate the error.

Joining Threads PARAMETERS thread thread_return Is the thread to wait for. thread_return If thread_return is not NULL, the return value of thread is stored in the location pointed to by thread_return

Example 1: void * thread2() { while(1){ printf("How are you?\n"); } Two threads displaying two strings “Hello” and “How are you?” independent of each other. #include <stdio.h> #include <pthread.h> #include <stdlib.h> void * thread1() { while(1){ printf("Hello!!\n"); }

cont. int main() { int status; pthread_t tid1,tid2; pthread_create(&tid1,NULL,thread1,NULL); pthread_create(&tid2,NULL,thread2,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0; }

Example /* pthread.c by detour@metalshell.com * Create multiple POSIX thread processes and wait for * each one to complete. * * http://www.metalshell.com/ */ #include <unistd.h> #include <pthread.h> #include <stdio.h> #include <time.h> #define NUM_THREADS 10

void. thread_function(void. arg) { fprintf(stdout, "Thread: %d running void *thread_function(void *arg) { fprintf(stdout, "Thread: %d running.\n", (int)arg); // Sleep for arg+1 seconds to verify pthread_join. sleep((int)arg+1); fprintf(stdout, "Thread: %d done.\n", (int)arg); pthread_exit(0); }

int main() { int cnt; pthread_t p_thread[NUM_THREADS]; for(cnt = 0; cnt < NUM_THREADS; cnt++) /* Returns 0 on successful creation of the thread. The second parameter, left NULL for this example, allows for options like CPU scheduling to be set. */ if(pthread_create(&p_thread[cnt], NULL, thread_function, (void *)cnt) != 0) fprintf(stderr, "Error creating the thread");

// Cycle through each thread and wait until it is completed. for(cnt = 0; cnt < NUM_THREADS; cnt++) { // Waits for p_thread[cnt] to finish. pthread_join(p_thread[cnt], NULL); } fprintf(stdout, "All threads completed.\n"); return 0;