Operating systems fundamentals - B06

Similar documents
Operating systems and concurrency - B03

CSE 333 SECTION 9. Threads

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

LSN 13 Linux Concurrency Mechanisms

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

CS 3305 Intro to Threads. Lecture 6

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Lecture 19: Shared Memory & Synchronization

pthreads CS449 Fall 2017

Multicore and Multiprocessor Systems: Part I

PThreads in a Nutshell

Threads. Threads (continued)

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Threads (light weight processes) Chester Rebeiro IIT Madras

Concurrency. Johan Montelius KTH

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

High Performance Computing Course Notes Shared Memory Parallel Programming

Section 4: Threads and Context Switching

CMPSC 311- Introduction to Systems Programming Module: Concurrency

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Introduction to pthreads

Threads. studykorner.org

CS510 Operating System Foundations. Jonathan Walpole

CMPSC 311- Introduction to Systems Programming Module: Concurrency

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

CS333 Intro to Operating Systems. Jonathan Walpole

Threads. lightweight processes

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

518 Lecture Notes Week 3

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Threaded Programming. Lecture 9: Alternatives to OpenMP

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Introduction to PThreads and Basic Synchronization

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Pthread (9A) Pthread

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Parallel Programming with Threads

Chapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:

Operating Systems & Concurrency: Process Concepts

Threads. Jo, Heeseung

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

POSIX Threads. Paolo Burgio

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

Operating Systems, laboratory exercises. List 2.

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Fall 2015 COMP Operating Systems. Lab #3

CSEN 602-Operating Systems, Spring 2018 Practice Assignment 2 Solutions Discussion:

CS 261 Fall Mike Lam, Professor. Processes

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

CS 261 Fall Mike Lam, Professor. Threads

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

CSC 1600 Unix Processes. Goals of This Lecture

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

Multithreaded Programming

POSIX Threads. HUJI Spring 2011

THREADS. Jo, Heeseung

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

Operating Systems Lab

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Midterm Exam Questions (document version 1.1)

Creating Threads. Programming Details. COMP750 Distributed Systems

Thread and Synchronization

Concurrent Server Design Multiple- vs. Single-Thread

4.8 Summary. Practice Exercises

Processes and Threads

ENCM 501 Winter 2019 Assignment 9

POSIX PTHREADS PROGRAMMING

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

System Programming. Process Control III

Threads. Today. Next time. Why threads? Thread model & implementation. CPU Scheduling

Programming with Shared Memory. Nguyễn Quang Hùng

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

Threads. Jo, Heeseung

Threads. To do. Why threads? Thread model & implementation. q q q. q Next time: Synchronization

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 1 September 29, 2014

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

ECE 598 Advanced Operating Systems Lecture 23

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

CS 326: Operating Systems. Process Execution. Lecture 5

real time operating systems course

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Compile the Hello World program

Multiprocessors 2007/2008

CS510 Operating System Foundations. Jonathan Walpole

Operating Systems. Lab. Class Project #5

Shared Memory Programming: Threads and OpenMP. Lecture 6

Introduction to Threads

Creating Threads COMP755

Transcription:

Operating systems fundamentals - B06 David Kendall Northumbria University David Kendall (Northumbria University) Operating systems fundamentals - B06 1 / 12

Introduction Introduction to threads Reminder of fork() pthreads example Comparison of pthreads with fork() Main pthread functions Some problems with threads David Kendall (Northumbria University) Operating systems fundamentals - B06 2 / 12

What are threads and why do we need them? We have already seen that it is very useful for the OS to provide real, or pseudo, concurrency We can divide our work up into meaningful units that can be considered separately When some unit of work is blocked waiting for I/O, another unit of work can make use of the CPU So far our unit of work is the process We can create multiple processes and allow the OS to schedule them to maximise the use of resources But the process is a heavyweight unit of work - it comes with lots of baggage: in addition to code, static data, registers, stack, heap etc. it also has open files, pipes, signals, sockets, devices etc The point of threads is to give the benefits of concurrency but in a much more lightweight form In fact, threads are sometimes called lightweight processes David Kendall (Northumbria University) Operating systems fundamentals - B06 3 / 12

