Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Operating systems and Advancing later on, Exams of Operating Systems

all the things related to OS all the things related to OS all the things related to OS all the things related to OS

Typology: Exams

2020/2021

Uploaded on 04/15/2021

KKSS12121212
KKSS12121212 🇮🇳

3 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
OPERATING SYSTEM
Threads & Concurrency
Dr Rahul Nagpal
Computer Science
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Introduction to Operating systems and Advancing later on and more Exams Operating Systems in PDF only on Docsity!

OPERATING SYSTEM

Threads & Concurrency

Dr Rahul Nagpal Computer Science

OPERATING SYSTEM

Threads & Concurrency

Dr. Rahul Nagpal Computer Science

Motivation

  • Process creation is heavy-weight while thread creation is light-weight
  • Most modern applications are multithreaded
  • Threads run within application
  • Multiple tasks in application can be implemented by threads
    • Update display
    • Fetch data
    • Spell checking
  • Can simplify code, increase efficiency
  • Kernels are generally multithreaded

Single and Multithreaded Processes

Benefits Responsiveness – may allow continued execution if part of process is blocked, especially important for user interfaces Resource Sharing – threads share resources of process, easier than shared memory or message passing Economy – cheaper than process creation, thread switching lower overhead than context switching Scalability – process can take advantage of multiprocessor architectures

Multicore Programming Multicore or multiprocessor systems putting pressure on programmers, challenges include: Dividing activities Balance Data splitting Data dependency Testing and debugging Parallelism implies a system can perform more than one task simultaneously Concurrency supports more than one task making progress Single processor / core, scheduler providing concurrency

Concurrency vs. Parallelism

  • Concurrent execution on single-core system:
  • Parallelism on a multi-core system:

User Threads and Kernel Threads

  • User threads - management done by user-level threads library
  • Three primary thread libraries:
    • POSIX Pthreads
    • Windows threads
    • Java threads
  • Kernel threads - Supported by the Kernel
  • Examples – virtually all general purpose operating systems, including: - Windows - Solaris - Linux - Tru64 UNIX - Mac OS X

Many-to-One

  • Many user-level threads mapped to single kernel thread
  • One thread blocking causes all to block
  • Multiple threads may not run in parallel on muticore system because only one may be in kernel at a time
  • Few systems currently use this model
  • Examples:
    • Solaris Green Threads
    • GNU Portable Threads

One-to-One

  • Each user-level thread maps to kernel thread
  • Creating a user-level thread creates a kernel thread
  • More concurrency than many-to-one
  • Number of threads per process sometimes restricted due to overhead
  • Examples
    • Windows
    • Linux
    • Solaris 9 and later

Two-level Model

  • Similar to M:M, except that it allows a user thread to be bound to kernel thread
  • Examples
    • IRIX
    • HP-UX
    • Tru64 UNIX
    • Solaris 8 and earlier

Thread Scheduling

  • Distinction between user-level and kernel-level threads
  • When threads supported, threads scheduled, not processes
  • Many-to-one and many-to-many models, thread library schedules user-level threads to run on LWP - Known as process-contention scope (PCS) since scheduling competition is within the process - Typically done via priority set by programmer
  • Kernel thread scheduled onto available CPU is system- contention scope (SCS) – competition among all threads in system

Pthreads Example

Slides Adapted from Operating System Concepts 9/e © Authors Pthread Scheduling API #include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 int main(int argc, char argv[]) { int i, scope; pthread_t tid[NUM THREADS]; pthread_attr_t attr; / get the default attributes / pthread_attr_init(&attr); / first inquire on the current scope / if (pthread_attr_getscope(&attr, &scope) != 0) fprintf(stderr, "Unable to get scheduling scope\n"); else { if (scope == PTHREAD_SCOPE_PROCESS) printf("PTHREAD_SCOPE_PROCESS"); else if (scope == PTHREAD_SCOPE_SYSTEM) printf("PTHREAD_SCOPE_SYSTEM"); else fprintf(stderr, "Illegal scope value.\n"); } / set the scheduling algorithm to PCS or SCS / pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); / create the threads / for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i],&attr,run ner,NULL); / now join on each thread / for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); } / Each thread will begin control in this function */ void *runner(void param) { / do some work ... */ pthread_exit(0); }