Administrivia

• Email: [email protected] CSC209: Software Tools and – Email must include your name. – Please set up your mail program to use plain text, Systems Programming not html. – Email is a formal method of communication: Richard Krueger • Use proper English. • State your question clearly, with enough context. Email : [email protected] • Sign it. Office hours: BA 3234

2

More on email Course Information • Not helpful – I used the makefile you gave us, but my program • Check the course information sheet (handed doesn’t compile. What could be wrong?” out and on the course web page) for – My program gets a seg fault error message, but I – Office hours don’t know why. – Contact information • Much better – Assignment schedule – When I compile my program, I get the following error message. It seems to indicate that there is a problem • The course web page is the official source of with the following lines of code. (cut and paste error announcements. messages and code.) http://www.cs.utoronto.ca/~csc209h/ – The debugger tells me that the seg fault I’m getting is • Make sure you have the prerequisites! on the following line. I don’t see what the problem is with this line. (file included below) 3 4 Assignments Submitting Assignments • A1: Shell programming (Bourne shell) • You will be using CVS to manage and submit • A2: Manipulating files and directories (in C) your assignments. • A3: Processes (in C) • The repositories will be set up this week. • A4: Sockets (in C) • You should start learning how to use it as • The assignments are best done over a couple of soon as possible. weeks, a few hours at a time. • Do not wait until the last minute to try to • All code must work on the CDF servers to commit your assignment for the first time. receive full marks. • This week’s tutorial will cover using CVS. • Don’t wait until the last day! • See web page for tutorial information.

5 6

Plagiarism What is cheating?

• “The work you submit must be your own, • Cheating is done without participation by others. It is an – copying parts or all of another student’s academic offense to hand in anything written assignment – including code from books, web sites, other by someone else without acknowledgement.” courses without attribution • You are not helping your friend when you – getting someone else to do substantial parts of give him or her a copy of your assignment. your assignment • You are hurting your friend when you ask him – giving someone else your solution or her to give you a copy of their assignment. • Cheating is not – helping to find a bug in a friend’s code (be careful) – helping each other understand man pages or 7 example code. 8 The Big Picture (in Java) The Big Picture

Source code file Compile it Object file Source code file Compile it Object file Hw.java hw.c Hw.class hw FODVV+Z^ LQFOXGH VWGLRK! SXEOLFVWDWLFYRLG YRLG PDLQ 6WULQJ>@DUJV ^ PDLQ VRSULQWOQ ´+HOORµ  % javac Hw.java ^ % gcc –o hw hw.c ` SULQWI ´+HOORZRUOGµ  ` `

Run it in VM Process Run it Process

90 % java Hw % hw +Z 9 10

Source Code Files Compiling a program hw.c LQFOXGH VWGLRK! • What is a file? % gcc –o hw hw.c YRLG • A compiler is a program PDLQ ^ • How does the system know translates source code into SULQWI ´+HOORZRUOGµ  where to find hw.c? object (machine) code. ` • Here we are running the compiler at the command line. • What is the meaning of • A shell is a program that can #include ? execute another program. • What does printf really do?

11 12 Shells Object Files/Executables

% gcc –o hw hw.c high address • Typical memory • The % is a shell prompt. stack layout of programs. • Shells – accept commands (programs) as Process control block (PCB) input sp (stack pointer) pc (program counter) – finds the executable ... – interprets the arguments heap – starts executing the command uninit. data • Shells also have some “built-in” init. data commands. text low address

13 14

Running a program Processes

% gcc –o hw hw.c • load a program into memory • A process is an executing instance of a % hw and hand it off to the OS to program. run the program. • The OS keeps track of information about the process. – process ID – a unique non-negative integer – process state – “running”, “ready”, “blocked” – program counter – which instruction is being executed. – a list of open files – etc.

15 16 A different big picture Course Overview

• Software Tools sh less vi perl gcc nedit grep ddd – Understanding the shell libc – C Interface to system services – Shell programming • Systems Programming

Unix system services – C – files Unix kernel (in C) – processes

computer – concurrency – communication

17 18

Self Study Topics Unix History • Inspired by to play Space Travel on • Using CVS - some tutorial coverage his DEC PDP-7 in 1969. • Thompson wrote the first version of Unix in • Using Unix - some tutorial coverage assembler in one month. • and Ken Thompson ported an • Learning an editor – nedit, vi, emacs enhanced version to a PDP-11/20 in 1970. • Learning a debugger – ddd is the • Ritchie and Rudd Canaday ported a cut down version of the BCPL language to Unix, calling it B. easiest • The PDP-11 was purchased for text processing. • Readings • The first user was Bell’s Patent Department. • Pipes and C were added in 1971-73

