Professional Documents
Culture Documents
Lect 2
Lect 2
Overview
Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh Unix tools project description
File Attributes
Stored in the file I-node Files ownership: user and group file permissions: read, write, execute file modification times file type: regular, directory, link, symbolic link, special file
2000 copyright Danielle S. Lahmani
File Permissions
Three types of permissions:
read, process may read contents of file write, process may write contents of file execute, process may execute file
Chmod command
Symbolic access modes
example: chmod +r file
read
no no no no yes yes yes yes
write
no no yes yes no no yes yes
execute
no yes no yes no yes no yes
Directory permissions
Same types and sets of permissions as for files
read: means process may a read a dir (i.e., list files) write: process add/rm files in dir execute: process can search, access files, in dir or subdir
Unix Processes
Definitions: program: collection of bytes and data stored in a file image: computer execution environment process: execution of an image multi-tasking: many processes can execute simultaneously in Unix.
2000 copyright Danielle S. Lahmani
Process Relationships
A process spawns another process using the fork(2) system call. The creating process is the parent process The newly created process is the child process. fork() returns 0 to the child process fork() returns the process_id of the child to the parent process
Example: Program that creates a new process to copy files Reference: M.Bach, "The Unix Operating system", p 11.
main(argc,argv) int(argcl char *argv[]; {/* assumes 2 args, source and target files */ if ( fork() == 0) { /* child process */ execl("cp"."cp",argv[1],argv[2],0); } /* parent process */ wait(int *) 0); printf("copy done\n");
Fork operation
After fork operation shared text
init execs
init execs
Init execs
getty
getty execs
getty
login execs
/bin/sh
2000 copyright Danielle S. Lahmani
Process permissions
real id and one of more real group id set at login. effective uid and effective group id determine process access to read/write/search/execute files or dir. umask() file mode creation mask, used when file or dir created by process
2000 copyright Danielle S. Lahmani
Signals
Signal: mesg a process can send to a process or process group, if it has appropriate permissions. mesg number represented by a symbolic name
Signals (continued)
Example: When a child exists, it send a SIGCHLD signal to its parent. When the parent issues a wait, it tells the system it wants to catch the SIGCHLD signal When a parent does not issue a wait, it ignores the SIGCHLD signal
2000 copyright Danielle S. Lahmani
Inter-process Communication
Related Processes
signals read/write regular files
Interprocess Communication
Unrleated Processes
FIFO (named Pipes) System V IPC msg queues semaphores shared memory sockets (client/server model)
File Descriptors
each process associates a number or handle, called file descriptor, (fd) with each file it has opened. At login, three files associated with terminal
standard input: fd 0, open for reading standard output: fd 1, open for writing standard error: fd 2, open for reading,writing
sleep nice
monitors status of processes terminate a process (by pid) parent process wait for one of its children to terminate makes a command immune to the hangup and terminate signal sleep in seconds run processes at low priority
2000 copyright Danielle S. Lahmani
Security Problems
Permissions on the executable program and directory in which it is contained must be correct, otherwise easily replaced by Trojan Horse. Some systems remove setuid and setgid bits whenever files are modified as a security precaution.
shell scripts: files of UNIX and shell commands executed from a UNIX shell
2000 copyright Danielle S. Lahmani
Redirection of input/ouput
Redirection of output: >, >>
example:$ man ls > info.ls
Complex commands
Multiple commands Command groupings Conditional command execution
Command substitution
A command can be placed with grave accents ` ` to capture the output of command often used with shell variables
Shell Scripts
A shell script is a regular text file that contains shell or UNIX commands Before running it , it must have execute permissions ( see chmod +x filename) Very useful for automating repetitive task and administrative tools and for storing commands for later execution
Here Documents
Shell provides alternative ways of supplying standard input to commands Shell allows in-line input redirection using << called here documents
format command [arg(s)] << arbitrary-delimiter command input : : arbitrary-delimiter arbitrary-delimiter should be a string that does not appear in text
2000 copyright Danielle S. Lahmani
Shell Variables
Shell has several mechanisms for creating variables. A variable is a name Representing a string value
Shell variables can save time and reduce typing errors, variables
Allow you to store and manipulate information two types: local and environmental
local are set by the user of by the shell itself Positional parameters variables are normally set only on a command line
2000 copyright Danielle S. Lahmani
Environmental Variables
NAME $HOME directory $PATH $MAIL $USER $SHELL $TERM MEANING absolute pathname of your home a list of directories to search for absolute pathname to mailbox your user id absolute pathname of login shell type of your terminal
Positional parameters
when a shell procedure is invoked, the shell implicitly creates positional parameters. The name for a positional parameter is a number. Positional parameters are used mainly in scripts.
$0 is the argument in position zero on the command line $1 is the first argument $1.. $9 $n refers to the nth argument on the command line if applicable $# the number of positional parameters, not counting 0 $* the list of all arguments
2000 copyright Danielle S. Lahmani
QUOTING
Quoting restores the literal meaning to characters that are processed specially by the shell. The literal quotes are not passed on to the command Single quotes ( ' ) inhibit wildcard replacement, variable substitution, and
command substitution
Double quotes ( " ) inhibit wildcard replacement only When quotes are nested, only the outer quotes have any effect
2000 copyright Danielle S. Lahmani
BUILT-IN commands
commands that are internal to the shell Faster to execute and more efficient than other commands
Shell does not have to fork to execute the command Trade-off: redirection of input/output not allowed for most of these
Subshells
When a parent shell forks a child to execute a command, the new child shell is sometimes called a subshell. This happens when: a group command is executed ( $(cmd1; cmd2; cmd3) ) a shell script is executed ( $myscript ) a background job is executed ( cmd1&) A shell inherits the parent's environment but not the parent's local variables.