Foreground and background processes Foreground processes are started
Foreground and background processes • Foreground processes are started by typing a command. While a foreground process is running the shell waits. • Background processes are started by appending a & to the command • While a background process is running the shell continues. • Output from the background process is mixed in. • If a background process needs input, it gets suspended. • Each process you start from the shell is known as a "job", i. e. , you have maximally one foreground job and can have multiple background jobs. • Background processes can be controlled: jobs lists background jobs and their process ID kill %N kill PID terminates background job #N fg %N brings job N to the foreground terminates background job with process ID PID
Foreground and background processes • Foreground processes can be suspended and brought to the background Ctrl Z suspends a foreground job Ctrl C bg bg %N terminates foreground job takes suspended job N to the background fg %N brings job N to the foreground takes a suspended job to the background • A background process will be suspended automatically if it needs to read data from stdin. Some programs, e. g. , perl check stdin when they start, so as background processes they hang. The solution is to redirect stdin to come from /dev/null (‘electronic sink’). nohup disown can be used to leave jobs running when you logout ("hangup").
Sequences and commands • Simple commands are commands • Pipelines of simple commands are commands • Sequences of simple commands with & are commands, and will be executed concurrently. • Sequences of simple commands with ; are commands, and will be executed sequentially. • Commands in ()s are simple commands. (ls -l | wc) & lists files and directories and counts lines as one job in the background ls -l ; ls –a lists files and directories consecutively ls –l & ls –& lists files and directories sequentially
Shell scripts • The values of shell variables can be set with the set command cleared with the unset command (tcsh, csh). In the bash shell the command is export to set a variable. • values of variables are accessed by adding a $ prefix • Variables are also like arrays • • • Use ()s to set a variable to a list of values (e. g. set variable = (value 1, value 2, …)(bash: variable[n]=valuen) $#variable - number of words in variable (bash: ${#variable}) $variable[index] - the indexth word from the variable, counting from 1. (bash: ${variable[n]}) • special variables • • • $$ - the shell's process ID $< - a word read from stdin (the keyboard by default, but more about this later) (bash: use read) $argv - command line arguments to the shell (useful for scripts)
A very simple shell scripts #!/bin/tcsh (ls -l | wc) & ls -l ; ls –a set My. LS = `ls` echo Listing is $My. LS `command` means the output of the command
Shell scripts #!/bin/tcsh #----Ask the user for the file extension if none provided if ($#argv < 1) then echo -n "What file type : " set File. Type=$< else first argument set File. Type = $1 endif echo "Looking through $File. Type files" #----Look in each file of that type foreach File (*. $File. Type) opens file of type () echo -n "Searching $File. . . " #----Count the number of lines containing "int" set Int. Count = `grep -c int $File` `` means the output of the command echo "It has $Int. Count lines containing int” end
A more complicated shell script #!/bin/tcsh #---------------------------------------echo -n "What is the limit on your rabbit population: " @ Rabbit. Limit = $< set Fibonaccis = (0 1) @ Epochs = 2 while ($Fibonaccis[$Epochs] < $Rabbit. Limit) @ Epochs++ @ Epochs 1 = $Epochs - 1 @ Epochs 2 = $Epochs - 2 @ Next. Fibonacci = $Fibonaccis[$Epochs 1] + $Fibonaccis[$Epochs 2] set Fibonaccis = ($Fibonaccis $Next. Fibonacci) end echo "After $Epochs epochs there are $Fibonaccis[$Epochs] rabbits" echo -n "Enter the first epoch of interest: " @ First = $< echo -n "Enter the last epoch of interest: " @ Last = $< echo "Rabbit populations were. . . " echo " $Fibonaccis[$First-$Last]" #--------------------------------------- while loop extending the array
- Slides: 7