33
Software Engineering 3156 26-Nov-01 #22: OS, Language, Design Patterns Phil Gross

Software Engineering 3156 26-Nov-01 #22: OS, Language, Design Patterns Phil Gross

  • View
    217

  • Download
    1

Embed Size (px)

Citation preview

Software Engineering 3156

26-Nov-01

#22: OS, Language, Design Patterns

Phil Gross

2

Administrivia

Research Fair– Nov 30th, 10am-4pm– http://www.cs.columbia.edu/acm/research

Advanced Perl– Networking and OO– Tuesday, Nov 27th, 6-8pm

JSP and Servlets– Wednesday, Nov 28th, 6-8pm

3

Systems Programming

Unix, Windows (and JVM) all written in C/C++ Couldn’t write a JVM for a bare system in Java Need access to underlying system resources Located at particular memory locations Operating system provides (very) basic

services System calls

4

System Calls

Man page section 2, not 3– Or weirder sections, particularly for Solaris

That is the sum total of what an OS does– It usually also comes with a bunch of software

Systems programming involves dealing directly with the system calls, or just above

5

Typical System Calls

File access Time and date Process management Signals Networking Security

6

File I/O

open, creat, close read, write

– Pure byte-oriented

7

Unix and Devices

Everything’s a file Devices are “special files” Character or block oriented Character devs include some weird things

– Mice– Audio

Block devices are storage Mknod

8

Filesystems

Sorta part of the OS Usually abstracted and modularized Primitive version has one filesystem per

“volume” Unix model has “mount points” Hard links vs. soft links

9

Special Filesystems

/proc– Virtual filesystem– Access to all kinds of information– Originally processes

NFS– Network filesystem– RPC based

Journalling Filesystems

10

Processes

The fork model Copy on write Vfork Most processes are sleeping/waiting at any

given time Scheduler in kernel picks from among the

ready processes for the next time slice

11

Virtual Memory and Paging

Every program allocated large flat memory space, mapped to real memory

Memory allocated by “pages” When full, some pages are copied to disk Replacement targets picked by some strategy

– What strategy?

Can leverage this yourself with “memory mapped files”

12

IPC

Message/queue– Every process has its own little mailbox

Shared memory/semaphore– We see the same memory– We have these things called semaphores that we

can use as locks to ensure no simultaneous writes– Locking sequence with multiple readers and writers?

13

Signals

“software interrupts” Asynchronous model When signal arrives, current task stops and

“signal handler” starts executing Not as nice as threads…

14

Sockets

Way ugly Highly confusing Janak wrote a nice, heavily commented

example

15

Socket call

Reserves a name, basically Supports one of a number of Protocol Domains Usually PF_INET for Internet Protocol

communication Also a Type, usually SOCK_STREAM (=TCP)

or SOCK_DGRAM (=UDP) Plus a specific protocol (rare)

16

System Call Notes

All three parameters are untyped Int, int, and int Better put them in the right sequence… Linux supports a bunch more protocols than

Solaris But Solaris TCP/IP support is among the best

17

Bind and Listen

Bind– Says on what port and interface to listen– Note delightful steps to define an address

Listen– Says that this will be a server socket– Defines pending queue size

18

Accept

Actually gives us the socket Can then read() and write() bytes

19

C++

C with classes And function overloading And operator overloading And an amazing standard library

20

Inheritance in C++

Can be public, protected or private class A : public class B { … }

– Inheritance type becomes visibility of class B public members

Has multiple inheritance– class A : public class B, private class C { … }– Useful for “mixins”– Beware the Death Diamond

21

Polymorphism in C++

Methods not late-binding by default To get polymorphic behavior, must explicitly

label methods with “virtual”– And only pass objects by reference or pointer– Not value

Extra “VPTR” added to class Should also make destructor virtual

22

When CTOR Allocates Memory…

You must– Free the memory in the destructor– Define your own copy constructor– Define your own assignment operator

Default Copy/Assignment is bitwise– Pointers are copied (“shallow” copy)

If a temp copy of your object is made, then freed, your memory will be deallocated!

23

Design Patterns

Seminal book is Design Patterns: Elements of Reusable Object-Oriented Software, by Gamma, Helm, Johnson, and Vlissides, 1995– Usually referred to, even in formal bibliographies, as

the Gang of Four book, or just Gamma

Owes a debt to A Pattern Language: Towns/Buildings/Construction, by Christopher Alexander, 1977

24

What is a Design Pattern?

From Alexander: “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice”

Generally presented as Name, Problem, Solution, Consequences

25

What is a Design Pattern cont.

Not data structures, and not extremely domain specific

“Descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context”

Generally involve designing with more flexibility and indirection than you might otherwise initially use

26

Strategy Pattern

Instead of hardcoding algorithm, create an interface

Allow runtime selection of concrete implementation

More elegant than if-then sequences My dispatcher is related to this

27

Iterator Pattern

The classic: (almost) fully absorbed into Java and C++

Abstract pointer-like object that can iterate through any data structure with a consistent interface

C++ has a beautiful version Java is half-baked Beware of updates while iterating!

28

Flyweight Pattern

Dealing with hordes of objects We want every character in our document to

be an object– Normally far too expensive

Instantiate only one object per character and share

Characters point to format objects to minimize state

29

Singleton Pattern

Ensure only one of a particular type of object class Singleton { public: static Singleton* instance(); protected: Singleton(); private: static Singleton* _instance; };

30

Factory Method

Provides an abstract interface for constructors Allows subclasses to substitute their own

constructors Typical Pattern technique of substituting a

more flexible “loose coupling” for built in OO rigidity

Java: java.net.SocketImplFactory

31

Chain of Responsibility

Decouple sender and receiver Ask for help on screen widget

– May be handled by element itself– Or parent– Or great-grandparent

Use instances of HelpHandler to find successor in chain– Or use existing chain

32

Memento

An opaque state object Allows internal state of objects to be saved and

restored without violating encapsulation Distinguishes between Caretaker class, that

can only save and transfer memento, and Originator class, that can use it

Useful for e.g. undo Externalizes storage responsibilities

33

Mediator

Distributed OO designs can be unmanageable Control is spread too widely to follow E.g. Dialog box with interdependencies Forcibly recentralize

– Create a mediator object– All component objects communicate only with

mediator– Policy is in one place