UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core...

27
UNIX/LINUX Shells • Glass & Ables ch. 5 • A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell

Transcript of UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core...

Page 1: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

UNIX/LINUX Shells• Glass & Ables ch. 5

• A picture of the relationship between UNIX shells

CommonCore

Bourne Shell

Korn Shell

CommonCore

C Shell

T Shell

Page 2: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

UNIX/LINUX Shells

• At UMB LINUX, we use bash, the Borne Again shell

• Note "common core" for all shell families

• Figure shows division of core features

• Many important features are there and we'll cover them first in Glass ch. 5

• More bash shell details in Glass ch. 6

Page 3: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

UNIX Processes

• Basic to UNIX is the idea of a process • Each process contains a program in execution (It

might be stopped, but it is in the system anyway).• Each process has own memory space with code,

data, and program stack• Most programs we use daily are written in C and

have "main (int argc, char *argv[])" through which they access the arguments on their command line

• Even if not written in C, they have similar access

Page 4: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

UNIX Shells

• Shells are just programs that provide a user with a command line interface

• Each shell runs in its own process for you as a user and you interact with it via commands

• Typically, have shell running in parent process handling command interface sometimes with a program running under it (e.g, a command) in its own child process

Page 5: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Core Shell Functionality

Shell Functions

Build-in Commands

Scripts

Variables

Redirection Wildcards Pipes

Sequences

Subshells BackgroundProcessing

Command substitution

local Environment Conditional Unconditional

Page 6: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

UNIX Shells

• The shell is a program that is basically an initialization and then a loop processing user commands

• Shell interprets a user command input, does what ever that command requires, and waits for another command

• Shell terminates when user types control-D at the beginning of a new line or enters the shell command to exit, typically “exit” or “logout”.

• UMB standard .login files disable the control-D option• The "logout" command causes this shell and all other

programs running in your UNIX session to “go away”

Page 7: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

UNIX Shells• Non-built-in shell commands are programs

– “ls” or “lpr” or “vi” commands– “myprog”

• How to see this – UNIX “which” commandblade64(5)% which ls find ls program path name/usr/ucb/ls path name to the ls executable

• These are all programs in system directories (or "myprog" which is in your own current dir)

• UNIX shell simply runs the program in a child process, passing its arguments to it via argc/argv and waits for the child to exit before next prompt

Page 8: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

UNIX Shells

• echo and cd are built-in shell commands--instead of running a program, the shell program detects these in the input line from the user and performs the right action itself

blade64(2)% which cd find cd program path namecd: shell built-in command.

• Note that cd needs to be built-in to change the current dir in the shell process

• Doing that action in a program run from the shell would only change the directory for the child process not for the parent shell process itself

Page 9: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Alias• Alias defines a new command name or overrides an

existing command name: blade64(36)% alias dir "pwd;ls -lg"blade64(37)% alias dirdir pwd;ls -lgblade64(38)% dir/home/cheungrtotal 6327drwxr-s --- 2 cheungr others 512 Oct 26 20:57 bin. . .

• To remove an alias:unalias dir

User group Others

Page 10: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Shell Variables• A shell variable or local variable is a name

with a value in the current shell process space% set x=5% set hwdir=~cheungr/cs240-1/hw4

• We access shell variable value via $name % echo cking variables: $x $hwdir cking variables: 5 /home/cheungr/cs240-1/hw4% cd $hwdir

• To delete the definition for a shell variable% unset x

Page 11: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Display Shell Variablesblade64(3)% set_

addsuffix argv ()autologout 60cwd /home/cheungrdirstack /home/cheungrecho_style bsdedit exec_prefix /tools/modules-2.2b1filecomp gid 12group others…

Page 12: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Environment Variables

• An environment variable is a name with a value that gets communicated from shell to programs running under the shell including other shells

• To define an environment variable of your own using the C shell:% setenv y 10% setenv printer lw_office

Page 13: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Environment Variables

• Values are accessed the same way as shell vars:% echo $y $printer

10 lw_office

% lpr -P$printer *.c

• To delete definition for an environment variable% unsetenv y

• There are many preexisting environment variables. See p.213. For bash shell, use the env command

Page 14: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Display Environment Variables

blade64(4)% setenvUSER=cheungrLOGNAME=cheungrHOME=/home/cheungrPATH=/tools/req/bin:/tools/backup/bin.sun4:/etc/operator/bin:/etc:/usr/

etc:/sbin:/usr/sbin:/groups/ulab/bin:/groups/ulab/pcdev/bin:/tools/jdk-1.6.0_03/usr/jdk/jdk1.6.0_03/bin:/home/cheungr/bin:/usr/local/bin:/usr/local/hosts:/opt/SUNWns6:/usr/ucb:/usr/bin:/bin:/usr/local/gnu/bin:/usr/openwin/bin:/usr/dt/bin:/usr/ccs/bin:.

