KEMBAR78
Unix Shell and System Boot Process | PPTX
THE SHELLSYSTEM BOOT AND THE INIT PROCESSA presentation byArvind Krishnaa J
Topics CoveredThe Shell
Algorithm for the Shell
Redirection of IO
Piping
Asynchronous Execution of commands
System Boot Process
Algorithm for the boot process
Init Process
Algorithm for the init process
Other types of processes
Characteristics of these processes“Try and try until you succeed”Old Wise saying
The ShellShell reads a command line from its standard input.
Interprets it according to a fixed set of rules.
Standard input and output for the login shell are both the terminal.
If the shell recognizes the input as an internal command(like cd, for, while and others)
Executes the command internally without creating  new processesElseAssumes that the command is the name of an         executable file
More about the SHELLSimplest command lines contain a program name and some parameters
such aswhogrep –n include *.cls –lShell forks and creates a child process.It execs the program that the user specified on the command line.The shell waits until the child process exits from the command and loops back to read the next command.
SHELL Algorithm/*read command line until “end of file” */while(read(stdin,buffer,numchars)){/*parse command line */if(/*command line contains & */)amper=1;elseamper=0;/*for commands not part of the shell command language */if(fork()==0){/*redirection of IO? */if( /*redirect output */ ){fd=creat(newfile,fmask);close(stdout);dup(fd);close(fd);/* stdout is now redirected */}
SHELL Algorithmif( /* piping */ ){pipe(fildes);if(fork()==0){/* first component of command line */close(stdout);dup(filedes[1]);close(filedes[1]);close(filedes[0]);/* stdout now goes to pipe *//* child process does command */execlp(command1,command1,0);}/* 2nd command component of command line */close(stdin);dup(filedes[0]);close(filedes[0]);close(filedes[1]);/*standard input now comes from pipe */}
SHELL Algorithmexecve(command2,command2,0);}/* parent continues over here…* waits for child to exit if required*/if(amper==0)retid=wait(&status);}
Continuing the shell algorithmTo run a process asynchronously in the background, nroff –mm bigdocument &Shell sets an internal variable amper when it parses the ampersand characterif amper is set at the end of the loopShell does not execute wait but immediately restarts the loop and reads next command line.
Redirection of IOChild process has access to a copy of the shell command line after the fork.To redirect standard output to a file,nroff –mm bigdocument > outputChild “creats” the output file specified on the command line if creat fails the child would exit immediatelyelse child closes its previous standard output file and dups the file descriptor of the new output file.Standard output file descriptor now refers to the redirected output file.Child process closes the file descriptor obtained from creat to conserve file descriptors for the execed program.“Similar redirection occurs for standard input and standard error files”
PipingConsider the commandls-l|wcAfter parent forks and creates a child process child creates a pipe.
Child then forks another process.
It and its child handle one component of the command line.

