Shell - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Shell

Description:

Slides for 2110311 Systems Programming, Department of Computer Engineering, ... prompting shell version 1 * Prompts for the command and its arguments. ... – PowerPoint PPT presentation

Number of Views:162
Avg rating:3.0/5.0
Slides: 22
Provided by: cpEngC
Category:
Tags: argv | prompting | shell

less

Transcript and Presenter's Notes

Title: Shell


1
  • Shell
  • A shell is a user program for interacting with
    the system and execute programs.
  • A shell is a command interpreter
  • It reads a command from standard input and forks
    a child.
  • The child executes the command.
  • The parent waits for the child to complete
    before reading in another command

2
  • Shell other features
  • command line handling (wildcard, history, etc.)
  • shell programming
  • pipeline
  • redirection
  • background process handling
  • signal handling
  • job control

3
The main loop of a shell while( !end_of_input
) Get command Execute command Wait for
command to finish
4
To write a shell, we need to know how to
  • Run a program from another program
  • Create a process
  • Wait for created process to finish

5
  • How a program run another program
  • exec System Call
  • exec executes another program.
  • execl("/usr/bin/ls", "ls", "-l", NULL)

Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
6
The exec System Call / exec1.c shows how a
program runs another program / main() char ar
glist3 arglist0 "ls" arglist1
"-l" arglist2 0 printf(" About to
exec ls -l\n") execvp( "ls" , arglist
) printf(" ls is done. bye\n") / Is it
printed? /
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
7
  • How exec works
  • Program calls execvp(progname, arglist)
  • Kernel loads program from disk into the process
  • Kernel copies arglist into the process
  • Kernel calls main(argc, argv)

Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
8
  • exec copies a new executable code into the
    process image
  • program text, variables, stack, and heap are
    overwritten
  • the new process inherits the environment
  • files that are open before exec are still open
    afterwards
  • exec is like a brain transplant. The original
    program is forgotten after calling exec.

Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
9
  • The exec Family
  • The are many variations of exec system call
  • execl, execlp, execle
  • execv, execvp, execve
  • They differ in whether command line arguments
    are passed as a list or an array, whether a full
    pathname has to be given, whether environment are
    passed or not.
  • All variations of exec finally call execve

Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
10
The exec Family include ltunistd.hgt extern
char environ int execl( const char path,
const char arg, ...) int execlp( const char
file, const char arg, ...) int execle( const
char path, const char arg , ..., char
const envp) int execv( const char path, char
const argv) int execvp( const char file,
char const argv) int execve (const char
filename, char const argv ,
char const envp)
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
11
The exec System Call / another example using
execl / main() pid_t childpid int
status if ((childpid fork()) -1)
perror("Error in the fork") exit(1)
else if (childpid 0) /child code/ if
(execl("/usr/bin/ls", "ls", "-l", NULL) lt 0)
perror("Exec of ls failed") exit(1)
else if (childpid ! wait(status))
/parent code/ perror("signal occurred
before the child exited") exit(0)
Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
12
/ prompting shell version 1
Prompts for the command and its arguments.
Builds the argument vector for the call
to execvp. Uses execvp(), and
never returns. / include
ltstdio.hgt include ltsignal.hgt include
ltstring.hgt define MAXARGS 20
/ cmdline args / define
ARGLEN 100
/ token length / int main() char
arglistMAXARGS1 / an array of
ptrs / int numargs
/ index into array / char
argbufARGLEN / read stuff
here / char makestring()
/ malloc etc /
numargs 0 while ( numargs lt MAXARGS )

printf("Argd? ", numargs)
if ( fgets(argbuf, ARGLEN, stdin)
argbuf ! '\n' )
arglistnumargs makestring(argbuf)
else
if ( numargs gt 0 ) / any args?
/
arglistnumargsNULL / close list /
execute( arglist )
/ do it /
numargs 0 / and reset /

return 0
13
int execute( char arglist ) / use
execvp to do it / execvp(arglist0,
arglist) / do it /
perror("execvp failed") exit(1) char
makestring( char buf ) / trim off newline
and create storage for the string /
char cp, malloc()
bufstrlen(buf)-1 '\0' / trim
newline / cp malloc( strlen(buf)1 )
/ get memory / if ( cp
NULL ) / or die /
fprintf(stderr,"no memory\n")
exit(1) strcpy(cp,
buf) / copy chars /
return cp / return ptr
/
14
  • How a shell uses exec
  • Shell must create a new process for each
    command, using fork()
  • With exec, parent and child processes can
    execute different programs (not only different
    code in the same program)
  • Typically the child process calls exec to
    execute the new program, while the parent process
    continues to execute the original program.

Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
15
/ prompting shell version 2 Solves the
one-shot' problem of version 1 Uses
execvp(), but fork()s first so that the shell
waits around to perform another command
New problem shell catches signals. Run vi,
press c. / include ltstdio.hgt include
ltsignal.hgt define MAXARGS 20
/ cmdline args
/ define ARGLEN 100
/ token length / main() char
arglistMAXARGS1 / an array of
ptrs / int numargs
/ index into array / char
argbufARGLEN / read stuff
here / char makestring()
/ malloc etc /
numargs 0 while ( numargs lt MAXARGS )

printf("Argd? ", numargs)
if ( fgets(argbuf, ARGLEN, stdin)
argbuf ! '\n' )
arglistnumargs makestring(argbuf)
else
if ( numargs gt 0 ) / any args?
/
arglistnumargsNULL / close list /
execute( arglist )
/ do it /
numargs 0 / and reset /

return 0
16
execute( char arglist ) / use fork
and execvp and wait to do it / int
pid,exitstatus / of child
/ pid fork()
/ make new process / switch( pid )
case -1
perror("fork failed")
exit(1) case 0
execvp(arglist0, arglist)
/ do it /
perror("execvp failed")
exit(1) default
while( wait(exitstatus) ! pid )

printf("child exited with status d,d\n",
exitstatusgtgt8,
exitstatus0377)
17
char makestring( char buf ) / trim off
newline and create storage for the string /
char cp, malloc()
bufstrlen(buf)-1 '\0' / trim
newline / cp malloc( strlen(buf)1 )
/ get memory / if ( cp
NULL ) / or die /
fprintf(stderr,"no memory\n")
exit(1) strcpy(cp,
buf) / copy chars /
return cp / return ptr
/
18
  • Internal Commands or Build-in Commands
  • Internal (or build-in) commands are executed by
    the shell itself.
  • External commands are executed by children of
    the shell.
  • A process cannot change the environment of its
    parents.
  • Since cd change the user's environment, it must
    be an internal command.
  • Other internal commands include exit, set, echo,
    history, ...

19
  • system function
  • include ltstdlib.hgt
  • int system(const char string)
  • The system function executes a shell command.
  • system(ls -l)
  • system executes a fork, and the child calls exec
    to execute a shell, and the shell executes the
    given command. For example,
  • execl(/usr/bin/sh, sh, -e, command, NULL)

Slides for 2110311 Systems Programming,
Department of Computer Engineering, Chulalongkorn
University, by Veera Muangsin
20
The Process Environment Global variable environ
points to process environment variables. extern
char environ void main(int argc, char
argv) int i for(i 0 environi
! NULL i) printf("environd s\n", i,
environi) exit(0) This program outputs
the contents of its environment list and exits.
For example, environ34 SHELL/bin/bash
21
getenv getenv gets the value of a specific
environment variable. include ltstdlib.hgt char
getenv(const char name) For example char
shell shell getenv("SHELL") printf(SHELLs
\n", shell) This also prints SHELL/bin/bash
Write a Comment
User Comments (0)
About PowerShow.com