+ All Categories
Home > Documents > Homework / Exam

Homework / Exam

Date post: 22-Jan-2016
Category:
Upload: draco
View: 59 times
Download: 0 times
Share this document with a friend
Description:
Homework / Exam. HW7 is due next class Starting Glass chapter 4 and parts of 7 Exam 3 – Class 26 Open Book / Open Notes Up through End of K&R Chapter 7 and Appendix B Standard Library Plus UNIX Shells / Processes / Shell Scripts. Remaining Course Schedule. Exam #3 is class 26 - PowerPoint PPT Presentation
Popular Tags:
23
1 Homework / Exam • HW7 is due next class • Starting Glass chapter 4 and parts of 7 • Exam 3 – Class 26 – Open Book / Open Notes – Up through End of K&R Chapter 7 – and Appendix B Standard Library – Plus UNIX Shells / Processes / Shell Scripts
Transcript
Page 1: Homework / Exam

1

Homework / Exam

• HW7 is due next class

• Starting Glass chapter 4 and parts of 7

• Exam 3 – Class 26– Open Book / Open Notes– Up through End of K&R Chapter 7– and Appendix B Standard Library– Plus UNIX Shells / Processes / Shell Scripts

Page 2: Homework / Exam

2

Remaining Course Schedule

• Exam #3 is class 26• HW8 is the last one - Due class 28• Last Week of Classes - Mostly Review

– Bring your questions to class– Email me any specific questions that you want

me to be prepared to go over for you

• Final Exam for CS240- See posted schedule and location

Page 3: Homework / Exam

3

UNIX Shells

• A picture of the relationship between UNIX shells

CommonCore

Bourne Shell

Korn Shell

CommonCore

C Shell

T Shell

Page 4: Homework / Exam

4

UNIX Shells

• At UMB, we use tcsh, the “T shell” which is based on the C shell

• Note "common core" for both shell families

• Figure shows division of core features

• Many important features are there and we'll cover them first, Glass Ch 4

• Then we'll go on to the C shell, Glass Ch 7

Page 5: Homework / Exam

5

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 6: Homework / Exam

6

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 7: Homework / Exam

7

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 8: Homework / Exam

8

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 itselfblade64(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: Homework / Exam

9

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 10: Homework / Exam

10

Hidden Files

• There are hidden files on your home directory – not normally displayed by ls

• Names of these files begin with ‘.’, e.g. .login• You can see them if you use ls with –A option

blade64(3)% ls -A.cshrc .plan cs240.f02 mbox private_stuff.login cs105 cs240.old playpen public_html.msgsrc cs240 cs241 playpen2 student

• .cshrc is a script file executed when shell is started• .login file is a script file executed at time of login

Page 11: Homework / Exam

11

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/bobwtotal 20drwxr-s--- 8 bobw faculty 512 Nov 19 20:57 cs105. . .

• To remove an alias:unalias dir

Page 12: Homework / Exam

12

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=~bobw/cs240/hw4

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

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

Page 13: Homework / Exam

13

Display Shell Variables

blade64(3)% set_ cat .cshrc

addsuffix argv ()autologout 60cwd /home/bobwdirstack /home/bobwecho_style bsdedit exec_prefix /tools/modules-2.2b1filecomp gid 12group faculty

Page 14: Homework / Exam

14

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 (NOTE: NO = sign):% setenv y 10% setenv printer lw_office

Page 15: Homework / Exam

15

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

Page 16: Homework / Exam

16

Display Environment Variables

blade64(4)% setenvUSER=bobwLOGNAME=bobwHOME=/home/bobwPATH=/tools/req/bin:/groups/ulab/bin:/groups/ulab/pcdev/

bin:/home/bobw/bin:/usr/local/bin:/usr/local/hosts:/tools/netscape-4.75:/tools/netscape-4.75/java/classes/java40.jar:/tools/xv/bin:/usr/ucb:/usr/bin:/bin:/usr/local/gnu/bin:/usr/openwin/bin:/usr/dt/bin:/usr/ccs/bin:.

MAIL=/var/mail//bobwSHELL=/bin/tcsh

Page 17: Homework / Exam

17

Environment Variables• Accessing environment variables in C programs

• To set an environment variable:#include <stdlib.h>

int return = putenv(“MY_EV=foobar”);

/* if not error, return == 0,

otherwise return != 0 */

• To read an environment variable:#include <stdlib.h>

char *path = getenv(“MY_EV”);

Page 18: Homework / Exam

18

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 19: Homework / Exam

19

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 20: Homework / Exam

20

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 21: Homework / Exam

21

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 22: Homework / Exam

22

Forking a child shell process and program• Fork and execute a new program in childif(fork() == 0) /* child */

{

execvp(argv[1], &argv[1]);

/* call should not return */

printf("%s\n”, “execvp failed”);

}

else /* parent */

wait(NULL);

Page 23: Homework / Exam

23

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)%


Recommended