Single- and multi-threaded processes Source: SGG12, Chp. 4 On the left is a standard process with a single thread of control On the right is a multi-threaded process a process that has created additional threads Notice what is shared and what is private David Kendall (Northumbria University) Operating systems fundamentals - B06 4 / 12

fork() reminder #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> int globvar = 6; int main(void) { int var = 88; pid_t pid; printf("before fork\n"); if ((pid = fork()) < 0) { fprintf(stderr, "fork failed\n"); exit(-1); } else if (pid == 0) { globvar++; //child var++; printf("child : pid = %d, globvar = %d, var = %d\n", getpid(), globvar, var); } else { waitpid(pid, NULL, 0); // parent printf("parent: pid = %d, globvar = %d, var = %d\n", getpid(), globvar, var); } exit(0); } David Kendall (Northumbria University) Operating systems fundamentals - B06 5 / 12

pthreads example #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <pthread.h> int globvar = 6; void *threadcontroller(void *arg) { globvar++; // var++; Notice this variable is not accessible printf("new thread : pid = %d, tid = 0x%lx, globvar = %d, " "var = %s\n", getpid(), (unsigned long)pthread_self(), globvar, "Not accessible"); pthread_exit((void *)0); } int main(void) { int var = 88; pthread_t thread; printf("before thread create\n"); if (pthread_create(&thread, NULL, threadcontroller, NULL)!= 0) { fprintf(stderr, "thread create failed\n"); exit(-1); } else { pthread_join(thread, NULL); // main thread printf("main thread: pid = %d, tid = 0x%lx, globvar = %d, " "var = %d\n", getpid(), (unsigned long)pthread_self(), globvar, var); } exit(0); } David Kendall (Northumbria University) Operating systems fundamentals - B06 6 / 12

fork() compilation and output $ gcc -o forkexample1 forkexample1.c $./forkexample1 before fork Child : pid = 9805, globvar = 7, var = 89 Parent: pid = 9804, globvar = 6, var = 88 pthread compilation and output $ gcc -pthread -o threadexample1 threadexample1.c $./threadexample1 before thread create New thread : pid = 11030, tid = 0x7f12ddb66700, \ globvar = 7, var = Not accessible Main thread: pid = 11030, tid = 0x7f12de347740, \ globvar = 7, var = 88 David Kendall (Northumbria University) Operating systems fundamentals - B06 7 / 12

Points to notice in the example threadexample must be compiled with the option -pthread this links the pthread library into the executable Process identifiers in fork example are different Process identifiers in pthread example are the same globvar and var in the parent and child processes are separate globvar in the main and new threads are shared var is only accessible in the main thread, not in the new thread The main and new threads can be distinguished using the thread identifiers (tid) David Kendall (Northumbria University) Operating systems fundamentals - B06 8 / 12

Thread creation and destruction Create thread #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Compile and link with -pthread. Creates thread with attributes attr and calls the start_routine with argument arg Terminate thread void pthread_exit(void *retval); Can also just return from the thread. Terminates the calling thread and returns a value that is available to another thread in the same process that calls pthread_join David Kendall (Northumbria University) Operating systems fundamentals - B06 9 / 12

Thread join and self Thread join #include <pthread.h> int pthread_join(pthread_t thread, void **retval); Wait for thread to terminate. Return immediately if thread has already terminated. If retval is not NULL then copy then exit status of the target thread into the location pointed to by *retval Thread self pthread_t pthread_self(void); Return the ID of the calling thread. This is the same value that is returned in *thread in the pthread_create() call that created this thread David Kendall (Northumbria University) Operating systems fundamentals - B06 10 / 12

Threads in action Output from htop Shows a machine (my laptop) with 4 cores Running 127 processes with 325 threads and 91 kernel threads 1 thread currently running David Kendall (Northumbria University) Operating systems fundamentals - B06 11 / 12

Some problems with threads Threads are not without their problems The main problems arise when multiple threads try to access shared data This can lead to unpredictable results The unpredictability arises because we don t know which thread will be chosen by the scheduler to execute next This leads to different instruction sequences and different instruction sequences can lead to different results! This has given rise to a whole discipline of concurrent programming with locks, semaphores, mutexes, condition variables etc. needed to recover predictable behaviour... outside the scope of this module David Kendall (Northumbria University) Operating systems fundamentals - B06 12 / 12