Instructional-Manuals-Sl-Ii.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Assignment No. 1: Shell programming Write a program to implement an address book with options given below: a) Create address book. b) View address book. c) Insert a record. d) Delete a record. e) Modify a record. f) Exit. The shell is a command programming language that provides an interface to the UNIX operating system. Its features include control-flow primitives, parameter passing, variables and string substitution. Constructs such as while, if then else, case and for are available. Two-way communication is possible between the shell and commands. String-valued parameters, typically file names or flags, may be passed to a command. A return code is set by commands that may be used to determine control-flow, and the standard output from a command may be used as shell input. The shell can modify the environment in which commands run. Input and output can be redirected to files, and processes that communicate through `pipes' can be invoked. Commands are found by searching directories in the file system in a sequence that can be defined by the user. Commands can be read either from the terminal or from a file, which allows command procedures to be stored for later use. 1.5 Syntax:- 1.5.1 To print message To print message on output screen, ‘echo’ statement is used. echo in shell is equivalent to printf in C. Syntax:- echo “ message” Example:- echo “SNJB” 1.5.2 To take input • To take input from console, ‘read’ statement is used. • read in shell is equivalent to scanf in C. • Syntax:- read variable_name • Example:- read num 1.5.3 Conditional Operators • -eq is equal to if [ "$a" -eq "$b" ] • -ne is not equal to if [ "$a" -ne "$b" ] • -gt is greater than if ["$a" -gt "$b" ] • -ge is greater than or equal to if [ "$a" -ge "$b" ] • -lt is less than if [ "$a" -lt "$b" ] • -le is less than or equal to if [ "$a" -le "$b" ] • < is less than (within double parentheses) (( "$a" < "$b" )) • <= is less than or equal to (within double parentheses) (( "$a" <= "$b" )) • > is greater than (within double parentheses) (( "$a" > "$b" )) • >= is greater than or equal to (within double parentheses) (( "$a" >= "$b" )) 1.5.4 if..then..else.. if [ condition ] then statements elif [ condition ] then statements else statements fi Example:- echo "Enter the number“ read no if [ $no –gt 0 ] then echo “Number is +ve“ elif [ $no –lt 0 ] then echo “Number is negative“ else echo “Number is 0“ fi 1.5.5 While statement while [ condition ] do command1 command2 commandN done Example:- c=1 while [ $c -le 5 ] do echo "Welcome $c times" (( c++ )) done 1.5.6 For statement for (( exp1; exp2; exp3 )) do command1 command2 commandN done Example:- for (( c=1; c<=5; c++ )) do echo "Welcome $c times“ done 1.5.7 Switch..case statement Read choice ‘ch’ case ch in 1) statements;; 2) statements;; 3) statements;; esac Example:- echo “Enter number” read num case num in 1) echo “ONE”;; 2) echo “TWO”;; 3) echo “THREE”;; esac Assignment No. 2: Process control system calls: The demonstration of FORK, EXECVE and WAIT system calls along with zombie and orphan states. a. Implement the C program in which main program accepts the integers to be sorted. Main program uses the FORK system call to create a new process called a child process. Parent process sorts the integers using sorting algorithm and waits for child process using WAIT system call to sort the integers using any sorting algorithm. Also demonstrate zombie and orphan states. b. Implement the C program in which main program accepts an integer array. Main program uses the FORK system call to create a new process called a child process. Parent process sorts an integer array and passes the sorted array to child process through the command line arguments of EXECVE system call. The child process uses EXECVE system call to load new program that uses this sorted array for performing the binary search to search the particular item in the array. Theory: What is a System Call? A system call (or system request) is a call to the kernel in order to execute a specific function that controls a device or executes a privileged instruction. The way system calls are handled is up to the processor. Usually, a call to the kernel is due to an interrupt or exception; in the call, there is a request to execute something special. For example, the serial port may be programmed to assert an interrupt when some character has arrived, instead of polling for it. This way, the processor can be used by other processes and service the serial port only when it is required. The internal operation between an interrupt request and its servicing involve several CPU registers and memory segments. Briefly, a device raises an interrupt by asserting an interrupt request line on the Peripheral Interrupt Controller (PIC) which informs the CPU by setting the interrupt request pin. After each instruction, the CPU checks this pin. If it is enabled, it gets the ID from the data bus, which points to the Interrupt Descriptor Table (IDT), where a number of task, interrupt and gate descriptors are stored. The descriptor contains a selector to the Global Descriptor Table (GDT) which contains the base address to a memory segment in which the Interrupt Service Routine (ISR) resides. FORK( ) fork - create a child process Syntax : #include<sys/types.h> #include <unistd.h> pid_t fork(void); fork creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0 The fork() function is used to create a new process by duplicating the existing process from which it is called. The existing process from which this function is called becomes the parent process and the newly created process becomes the child process. As already stated that child is a duplicate copy of the parent but there are some exceptions to it. The child has a unique PID like any other process running in the operating system. The child has a parent process ID which is same as the PID of the process that created it. Resource utilization and CPU time counters are reset to zero in child process. Set of pending signals in child is empty. Child does not inherit any timers from its parent The Return Type Fork() has an interesting behavior while returning to the calling method. If the fork() function is successful then it returns twice. Once it returns in the child process with return value ’0′ and then it returns in the parent process with child’s PID as return value. This behavior is because of the fact that once the fork is called, child process is created and since the child process shares the text segment with parent process and continues execution from the next statement in the same text segment so fork returns twice (once in parent and once in child). ZOMBIE PROCESS 1) A zombie process or defunct process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the parent process to read its child's exit status. The term zombie process derives from the common definition of zombie — an undead person. 2) When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes. However, the process's entry in the process table remains. The parent can read the child's exit status by executing the wait system call, whereupon the zombie is removed. 3) After the zombie is removed, its process identifier (PID) and entry in the process table can then be reused. However, if a parent fails to call wait, the zombie will be left in the process table. In some situations this may be desirable, for example if the parent creates another child process it ensures that it will not be allocated the same PID. Code to create processes using fork() and check zombie state in C programming # include <stdio.h> int main() { int pid; pid=getpid(); printf("Current Process ID is : %d\n",pid); printf("[ Forking Child Process ... ] \n"); pid=fork(); /* This will Create Child Process and Returns Child's PID */ if(pid < 0) { /* Process Creation Failed ... */ exit(-1); } elseif(pid==0) { /* Child Process */ printf("Child Process Started ...\n"); printf("Child Process Completed ...\n"); } else { /* Parent Process */ sleep(10); printf("Parent Process Running ... \n"); printf("I am In Zombie State ...\n"); while(1) { /* Infinite Loop that Keeps the Process Running */ } } return 0; } /* Output [divyen@localhost PP-TW1]$ ./Prog01-Z & [1] 2320 Current Process ID is : 2320 [ Forking Child Process ... ] Child Process Started ... Child Process Completed ... [divyen@localhost PP-TW1]$ ps –l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 500 2193 2192 0 75 0 - 1078 wait4 pts/2 00:00:00 bash 0 S 500 2320 2193 0 75 0 - 336 schedu pts/2 00:00:00 Prog01-Z 1 Z 500 2321 2320 0 75 0 - 0 t> pts/2 00:00:00 Prog01-Z <defunct> 0 R 500 2322 2193 0 81 0 - 788 - pts/2 00:00:00 ps [divyen@localhost PP-TW1]$ Parent Process Running ... I am In Zombie State ... [divyen@localhost PP-TW1]$ ps –l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 500 2193 2192 0 76 0 - 1078 wait4 pts/2 00:00:00 bash 0 R 500 2320 2193 26 80 0 - 336 - pts/2 00:00:04 Prog01-Z 1 Z 500 2321 2320 0 75 0 - 0 t> pts/2 00:00:00 Prog01-Z <defunct> 0 R 500 2323 2193 5 81 0 - 787 - pts/2 00:00:00 ps AN ORPHAN PROCESS: An orphan process is a process that is still executing, but whose parent has died.