lecture 2
play

Lecture 2 Log into Linux Does everyone have completed mymath.h, - PowerPoint PPT Presentation

Lecture 2 Log into Linux Does everyone have completed mymath.h, mymath.cpp (defining the sumtwo function) and second.cpp (using the sumtwo function) files? Questions? Homework 1 out, due next Tuesday Tuesday, August 31 CS 375 UNIX


  1. Lecture 2  Log into Linux  Does everyone have completed mymath.h, mymath.cpp (defining the sumtwo function) and second.cpp (using the sumtwo function) files?  Questions?  Homework 1 out, due next Tuesday Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 1

  2. Outline  Libraries  UNIX philosophy  Essential commands  Files and directories Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 2

  3. UNIX Libraries  Linux supports static and shared libraries.  Shared libraries are used by default. Code from shared libraries is loaded at runtime and may be shared by many applications.  Shared libraries are usually located in /lib or /usr/lib and have a .so filename extension.  Static libraries are copied into the executable making them much larger, but may be desired if you want to distribute a program in binary form. Use the -static option with g++ to use. Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 3

  4. UNIX Libraries  Example: Put the sumtwo function into a static library $ g++ -c mymath.cpp # generate mymath.o object file $ ar rv libmymath.a mymath.o # add obj file to lib $ ranlib libmymath.a # not required under Linux $ g++ -o second second.cpp -L. -lmymath  -L. option (required) tells g++ to look in the current directory for libraries. -lmypath tells g++ to open the library name libmypath.a  gcc automatically looks in standard directories for libraries: /usr/lib, /usr/local/lib Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 4

  5. UNIX Libraries  The ar utility is used to create, extract, list, and add modules to libraries. $ ar t /usr/lib/libm.a # List modules in math lib $ ar x libmymath.a mymath.o # Extract obj file  The nm utility can be used to list all of the symbols in a library, object file, or executable. $ nm -C libmymath.a $ nm -C mymath.o $ nm -C second Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 5

  6. UNIX Libraries  The ldd utility will list the shared libraries required by a program: $ ldd /bin/ls linux-gate.so.1 => (0xb774b000) librt.so.1 => /lib/tls/i686/cmov/librt.so.1 ... ... $ ldd second linux-gate.so.1 => (0xb77d7000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 ... libm.so.6 => /lib/tls/i686/cmov/libm.so.6 ... ...  ldconfig is used by the administrator to make shared libraries available to others on the system. Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 6

  7. UNIX Philosophy  Here are a few notes on UNIX programming philosophy:  Write small programs that do one thing well.  Read data from standard input (scanf, cin) and write data to standard output (printf, cout). Such programs are known as filters . Very complex applications can be created combining filters using pipes .  Move core routines into well-documented libraries so that they can be reused. Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 7

  8. Essential Commands COMMAND DESCRIPTION ls Displays directory listing cd directory Change the current (working) directory cd Return to HOME directory pwd Display the current directory mkdir dirname Create a directory file filename Display file type cat textfile Display file contents less textfile Page file contents to display (also more) exit or ^D Leave this session passwd Change your password man command Display man pages on command info command Display info pages on command Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 8

  9. Useful Commands COMMAND DESCRIPTION apropos string Search the whatis (manual page) database for string (same as “man -k string”) whatis command Display brief description from whatis database (same as “man -f command”) command –– help Display brief help on command ps aux Display all processes kill Kill a process (requires ownership) du foo Display disk usage by file or dir foo df Display free disk space grep Search for character strings find Find files meeting criteria tar Standard UNIX archive tool Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 9

  10. File Information  Use “ls -l” to display complete file info: $ ls -l drwxrwxr-x 11 hwang faculty 4096 Jul 25 10:43 qt -rw-r--r-- 1 hwang faculty 6455 Aug 19 14:53 readme.txt lrwxrwxrwx 1 hwang faculty 23 Aug 8 12:59 rt -> a.txt prw-rw-r-- 1 hwang faculty 0 Aug 27 15:18 mypipe  Shown: file type, permissions, # of hard links, owner, group, size, last modification time, and name.  File type is indicated by first character - Regular file d Directory l Soft link p Named pipe c Char device b Block device Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 10

  11. Soft (Symbolic) LInks  Use “ ln -s ” to create soft or symbolic links. A soft link is a pointer to a “real name” for a file. These are similar to Windows shortcuts but are supported by both the CLI and the GUI. $ ln -s target linkname  The source file may be on another filesystem.  You can create soft links to directories.  Deleting the link has no effect on the file.  Deleting the file can leave a soft link to nothing. I.e. a "dangling reference". Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 11

  12. Hard Links  Use ln to create hard links. Creating a hard link creates another “real name” for a file. $ ln target newname  Data is not deleted until all hard links are deleted. Names are aliases, so changes are reflected.  Data must be on the same filesystem as the link.  System call to delete a file is actually “unlink”.  Only the administrator can create hard links to directories (to prevent filesystem corruption due to directory “loops” by unwitting users) . Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 12

  13. File Links name1 DATA name2 File with two hard links name1 DATA name2 File with one hard link and one soft link Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 13

  14. Special File Names  A filename whose first character is a period will not show up in normal directory listing.  Use 'ls -a' to list hidden files  This has NOTHING to do with security.  Every directory automatically includes . and .. which are links to itself and its parent directory. $ cp /etc/passwd . $ cd ../../../datadir Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 14

  15. Device Files  Device files provide access to hardware. There are two types: character and block  They reside in the /dev directory.  They can be created with mknod by the administrator.  Permissions determine access to corresponding hardware.  Character devices provide sequential access (serial ports) while block devices are used for random access (drives) Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 15

  16. Device Files  See the MAKEDEV man page and the devices.txt file in the kernel source archive for a list of devices. See also man pages for hd (hex dump), od (octal dump), random, etc. Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 16

  17. A Few Standard Device Files /dev/hda Master IDE drive on first controller /dev/hdb Slave IDE drive on first controller /dev/hdc Master IDE drive on second controller (often is CDROM/DVD drive) /dev/hdd Slave IDE drive on second controller (often was used for ZIP drive) /dev/cdrom Link to CDROM device /dev/sda First SCSI drive /dev/sdc0 SCSI CD drive /dev/st0 SCSI tape drive /dev/hda1 First primary partition on hda drive (C: drive in Windows - partitioning sda is similar) /dev/hda2 Second primary partition on hda drive /dev/hda5 First logical partition Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 17

  18. A Few Standard Device Files /dev/tty Current terminal device /dev/tty1 First console device /dev/pts/1 First pseudo-terminal (xterm) device /dev/fd/0 File descriptor 0 or standard input (1 is stdout, 2 is stderr) /dev/stdin Alias for /dev/fd/0 (also /dev/stdout, /dev/stderr) /dev/ttyS0 First serial port (COM1) /dev/lp0 First parallel port /dev/audio Sound card i/o /dev/null Null device (bit bucket) /dev/zero Zero device (all zeroes) /dev/random Random number generator /dev/mem Physical memory /dev/psaux PS/2 mouse port Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 18

  19. Cool Device Tricks $ dd if=/dev/cdrom of=cdrom.iso # Create ISO image of CD $ cat /dev/audio > sound.au # Record from microphone $ cat sound.au > /dev/audio # Play sound $ program 2> /dev/null # Redirect program error # messages to bit bucket $ program /dev/stdin # Pass stdin as filename argument $ od -A n -N 8 -t u2 < /dev/random # Generate 4 16-bit unsigned random #s You can create, format, and mount disk images! $ dd if=/dev/zero of=f.img bs=1k count=1440 # Create 1.44MB zero file $ mkfs -t msdos f.img # Create a file system $ mkdir a # Create a mount point $ mount -o loop f.img a # Mount the file system Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 19

  20. Cool Device Tricks // File: random.cpp // Print 10 random 32-bit integers #include <fstream> #include <iostream> using namespace std; int main() { int randnum; ifstream randstr("/dev/random"); for(int count=0; count<10; count++) { randstr.read((char *)&randnum,sizeof(randnum)); cout << count << ": " << randnum << endl; } randstr.close(); return 0; } Tuesday, August 31 CS 375 UNIX System Programming - Lecture 2 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend