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.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Introduction to Linux/UNIX. History UNIX beginnings in 1969 (Linus Torvalds is born!) AT & T Bell Laboratories (Ken Thompson & Dennis Richie) Working.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
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.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Chapter 2: Linux & POSIX “She sells bash shells by the C shore”
Creating and Executing Processes
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
ICS 431 – Operating System. a command-line interpreter. a program that interprets commands and acts as an intermediary between the user and the inner.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
System calls for Process management
Operating Systems Process Creation
Customizing the Shell Environment. UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive:
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Process Management Azzam Mourad COEN 346.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
System calls for Process management Process creation, termination, waiting.
Agenda Managing Processes (Jobs) Command Grouping Running jobs in background (bg) Bringing jobs to foreground (fg), Background job status (jobs) Suspending.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Implementation of a simple shell, xssh
Introduction to Shells
Implementation of a simple shell, xssh (Section 1 version)
Section 8: Processes What is a process Creating processes Fork-Exec
5.13 Recursion Recursive functions Functions that call themselves
Avani R.Vasant V.V.P. Engineering College
Implementation of a simple shell, xssh
Processes in Unix, Linux, and Windows
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Programming Assignment 1
Processes in Unix, Linux, and Windows
CS 240 – Lecture 21 Alloc Implementation.
Introduction to Linux/UNIX
Tutorial: The Programming Interface
Shell Programming.
Discussions on hw7 Memory Allocation Exercise 3 functions:
Linux Shell Script Programming
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Chapter 3 The UNIX Shells
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

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

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

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

Core Shell Functionality Shell Functions Build-in Commands Scripts Variables RedirectionWildcardsPipes Sequences SubshellsBackground Processing Command substitution localEnvironmentConditionalUnconditional

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”

UNIX Shells Non-built-in shell commands are programs –“ls” or “lpr” or “vi” commands –“myprog” How to see this – UNIX “which” command blade64(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

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 name cd: 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

Alias Alias defines a new command name or overrides an existing command name: blade64(36)% alias dir "pwd;ls -lg" blade64(37)% alias dir dir pwd;ls -lg blade64(38)% dir /home/cheungr total 6327 drwxr-s cheungr others 512 Oct 26 20:57 bin... To remove an alias: unalias dir User group Others

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

Display Shell Variables blade64(3)% set _ addsuffix argv () autologout 60 cwd /home/cheungr dirstack /home/cheungr echo_style bsd edit exec_prefix /tools/modules-2.2b1 filecomp gid 12 group others …

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

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

Display Environment Variables blade64(4)% setenv USER=cheungr LOGNAME=cheungr HOME=/home/cheungr PATH=/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 _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:/us r/openwin/bin:/usr/dt/bin:/usr/ccs/bin:. MAIL=/var/mail//cheungr SHELL=/bin/tcsh …

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

Forking a child process Which branch of the “if statement” executes? #include 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; }

Forking a child process Answer: Both!! blade64(5)% fork child process from parent parent process forked child blade64(6)% One branch executes in the parent process and the other executes in the child process

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

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]  PID for child process [1] Done grep ju junk.c > junk.g blade64(56)% cat junk.g /* junk.c*/ blade64(57)%

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

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 = allocbuf Blockrp = allocbuf + ALLOCSIZE -TAGSIZE allocbuf ALLOCSIZE TAGSIZE FF Struct blockr{ unsigned int tag:8; unsigned int size:24;}; TAGSIZE = sizeof(blockr) MINFREESIZE= sizeof(blockl) + sizeof(blockr) USEDTAG = 0xaa FREETAG = 0x55

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 UU TAGSIZE n bytes in multiple of 4 pointer returned by alloc

Allocation Algorithm I) cursorp->size < allocsize=4*((n-1)/4 +1) +2*TAGSIZE return null if all free blocks are not big enough II) cursorp->size <allocsize + MINFREESIZE return pointer =holdp + TAGSIZE III) cursor->size > allocsize + MINFREESIZE return pointer =holdp + TAGSIZE blockrp = p +holdp->size -TAGSIZEp=cursorp=holdp allocsize UU blockrp =p + allocsize-TAGSIZE cursorp allocsize FU FU new free size holdp = pblockrp = p-TAGSIZE

Enchaining Free Buffers Allocated buffers are not chained An example of chaining 4 free buffers nextp =3 prevp =1 prevp =2prevp =3prevp =4 BUF 4BUF 3BUF 2BUF 1 nextp =1nextp =2nextp =4 Most recent Least recentfreepcursorp

Unchaining free buffers nextp =3 prevp =1 prevp =2prevp =3prevp =4 BUF 4BUF 3BUF 2BUF 1 nextp =1nextp =2nextp =4 Most recentLeast recent nextp =2 prevp =1 prevp =2prevp =4 BUF 4BUF 2BUF 1 nextp =1nextp =4 freepcursorp

Multiple cases of freef Case 1: Left block is free Case 2: Left block is not free FUFUUU F UFUFF Case 3: Both left and right blocks are free

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