unix shell and system boot process
DESCRIPTION
A presentation on the Unix Shell, System Boot and Init ProcessTRANSCRIPT
THE SHELLSYSTEM BOOT AND THE INIT PROCESS
A presentation byArvind Krishnaa J
Topics Covered
The 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 Shell
Shell 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 processesElse Assumes that the command is the name of an executable file
More about the SHELL
Simplest command lines contain a program name and some parameters such as
whogrep –n include *.cls –l
Shell 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;else
amper=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 Algorithm
execve(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 IO
Child 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”
Piping
Relationship of processes for ls=l|wc
Consider the command
ls-l|wc
• After 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.• Grand child process created by
second fork executes the first command line component(ls).
• It writes to the pipe, dups it and closes its standard output FD.
• The parent(wc) closes its standard input file and dups the pipe read descriptor causing it to be the standard input file descriptor.
• Closes the original pipe’s read descriptor
• Executes the second component of the original command line.
Piping (contd..)
Relationship of processes for ls=l|wc
ls-l|wc
• Both the processes execute asynchronously.
• Output of one process goes as the input of the other process.
• Parent shell (i.e the actual shell) waits until wc exits then proceeds as usual.
• Entire command line completes when wc exits.
• Shell loops and reads the next command
SYSTEM BOOT
To initialize a system from an inactive state, an administrator goes through a “bootstrap” sequence.
Administrator “boots” the system.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.
UNIX SYSTEM BOOT
Bootstrap procedure eventually reads the boot block (block 0) of a disk and load it into memory.
Program in the boot block loads the kernel from the file system from the file /unix
After the kernel is 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*/
}
start Algorithm
/* proc 0 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;}
Explanation of start Algorithm Kernel initializes its internal data structures.
eg., Constructs linked list of free buffers and inodes, constructs hash queues for buffers and inodes, initializes region structures , page table entries and so on.
Then it mounts the root file system onto root (“/”) and fashions the environment for process 0 Creating a new u area Initializing slot 0 in the process table Making root the current directory of process 0
Explanation of start Algorithm (contd..)When the environment of process 0 is set up, the system is running as process 0.
Process 0 forks invoking 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.
Grows the region to its proper size and copies code from the kernel address space to the new region.
Sets up the saved user register context, and “returns” from kernel to user mode , and executes the code it had just copied.
Process 1 is a 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
a call to exec system call to execute the process /etc/init
Process 1 calls exec and executes the program in the normal fashion.
Process 1 is called 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?
That would be complicated because exec would have to parse file names in kernel
space, not just in user space this generality for exec would be used only by the
init process would slow down its performance in more common cases.
init Algorithm
algorithm 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 */
}
init Algorithm
while((id= wait((int *) 0)) !=-1){
/* check here if a spawned child died * consider spawning it *//* Otherwise, just continue */
}
Explanation of init Algorithm Init process is a process dispatcher, spawning processes that allow users to log in to the system among others.
init reads the file “/etc/inittab” for instructions about which processes to spawn.
The above file contains an “id”, a state identifier(single-user, multi-
user etc.,) an “action” and a program specification
Explanation of init Algorithm(contd…) Init reads the file and if the state it was invoked matches the state identifier creates a process that executes the given
program specifications
For 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.
Other types of Processes and their characteristics Processes in UNIX are either user processes, daemon
processes or kernel processes. User Processes: Most processes are user processes
associated with users at a terminal Daemon Processes: Not associated 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.
Init may spawn daemon processes that exist throughout the lifetime of a system, or on occasion users may spawn them
Kernel Processes: Executes only in kernel mode Process 0 spawns kernel processes Similar to daemon processes but have greater
control over their execution priorities. Extremely powerful since it can access kernel
algorithms and data structures directly without the use of system calls.
Not as flexible as daemon processes since kernel needs to be recompiled to change them.
THANK YOU
Contact Me:
Facebook : www.facebook.com/arvind.krishnaaTwitter: @TheAkjE-Mail: [email protected]