Download presentation
Presentation is loading. Please wait.
1
COMS W3156: Software Engineering, Fall 2001 Lecture #22: C, C++, OS, etc… Janak J Parekh janak@cs.columbia.edu
2
Administrativia Prototype is in: enough for integration –Our work never finishes: now working on reference model –If problems with the prototype, come see us early Final exam: any questions? –Open book doesn’t mean easier… Reminder: Research Fair this Friday –http://acm.cs.columbia.edu/researchhttp://acm.cs.columbia.edu/research Intro to Java Server pages and Servlets tomorrow –See CS front doors…
3
Next class Finish everything Monday will be MMM and final exam review Can you believe the semester has less than two weeks left?
4
Today’s class A little bit more on C C++ Begin operating systems Begin design patterns, if time
5
Struct Basically, a class without methods struct Point { int x; int y; }; /* note the semicolon */ int main(void) { struct Point myPoint; myPoint.x = 5; myPoint.y = 5; }
6
Miscellany on structs (*myP).x looks ugly –C has a bizarre shortcut: myP -> x –Yes, a dash followed by a greater than –Always remember that a -> b is equivalent to (*a).b –Precedence of operators It’s cumbersome to have to say struct Point every time –Use typedefs: define “aliases” –typedef struct Point PointT; –or even typedef struct Point *PointP;
7
More miscellany on structs Since not everything happens in main, malloc’ing structs is very common –The sizeof operator works on pretty much any datatype, be it structs or primitive datatypes –Guaranteed tip: you will come across PointP myP = (PointP)malloc(sizeof(PointP)); –and be glad that you were attending this class…
8
Function prototypes As I said, C’s compiler is not all that smart If you have: int main(void) { int i = foo(); } int foo(void) { return 5; } It will not work
9
Function prototypes (II) Need to clue in the compiler by putting the following prototype before main: int foo(void); Looks like a Java interface construct Often, you collect the prototypes into a header (.h) file, then #include it into your various source (.c) files
10
On various.c files… A common way to build multi-source-file code in C is to –build a set of.h files that export functionality (i.e. “public” methods) –#include these.h files –Compile and link the.c files together As long as you have a prototype, the compiler will not care about the definition –Like compiling against an Interface –The Linker will, though…
11
Useful libc commands (I) printf: Output text: like System.out.println –printf(“%d\n”, intval); –printf(“%d%f\n”, intval, floatval); –man printf scanf: Input text: no BufferedReader “crap” –scanf(“%d”, &intval) –note the ampersand
12
Useful libc commands (II) gets(string) –gets a line of text and puts it in string –Should technically use fgets(stdin, string) –Why not &string? –puts() also exists: more like println, actually –Can’t use + as a concatenator atoi(string) –Roughly equivalent to Integer.parseInt, although not nearly as smart –No exceptions
13
C++ Not strictly a superset of C, but C++ compilers backwards-compatible with C –Certain subtle language differences –C with classes, but a whole lot more Function, operator overloading Powerful “template” functionality Exceptions
14
Quick primer to C++ g++ is the GNU C++ compiler –Actually a C++ plugin to gcc Can feed C code to it, will work fine Can integrate C and C++ code pretty easily However, ideally, you want to avoid using many C constructs
15
Hello, world in C++ #include using namespace std; int main(void) { cout << “Hello world\n”; } Note use of namespaces, streams << operator is overloaded to allow combination/concatenation of strings with streams Can do cout << “foo” << “bar” << endl;
16
Class declarations in C++ class Rectangle { private: double width; double height; public: Rectangle(int width, int height); ~Rectangle(); void setWidth(int width); int getArea(); // and so on… }
17
Implementing methods Inline in the class declaration Or, use the class as a prototype and implement elswhere void Rectangle::setWidth(int w) { width = w; }
18
Constructors and destructors You have both, not just one Motivation of destructor: cleanup the object, free any memory, etc. Don’t use C++’s default destructor if you new anything –You should delete anything you new –Don’t use malloc/free unless absolutely necessary
19
Inheritance in C++ class C: public class A { /* stuff */ } –Motivation: make A’s members public in C –if “: private class A”, A’s members become private in C –Java only has the public mechanism Multiple inheritance! –class C: public class A, private class B {…} –Can be extremely useful, but beware of “the Death Diamond”, among other things
20
Polymorphism in C++ If B extends A, and you new a B but “call it” an A: –In Java, if a method in A is overridden, will actually call the overloaded one automatically –In C++, will not by default: must declare a method virtual; also, cannot pass objects call- by-value into method Should also make destructor virtual
21
Memory allocation in C++ new, delete When you allocate memory, you must also –write a destructor –define a assignment operator –define a copy operator Why? –In C++, default copy/assignment is bitwise –Your pointers will just get “copied”, instead of new memory being allocated
22
C++ miscellany Operator overloading: nice, but must be careful References in function prototypes (finally!) Templates: “type”ify data structures STL: Standard Template Library –You now have “string”s! –Lots of other data structures –Somewhere between C and Java API’s –Very, very fast if used correctly Wish we had more time to cover this…
23
Systems programming Most software written in C/C++ –UNIX –Windows –Java VM’s Why? –Need access to underlying system resources But multiple programs need those resources –An operating system’s primary responsibility is to manage resources
24
Managing resources An OS often abstracts away the resources, and builds an API to access them –“Virtualization” Operating system’s API: system calls Why don’t you always see them? –The C, C++, Java libraries some of the system call work for you
25
System calls See man pages in section 2 –FYI: man 2 open That’s the prime responsibility of an OS to its applications –Of course, it’s more complicated than that –Most functionality we consider standard to an OS is actually bundled, however Really cool: truss or strace
26
Typical system calls File access (managing disks) Process management (managing processor) Memory allocation (managing memory) Date/time (managing clock) At a higher level: –Networking –Security
27
Example system calls: file I/O open, create, close These work with file descriptors –Actually generalized in UNIX to be far beyond files: sockets represented by file descriptors read, write –Purely byte-oriented Do a man on these…
28
UNIX Core concept: everything is a file Devices are “special files”, stored in /dev UNIX devices are character or block oriented –tty vs. disk, for example –mice, audio? mknod command
29
Speaking of files… Filesystems are often considered a core functionality of the OS However, it’s usually separately modularized: convenience abstraction layer Two common models: –Rooted (UNIX and cousins): mount points –Non-rooted (DOS, Windows) Links…
30
Special filesystems “Virtual” filesystems –/proc In Linux and Solaris Has a number of useful sets of information, no longer just “processes” –/tmp On certain machines, actually ramdisk Is this really virtual? NFS filesystems
31
Processes The “fork” model: start off a new process as a duplicate of an existing one Copy-on-write methodology: only create as much of the new process as necessary Multitasking of processes –Cooperative: “hey, let me know when you’re done, so I can give time to another process” –Preemptive: “yo, your slot is up” Interprocess communication: messages/queues, shared memory, pipes, domain sockets
32
Memory management Allocation: assign memory blocks to processes, as well as dynamic memory allocation from a “heap” Segmentation: prevent programs from overwriting each other’s memory Virtual memory: abstract away memory into “pages” which may be swapped in and out of disk –What to swap requires “replacement strategy” Memory-mapped files
33
Networking At first, write directly to network hardware (yuck!) TCP/IP, socket API built on top of most OS’s (BSD socket API) You have no idea how nice Java sockets are in comparison to C/BSD sockets…
34
Shell Not really part of the OS, but expected to be bundled with it Front-end to operating system Allow for convenient file management and process control bash, cmd just make a bunch of system calls… Explorer: a GUI front-end eshell: now this is freaky
35
What does this mean for you? C/C++: small program to be written for HW4 OS’s: covered lightly on FE, if at all You’ll learn about each of these in the actual operating systems class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.