MAIL=/var/mail//cheungrSHELL=/bin/tcsh…

Page 15: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Forking a child process

• Processes can give birth to other processes using the fork() system call (K&R, Chap 8)

• Then, there are both a parent and a child process• Typically, the parent keeps track of the child but

not vice versa• A common thing for a parent to do is just wait

until the child finishes its work and exits by:– returning at level of main ( ) or – executing exit ( )

Page 16: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Forking a child process• Which branch of the “if statement” executes?

#include <stdio.h>#include <unistd.h>int main ( ){ int pid; pid = fork(); if (pid) printf("parent process %d forked child %d\n", getpid(), pid); else printf("child process %d from parent %d\n", getpid(), getppid()); return 0;}

Page 17: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Forking a child process

• Answer: Both!!blade64(5)% fork

child process 29098 from parent 29097

parent process 29097 forked child 29098

blade64(6)%

• One branch executes in the parent process and the other executes in the child process

Page 18: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Forking a child shell process• A child shell inherits parent’s environment

variables and gets a new clean copy of shell variables (Glass, Figure 3.5)

Environment

Local

Parent Shell

Environment

Local

Child Shell

Copied from parent

Clean, initialized

Page 19: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Background Processing

• Launch a command in a background (child shell) process using & at the end of the command line:

blade64(54)% grep ju junk.c/* junk.c*/blade64(55)% grep ju junk.c >junk.g &[1] 19913 PID for child process[1] Done grep ju junk.c > junk.gblade64(56)% cat junk.g/* junk.c*/blade64(57)%

Page 20: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Discussions on hw6

• Memory Allocation Exercise

• 3 functions:void initalloc(void);

char * alloc(int n);

void freef(char *p);

• You are asked to write the freef() function

• The alloctest.c program is provided to test the functions

Page 21: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

initalloc function• Initializes a large free buffer

struct blockl{unsigned int tag:8;unsigned int size :24;struct blockl *nextp;struct blockl *prevp;};

Cursorp = freep = allocbufBlockrp = allocbuf + ALLOCSIZE -TAGSIZE

allocbufALLOCSIZE

TAGSIZE

F F

Struct blockr{ unsigned int tag:8; unsigned int size:24;};

TAGSIZE = sizeof(blockr)MINFREESIZE= sizeof(blockl) + sizeof(blockr)USEDTAG = 0xaaFREETAG = 0x55

Page 22: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Function alloc(n)• Allocate a buffer of allocsize = n (in multiple of 4 ) + 2*TAGSIZE bytes from the free buffer pool

• The tags for both ends of the allocated buffer are of type struct blockr. They are not kept using a linked list (no need for the char * elements)

• Function returns a pointer = holdp + TAGSIZE

holdp

U U

TAGSIZE TAGSIZE

n bytes in multiple of 4

pointer returned by alloc

Page 23: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Allocation AlgorithmI) cursorp->size < allocsize=4*((n-1)/4 +1) +2*TAGSIZE

return null if all free blocks are not big enough

II) cursorp->size <allocsize + MINFREESIZEreturn pointer =holdp + TAGSIZE

III) cursor->size > allocsize + MINFREESIZE return pointer =holdp + TAGSIZE

blockrp = p +holdp->size -TAGSIZEp=cursorp=holdp

allocsizeU U

blockrp =p + allocsize-TAGSIZEcursorp

allocsize

F UF U

new free size

holdp = pblockrp = p-TAGSIZE

Page 24: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Enchaining Free Buffers• Allocated buffers are not chained

• An example of chaining 4 free buffers

nextp =3

prevp =1 prevp =2prevp =3prevp =4

BUF 4 BUF 3 BUF 2 BUF 1

nextp =1nextp =2 nextp =4

Most recent Least recent freep cursorp

Page 25: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Unchaining free buffers

nextp =3

prevp =1 prevp =2prevp =3prevp =4

BUF 4 BUF 3 BUF 2 BUF 1

nextp =1nextp =2 nextp =4

Most recent Least recent

nextp =2

prevp =1 prevp =2prevp =4

BUF 4 BUF 2 BUF 1

nextp =1 nextp =4

freep cursorp

Page 26: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Multiple cases of freef

Case 1:Left block is free

Case 2:Left block is not free

F UF U U U

F UF U F F

Case 3:Both left and right blocks are free

Page 27: UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.

Pseudocode for freef(char *p)p is the same pointer obtained from alloc()

Change tag to FREETAG on blockl

Change tag to FREETAG on blockr

No need to update size on blockl and blockr because alloc() sets the sizes correctly

Check if block on left is also free?

case 1: yes, coalesce free block on left and p

case 2: no, enchain buffer

Check if block on right is also free?

case 3: yes, unchain the block on right and coalesce p with block on right.

return