Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

UNIX UNLEASHED PHẦN 4 pps
MIỄN PHÍ
Số trang
60
Kích thước
487.2 KB
Định dạng
PDF
Lượt xem
1371

UNIX UNLEASHED PHẦN 4 pps

Nội dung xem thử

Mô tả chi tiết

Part IV — Process Control

What Is a Process?

Administering Processes

Scheduling Processes

 18 – What Is a Process?

 By Rachel and Robert Sartin

 What Happens When You Execute a Command?

 Forking a Process

 Running a Command

 Looking at Process

 Visiting the Shell Again

 Processing a Command

 Checking the Aliases and Built-Ins

 Make a New Process with fork

 Start a New Command with exec

 An Example

 Executing in the Background

 An Example

 Kinks in Your Pipeline

 A Special Process Called Daemon

 init

 inetd

 cron

 Summary

18 – What Is a Process?

By Rachel and Robert Sartin

This chapter introduces the concept of processes and how you use UNIX to interact with

them.

What Happens When You Execute a Command?

When you execute a program on your UNIX system, the system creates a special

environment for that program. This environment contains everything needed for the

system to run the program as if no other program were running on the system.

Forking a Process

Each process has process context, which is everything that is unique about the state of the

program you are currently running. The process context includes then following:

 The text (program instructions) being run

 The memory used by the program being run

 The current working directory

 The files that are open and positions in the files

 Resource limits

 Access control information

 Others—various low-level information

Every time you execute a program the UNIX system does a fork, which performs a series

of operations to create a process context and then execute your program in that context.

The steps include the following:

1. Allocate a slot in the process table, a list of currently running programs kept by

UNIX. UNIX creates the illusion of multiple programs running simultaneously by

switching quickly between active processes in the process table. This allocation

can fail for a number of reasons, including these:

o You have exceeded your per user process limit, the maximum number of

processes your UNIX system will allow you to run.

o The system runs out of open process slots. The UNIX kernel stores

information about currently running processes in a table of processes.

When this table runs out of room for new entries, you are unable to fork a

new process.

o UNIX has run out of memory and does not have room for the text and data

of the new process.

2. Assign a unique process identifier (PID) to the process. This identifier can be used

to examine and control the process later.

3. Copy the context of the parent, the process that requested the spawning of the new

process.

4. Return the new PID to the parent process. This enables the parent process to

examine or control the process directly.

After the fork is complete, UNIX runs your program. One of the differences between

UNIX and many other operating systems is that UNIX performs this two-step procedure

to run a program. The first step is to create a new process that's just like the parent. The

second is to execute a different program. This procedure allows interesting variations.

(See the section "A Special Process Called Daemon.")

Running a Command

When you enter ls to look at the contents of your current working directory, UNIX does a

series of things to create an environment for ls and the run it:

1. The shell has UNIX perform a fork. This creates a new process that the shell will

use to run the ls program.

2. The shell has UNIX perform an exec of the ls program. This replaces the shell

program and data with the program and data for ls and then starts running that

new program.

3. The ls program is loaded into the new process context, replacing the text and data

of the shell.

4. The ls program performs its task, listing the contents of the current directory.

Looking at Process

Because processes are so important to getting things done, UNIX has several commands

that enable you to examine processes and modify their state. The most frequently used

command is ps, which prints out the process status for processes running on your system.

Each system has a slightly different version of the ps command, but there are two main

variants, the System V version and the Berkeley version, covered in this section.

Different versions of ps do similar things, but have somewhat different output and are

controlled using different options. The X/Open Portability Guide makes an attempt to

standardize somewhat on output of the ps command. The ps command is covered in more

detail in chapter 19, "Administrative Processes."

On a System V or XPG-compliant system, you can examine all the processes you are

running by entering ps -f and you will get output such as the following:

$ ps -f

UID PID PPID C STIME TTY TIME COMMAND

root 14931 136 0 08:37:48 ttys0 0:00 rlogind

sartin 14932 14931 0 08:37:50 ttys0 0:00 -sh

sartin 15339 14932 7 16:32:29 ttys0 0:00 ps -f

$

NOTE: After the first line, which is the header, each line of output tells about the

status of a single process. The UID column tells the owner of the process. The PID

column tells the process ID. The PPID tells the process ID of the parent process (the

process that executed the fork). The STIME is the time the process began executing. The