19 20 More Unix History Why Unix? • BTL Lawyers, “License to universities, but no support.” • This led to extensive sharing. • Available on a number of platforms. • University of Toronto on the first mailing list in 1975. • Multi-user, multi-programmed. • Software Tools User Group formed in 1978. • Shares computer resources sensibly. • Canadian connection! – Bill Reeves, Brian Kernighan, Rob Pike... • Permits manipulation of files, processes, and • Berkeley Software Distribution grew out of collecting and programs. distributing bug fixes. (Led to FreeBSD, NetBSD) • Allows inter-process and inter-machine • Bill Joy started at Berkeley but joined the startup Sun communication. Microsystems in 1982. • Permits access to its operating features. • 1991, Linus Torvalds posts a note describing his experimental OS modeled on minix.

21 22

The Unix Philosophy Files and Directories

• Write programs that do one thing and • “Everything is a file.” do it well. • Unix provides a file interface for all Input/Output. – regular files Try ls –l /dev and look • Write programs to work together. – directories at the permissions string. crw------– devices brw------• Write programs that handle text streams, • video (block) c = character, b = block because that is a universal interface. • keyboard (character) • sound (audio) • network (block) • File interface = open, read, write, close

23 24 File System Hierarchy File System Hierarchy / hard link • Everything starts in the “root” directory whose name is “/” u h usr • A directory is a file that contains directory entries. (Ch 4.3) u1 u2 u3 bin lib

• A directory entry maps a file name to an F inode. csc209h symbolic krueger  • An inode is the data structure that contains links IDOO information about a file, including which disk c4reidka Use df to see all the different disk blocks contain the file data. SXE partitions on CDF.

UHSR 25 26

File Systems and Links Directories and Links Ch. 4.5, GLUHFWRU\ILOH 4.3, 3.2 • One file system per disk partition. 2 . • A file system can be mounted at any point in the 2 .. directory tree of another file system. 14 u 46505 home • An entry in a directory file which specifies an inode is 139412 cdrom a hard link. 201345 lib • There can be several hard links to a file, but hard % ls –l / links cannot cross file systems. drwxr-xr-x 2 root root 4096 Nov 8 17:56 bin/ drwxr-xr-x 2 root root 4096 Aug 10 14:46 cdrom/ • A soft link (symbolic link) is a small file containing the drwxrwsr-x 2 root staff 4096 Feb 8 2002 home/ path name of the linked file or directory. drwxr-xr-x 6 root root 4096 Sep 2 15:26 lib/ lrwx------1 root root 6 Sep 2 15:32 u -> /cdf/u/ • Soft links work across file systems.

27 28 &K Inodes and Directory Entries Stat Inode 12345 Directory Entry size eddie% stat csc209h owner UID, GID File: "csc209h" 12345 afile access time Size: 3584 Allocated Blocks: 8 modified time Filetype: Directory creation time Mode: (0755/drwxr-xr-x) link and block counts Uid: ( 0/ root) Gid: ( 517/ permissions csc209h) direct pointers Device: 0/6 Inode: 1055265 Links: 143 to file blocks Device type: 0/0 Access: Sun Aug 26 15:00:58 2001 single indirect pointer pointers to Modify: Mon Jul 23 09:26:51 2001 double indirect pointer next file Change: Mon Jul 23 09:26:51 2001 “man 2 stat” blocks shows the triple indirect pointer C function 29 30

&K Permissions Example UZ[U[U[ UHLG -DQ DOOH[HF  UUU UHLG -DQ UHDGRQO\ UZUU UHLG -DQ DOOUHDG p r r GU[U[U[ UHLG -DQ GLUUHDG UZ UHLG -DQ RZQHUUHDG se rou the u g o G[[[ UUU UHLG -DQ UHDGRQO\ 1 reid 0 Jan 11 22:23 dir-search GUZ[U[U[ UHLG -DQ GLUUHDG UZ[U[U[ ---x--x--x 1 reid 0 Jan 11 22:23 dir-search/xfile GUZ[[[ UHLG -DQ GLUVHDUFK • File permissions What is the result of the following: – read, write, execute – pretty much what you think $ ls readonly • Directory permissions $ readonly – read – you can run ls on the directory $ ls dir-search Use chmod to – write – you can create and delete files in the change file directory $ dir-search/xfile permssions. – execute – you can “pass through” the directory $ cd dir-search when searching subdirectories. 31 32