Page 417 - DCAP103_Principle of operating system
P. 417

Principles of Operating Systems



                   Notes
                                                 Figure 14.6: Some System Calls Relating to Processes

                                     System call                     Description

                                     pid = fork( )                   Create a child process identical to the parent
                                     pid = waitpid(pid, & statloc, opts)  Wait for a child to terminate

                                     S = execve(name, argv, envp)    Replace a process’ core image
                                     exit(status)                    Terminate process execution and return status

                                     S = sigaction(sig. & act, & oldact)  Define action to take on signals
                                     S = sigreturn (&context)        Return from a signal

                                     S = sigprocmask(how, &set, &old)  Examine or change the signal mask
                                     S = sigpending (set)            Get the set of blocked signals

                                     s = sigsuspend(sigmask)         Replace  the  signals  mask  and  suspend  the
                                                                     process
                                     s = kill(pid, sig)              Send a signal to a process
                                     residual = alarm(seconds)       Set the alarm clock

                                     s = pause( )                    suspend the caller until the next signal


                                 Waitpid has three parameters. The return code s is ~ 1 If an error has occurred, pid is a process
                                 ID, and residual is the remaining time in the previous alarm. The parameters are what the name
                                 suggests. First one allows the caller to wait for a specific child. If it is ~ 1, any old child (i.e.,
                                 the first child to terminate) will do. The second parameter is the address of a variable that will
                                 be set to the child’s exit status (normal or abnormal termination and exit value). The third one
                                 determines whether the caller blocks or returns if no child is already terminated. In the case of
                                 the shell, the child process must execute the command typed by the user. It does this by using
                                 the exec system call, which causes its entire core image to be replaced by the file named in its
                                 first parameter. A highly simplified shell illustrating the use of fork, waitpid, and exec is shown
                                 in Figure. 14.7. In the most general case, exec has three parameters: the name of the file to be
                                 executed, a pointer to the argument array, and a pointer to the environment array. These will
                                 be  described  shortly.  Various  library  procedures,  including  execl,  execv,  execle,  and  execve,
                                 are provided to allow the parameters to be omitted or specified in various ways. All of these
                                 procedures invoke the same underlying system call. Although the system call is exec, there is
                                 no library procedure with this name; one of the others must be used. Let us consider the case
                                 of a command typed to the shell such as cp file1 file2 used to copy file1 to file2. After the shell
                                 has been forked, the child locates and executes the file cp and passes its information about the
                                 files to be copied. The main program of cp (and many other programs) contains the function
                                 declaration where argc is a count of the number of items on the command line, including the
                                 program name. For the example above, argc is 3. The second parameter, argv, is a pointer to an
                                 array. Element i of that array is a pointer to the i-th string on the command line. In this example,
                                 argv[0] would point to the string ‘’cp’’. Similarly, argv[1] would point to the 5-character string ‘’file1’’
                                 and argv[2] would point to the 5-character string ‘’file2’’. The third parameter of main, envp, is a
                                 pointer to the environment, an main(argc, argv, envp) array of strings containing assignments of




        410                               LOVELY PROFESSIONAL UNIVERSITY
   412   413   414   415   416   417   418   419   420   421   422