Operating Systems, Reciation 5
Download
Report
Transcript Operating Systems, Reciation 5
slide 13 updated April 28th
Operating Systems
Recitation 5, April 21-22, 2002
Shell
User
Linux shell program
• Interactive use
• Session customization
• Programming
Linux kernel
Command interpretation by the shell
user commands
Shell
Shell commands
cd, pwd, kill, exit, etc.
External commands
Unix utility programs
ls, rm, cp, etc.
Unix kernel
Unix system calls
fork, iocntl, read, exec,
open, write, etc.
Other applications
emacs, xv, etc.
csh (C shell) - example 1
• A series of individual commands (shell or other
Linux commands) combined into one executable
file is called a shell script, similar to a batch file
in MS-DOS.
• Many of the programming constructs resemble
those of C.
#!/bin/csh
foreach f (*)
echo $f
end
ls -l
[root@pc ex-ps]# csh ex-ls
csh - example 2
#!/bin/csh
set argc = $#argv
if ($argc < 2) then
echo "usage: ex-mv <extension> <filenames>"
exit(1)
endif
set extension = $argv[1]
set filenames = ($argv[2-$argc])
foreach name ($filenames)
set prefix = $name:r
set newname = $prefix.$extension
mv $name $newname
end
[root@pc ex-ps]# csh ex-mv cpp 1.c 2.c
csh - example 3
#!/bin/csh
set argc = $#argv
if ($argc < 2) then
echo "usage: ex-large <size>
<filenames>"
exit(1)
endif
set size = $argv[1]
set filenames = ($argv[2-$argc])
…
set column = 5
set sum = 0
foreach file ($filenames)
set line = `ls -l $file`
set filesize = $line[$column]
if ($filesize > $size) then
echo $file
endif
@ sum = $sum + $filesize
end
@ n = $argc - 1
if ($n != 0 && $sum != 0) then
@ average = $sum / $n
echo "average size is $average"
endif
Virtual file system
• Kernel software layer that handles system
calls related to a standard Unix file-system.
• Common model representing all supported
file systems: disk-based, network, and
special file-systems that do not manage disk
space at all, such as the process filesystem.
Example
cp
]# cp /floppy/TEST /tmp/test
Virtual file system
Ext2
MS-DOS
/tmp/test
/floppy/TEST
Process file system (/proc)
• Interface, no persistent data.
• Each subdirectory corresponds to an
active process, directory name is PID.
• Additional directories provide global
statistics about the kernel and drivers.
• Access via plain text, computed on
demand.
Example
[root@pc os]# emacs HelloWorld.java &
[1] 2912
[root@pc os]# cd /
[root@pc /]# cd proc
[root@pc /proc]# ls
1 1292 1408 1972 4 8 cmdline
interrupts meminfo
slabinfo 1007 1296 1409 1974 5 845 cpuinfo
… 2912 …
[root@pc /proc]# cd 2912
[root@pc 2912]# ls -l
total 0
-r--r--r-1 root root
0 Apr 20 01:18 cmdline
lrwxrwxrwx
1 root root
0 Apr 20 01:18 cwd -> /home/idrori/os
-r-------1 root root
0 Apr 20 01:18 environ
lrwxrwxrwx
1 root root
0 Apr 20 01:18 exe -> /usr/bin/emacs
dr-x-----2 root root
0 Apr 20 01:18 fd
-r--r--r-1 root root
0 Apr 20 01:18 maps
-rw------1 root root
0 Apr 20 01:18 mem
lrwxrwxrwx
1 root root
0 Apr 20 01:18 root -> /
-r--r--r-1 root root
0 Apr 20 01:18 stat
-r--r--r-1 root root
0 Apr 20 01:18 statm
-r--r--r-1 root root
0 Apr 20 01:18 status
Example
[root@pc 2912]# more cmdline
emacsHelloWorld.java
[root@pc 2912]# more status
Name: emacs
State: S (sleeping)
Pid: 2912
PPid: 2189
TracerPid:
0
Uid: 0
0
0
0
Gid: 0
0
0
0
FDSize: 32
Groups: 0 1 2 3 4 6 10
VmSize: 9000 kB …
VmData:
896 kB
VmStk:
672 kB
VmExe:
992 kB
VmLib:
3576 kB
SigPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 8000000000000000 ...
[root@pc 2912]#
Exercise description
• Write a csh program (ex-ps) that specifies
the running processes in the system, their
user name and running command. The
program should print a line for each
process.
Exercise - notes
• Write a small C program, isnum.c (whose executable is
isnum), that checks if its argument is numeric, and use it to
check for directories in proc that correspond to processes.
• The following table provides the location of necessary
information:
information location
Comment
pid
/proc/*
check for numbered directory names by
calling the executable of isnum.c
uid
/proc/pid/status
grep Uid field
cmdline
/proc/pid/cmdline
username
/etc/passwd
ypmatch $uid passwd.byuid |
gawk 'BEGIN {FS=":|,";} {print $5;}‘
• you can use the –x flag to echo each line of your script (for debugging purposes only)
#!/bin/csh -x
Notes
• When you log in, the system determines which
shell to run by your entry in /etc/passwd
• The default shell on Linux is bash.
• You can change the shell using the command
chsh –s /bin/csh , or by typing the program name
at the command line, exec csh
• Chapter 4.10, pages 77-78, in Toledo’s book.
• Submission: Monday, May 6th.
• Software
Directory: ~username/os02b/ex-ps
Files: ex-ps, isnum.c
Permissions: chmod ugo+rx (to above)
• Hardcopy
name, ID, login, CID
ex-ps, isnum.c
submit in mailbox 281, Nir Neumark, [email protected]