written 2.6 years ago by |
Process Management
- A process is the basic context within which all user- requested activity is serviced within the operating system.
- To be compatible, Linux must use a process model similar to those of other versions of UNIX.
Linux also has its own threading model.
1.The fork() and exec() process model
- Processes and Threads
1.The fork() and exec() Process Model
• The basic principle of UNIX process management is to separate two operations: the creation of a process and the running of a new program.
• A new process is created by the fork() system call, and a new program is run after a call to exec().
• Equally, running a new program does not require that a new process be created first: any process may call exec() at any time. The currently running program is immediately terminated, and the new program starts executing in the context of the existing process.
• This model has the advantage of great simplicity. No need to specify every detail of the environment of the new program in the system call.
• If a parent process wishes to modify the environment in which a new program is to be run, it can fork and then, still running the original program in a child process, make any system calls it requires to modify that child process before finally executing the new program.
To set environment variable.
# env LOGPATH=/var/log
• Under Linux, all the information that the operating system must maintain to track the context of a single execution of a single program can be broken down into a number of specific sections.
• Broadly, process properties fall into three groups:
Process identity
Process environment
Process context.
1a.Process Identity
- Process ID (PID)
- Credentials
- Personality
1b.Process Environment
• The process' s environment is inherited from its parent, and is composed of two null-terminated vectors:
• The argument vector lists the command-line arguments used to invoke the running program; conventionally starts with the name of the program itself.
• The environment vector is a list of "NAME-VALUE" pairs that associates named environment variables with arbitrary textual values.
• Stored in the process's own user-mode address space as the first datum at the top of the process's stack.
• On calling exec, a process can supply a new environment.The kernel passes these environment variables to the next program,replacing the process's current environment.
1c.Process Context
• The (constantly changing) state of a running program at any point in time
• The scheduling context is the most important part of the process context; it is the information that the scheduler needs to suspend and restart the process.
• The kernel maintains accounting information about the resources currently being consumed by each process, and the total resources consumed by the process in its lifetime so far.
• The file table is an array of pointers to kernel file structures
• When making file I/O system calls, processes refer to files by their index into this table, the file descriptor (fd)
• File-system context applies to requests to open new files
• The current root and default directories to be used for new file searches are stored here
• The signal-handler table defines the routine in the process' s address space to be called when specific signals arrive
• The virtual-memory context of a process describes the full contents of a process's private address space
2.Processes and Threads
• Linux uses the same internal representation for processes and threads; a thread is simply a new process that happens to share the same address space as its parent
• Flow of control within a program called as a task in Linux
• A distinction is only made when a new thread is created by the clone() system
flag | meaning |
---|---|
CLONE FS | File-system information is shared. |
CLONE VM | The same memory space is shared. |
CLONE SIGHAND | Signal handlers are shared. |
CLONE FILES | The set of open files is shared. |
• If clone() is passed the flags CLONE_FS, CLONE_VM, CLONE_SIGHAND, and CLONE_FILES, the parent and child tasks will share : same file-system information (such as the current working directory), same memory space, same signal handlers, and same set of open files
• If none of these flags is set when clone () is invoked, no sharing takes place, creates a new task with its own entirely new task context. In Linux, a process holds the context within independent subcontexts.
• The process data structure simply contains pointers to these other structures, so any number of processes can easily share a subcontext by pointing to the same subcontext. call.