Multiple processes can run concurrently
at any point, there is exactly one process that you can interact with through the keyboard (“foreground process”)
remaining processes execute “in the background”
A process can be started in the background:
processName &
The execution of the current foreground process can be paused via ctrl-z
“bg” will then start it executing in the background
“fg” will bring it to the foreground
43 trang |
Chia sẻ: tuanhd28 | Lượt xem: 1867 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Basic Unix, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
CSc 352: Basic UnixSaumya DebrayDept. of Computer ScienceThe University of Arizona, Tucsondebray@cs.arizona.eduReadingChapter 1:Upto “Background Jobs” (page 17)*What is Unix?Unix is an operating systemsits between the hardware and the user/applicationsprovides high-level abstractions (e.g., files) and services (e.g., multiprogramming)Linux:a “Unix-like” operating system: user-level interface very similar to Unixcode base is different from original Unix code*Layers of a Unix system*hardwareshellUnix operating system kernelusersapplicationsshell commandssystem callsThe file systemA file is basically a sequence of bytesCollections of files are grouped into directories ( folders)A directory is itself a filefile system has a hierarchical structure (i.e., like a tree)the root is referred to as “/”*ddccbb/ffee“Everything is a file”In Unix, everything looks like a file:documents stored on diskdirectoriesinter-process communicationnetwork connectionsdevices (printers, graphics cards, interactive terminals, )They are accessed in a uniform way:consistent API (e.g., read, write, open, close, )consistent naming scheme (e.g., /home/debray, /dev/cdrom)*Referring to files: Absolute PathsAn absolute path specifies how to get to a file starting at the file system rootlist the directories on the path from the root (“/”), separated by “/”*ddccbb/ffeeggReferring to files: Absolute PathsAn absolute path specifies how to get to a file starting at the file system rootlist the directories on the path from the root (“/”), separated by “/”*ddccbb/ffeeggabsolute path: /dd/ee/ggReferring to Files: Relative PathsTypically we have a notion of a “current directory”A relative path specifies how to get to a file starting from the current directory‘..’ means “move up one level”‘.’ means current directorylist the directories on the path separated by “/”*ddccbb/ffeeggReferring to files: Relative PathsTypically we have a notion of a “current directory”A relative path specifies how to get to a file starting from the current directory‘..’ means “move up one level”‘.’ means current directorylist the directories on the path separated by “/”*ddccbb/ffeeggExample:ff relative to ee is: ../ffReferring to files: Relative PathsTypically we have a notion of a “current directory”A relative path specifies how to get to a file starting from the current directory‘..’ means “move up one level”‘.’ means current directorylist the directories on the path separated by “/”*ddccbb/ffeeggExample:cc relative to ee is: ../../ccHome directoriesEach user has a “home directory”specified when the account is createdgiven in the file /etc/passwdWhen you log in, your current directory is your home directorycan then start a shell and issue commandsNotational shorthand:one’s own home directory: ~some other user joe’s home directory: ~joe*A shell is just an interpreter for Unix commandsInput and outputData are read from and written to i/o streamsThere are three predefined streams:stdin : “standard input” usually, keyboard inputstdout : “standard output” usually, the screenstderr : “standard error” for error messages (usually, the screen)Other streams can be created using system calls (e.g., to read or write a specific file)*ProcessesPrograms are executed via processesa process is the unit of executionconsists of:the code that is executedthe data this code manipulatesDifferent processes execute concurrentlyeach process has its own address space, stdin, stdout, etc.their execution is managed by the operating systemCommon tasks are carried out using a set of system-provided programs called commandsthe shell is also a system-provided program*Unix CommandsEach command performs [variations of] a single task“options” can be used to modify what a command doesdifferent commands can be “glued together” to perform more complex tasksSyntax:command options argumentsExamples: *CommandOptionsArgumentspwdcd/home/debrayls-a -lls-al/usr/localOptions can (usually) be combined together:these are equivalentUnix CommandsEach command performs [variations of] a single task“options” can be used to modify what a command doesdifferent commands can be “glued together” to perform more complex tasksSyntax:command options argumentsExamples: *CommandOptionsArgumentspwdcd/home/debrayls-a -lls-al/usr/localNot always required: may have default valuesdefaults to current directorytypical defaults: input: stdin output: stdout directory: currentExamples of Unix commands IFiguring out one’s current directory: pwdMoving to another directory: cd targetdirExamples:*cd /move to the root of the file systemcd ~(also: just “cd” by itself)move to one’s home directorycd /usr/local/srcmove to /usr/local/srccd ../..move up two levelsExamples of Unix commands IICommand: ls — lists the contents of a directoryExamples:*lslist the files in the current directorywon’t show files whose names start with ‘.’ls /usr/binlist the files in the directory /usr/binls -lgive a “long format” listing (provides additional info about files)ls -alist all files in the current directory, including those that start with ‘.’ls -al /usr/localgive a “long format” listing of all the files (incl. those starting with ‘.’) in /usr/localCombining commandsThe output of one command can be fed to another command as input.Syntax: command1 | command2Example: *“pipe”lslists the files in a directorymore fooshows the file foo one screenful at a timels | morelists the files in a directory one screenful at a timeHow this works: ls writes its output to its stdout more’s input stream defaults to its stdin the pipe connects ls’s stdout to more’s stdin the piped commands run “in parallel”Finding out about commands IFiguring out which command to use apropos keyword man –k keyword“searches a set of database files containing short descriptions of system commands for keywords”Helpful, but not a panacea:depends on appropriate choice of keywordsmay require trial and errormay return a lot of results to sift throughpipe through more*Finding out about commands IIFiguring out how to use a command man command“displays the on-line manual pages”Provides information about command options, arguments, return values, bugs, etc.*Example: “man ls”*items within square brackets are optionalExample: “man man”*Example: “man man”*we can specify what kind of information we wantSome other useful commandswc [file]word count: counts characters, words, and lines in the inputgrep pattern [file]select lines in the input that match patternhead –n [file]show the first n lines of the inputtail –n [file]show the last n lines of the inputcp file1 file2copy file1 to file2mv file1 file2move file1 to file2*Example: Deleting a fileFiguring out which command to use:apropos deleteproduces many screenfuls of output that go by too quicklyapropos delete | moremany screenfuls of output, but shown one screenful at a timemost of the commands shown aren’t relevant*Example: Deleting a file (1)Idea 1: filter out irrelevant stuff man –k delete | grep file*a lot fewer results;nothing relevantExample: Deleting a file (2) Idea 2: try a different keyword man –k remove | grep file*Example: Deleting a file (3) Idea 2: try a different keyword man –k remove | grep file*these are the only commands that refer to removing filesExample: Deleting a file (4) Idea 2: try a different keyword man –k remove | grep file*this is the only user command that refers to removing filesExample: Deleting a file (5) Confirm that this is the appropriate command: “man rm”*strongly suggest making this your defaultSetting defaults for your commandsCreate an “alias” for your commandsyntax different for different shellsbash: alias aliasName=“cmdName”e.g.: alias rm=“rm –i”see “man alias” for detailsTo have this alias in force whenever you log in, add this line to the file~/.bashrc // assuming your login shell is “bash”To find out your login shell, run the commandecho $0*Pattern matching: grep*Pattern matching: grep (1)*print the current directoryshow the contents of this fileprint out the lines that match “nation”Pattern matching: grep (2)*“print all lines in the input that match the string er”Pattern matching: grep (3)*“print all lines in the input that match the string er or reprint all lines in the input that begin with the string er or rePattern matching in the shellWe can also use patterns in shell commands, e.g.:Example: **matches any string[ ]matches any one of the characters within bracesls b*clist files that begin with b and end with cls a[xyz]*list files starting with a followed by x, y, or zls *.pdflist files ending with “.pdf”I/O RedirectionDefault input/output behavior for commands:stdin: keyboard; stdout: screen; stderr: screenWe can change this using I/O redirection:*cmd fileredirect cmd’s stdout to filecmd >> fileappend cmd’s stdout to filecmd &> fileredirect cmd’s stdout and stderr to filecmd1 | cmd2redirect cmd1’s stdout to cmd2’s stdinGetting more information about filesls –l : provides additional info about files*Getting more information about files (1)*file namelast-modified timesizegroupownerno. of hard linksaccess permissionsfile typenormal fileddirectoryl (ell)symbolic linkFile access permissions*access permissions for owner (u)access permissions for group (g)access permissions for others (o)rreadwwritexexecute (executable file)enter (directory)no permissionChanging file access permissionsCommand:chmod whowhat file1 file2 filenExample:*∈ {r, w, x}∈ {a, u, g, o}chmod u-w foo remove write permission for user on file foochmod g+rx bar give read and execute permission to group for barchmod o-rwx *.docremove all access permissions for “other users” (i.e., not owner or group members) for *.doc fileschmod a+rw p*give read and write permission to everyone for all files starting with pForeground and Background ProcessesMultiple processes can run concurrentlyat any point, there is exactly one process that you can interact with through the keyboard (“foreground process”)remaining processes execute “in the background”A process can be started in the background: processName &The execution of the current foreground process can be paused via ctrl-z“bg” will then start it executing in the background“fg” will bring it to the foreground*
Các file đính kèm theo tài liệu này:
- 01_basic_unix_6968.ppt