
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 24
This page cannot be seen from the preview
Don't miss anything!
Dr Rahul Nagpal Computer Science
Dr. Rahul Nagpal Computer Science
Motivation
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
User Threads and Kernel Threads
Many-to-One
One-to-One
Two-level Model
Thread Scheduling
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); }