Download presentation
Presentation is loading. Please wait.
Published byPoppy Prudence Waters Modified over 9 years ago
1
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University of Calgary
2
Review: Threads Threads in a process allow multiple executions to take place in the same process environment A thread is a unit of execution of a process. Basically, mini-processes within a process. Threads are scheduled independently and can make system calls simultaneously A thread requires an address space and other resources but it can share many of those resources with other threads 2 shared unshared
3
Review: Processes vs. Threads Processes Processes are used when the tasks are essentially unrelated Each process has its own address space and PCB Address spaces are protected from each other Switching between processes is done at the kernel level Owned by one or more users 3 Threads Threads are used when tasks are actually part of the same job Threads belonging to the same process share the process’ address space, code data, and files, but not the registers and stack No address space protections Switching between threads can be done at either the user level or the kernel level Usually own by a single user
4
Review: Thread Libraries A thread library provides the programmer with an API for creating and managing threads. Three main libraries in use today POSIX Pthreads Win32 Java 4
5
Review: How to Use Pthreads Library? Include the Pthreads library #include Create your thread pthread_create(thread, attr, start_routine, arg) thread: pointer to the created object attr: joinable, detached, … You can use NULL most of the time. start_routine: routine to be executed by the new thread arg: a single argument for the start_routine Destroy your thread when it is done pthread_exit(status) Make sure the process waits for the threads to finish pthread_join(thread, status) 5
6
Example Code Slide (Replace this) 6 #include #define NUMBER_OF_THREADS 10 void *print_hello_world(void * tid) { printf("Hello Word. Greetings from thread %d\n", tid); pthread_exit(0); } int main(int argc, char *argv[]) { pthread_t threads[NUMBER_OF_THREADS]; int status, i; for (i = 0; i < NUMBER_OF_THREADS; i++){ printf("Main here, Creating thread %d\n", i); status = pthread_create(&threads[i], NULL, print_hello_world, (void *) i); if (status != 0) { printf("Oops, pthread_create returned error code %d\n", status); exit(-1); } } exit(0); } What is missing?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.