Download presentation
Presentation is loading. Please wait.
Published byMorgan Barrett Modified over 8 years ago
2
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Contents Why an Operating System (OS) How a Program Runs on a Computer Running Multiple Programs Key Concepts UNIX Architecture: The Kernel UNIX Architecture: The Shell UNIX Fragmentation Why UNIX Commands Are Noninteractive Structure of a Command Types of Commands How the Shell Determines the Command to Run The PATH Flexibility of Command Usage Command Classification: A Different Approach Using manUsing man Understanding a man Page The UNIX File File Types The Hierarchical Structure of the File System Pathnames: Two Types Absolute Pathname Relative Pathname The Home Directory File Attributes Stored in Inode File Permissions Directory Permissions How a Directory Influences File Permissions An Ownership-Permissions Problem The User Mask File Systems The Inode Revisited Making a Program Behave Differently Links or Hard Links Advantages of Hard Links Limitations of Hard Links Symbolic Links or Symlinks Understanding Ownership and Group Ownership Ownership in SVR4 and BSD The Shell The Shell Metacharacters Examples of Shell Behavior Wild Cards Wild-card Set Escaping (Using a \ before a character) Quoting Single Quotes or Double Quotes? How the Shell Handles Files Redirection Standard Output Standard Input Standard Error Possible Destinations of Standard Output Pipes Command Substitution Shell Variables How to Use Shell Variables The Process The Process Cycle Process Attributes Not Inherited by Child Process Attributes Inherited by Child How a Variable is Transformed to an Environment Variable When the Child Dies Before the Parent When the Parent Dies Before the Child Signals Running a Job in Background Job Control When a Process is Created and When It Is Not
3
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Why an Operating System (OS) OS interacts with hardware and manages programs. Programs not expected to know which hardware they will run on. Must be possible to change hardware without changing the programs. Programs can’t manage themselves. OS provides a safe environment for programs to run.
4
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. How a Program Runs on a Computer OS loads program from disk and allocates memory and CPU. Instructions in program are run on CPU and OS keeps track of last instruction executed. If program needs to access the hardware, OS does the job on its behalf. OS saves the state of the program if program has to leave CPU temporarily. OS cleans up memory and registers after process has completed execution.
5
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Running Multiple Programs Multiprogramming: Multiple programs can be in memory. Multiuser: Multiple users can run programs. Multitasking: One user can run multiple programs.
6
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Key Concepts Everything in the system is represented as a file. Work gets done by processes. Workload shared by two separate programs (kernel and shell). Kernel uses system calls to do most of the work. All UNIX systems use the same system calls.
7
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX Architecture: The Kernel Program always resides in memory. Has direct access to the hardware. Handles file I/O. Manages processes. Only one copy shared by all users.
8
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX Architecture: The Shell A program or command invoked only when the user logs in. Accepts user input, examines and rebuilds the command line. Makes calls to the kernel for all other functions. At least one shell is invoked by every user. User has a choice of shells.
9
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX Fragmentation System V from AT&T (SVR4) BSD UNIX from Berkeley Linux with help from GNU
10
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Why UNIX Commands Are Noninteractive Command may take input from the output of another command. May be scheduled to run at specific times. User input can also be provided through command line arguments. Command arguments need not be known in advance. Allows designing of applications that determine their own behavior by reading configuration files.
11
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Structure of a Command e.g. ls -l -u -t chap01 Command filenames need no specific extensions. A command’s behavior is determined by its arguments and options. Command and arguments must be separated by whitespace. Generally possible to combine multiple options into a single one (like ls -l -u -t == ls -lut) Order of combining is generally not important (like ls -lut == ls -utl)
12
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Types of Commands External program on disk which could be a binary executable (written in C, C++). a script file (like a shell or perl script). Internal command of the shell which could be a builtin (like cd, pwd, etc.) an alias defined by the user that invokes the disk or internal version in a specific manner.
13
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. How the Shell Determines the Command to Run If command is invoked with a pathname (like /bin/echo), the shell runs program at the specified location. If command is invoked without a pathname, the shell first checks whether it is an alias or builtin: If alias or builtin, the shell runs it without looking in disk. If not, the shell looks at the PATH variable for directories where the command may reside.
14
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The PATH A shell variable (or environment variable) that specifies a list of directories to search. Shell looks at PATH only when command is not used with a pathname and is also not a shell builtin. Command can still be executed if not in PATH by Using a pathname. Modifying PATH to include the directory containing the command. PATH can be modified in an absolute or relative manner: PATH=/usr/bin:. (Absolute) PATH=$PATH:/usr/local/bin (Relative) Modified setting is lost after user has logged out unless saved in a startup file.
15
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Flexibility of Command Usage Run multiple commands by specifying them in the same line: date ; echo $PATH Split a command into multiple lines: $ echo “Hello > Dolly” Save command output in a file: date > foo Use output of one command as input of another: date | cut -d” “ -f2 Run a command in the background with &: ls -lRa / > $HOME/ls-lRa.txt &
16
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Command Classification: A Different Approach Utilities that are generally used in standalone mode (vi, stty, bc). Commands that do useful work but produce no output (mkdir, cp, rm). Commands that produce output which may need further processing (date, who, ls). Commands specially designed to accept output of other commands as their input and vice versa (grep, sort, head).
17
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Using man Displays documentation of commands, configuration files, system calls and library functions. Organized in a number of sections. Commands are found in Section 1. May need to use section number when entry exists in multiple sections (e.g. man passwd and man -s 5 passwd). man documentation not available for most internal commands of the shell. Use man man first to know how man should be used.
18
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Understanding a man Page Example: wc Syntax/Synopsis wc [ -c | -m | -C ] [ -lw ] [ file... ] Most useful information available in SYNOPSIS and DESCRIPTION. When options grouped in [ ] without a |, one or more of them can be used. (-l, -w and -lw are valid.) The | signifies an OR condition. (Only one of -c, -m or -C can be used.) The... means that multiple occurrences of the preceding item are possible. (wc can be used with multiple files.) EXIT STATUS indicates values returned on error.
19
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The UNIX File Filename limited to 255 characters. Can’t contain / or NULL character. Filenames are case-sensitive; chap and Chap are two different filenames. Group of filenames held together in a directory. Directory contains name of the file. Both files and directories are subject to access control.
20
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. File Types Ordinary or regular file: Contains data. This file can be a text file (program sources, configuration files). binary file (executables, graphic and multimedia files). Directory: Contains the filename and a number (inode number). Device file: Contains no data whatsoever. Symbolic link: Contains the location of another file.
21
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The Hierarchical Structure of the File System A single hierarchical structure that contains all files. Top signified by root (/). Existence of a parent-child relationship. Parent of any file must be a directory. Files accessed with pathnames (like /etc/passwd).
22
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Pathnames: Two Types Absolute pathname: Specifies location with reference to the file system top (like cat /etc/passwd). Relative pathname: Specifies location with reference to the user’s current location (like cd../include). Both commands and filename arguments can be represented in either form.
23
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Absolute Pathname Begins with a / (like /etc/passwd). First / signifies the root directory. System configuration files that normally don’t change location should be addressed in absolute manner. Used with a command that doesn’t feature in PATH. resides in two or more directories of PATH.
24
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Relative Pathname Uses. to signify the current directory. Uses.. to signify the parent directory. Used to refer to files that are either impossible or inconvenient to access in an absolute manner. Has a synonym for a filename argument that doesn’t have a /. (cat foo is the same as cat./foo.) Same synonym doesn’t automatically exist for commands. (cat foo MAY NOT be the same as./cat foo.)
25
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The Home Directory Directory where user is placed on login. Determined by sixth field in /etc/passwd: romeo:x:500:100:romeo vincent:/home/romeo:/bin/bash Can also be referred to by the shell variable $HOME (e.g. cat $HOME/foo). tilde expansion in most shells: ~ (e.g. cat ~/foo). cd command used without arguments returns user to home directory. User can create and remove files in their home directory but not in other directories.
26
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. File Attributes Stored in Inode Type: Whether ordinary, directory, device, etc. Permissions: Determines who can read, write or execute a file. Links: Number of names a file can have. A program can be designed to behave differently depending on the name by which it is invoked. Owner: A file is owned by a user, by default its creator. The owner can change many file attributes and set the permissions. Group Owner: The group which owns the file. The owner by default belongs to this group. File Size: Number of bytes of data contained. File Time Stamps: Date and time of last modification Date and time of last access
27
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. File Permissions A file has three types of permissions (read, write and execute). Available to three categories of users (user, group and others). Only file owner or superuser can change file permissions. Permissions can be assigned or removed in relative manner (e.g. chmod +x foo.sh). absolute manner (e.g. chmod 744 foo.sh). Significance of permissions different for file and directory.
28
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Directory Permissions Read permission Whether filenames in directory can be listed by a program (like ls). Write permission Whether files and directories can be created in the directory. Execute or search permission Whether one can pass through directory to search for filenames. Desirable permission setting: 755
29
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. How a Directory Influences File Permissions Examining only the user category File Directory Significance r--r--r-- rwxr-xr-x A write-protected file; can’t be modified but can be removed. rw-r--r-- r-xr-xr-x A write-protected directory; file can’t be removed but can be modified. r--r--r-- r-xr-xr-x A write-protected file and directory; file can’t be modified or removed. rw-r--r-- rwxr-xr-x Normal setting; file can be modified and removed. rw-r--r-- rw-r-xr-x File can’t be removed even though directory is writable. (An unusual setting) How a Directory Influences File Permissions Examining only the user category File Directory Significance r--r--r-- rwxr-xr-x A write-protected file; can’t be modified but can be removed. rw-r--r-- r-xr-xr-x A write-protected directory; file can’t be removed but can be modified. r--r--r-- r-xr-xr-x A write-protected file and directory; file can’t be modified or removed. rw-r--r-- rwxr-xr-x Normal setting; file can be modified and removed. rw-r--r-- rw-r-xr-x File can’t be removed even though directory is writable. (An unusual setting)
30
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. An Ownership-Permissions Problem Assumption: romeo and juliet belong to the users group. Ownership and Permissions of File foo and its Directory $ who am i romeo $ ls -l foo -r-x-w-r-x 1 julietusers 7017 2004-11-14 13:53 foo $ ls -ld. drwxr-xr-x 21 romeousers 8192 2004-11-28 11:40. Note: foo is owned by juliet but directory is owned by romeo. juliet: can’t edit foo without changing the permissions. can change permissions (as owner) and then edit foo. can’t delete foo (directory write-protected for group). romeo: can edit or delete foo. can’t change permissions of foo. can’t display or copy foo.
31
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The User Mask Reassigns default file and directory permissions. Default permissions before applying mask are completely insecure: 666 for files 777 for directories Systemwide default changed by umask (a shell builtin). umask statement placed in a startup script (typically, /etc/profile).
32
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. File Systems System of organizing files into multiple manageable units. Each file system has a separate directory structure with at top. For a file to be visible, its file system must be attached to the main file system. Two files in two file systems may have the same inode number. Not easy to understand whether a directory structure comprises multiple file systems.
33
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The Inode Revisited System of organizing file attributes separately from content. Identified by inode number but inode doesn’t contain this number. Inode number displayed by ls -i. Both inode and directory entries are looked up by inode number. Possible to consume all inodes even when there is adequate disk space.
34
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Making a Program Behave Differently Using arguments and options. Manipulating a configuration file read by a program on startup. Using different names for the same file.
35
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Links or Hard Links Mechanism by which a file is allowed to have multiple names. Linked filenames share inode but have separate directory entries. Each link increments link count in inode by 1 and adds an entry to the directory. File considered to be deleted and inode freed only when link count drops to 0. Linked filenames equivalent in all respects.
36
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Advantages of Hard Links Backup: Prevention from accidental deletion. Allows the same file to be executed as two similar but separate programs. Takes care of old programs that accesses a file whose name or location has changed.
37
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Limitations of Hard Links Can’t link directories. Can’t link across file systems.
38
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Symbolic Links or Symlinks Separate file type and having its own inode. Contains the pathname of another file or directory. Can link across file systems. Link and file pointed to are not equivalent. Pathname may be stored either in inode or in a separate file.
39
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Understanding Ownership and Group Ownership Entry for romeo in /etc/passwd: romeo:x:1003:101::/export/home/romeo:/usr/bin/ksh Entry for romeo’s group (101) in /etc/group: users::101: A file has a UID (name and number). Both UID parameters maintained in /etc/passwd (1st and 3 rd field). A file has a GID (name and number). Both GID parameters maintained in /etc/group (1st and 3rd field). The numeric GID also maintained in /etc/passwd.
40
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Ownership in SVR4 and BSD AttributeSVR4BSD OwnershipCan be transferredOnly by superuser but can be removed. Group Ownership Can be transferredOnly to another group to removed but can be modified.
41
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The Shell Program that constantly runs at terminal after a user has logged in. Interprets command line and makes arrangements for its execution. Generally waits for command to complete execution. Some commands are built into the shell. Killed on logging out.
42
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The Shell Metacharacters Wild-card characters like the *, ?, etc. Redirection characters like >, <, etc. The pipe character, |. Command substitution characters (` `). The $ as a variable prefix.
43
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Examples of Shell Behavior cat chap* Shell expands * to match all filenames in the current directory that begin with chap. date > foo Shell sees the > first, opens the file foo and connects the date output to it. who | sort Shell understands the strings on either side of the | as two separate programs and connects them. ls `cat foo` Shell first runs cat and supplies the output as arguments to ls. echo $HOME Evaluates $HOME as a variable before running echo. Examples of Shell Behavior
44
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Wild Cards Set of metacharacters used in an expression to match multiple but similar filenames. Shell creates list of filenames before allowing command to run. Expansion can be prevented by quoting and escaping. Feature also found in find and shell’s for and case constructs. Filenames must not contain wild-card characters.
45
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Wild-cards Set * Any number of characters including none ls *.lst Lists all files with extension.lst. ? A single character rm ??* Removes all files comprising at least 2 characters. [ch] A single character that is either a c or h cp *.[ch] cprogs Copies all files with.c or.h extension. [!ch] A single character that is not a c or h rm *[!a-zA-Z]* Removes files not containing at least one letter. ls.??* Lists all filenames beginning with a dot and comprising at least two more characters.
46
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Escaping (Using a \ before a character) Reverses usual meaning of metacharacter following it. (rm \* removes a file named *.) Can also protect itself. (echo \\ prints a \.) Protects space and [Enter]. (cd My\ Documents will work.) Inconvenient to use when command line contains too many metacharacters that need to be escaped. Principle also used by commands in their expressions. (grep “\.” foo looks for a dot in foo.)
47
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Quoting Protects most metacharacters from interpretation by the shell. (echo “*” prints a *.) More convenient than escaping when protecting a group of metacharacters. Quoted string understood as a single argument by shell and C programs. (a.out foo “My Documents” has 2 arguments and not 3.) Double quotes and single quotes are not equivalent. (echo “$SHELL” not the same as echo ‘$SHELL’) Quoting doesn’t protect the \; escaping is also required. Quoting
48
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Single Quotes or Double Quotes? Single quotes protect all characters except \. (echo ‘\’ won’t work.) Double quotes protect all characters except the \, $ and ` (echo “$” doesn’t print a $.) Single quotes protect the “. Double quotes protect the ‘. Double quotes permit variable evaluation and command substitution.
49
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. How the Shell Handles Files A file is opened by accessing it by its pathname. Opening returns a file descriptor (an integer). Subsequent read/write operations on the file use the descriptor. Kernel allocates lowest available number as descriptor. First three descriptors (0, 1 and 2) are always allocated.
50
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Redirection Shell feature for manipulating command input and output. Commands designed to write output to a generic file. Commands designed to take input from a generic file when used without a filename as argument. Shell can assign these files to disk files. Commands themselves have no knowledge of the reassignment.
51
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Standard Output Uses file descriptor 1. By default, assigned to the terminal. When command line contains > or >>, standard output file reassigned to a disk file. Can also be used as input to another program. Commands continue to write to descriptor 1 and have no knowledge of this manipulation.
52
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Standard Input Uses file descriptor 0. By default, assigned to the terminal. When command line contains <, standard input file reassigned to a disk file. Can also be obtained from standard output of another program. Commands continue to read descriptor 0 and have no knowledge of this manipulation.
53
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Standard Error Uses file descriptor 2. By default, file is assigned to the terminal. When command line contains 2>, standard error file reassigned to a disk file. But standard error output can’t be used as input to another program. Commands continue to write error messages to descriptor 2 and have no knowledge of this manipulation.
54
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Possible Destinations of Standard Output Allow it to come to the terminal. Redirect it with > and >>. Merge it with standard error using 1>&2. Force output to /dev/null. Force output to /dev/tty. Connect it to standard input of another program.
55
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Pipes Connects standard output on one command to standard input of another. Takes care of flow control; reader and writer work in unison. No temporary file is created. Used to solve text manipulation problems. Commands have no knowledge that they are reading from or writing to pipe.
56
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Command Substitution Allows command arguments to be obtained from standard output of another command. In command1 `command2`, command2 is run first and its standard output used as arguments to command1. Command enclosed by ` ` must write to standard output. Convenient mechanism for running commands whose arguments are known only at runtime. Enabled within double quotes but not in single quotes.
57
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Shell Variables Defined as variable=value. Variables have no type and need no declaration before they are used. Used by UNIX system to store state information. Evaluation enabled in double quotes but not in single quotes. Variable defined in current shell MAY NOT be available in programs run from the shell.
58
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. How to Use Shell Variables Set a variable x to 5 and display the value: x=5 ; echo $x No whitespace on either side of =: x = 5 is illegal; x is interpreted as a command to run. Can be set from command substitution: directory=`pwd` No operator needed for concatenation; curly braces and quotes often useful: z=$x$y or z=${x}${y} z=$file’01' or z=${file}01 Store pathnames that are repeatedly used in a shell script: addressbook=$HOME/data/addressbook
59
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The Process An instance of a program in execution. Identified by a unique PID (Process-id). Created by another process as its child. One process can be parent of multiple children. Can be killed or stopped by sending it a signal.
60
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. The Process Cycle Parent forks a child by first replicating its own process image. Child execs (overwrites) this image with that of another program. While child is running, parent may wait for child to complete execution (foreground execution). continue with its other tasks (background execution). Process terminates and parent picks up exit status of child. Kernel removes entry for dead child from process table.
61
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Process Attributes Not Inherited by Child PID PPID
62
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Process Attributes Inherited by Child Real UID and GID Effective UID and GID Current directory File descriptors umask value Environment variables
63
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. How a Variable is Transformed to an Environment Variable A variable assignment creates a local variable which is not visible in child processes: $ x=5 $ sh Create a child process $ echo $x No value! $ _ When variable is exported, it is converted to an environment variable which is visible in all child processes: $ x=5 ; export x $ sh $ echo $x 5 However, when value is changed in child, changed value is not available in parent: $ x=7 Value in child $ exit $ echo $x 5 Value in parent
64
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. When the Child Dies Before the Parent Child leaves behind exit status in process table. Child turns to zombie state. Parent may pick up exit status; child is now completely dead. may not wait; child continues to remain in zombie state. Zombies can’t be killed; shown as in ps output.
65
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. When the Parent Dies Before the Child Child adopted by init. PPID of child changes to 1. When child dies, init picks up the exit status.
66
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Signals Notification of occurrence of an event. Every signal associated with a default action (disposition). Process may perform the default action. ignore the signal. catch the signal and invoke a signal-handling function. Two signals (SIGSTOP and SIGKILL) can’t be ignored or caught. The keyboard and kill command generate signals.
67
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Running a Job in Background Job is run in background with &. If nohup is also not used, job could be terminated on logout. Job run with nohup and & continues to run even after logging out. init acquires parentage of job whose parent dies before the job. Background jobs can’t be killed by using interrupt key.
68
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. Job Control Job accessed by job number, name or a string embedded in name. Apart from background execution, job control allows suspending a job ([Ctrl-z]). bringing a background job to foreground (fg %1). moving a suspended job to foreground or background (bg %find). Job control scheme also includes a built-in kill command. Built-in kill command also works with a job identifier as argument (kill %find). Job control supported by most shells but not Bourne.
69
second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. When a Process is Created and When It Is Not External command executed in a separate process. Internal shell command executed without creating a process. Shell script executed in separate sub-shell which runs commands in script. Startup shell script (like.profile) executed without creating a process for script. Alias executed without creating a separate process for alias.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.