Ch 9: Control flow Sequencers • We will study a number of alternatives • A sequencer is a language construct to transfer control to some other point in a program, destination • traditional sequencers: • A sequencer implements a flow of control − sequential − conditional − iterative • jumps, low-level sequencers to transfer control • escapes, sequencers to transfer control out of commands and procedures • exceptions, sequencers to signal abnormal situations / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 0 20-11-2008 PAGE 1 Jumps Jumps • A jump transfers control to a specified program point • Unrestricted jumps lead to spaghetti code, thus hard to understand, • A jump has typical the form goto L; and transfer the control to see http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF program point L , which is a label if (E 1 ) • Most programming languages support gotos C 1 • Java refuses jumps, because it supports higher-level escapes and else { exceptions C 2 goto X; • Some languages have restrictions: } • the jump goto L; is legal only within the scope of L C 3 ; − in C the scope of each label is the smallest enclosing block: while (E 2 ) { − jumps within a block C 4 ; X: C 5 − jumps from a block to an enclosing one } − jumps into a block from outside is not allowed / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 2 / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 3
Jumps Escapes • An escape is a sequencer that terminates the execution of a • C restricts jumps because a jump out of a block textually enclosing command or procedure must destroy the local variables • In C, C++, Java: break sequencer to break loops • Jumps out of a procedure should lead to destroying • In C, C++, Java: return sequencer to break loops and end local variables and termination of procedure procedures • multiple return sequencers in a procedure is hard to activation maintain • Jumps out of a recursive procedure is even more • Escapes are restricted so that control cannot be transfer out of complicated procedures • A halt sequencer stops the entire program • Jumps introduce unwanted complexity in the • exit() in C semantics of high-level programming languages / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 4 20-11-2008 PAGE 5 Exceptions Exceptions • Exceptions are a mechanism to deal with abnormal • Code that detects an abnormal situation throws an situations: exception • arithmetic operation overflows • the exception can be caught in another part of the program • uncompleted input/output operations • programmer have control over exceptions and handling of it • Exceptions take of two things: • error handling • C++ and Java, being object-oriented languages, treat • Controlled termination of flow of control exceptions as objects / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 6 / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 7
Exceptions Exceptions • Java has a built-in Exception class • Exceptions can be caught at various levels: • every exception is a subclass of Exception static float readFloat(BufferedReader input) • every exception represents a different abnormal situation throws IOException, NumberFormatException { try C 0 … catch (T 1 I 1 ) C 1 if (…) … throw new IOException(“end of input”); catch (T n I n ) C n String literal = …; finally C f float f = Float.parseFloat(literal); • Able to catch any exception from class T 1 or … or T n return f; • if C 0 throws an exception of type T i handler C i will be executed with identifier I i bound to that exception } • Float.parseFloat throws the NumberFormatException / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 8 20-11-2008 PAGE 9 Exceptions Exceptions • The main program calls readAnnual and deals with the IOException • Method annual rainfall data may catch the NumberFormatException static float[] readAnnual(BufferedReader input) static void main() { throws IOException { float[] rainfall; … … try { try { float r = readFloat(input); rainfall = readAnnual(); … … } } catch (NumberFormatException e) { catch (IOException e) { … … } } … … } } / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 10 / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 11
Ch 10: Concurrency Programs and processes • Why concurrency? • A sequential process is a totally ordered set of steps • program design becomes more complex • each step is a change of state • testing becomes less effective • A sequential program specifies the state changes of a sequential process • historically, improvement of efficiency via − multiprogramming systems, usage of idle resources • A concurrent program specifies the possible state changes of 2 or more sequential processes • currently, end of Moore’s law, efficiency gain via − multi-core processors / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 12 20-11-2008 PAGE 13 Problems with concurrency Problems with concurrency • Nondeterminism: • Speed dependence • sequential programs are deterministic • sequential programs are speed-independent because • collateral and nondeterministic conditional commands correctness does not depend on execution speed may introduce unpredictability in sequential programs • concurrent programs are speed-dependent − compiler is free to determine the order of execution − behavior depends on the relative speed at which its − different compilers may lead to different behavior constituent sequential processes run • A concurrent program is genuinely nondeterministic − if absolute speeds are considered, outside world, we even for a specific compiler have real-time behavior − incorrect concurrent programs may behave correctly − if the outcome is speed-dependent, there is a race in general, but have sometimes unpredictable condition behavior / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 14 / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 15
Problems with concurrency Problems with concurrency • Deadlock means that processes are unable to make progress • Solutions for deadlocks: because of mutually incompatible demands for resources • Ignore them and restart system if it occurs • mutual exclusion : a process may be given exclusive access • Recovery by detecting and kill involved processes to resources • Prevention by remove some of the preconditions • incremental acquisition : a process holds previously acquired • Avoid by scheduling of resources resource while waiting for new resources • no preemption : resources cannot be removed from a process until it voluntarily releases them • circular waiting : a cycle of resources or processes in which each process is waiting for resources that are held by the next process in the cycle / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 16 20-11-2008 PAGE 17 Problems with concurrency Process interactions • Starvation • Sequential and collateral commands do not allow simultaneously execution of commands • A concurrent program has the liveness property if it is guaranteed that every process will make some • The parallel command “ B || C ” indicates that B and progress over a sufficiently long period of time C may executed simultaneously or arbitrarily − Free of deadlock interleaved − Fair scheduling • Fair scheduling means that no process needing a resource is indefinitely prevented from obtaining it / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 18 / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 19
Process interactions Process interactions • Competing processes • Independent processes: • Commands B and C are competing if they need exclusive • commands B and C are independent if the execution of access to the same resource r B has no effect on the execution of C , and vice versa − Let B be the sequence B 1 ; B 2 ; B 3 • concurrent composition of independent processes is − Let C be the sequence C 1 ; C 2 ; C 3 deterministic − B 1 , C 1 , B 3 , C 3 are independent, none need r − B 2 and C 2 both need r , so they cannot take place simultaneously, they are critical section wrt r − B || C may be executed as: − …; B 2 ; … ; C 2 ; … − …; C 2 ; … ; B 2 ; … − but not as …; B 2 ||C 2 ; … / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 20 20-11-2008 PAGE 21 Process interactions Concurrency primitives • Process until now was a flow of control through a program • Communicating processes • a conventional, heavyweight, process is a program, which involves: • let B be the sequence B 1 ; B 2 ; B 3 − an address space • let C be the sequence C 1 ; C 2 ; C 3 − allocation of main storage, − share of CPU time • there is communication from B to C if B 2 produces data − access to files, devices, etc. that C 2 consumes, so B 2 must end before C 2 starts − context switching from one process to another involves a lot of • Thus B || C has the same behavior as B; C time • a thread is a lightweight alternative − flow of control through a program without computational • Processes B and C intercommunicate if there is resources communication in both directions: − switching of threads involves swapping of content of working registers − B || C yields numerous outcomes / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 22 / Faculteit Wiskunde en Informatica 20-11-2008 PAGE 23
Recommend
More recommend