Lecture Notes on Programming Concurrent Computer Systems
Total Page:16
File Type:pdf, Size:1020Kb
UNU/IIST International Institute for Software Technology Lecture Notes on Programming Concurrent Computer Systems Zhiming Liu May 2005 UNU/IIST Report No. 325 T UNU-IIST and UNU-IIST Reports UNU-IIST (United Nations University International Institute for Software Technology) is a Research and Training Centre of the United Nations University (UNU). It is based in Macau, and was founded in 1991. It started oper- ations in July 1992. UNU-IIST is jointly funded by the Governor of Macau and the governments of the People’s Republic of China and Portugal through a contribution to the UNU Endownment Fund. As well as providing two- thirds of the endownment fund, the Macau authorities also supply UNU-IIST with its office premises and furniture and subsidise fellow accommodation. The mission of UNU-IIST is to assist developing countries in the application and development of software tech- nology. UNU-IIST contributes through its programmatic activities: 1. Advanced development projects, in which software techniques supported by tools are applied, 2. Research projects, in which new techniques for software development are investigated, 3. Curriculum development projects, in which courses of software technology for universities in developing countries are developed, 4. University development projects, which complement the curriculum development projects by aiming to strengthen all aspects of computer science teaching in universities in developing countries, 5. Schools and Courses, which typically teach advanced software development techniques, 6. Events, in which conferences and workshops are organised or supported by UNU-IIST, and 7. Dissemination, in which UNU-IIST regularly distributes to developing countries information on interna- tional progress of software technology. Fellows, who are young scientists and engineers from developing countries, are invited to actively participate in all these projects. By doing the projects they are trained. At present, the technical focus of UNU-IIST is on formal methods for software development. UNU-IIST is an internationally recognised center in the area of formal methods. However, no software technique is universally applicable. We are prepared to choose complementary techniques for our projects, if necessary. UNU-IIST produces a report series. Reports are either Research R , Technical T , Compendia C or Adminis- trative A . They are records of UNU-IIST activities and research and development achievements. Many of the reports are also published in conference proceedings and journals. Please write to UNU-IIST at P.O. Box 3058, Macau or visit UNU-IIST’s home page: http://www.iist.unu.edu, if you would like to know more about UNU-IIST and its report series. G. M. Reed, Director UNU/IIST International Institute for Software Technology P.O. Box 3058 Macau Lecture Notes on Programming Concurrent Computer Systems Zhiming Liu Abstract Concurrent programming is the activity of constructing a program containing multiple processes that execute in parallel. The aim of the course is to introduce the basic concepts, principles and techniques in programming concurrent computing systems, and to provide practice in solving problems and skill in writing concurrent programs. The course will also provide methods for evaluating systems, algorithms and languages from a broad perspective. The course provides the students with understanding of the notations of concurrency, nondeterminism, synchronization, deadlock, livelock, safety and liveness. It provides students with the history of the development different language mechanisms for the realization of synchronization and interactions, in- cluding synchronization without using synchronization primitives, synchronization by semaphores, con- ditional regions, monitors, hand-shake communication and remote invocation. It covers communication via shared variables and message passing. We will take an informal but prices approach in the discussion of of requirements, design and valida- tion of concurrent programs. Zhiming Liu is a research fellow at UNU/IIST. His research interests include theory of computing sys- tems, including sound methods for specification, verification and refinement of fault-tolerant, real-time and concurrent systems, and formal techniques for OO development. His teaching interests are Com- munication and Concurrency, Concurrent and Distributed Programming, Software Engineering, Formal specification and Design of Computer Systems. E-mail: [email protected]. Copyright °c 2005 by UNU/IIST, Zhiming Liu Contents i Contents 1 PREFACE 1 1.1 Aims & Objectives . 1 1.2 Syllabus . 1 1.3 Reading List . 2 2 INTRODUCTION 3 2.1 Goals for Chapter 1 . 3 2.2 Course Organisation . 3 2.3 Reading List . 3 2.4 Course Assessment . 4 2.5 Overview . 4 2.6 Concurrent Programming . 5 2.6.1 Sequential programming . 5 2.6.2 Concurrent programming as partial ordering . 8 2.6.3 Concurrent programming as interleaving . 11 2.6.4 Concurrent computer systems . 13 2.7 The motivation for concurrent programming . 14 2.8 Problems in concurrent programming . 15 2.9 Seat Reservation Problem . 16 2.9.1 Critical sections and Mutual exclusion . 19 2.10 Concurrent programming in Pascal-FC . 20 2.11 Exercises . 24 3 MUTUAL EXCLUSION 27 3.1 Process interaction . 27 3.2 Active and passive entities . 28 3.3 Communication via shared variable . 28 3.3.1 The Ornamental Gardens problem . 29 3.4 Old-fashioned recipes for synchronization . 32 3.4.1 An old-fashioned recipe for condition synchronization . 33 3.4.2 Old-fashioned recipes for mutual exclusion . 34 3.5 Correctness of concurrent programs . 42 3.6 Exercises . 43 4 SEMAPHORES 51 4.1 Introducing Semaphores . 51 4.2 Definition of semaphores . 52 4.3 Mutual exclusion with semaphores . 54 4.4 Implementation of semaphores . 55 4.5 Analysing semaphore programs . 56 4.6 Condition synchronization with semaphores . 57 4.6.1 Producer and consumer with an unbounded buffer . 57 4.6.2 Producer-consumer with bounded buffers . 59 4.7 More examples . 62 Report No. 325, May 2005 UNU/IIST, P.O. Box 3058, Macau Contents ii 4.7.1 The Readers and Writers Problem . 62 4.7.2 The Dining Philosophers problem . 66 4.8 Final remarks on semaphores . 69 4.9 Exercises . 70 5 CONDITIONAL CRITICAL REGIONS AND MONITORS 72 5.1 Introduction . 72 5.2 Critical regions . 73 5.3 Conditional critical regions . 74 5.3.1 Example - condition synchronization . 76 5.4 Monitors . 78 5.4.1 The ideas behind monitors . 78 5.4.2 Definition of monitors . 79 5.4.3 Mutual exclusion with monitors . 80 5.4.4 Condition synchronization with monitors . 82 5.4.5 More examples . 85 5.4.6 Producer-Consumer with a bounded buffer . 85 5.4.7 Readers and writers . 87 5.4.8 Reasoning about monitor programs . 89 5.4.9 The expressiveness of monitors . 91 5.5 Exercises . 91 6 SYNCHRONOUS MESSAGE PASSINGS 93 6.1 Introduction . 93 6.2 Three forms of communication . 94 6.3 Naming the destinations and sources of messages . 95 6.4 Channels in Pascal-FC . 96 6.5 The types of channels . 96 6.6 A classification of distributed processes . 98 6.6.1 An network of filters – Prime number generation . 99 6.7 Synchronous channels . 102 6.8 Selective waiting construct . 103 6.9 Guarded alternatives . 106 6.10 The terminate alternative . 107 6.11 Else and timeout alternatives . 111 6.12 Exercises . 112 7 REMOTE INVOCATION 113 7.1 Introduction . 113 7.2 The message passing primitives in the remote invocation model . 114 7.3 Selective waiting with remote invocation . 119 7.4 Examples . 121 7.4.1 Resource allocation problem . 121 7.4.2 Dining philosophers problem . 123 7.4.3 Process idioms . 124 Report No. 325, May 2005 UNU/IIST, P.O. Box 3058, Macau Contents iii 7.4.4 A reactive example - a controlling system . 126 7.5 The limitation of the Pascal-FC select construct . 129 7.6 Exercises . 131 8 SUMMING UP 132 8.1 The course . ..