Unix Shell and System Boot Process

  • 1.
    THE SHELLSYSTEM BOOTAND THE INIT PROCESSA presentation byArvind Krishnaa J
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
    Algorithm for theboot process
  • 9.
  • 10.
    Algorithm for theinit process
  • 11.
  • 12.
    Characteristics of theseprocesses“Try and try until you succeed”Old Wise saying
  • 13.
    The ShellShell readsa command line from its standard input.
  • 14.
    Interprets it accordingto a fixed set of rules.
  • 15.
    Standard input andoutput for the login shell are both the terminal.
  • 16.
    If the shellrecognizes the input as an internal command(like cd, for, while and others)
  • 17.
    Executes the commandinternally without creating new processesElseAssumes that the command is the name of an executable file
  • 18.
    More about theSHELLSimplest command lines contain a program name and some parameters
  • 19.
    such aswhogrep –ninclude *.cls –lShell forks and creates a child process.It execs the program that the user specified on the command line.The shell waits until the child process exits from the command and loops back to read the next command.
  • 20.
    SHELL Algorithm/*read commandline until “end of file” */while(read(stdin,buffer,numchars)){/*parse command line */if(/*command line contains & */)amper=1;elseamper=0;/*for commands not part of the shell command language */if(fork()==0){/*redirection of IO? */if( /*redirect output */ ){fd=creat(newfile,fmask);close(stdout);dup(fd);close(fd);/* stdout is now redirected */}
  • 21.
    SHELL Algorithmif( /*piping */ ){pipe(fildes);if(fork()==0){/* first component of command line */close(stdout);dup(filedes[1]);close(filedes[1]);close(filedes[0]);/* stdout now goes to pipe *//* child process does command */execlp(command1,command1,0);}/* 2nd command component of command line */close(stdin);dup(filedes[0]);close(filedes[0]);close(filedes[1]);/*standard input now comes from pipe */}
  • 22.
    SHELL Algorithmexecve(command2,command2,0);}/* parentcontinues over here…* waits for child to exit if required*/if(amper==0)retid=wait(&status);}
  • 23.
    Continuing the shellalgorithmTo run a process asynchronously in the background, nroff –mm bigdocument &Shell sets an internal variable amper when it parses the ampersand characterif amper is set at the end of the loopShell does not execute wait but immediately restarts the loop and reads next command line.
  • 24.
    Redirection of IOChildprocess has access to a copy of the shell command line after the fork.To redirect standard output to a file,nroff –mm bigdocument > outputChild “creats” the output file specified on the command line if creat fails the child would exit immediatelyelse child closes its previous standard output file and dups the file descriptor of the new output file.Standard output file descriptor now refers to the redirected output file.Child process closes the file descriptor obtained from creat to conserve file descriptors for the execed program.“Similar redirection occurs for standard input and standard error files”
  • 25.
    PipingConsider the commandls-l|wcAfterparent forks and creates a child process child creates a pipe.
  • 26.
    Child then forksanother process.
  • 27.
    It and itschild handle one component of the command line.
  • 28.
    Grand child processcreated by second fork executes the first command line component(ls).
  • 29.
    It writes tothe pipe, dups it and closes its standard output FD.
  • 30.
    The parent(wc) closesits standard input file and dups the pipe read descriptor causing it to be the standard input file descriptor.
  • 31.
    Closes the originalpipe’s read descriptor
  • 32.
    Executes the secondcomponent of the original command line.Relationship of processes for ls=l|wc
  • 33.
    Piping (contd..)ls-l|wcBoth theprocesses execute asynchronously.
  • 34.
    Output of oneprocess goes as the input of the other process.
  • 35.
    Parent shell (i.ethe actual shell) waits until wc exits then proceeds as usual.
  • 36.
    Entire command linecompletes when wc exits.
  • 37.
    Shell loops andreads the next commandRelationship of processes for ls=l|wc
  • 38.
    SYSTEM BOOT Toinitialize a system from an inactive state, an administrator goes through a “bootstrap” sequence.
  • 39.
    Administrator “boots” thesystem.Goal of the boot process:Get a copy of the operating system into machine memoryStart executing it.It is usually done in a series of stages.Administrator may set switches on the computer console to specify the address of a special hardcoded bootstrap programOr just push a button that instructs the machine to load a bootstrap program from its microcode.
  • 40.
    UNIX SYSTEM BOOTBootstrap procedure eventually reads the boot block (block 0) of a disk and load it into memory.
  • 41.
    Program in theboot block loads the kernel from the file system  from the file /unix
  • 42.
    After the kernelis loaded in memory, the boot process transfers control to the start address of the kernel, and the kernel starts running.start Algorithmalgorithm start/* system startup procedure */input : noneoutput : none{initialize all kernel data structures;pseudo-mount of root;hand-craft environment of process 0;fork process 1;{/* process 1 in here */allocate region;attach region to init address space;grow region to accommodate code about to copy in;copy code from kernel space to init user space to exec init;change mode: return from kernel mode to user mode;/* init never gets here—as a result of above change mode.* init execs /etc/init and becomes a “normal” user process* with respect to invocation of system calls*/}
  • 43.
    start Algorithm/* proc0 continues here */fork kernel process;/* process 0 invokes the swapper to manage the allocation of* process address space to main memory and the swap devices.* This is an infinite loop;process 0 usually sleeps in the * loop unless there is work for it to do*/execute code for swapper algorithm;}
  • 44.
    Explanation of startAlgorithmKernel initializes its internal data structures.
  • 45.
    eg., Constructs linkedlist of free buffers and inodes, constructs hash queues for buffers and inodes, initializes region structures , page table entries and so on.
  • 46.
    Then it mountsthe root file system onto root (“/”) and fashions the environment for process 0
  • 47.
  • 48.
    Initializing slot 0in the process table
  • 49.
    Making root thecurrent directory of process 0Explanation of start Algorithm (contd..)When the environment of process 0 is set up, the system is running as process 0.
  • 50.
    Process 0 forksinvoking the fork algorithm directly from the kernel since it is executing in the kernel mode.Explanation of start Algorithm (contd..)New process, process 1, running in kernel mode, creates its user-level context by allocating a data region and attaching it to its address space.
  • 51.
    Grows the regionto its proper size and copies code from the kernel address space to the new region.
  • 52.
    Sets up thesaved user register context, and “returns” from kernel to user mode , and executes the code it had just copied.
  • 53.
    Process 1 isa user-level process whereas process 0 is a kernel-level process.Explanation of start Algorithm (contd..)The text for process 1 is copied from the kernel consists of
  • 54.
    a call toexec system call
  • 55.
    to execute theprocess /etc/init
  • 56.
    Process 1 callsexec and executes the program in the normal fashion.
  • 57.
    Process 1 iscalled the “init” process, because it is responsible for initialization of new processes.Why is Process 1 in user mode?It could simply invoke an internal version of exec directly from the kernel, right?
  • 58.
    That would becomplicated because
  • 59.
    exec would haveto parse file names in kernel space, not just in user space
  • 60.
    this generality forexec would be used only by the init process would slow down its performance in more common cases.init Algorithmalgorithm init/* init process, process 1 of the system */input : noneoutput : none{fd=open(“/etc/inittab”,O_RDONLY);while(line_read(fd,buffer)){/* read every line of file */if(invoked state!=buffer state)continue; /*loop back to while *//* state matched */if(fork()==0){execl(“process specified in buffer”);exit();}/* init process does not wait *//* loop back to while */}
  • 61.
    init Algorithmwhile((id= wait((int*) 0)) !=-1){/* check here if a spawned child died * consider spawning it *//* Otherwise, just continue */}
  • 62.
    Explanation of initAlgorithmInit process is a process dispatcher, spawning processes that allow users to log in to the system among others.
  • 63.
    init reads thefile “/etc/inittab” for instructions about which processes to spawn.
  • 64.
  • 65.
    an “id”, astate identifier(single-user, multi-user etc.,)
  • 66.
  • 67.
    and a programspecificationExplanation of init Algorithm(contd…)Init reads the file and if the state it was invoked matches the state identifier
  • 68.
    creates a processthat executes the given program specificationsFor example, it may launch the getty(get teletype) processes to monitor the terminal lines configured n a system.When a user logs in, it goes through the login procedure and execs a login shell.Meanwhile, init executes a wait system call, monitoring the death of its child processes and the death of processes “orphaned” by exiting parentsSometimes in case of essential processes init may consider to respawn it.
  • 69.
    Other types ofProcesses and their characteristicsProcesses in UNIX are either user processes, daemon processes or kernel processes.
  • 70.
    User Processes:Most processesare user processes associated with users at a terminal
  • 71.
    Daemon Processes: Notassociated with any users, but do system-wide functions, such as administration and control of networks, executing of time-dependent activities, line printer spooling and so on.
  • 72.
    Init may spawndaemon processes that exist throughout the lifetime of a system, or on occasion users may spawn themKernel Processes:Executes only in kernel mode
  • 73.
    Process 0 spawnskernel processes
  • 74.
    Similar to daemonprocesses but have greater control over their execution priorities.
  • 75.
    Extremely powerful sinceit can access kernel algorithms and data structures directly without the use of system calls.
  • 76.
    Not as flexibleas daemon processes since kernel needs to be recompiled to change them.THANK YOUContact Me:Facebook : www.facebook.com/arvind.krishnaaTwitter: @TheAkjE-Mail: arv100.kri@gmail.com