TIME is the amount of computer time the process has used. The COMMAND field tells

what command line was executed.

Look at this example and you can see that root (the system administration user) is

running rlogind as process 14931. This process is a special kind of administrative

program, called a daemon (daemons are described in the section "A Special Process

Called Daemon"). This particular daemon is responsible for managing a connection from

rlogin, which is described in Chapter 8, "Getting Around the Network." As you can see

from the next line, there is a process called -sh, which is a Bourne shell. The shell has

rlogind as its parent because the daemon did a fork to run the login shell. Similarly, there

is a ps -f command that has the shell as its parent.

TIP: The leading hyphen on the -sh in the output of ps means that the shell is

executing as a login shell, which does certain special processing that other instances of

the shell do not. See the chapter on your shell for more information on login shells.

Visiting the Shell Again

Earlier in this chapter you learned that the shell creates a new process for each command

you execute. This section covers in a bit more detail how the shell creates and manages

processes.

Processing a Command

When you type a command to your shell user interface, the shell performs a series of

tasks to process the command. Although the steps may seem a bit cumbersome at first,

they create an environment that is highly flexible.

Checking the Aliases and Built-Ins

The first thing the shell does is alias and built-in processing to see if your command is

one of the shell's internally implemented functions. Each shell implements a number of

functions internally either because external implementation would be difficult (for

example, while loops) or because internal implementation is a big performance win (for

example, echo in some shells). One reason the built-in commands are easier is that they

can operate directly in the shell process rather than forcing the shell to create a new

process to run a different command. That new command would not have access to the

shell's memory.

Make a New Process with fork

If the command you typed is not a built-in command (for example, if you entered ps), the

shell performs a fork to create a new process. Your UNIX system allocates the necessary

resources. The shell modifies the process environment to configure correctly for the

command to be executed. This includes any input or output redirect you may have

requested (including command pipelines) and creating a new background process group

if you executed the command in the background.

Start a New Command with exec

Finally, the shell performs an exec to execute the program that you requested. The

program will replace the shell with a forked shell, but your shell will still be running.

An Example

The following happens when you enter ps -f > t1 followed by cat t1:

$ ps -f > t1

$ cat t1

UID PID PPID C STIME TTY TIME COMMAND

root 14931 136 0 08:37:48 ttys0 0:00 rlogind

sartin 14932 14931 0 08:37:50 ttys0 0:00 -sh

sartin 15339 14932 7 16:32:29 ttys0 0:00 ps -f

$

UNIX performs the following steps to execute ps -f> t1:

1. Shell command processing. The login shell (PID 14932 in this example)

performs variable substitution and examines the command line to determine that

ps is not a built-in or an alias.

2. fork/wait. The login shell (PID 14932) forks a new process (PID 15339). This

new process is an exact copy of the login shell. It has the same open files, the

same user ID, and a copy of the memory, and it is executing the same code.

Because the command was not executed in the background, the login shell

(14932) will execute a wait to wait for the new child (15339) to complete.

3. setup. The new shell (PID 15339) performs the operations it needs to do in order

to prepare for the new program. In this case, it redirects the standard output to a

file (if it existed) in the current directory named t1, overwriting the file.

4. exec. The new shell (PID 15339) asks the UNIX system to exec the ps command

with -f as its argument. UNIX throws away the memory from the shell and loads

the ps command code and data into the process memory. The ps command will

run and write its output to the standard output, which has been redirected to the

file t1.

5. wait ends. When the ps command is done executing, the login shell (PID 14932)

receives notification and will prompt the user for more input.

Executing in the Background

You can tell your shell to execute commands in the background, which tells the shell not

to wait for the command to complete. This enables you to run programs without having to

wait for them to complete.

TIP: For long-running commands that are not interactive, you can run the

command in the background and continue to do work while it executes. Use the nohup

command to make sure the process will not get interrupted; nohup will redirect the

command output to a file called nohup.out. For example, to run a make in the background

enter nohup make all. When the make terminates, you can read nohup.out to check the

output.

An Example

This example is almost the same as the previous example. The only difference is that the

ps command is executed in the background. The following happens when you are using

the Bourne shell and enter ps -f > t1 & followed by cat t1:

$ ps -f > t1 &

15445

$ cat t1

UID PID PPID C STIME TTY TIME COMMAND

Tải ngay đi em, còn do dự, trời tối mất!