Page 418 - DCAP103_Principle of operating system
P. 418

Unit 14: Case Study of Linux Operating System



            the form name = value used to pass information such as the terminal type and home directory   Notes
            name to a program. In Figure 14.7, no environment is passed to the child, so the third parameter
            of execve is a zero in this case.

                                  Figure 14.7: A Highly Simplified Shell

                       while (TRUE) {ty                  /* repeat forever /*/
                           type_prompt( );               /* display prompt on the screen */
                           read_command(command, params);  /* read input line from keyboard */

                           pid = fork( );                /* fork off a child process */
                           if (pid < 0) {                /* error condition */
                               printf(“Unable to fork0);   /* error condition */
                               continue;

                           }

                           if (pid) !=0) {
                               waitpid (–1, &status, 0);   /* parent waits for child */
                           } else {
                               execve(command, params, 0);  /* child does the work */
                           }
                       }


            If exec seems complicated, do not despair; it is the most complex system call. All the rest are
            much simpler. As an example of a simple one, consider exit, which processes should be used
            when  they  are  finished  executing.  It  has  one  parameter,  the  exit  status  (0  to  255),  which  is
            returned to the parent in the variable status of the waitpid system call. The low-order byte of
            status contains the termination status, with 0 being normal termination and the other values
            being various error conditions. The high-order byte contains the child’s exit status (0 to 255),
            as specified in the child’s call to exit. For example, if a parent process executes the statement n
            = waitpid(~1, &status, 0); it will be suspended until some child process terminates. If the child
            exits with, say, 4 as the parameter to exit, the parent will be awakened with n set to the child’s
            PID and status set to 0x0400 (0x as a prefix means hexadecimal in C).
            The low-order byte of status relates to signals; the next one is the value the child returned in
            its call to exit. If a process exits and its parent has not yet waited for it, the process enters a
            kind of suspended animation called the zombie state. When the parent finally waits for it, the
            process terminates.
            Several system calls relate to signals, which are used in a variety of ways. For example, if a user
            accidently tells a text editor to display the entire contents of a very long file, and then realizes
            the error, some way is needed to interrupt the editor. The usual choice is for the user to hit
            some special key (e.g., DEL or CTRLC), which sends a signal to the editor. The editor catches
            the signal and stops the print-out.
            To announce its willingness to catch this (or any other) signal, the process can use the sigaction
            system call. The first parameter is the signal to be caught (see Figure. 14.5). The second is a
            pointer to a structure giving a pointer to the signal handling procedure, as well as some other
            bits and flags. The third one points to a structure where the system returns information about
            signal handling currently in effect, in case it must be restored later. The signal handler may run
            for as long as it wants to. In practice, though, signal handlers are usually fairly short. When the
            signal handling procedure is done, it returns to the point from which it was interrupted. The
            sigaction system call can also be used to cause a signal to be ignored, or to restore the default
            action, which is killing the process. Hitting the DEL key is not the only way to send a signal. The



                                             LOVELY PROFESSIONAL UNIVERSITY                                   411
   413   414   415   416   417   418   419   420   421   422   423