Kernel Data Structures: User Space
Kernel Data Structures: User Space
1
Unix process genealogy Background Jobs
Process generation
Init process 1
forks init processes
• By default, executing a command in the
shell will wait for it to exit before printing
init init Init
execs execs execs out the next prompt
getty getty getty
• Trailing a command with & allows the shell
execs
and command to run simultaneously
login
execs
$ /bin/sleep 10 &
[1] 3424
/bin/sh $
2
Setuid and Setgid Mechanisms Environment of a Process
• The kernel can set the effective user and • A set of name-value pairs associated with a
group ids of a process to something different process
than the real user and group • Keys and values are strings
– Files executed with a setuid or setgid flag set • Passed to children processes
cause the these values to change
• Cannot be passed back up
• Make it possible to do privileged tasks:
– Change your password • Common examples:
– PATH: Where to search for programs
• Open up a can of worms for security if buggy
– TERM: Terminal type
Shell Variables
• Shells have several mechanisms for creating
Variables (con’t)
variables. A variable is a name representing a
string value. Example: PATH • Syntax varies by shell
– varname=value # sh, ksh
– Shell variables can save time and reduce typing errors
– set varname = value # csh
• Allow you to store and manipulate information
– Eg: ls $DIR > $FILE • To access the value: $varname
• Two types: local and environmental
– local are set by the user or by the shell itself • Turn local variable into environment:
– environmental come from the operating system and are – export varname # sh, ksh
passed to children – setenv varname value # csh
3
Environmental Variables
Inter-process Communication
NAME MEANING
$HOME Absolute pathname of your home directory Ways in which processes communicate:
$PATH A list of directories to search for • Passing arguments, environment
$MAIL Absolute pathname to mailbox • Read/write regular files
$USER Your user id • Exit values
$SHELL Absolute pathname of login shell • Signals
$TERM Type of your terminal • Pipes
$PS1 Prompt
Signals
An Example of Signals
• Signal: A message a process can send to a process
or process group, if it has appropriate permissions. • When a child exists, it sends a SIGCHLD
• Message type represented by a symbolic name signal to its parent.
• For each signal, the receiving process can: • If a parent wants to wait for a child to exit,
– Explicitly ignore signal it tells the system it wants to catch the
– Specify action to be taken upron receipt (signal handler) SIGCHLD signal
– Otherwise, default action takes place (usually process is
killed) • When a parent does not issue a wait, it
• Common signals: ignores the SIGCHLD signal
– SIGKILL, SIGTERM, SIGINT
– SIGSTOP, SIGCONT
– SIGSEGV, SIGBUS
4
Pipes Pipes (2)
• General idea: The input of one program is
the output of the other, and vice versa • Often, only one end of the pipe is used
A B A B
Interprocess Communication
More about Pipes
For Unrelated Processes
• Pipes are often chained together
• FIFO (named pipes)
– Called filters
– A special file that when opened represents pipe
• System V IPC
standard out standard in – message queues p1 p2
A B C – semaphores
– shared memory
• Sockets (client/server model)
5
Pipelines What’s the difference?
Both of these commands send input to command from a
• Output of one program becomes input to file instead of the terminal:
another
– Uses concept of UNIX pipes
• Example: $ who | wc -l $ cat file | command
– counts the number of users logged in
vs.
• Pipelines can be long
$ command < file
6
head tail
• Display the first few lines of a specified file • Displays the last part of a file
• Syntax: tail +|-number [lbc] [f] [filename]
• Syntax: head [-n] [filename...]
or: tail +|-number [l] [rf] [filename]
– -n - number of lines to display, default is 10 – +number - begins copying at distance number from
– filename... - list of filenames to display beginning of file, if number isn’t given, defaults to 10
– -number - begins from end of file
• When more than one filename is specified,
– l,b,c - number is in units of lines/block/characters
the start of each files listing displays – r - print in reverse order (lines only)
==>filename<== – f - if input is not a pipe, do not terminate after end of
file has been copied but loop. This is useful to monitor
a file being written by another process
head –100 /etc/passwd | tail -5 • Copy standard input to standard output and
tail –f /usr/local/httpd/access_log one or more files
– Captures intermediate results from a filter in the
pipeline
output Colon-separated
• Examples root:ZHolHAHZw8As2:0:0:root:/root:/bin/ksh
jas:nJz3ru5a/44Ko:100:100:John Shepherd:/home/jas:/bin/ksh
ls | head –10 | tee first_10 | tail –5 cs1021:iZ3sO90O5eZY6:101:101:COMP1021:/home/cs1021:/bin/bash
cs2041:rX9KwSSPqkLyA:102:102:COMP2041:/home/cs2041:/bin/csh
who | tee user_list | wc cs3311:mLRiCIvmtI9O2:103:103:COMP3311:/home/cs3311:/bin/sh
7
cut: select columns cut examples
cut -f 1 < data
• The cut command prints selected parts of input lines.
– can select columns (assumes tab-separated input) cut -f 1-3 < data
– can select a range of character positions cut -f 1,4 < data
• Some options: cut -f 4- < data
_ -f listOfCols: print only the specified columns (tab- cut -d'|' -f 1-3 < data
separated) on output
_ -c listOfPos: print only chars in the specified positions cut -c 1-4 < data
_ -d c: use character c as the column separator Unfortunately, there's no way to refer to "last
• Lists are specified as ranges (e.g. 1-5) or comma- column" without counting the columns.
separated (e.g. 2,4,5).
8
sort: Specifying fields sort Examples
• Delimiter : -td
sort +2nr < data
• Old way: sort –k2nr data
– +f[.c][options] [-f[.c][options]
• +2.1 –3 +0 –2 +3n sort -t: +4 /etc/passwd
– Exclusive sort -o mydata mydata
– Start from 0 (unlike cut, which starts at 1)
• New way:
– -k f[.c][options][,f[.c][options]]
• -k2.1 –k0,1 –k3n
– Inclusive
– Start from 1
9
tr (continued) tr uses
• tr reads from standard input.
– Any character that does not match a character in string1 • Change delimiter
is passed to standard output unchanged tr ‘|’ ‘:’
– Any character that does match a character in string1 is • Rewrite numbers
translated into the corresponding character in string2
and then passed to standard output tr ,. .,
• Examples • Import DOS files
– tr s z replaces all instances of s with z tr –d ’\r’ < dos_file
– tr so zx replaces all instances of s with z and o • Find printable ASCII in a binary file
with x
tr –cd ’\na-zA-Z0-9 ’ < binary_file
– tr a-z A-Z replaces all lower case characters with
upper case characters
– tr –d a-c deletes all a-c characters
Next Time
• Regular Expressions
– Allow you to search for text in files
– grep command
• We will soon learn how to write scripts that
use these utilities in interesting ways.
10