Download presentation
Presentation is loading. Please wait.
Published byAileen Carpenter Modified over 8 years ago
1
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science, University of Calgary
2
Outline Multi-process Programming Process ids Process creation Process wait and exec family of functions Exercise - 1 Multi-threaded programming Thread creation Thread wait (join) Parameter passing Exercise - 2 2
3
Process Execution When a child process is created… The parent may continue to execute concurrently with its children At some point the parent shall wait until each of its children have terminated Otherwise zombie processes may remain in the system. 3
4
Example Code #include int main () Ref: Example1.c { printf("Hello World\n"); fork(); printf("Goodbye World\n"); } How many prints of ”Hello World” and ”Goodbye World”? 4
5
wait() Wait for one of the child processes to terminate exec() Replace current process image with new one Signals Communicating with processes Special messages to the processes More on this topic in later tutorial 5
6
Exercise - 1 Overview Discussion Sample codes 6
7
Review: Threads Threads in a process allow multiple executions to take place in the same process environment A thread is a Mini-process within a process. Threads are scheduled independently 7 shared unshared
8
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 8
9
Review: 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) 9
10
Example Code Refer example-0.c Discussion 10
11
Exercise - 2 Overview Discussion 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.