20
CS222 Week 4 - Friday

Week 4 - Friday. What did we talk about last time? Some extra systems programming stuff Scope

Embed Size (px)

Citation preview

Page 1: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

CS222Week 4 - Friday

Page 2: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Last time

What did we talk about last time? Some extra systems programming

stuff Scope

Page 3: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Questions?

Page 4: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Project 2

Page 5: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Quotes

Unix never says "please."

Rob Pike

It also never says: "Thank you" "You're welcome" "I'm sorry" "Are you sure you want to do that?"

Page 6: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Compiling multiple files

All real programs are written in multiple files

To compile such files, do the following Create a header file (with a .h

extension) for every file that contains prototypes for functions you're going to use in other files

#include those header files in every file that uses them

When you run gcc, put all the .c files needed on the line at the same time

Page 7: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Multiple compilation example Your main() function and core program is in a file called program.c

You have files called networking.c and graphics.c that have networking and graphics functions that your program uses You should have headers called networking.h and graphics.h (names don't have to match, but it is better if they do)

At the top of program.c should be:

To run gcc you type:

#include "networking.h"#include "graphics.h"

gcc program.c networking.c graphics.c -o program

Page 8: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Or you can use compile but not link

You can compile a file into object code without linking it into an executable Produces a .o file

That way, you can compile all the pieces separately and then link them later

This can be more efficient if you are updating some of the code but not all of it

To compile but not link, use gcc -c We could compile the previous example as followsgcc –c program.cgcc –c networking.cgcc –c graphics.cgcc program.o networking.o graphics.o -o program

Page 9: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Makefiles to the rescue

Now that we're talking about compiling multiple files, a makefile really makes (ha, ha) sense

all: program

program: program.o networking.o graphics.ogcc program.o networking.o graphics.o -o program

program.o: program.c networking.h graphics.hgcc –c program.c

networking.o: networking.cgcc –c networking.c

graphics.o: graphics.cgcc –c graphics.c

clean:rm -f *.o program

Page 10: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Processes

Page 11: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Processes

Recall that a process is a running program Multiple copies of a single program can be

running as different processes A program is a binary file (generated by the

compiler) Formats used to be:

Assembly output (where the name a.out comes from) COFF (Common Object File Format)

Now they are usually ELF (Executable and Linking Format)

Details about binary formats are more interesting when you're writing a compiler

Page 12: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

PIDs

Every running process has a process ID (PID) You can find out the PID of the currently

executing code by calling the getpid() function

Every process also has a parent process (which has a parent PID) Get that by calling getppid() The parent is the process that created the current

process This parent-child relationship forms a tree all

the way back to the first process init, which always has PID 1

Page 13: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Process memory segments

Layout for 32-bit architecture Could only

address 4GB Modern layouts

often have random offsets for stack, heap, and memory mapping for security reasons

Text

Data

BSS

Heap

Memory Mapping

Stack

Kernel Space 1GB

3GB

0xc0000000

0x40000000

0x080480000x00000000

Only for Linux kernel

Memory for function calls

Addresses for memory mapped

files

Dynamically allocated data

Uninitialized globals

Initialized globals

Program code

Page 14: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Virtual memory

Those addresses (from 0 to 4GB) are virtual addresses

Each program sees an address space stretching from 0 to 4GB

The OS transparently manages how those addresses are mapped to physical memory

The virtual address space is divided up into pages

Each page can be in memory or sitting on disk Pages are typically moved into memory only

when needed

Page 15: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Why virtual memory?

It isolates processes from each other Since they have different address spaces, it is

harder for one program to read the data out of another

Processes can share the same memory without inadvertently trampling on each other

Since paging is controlled by the OS, pages can be marked read-only

Programmers don't need to worry about the actual layout of memory

Programs can load faster because only part of them needs to be put into RAM

Page 16: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Process commands

ps gives a snapshot of the current processes running

top gives repeatedly updated information about the processes running

The kill command lets you end a process You have to have sufficiently high

privileges to do so

Page 17: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Lab 4

Page 18: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Upcoming

Page 19: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Next time…

Arrays

Page 20: Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope

Reminders

Read K&R chapter 5 Keep working on Project 2