0% found this document useful (0 votes)
60 views4 pages

26 Processes : 26.1 Running A Command

Processes are the basic units for allocating system resources. Each process has its own address space and thread of control. A process executes a program, and multiple processes can execute the same program independently. Processes are organized hierarchically, with each process having a parent process that created it. Child processes inherit attributes from their parent process. The system function runs another program but does not provide much control, while fork, exec, and wait/waitpid provide more control over process creation and execution.

Uploaded by

Pankaj Vaksh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

26 Processes : 26.1 Running A Command

Processes are the basic units for allocating system resources. Each process has its own address space and thread of control. A process executes a program, and multiple processes can execute the same program independently. Processes are organized hierarchically, with each process having a parent process that created it. Child processes inherit attributes from their parent process. The system function runs another program but does not provide much control, while fork, exec, and wait/waitpid provide more control over process creation and execution.

Uploaded by

Pankaj Vaksh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

26 Processes ****************

"Processes" are the primitive units for allocation of system resources. Each process has its own address space and (usually) one thread of control. A process executes a program; you can have multiple processes executing the same program, but each process has its own copy of the program within its own address space and executes it independently of the other copies. Processes are organized hierarchically. Each process has a "parent process" which explicitly arranged to create it. The processes created by a given parent are called its "child processes". A child inherits many of its attributes from the parent process. This chapter describes how a program can create, terminate, and control child processes. Actually, there are three distinct operations involved: creating a new child process, causing the new process to execute a program, Coordinating the completion of the child process with the original program. The `system' function provides a simple, portable mechanism for running another program; it does all three steps automatically. If you need more control over the details of how this is done, you can use the primitive functions to do each step individually instead. 26.1 Running a Command ====================== The easy way to run another program is to use the `system' function. This function does all the work of running a subprogram, but it doesn't give you much control over the details: you have to wait until the subprogram terminates before you can do anything else. -- Function: int system (const char *COMMAND) This function executes COMMAND as a shell command. In the GNU C library, it always uses the default shell `sh' to run the command. In particular, it searches the directories in `PATH' to find programs to execute. The return value is `-1' if it wasn't possible to create the shell process, and otherwise is the status of the shell process. If the COMMAND argument is a null pointer, a return value of zero indicates that no command processor is available. This function is a cancellation point in multi-threaded programs. This is a problem if the thread allocates some resources (like memory, file descriptors, semaphores or whatever) at the time `system' is called. If the thread gets canceled these resources stay allocated until the program ends. To avoid this call to `system' should be protected using cancellation handlers. The `system' function is declared in the header file `stdlib.h'. *Portability Note:* Some C implementations may not have any notion of a command processor that can execute other programs. You can determine whether a command processor exists by executing `system (NULL)'; if the return value is nonzero, a command processor is available.

26.2 Process Creation Concepts ========================== This section gives an overview of processes and of the steps involved in creating a process and making it run another program. Each process is named by a "process ID" number. A unique process ID is allocated to each process when it is created. The "lifetime" of a process ends when its termination is reported to its parent process; at that time, all of the process resources, including its process ID, are freed. Processes are created with the `fork' system call (so the operation of creating a new process is sometimes called "forking" a process). The "child process" created by `fork' is a copy of the original "parent process", except that it has its own process ID. After forking a child process, both the parent and child processes continue to execute normally. If you want your program to wait for a child process to finish executing before continuing, you must do this explicitly after the fork operation, by calling `wait' or `waitpid'. These functions give you limited information about why the child terminated--for example, its exit status code. A newly forked child process continues to execute the same program as its parent process, at the point where the `fork' call returns. You can use the return value from `fork' to tell whether the program is running in the parent process or the child. Having several processes run the same program is only occasionally useful. But the child can execute another program using one of the `exec' functions. The program that the process is executing is called its "process image". Starting execution of a new program causes the process to forget all about its previous process image; when the new program exits, the process exits too, instead of returning to the previous process image. 26.3 Process Identification ======================= The `pid_t' data type represents process IDs. You can get the process ID of a process by calling `getpid'. The function `getppid' returns the process ID of the parent of the current process (this is also known as the "parent process ID"). Your program should include the header files ` unistd.h' and `sys/types.h' to use these functions. Data Type: pid_t -> the `pid_t' data type is a signed integer type which is capable of representing a process ID. In the GNU library, this is an `int'. Function: pid_t getpid (void) -> the `getpid' function returns the process ID of the current process. Function: pid_t getppid (void) -> the `getppid' function returns the process ID of the parent of the current process. 26.4 Creating a Process =================== The `fork' function is the primitive for creating a process. It is declared in the header file `unistd.h'. Function: pid_t fork (void) -> the `fork' function creates a new process. If the operation is successful, there are then both parent and child processes and both see `fork' return, but with different values: it returns a value of

`0' in the child process and returns the child's process ID in the parent process. If process creation failed; `fork' returns a value of `-1' in the parent process. The following `errno' error conditions are defined for `fork': EAGAIN -> There aren't enough system resources to create another process, or the user already has too many processes running. This means exceeding the `RLIMIT_NPROC' resource limit, which can usually be increased. ENOMEM -> The process requires more space than the system can supply. The specific attributes of the child process that differ from the parent process are: The child process has its own unique process ID. The parent process ID of the child process is the process ID of its parent process. The child process gets its own copies of the parent process's open file descriptors. Subsequently changing attributes of the file descriptors in the parent process won't affect the file descriptors in the child, and vice versa. However, the file position associated with each descriptor is shared by both processes. The elapsed processor times for the child process are set to zero. The child doesn't inherit file locks set by the parent process. The child doesn't inherit alarms set by the parent process. The set of pending signals for the child process is cleared. (The child process inherits its mask of blocked signals and signal actions from the parent process.) Function: pid_t vfork (void) -> the `vfork' function is similar to `fork' but on some systems it is more efficient; however, there are restrictions you must follow to use it safely. While `fork' makes a complete copy of the calling process's address space and allows both the parent and child to execute independently, `vfork' does not make this copy. Instead, the child process created with `vfork' shares its parent's address space until it calls `_exit' or one of the `exec' functions. In the meantime, the parent process suspends execution. You must be very careful not to allow the child process created with `vfork' to modify any global data or even local variables shared with the parent. Furthermore, the child process cannot return from (or do a long jump out of) the function that called `vfork'! This would leave the parent process's control information very confused. If in doubt, use `fork' instead. Some operating systems don't really implement `vfork'. The GNU C library permits you to use `vfork' on all systems, but actually executes `fork' if `vfork' isn't available. If you follow the proper precautions for using `vfork', your program will still work even if the system uses `fork' instead. 26.5 Executing a File ================= This section describes the `exec' family of functions, for executing a file as a process image. You can use these functions to make a child process execute a new program after it has been forked.

The functions in this family differ in how you specify the arguments, but otherwise they all do the same thing. They are declared in the header file `unistd.h'. Function: int execv (const char *FILENAME, char *const ARGV []) -> the `execv' function executes the file named by FILENAME as a new process image. The ARGV argument is an array of null-terminated strings that is used to provide a value for the `argv' argument to the `main' function of the program to be executed. The last element of this array must be a null pointer. By convention, the first element of this array is the file name of the program sans directory names. *Note Program Arguments::, for full details on how programs can access these arguments. The environment for the new process image is taken from the `environ' variable of the current process image; see *Note Environment Variables::, for information about environments. -- Function: int execl (const char *FILENAME, const char *ARG0, ...) This is similar to `execv', but the ARGV strings are specified individually instead of as an array. A null pointer must be passed as the last such argument.

You might also like