Week 2 The Crunchy Shell to the Soft

  • Slides: 36
Download presentation
Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010

Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP 4610 / CGS 5765 1

Why is the Shell Important? n Shells provide us with a way to interact

Why is the Shell Important? n Shells provide us with a way to interact with the core system q q q n n Executes programs on our behalf Shows us our stuff No OS should be without one! Can think of a shell as “built around” a component So what are some examples of shells? 2

How do we Crack the Shell? n In other words, how will our shell

How do we Crack the Shell? n In other words, how will our shell interact with the soft and chewy kernel? q q /proc file system System calls 3

What is /proc? n Virtual file system created to pass information via files to

What is /proc? n Virtual file system created to pass information via files to and from the kernel q n Inside /proc directory Often used in kernel debugging or when a developer does not want to create a new system call 4

Why /proc? n You will be reading and displaying a bunch of goodies from

Why /proc? n You will be reading and displaying a bunch of goodies from the kernel through the /proc interface – – – – Date Up Time Idle Time CPU Info Memory Info Kernel Version Terminal Process Details 5

How /proc? n Looking to our good friend bash… $> cat /proc/cpuinfo n (Hint

How /proc? n Looking to our good friend bash… $> cat /proc/cpuinfo n (Hint – your viewproc command should do something similar…) 6

System Calls n n n What are they again? The traditional way to ask

System Calls n n n What are they again? The traditional way to ask the OS to do something on the user’s behalf Some important ones q q Fork() Execv() 7

Shell Basics (Project 1) 8

Shell Basics (Project 1) 8

Inside main() n Continuous loop q q Parse user input Make something happen 9

Inside main() n Continuous loop q q Parse user input Make something happen 9

Inside main() while(1) { } 10

Inside main() while(1) { } 10

Inside main() while(1) { */ Get user input */ } 11

Inside main() while(1) { */ Get user input */ } 11

Inside main() while(1) { */ Get user input */ */ Exit? */ } 12

Inside main() while(1) { */ Get user input */ */ Exit? */ } 12

Inside main() while(1) { */ Get user input */ */ Exit? */ */ Do

Inside main() while(1) { */ Get user input */ */ Exit? */ */ Do something with input */ } 13

Inside main() while(1) { */ Get user input */ */ Exit? */ */ Do

Inside main() while(1) { */ Get user input */ */ Exit? */ */ Do something with input */ */ Reset the shell */ } 14

I/O Streams Standard I/O Stream File descriptor Standard input (stdin) 0 Standard output (stdout)

I/O Streams Standard I/O Stream File descriptor Standard input (stdin) 0 Standard output (stdout) 1 Standard error (stderr) 2 n Examples q q scanf() reads from standard input fscanf() reads from any I/O stream printf() prints to standard output fprintf() prints to any I/O stream 15

Environmental Variables n Gives programs specific information about your environemnt, such as your execution

Environmental Variables n Gives programs specific information about your environemnt, such as your execution paths sarah@trogdor: ~$ echo $PATH /usr/local/bin: /usr/bin: /usr/games n May be set by you or other shell scripts (like. bashrc) sarah@trogdor: ~$ export TEST=hello sarah@trogdor: ~$ echo $TEST hello 16

Environmental Variables char *getenv(const char *name); n n Returns value of an environmental variable

Environmental Variables char *getenv(const char *name); n n Returns value of an environmental variable Returns NULL if not found 17

Environmental Variables n Important examples q q q n $PATH $USER $PWD (Hint: may

Environmental Variables n Important examples q q q n $PATH $USER $PWD (Hint: may want to use these in building the shell prompt) 18

Command Line Parsing n n Standard input (stdin) is the source of input data

Command Line Parsing n n Standard input (stdin) is the source of input data for command line programs Parsing can be done in multiple stages q q q Strip the whitespace Interpret the command Resolve the pathname Variable expansion I/O redirection Final execution 19

Parsing Example ls -l a Too much whitespace! 20

Parsing Example ls -l a Too much whitespace! 20

Parsing Example ls ls –la -l a Parse out the whitespace 21

Parsing Example ls ls –la -l a Parse out the whitespace 21

Parsing Example ls -l a ls –la Resolve the pathname /bin/ls -la 22

Parsing Example ls -l a ls –la Resolve the pathname /bin/ls -la 22

Resolving Pathnames? n You may not just pass ‘ls’ to the execute command q

Resolving Pathnames? n You may not just pass ‘ls’ to the execute command q n What is ‘ls’? You must search all of the users paths stored in the $PATH environmental variable 23

Finding full pathname for ‘ls’ $PATH=/usr/local/bin: /usr/bin: /bin n Does /usr/local/bin/ls exist? q n

Finding full pathname for ‘ls’ $PATH=/usr/local/bin: /usr/bin: /bin n Does /usr/local/bin/ls exist? q n Does /usr/bin/ls exist? q n No No Does /bin/ls exist? q Yes! 24

Processes n Our shell process must continually run q …but we need to execute

Processes n Our shell process must continually run q …but we need to execute other stuff on the user’s behalf n How can we create “children” processes to do our work? n Fork! 25

Fork n n Child pid==0 Parent pid==something else #include <stdio. h> #include <unistd. h>

Fork n n Child pid==0 Parent pid==something else #include <stdio. h> #include <unistd. h> #include <sys/types. h> int main() { pid_t pid; if ((pid = fork()) == 0) { printf(“I am a child with pid %dn”, pid); } else { printf(“I am the parent with pid %dn”, pid); } return 0; } 26

Exec n n Once we have created a child process, we need the child

Exec n n Once we have created a child process, we need the child to execute a command for us Exec has many forms q q q n Execlp Execle Execvp Must use execv() for project 1! 27

Execv() n Takes two arguments q q n Absolute pathname Array of string arguments,

Execv() n Takes two arguments q q n Absolute pathname Array of string arguments, ending with NULL What is an absolute pathname? q q Full execution path starting from root “/” /bin/ls 28

Execv() char *command = “/bin/ls”; char *argv[] = {“/bin/ls”, “-l”, NULL}; execv(command, argv); n

Execv() char *command = “/bin/ls”; char *argv[] = {“/bin/ls”, “-l”, NULL}; execv(command, argv); n Execv() replaces running image of child with a new process! 29

Wait up? n How does our parent process know to wait until the child

Wait up? n How does our parent process know to wait until the child is done? q n waitpid() Performing a wait allows the system to release the resources associated with the child q If child is not waited on, it will become a zombie! 30

Zombie? n n n Process that shows up with a “Z” status or the

Zombie? n n n Process that shows up with a “Z” status or the word <defunct> Child process has terminated, but parent has not waited on it Child process stays allocated on the system until q q Wait() or waitpid() is called by the parent The parent exits, and init “adopts” the zombie processes and performs a wait() 31

waitpid() int waitpid(pid_t pid, int *status, int options); n pid – type of children

waitpid() int waitpid(pid_t pid, int *status, int options); n pid – type of children to wait on q n n For this project, pid==0 to mean wait for any child process created by our parent *status – returns the status of the child process options – return if additional things have happened to the child 32

waitpid() n Comment waitpid() line to see a defunct process for 10 seconds through

waitpid() n Comment waitpid() line to see a defunct process for 10 seconds through ‘ps’ #include <stdio. h> <unistd. h> <sys/types. h> <stdlib. h> int main() { pid_t pid; if ((pid = fork()) == 0) { printf(“I am a child with pid %dn”, pid); } else { printf(“I am the parentn”); waitpid(-1, status, 0); sleep(10); } return 0; } 33

In Summary n Pieces necessary for some of project 1 q q Part 1

In Summary n Pieces necessary for some of project 1 q q Part 1 – Command line parsing Part 2 – Environmental variables and expansion Part 3 – Command execution Part 7 – Built-ins including /proc n q Hint: chdir() may be of some use Part 8 – The prompt via parsing environmental variables and updating $PWD 34

Next Recitation n Part 4 – Input/output redirection Part 5 – Pipes Part 6

Next Recitation n Part 4 – Input/output redirection Part 5 – Pipes Part 6 – Background processing 35

Any Questions? n Time for some of my demos?

Any Questions? n Time for some of my demos?