Presentation is loading. Please wait.

Presentation is loading. Please wait.

Foreground and background processes

Similar presentations


Presentation on theme: "Foreground and background processes"— Presentation transcript:

1 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 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: 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. jobs lists background jobs and their process ID kill %N terminates background job #N kill PID terminates background job with process ID PID fg %N brings job N to the foreground

2 Foreground and background processes
Foreground processes can be suspended and brought 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’). Ctrl Z suspends a foreground job Ctrl C terminates foreground job bg takes a suspended job to the background bg %N takes suspended job N to the background fg %N brings job N to the foreground nohup can be used to leave jobs running when you logout ("hangup"). disown

3 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) & ls -l ; ls –a ls –l & ls –& lists files and directories and counts lines as one job in the background lists files and directories consecutively lists files and directories sequentially

4 Shell scripts The values of shell variables can be set with the set command and 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 special variables Use ()s to set a variable to a list of values (e.g. set variable = (value1, value2, …)(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]}) $$ - 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)

5 A very simple shell scripts
#!/bin/tcsh (ls -l | wc) & ls -l ; ls –a set MyLS = `ls` echo Listing is $MyLS `command` means the output of the command

6 Shell scripts #!/bin/tcsh
#----Ask the user for the file extension if none provided if ($#argv < 1) then echo -n "What file type : " set FileType=$< else set FileType = $1 endif echo "Looking through $FileType files" #----Look in each file of that type foreach File (*.$FileType) echo -n "Searching $File ... " #----Count the number of lines containing "int" set IntCount = `grep -c int $File` echo "It has $IntCount lines containing int” end first argument opens file of type () `` means the output of the command

7 A more complicated shell script
#!/bin/tcsh # echo -n "What is the limit on your rabbit population: " @ RabbitLimit = $< set Fibonaccis = (0 1) @ Epochs = 2 while ($Fibonaccis[$Epochs] < $RabbitLimit) @ Epochs++ @ Epochs1 = $Epochs - 1 @ Epochs2 = $Epochs - 2 @ NextFibonacci = $Fibonaccis[$Epochs1] + $Fibonaccis[$Epochs2] set Fibonaccis = ($Fibonaccis $NextFibonacci) 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


Download ppt "Foreground and background processes"

Similar presentations


Ads by Google