linux进程简述

来源:互联网 发布:武器发射工程 知乎 编辑:程序博客网 时间:2024/06/06 04:20

进程的Introduction:

A process is a program in execution (进程是程序在执行)

A process is created every time you run an external command and is removed after the command finishes its execution

(进程是每次你运行一个外部命令,命令完成后执行删除)

内部命令:                        

s12507@Linux:~$ type pwd                     

pwd is a shell builtin

外部命令:

s12507@Linux:~$ type mkdir

mkdir is /bin/mkdir

 

关于“ps -l”(ps显示瞬间行程的状态)

F S   UID   PID     PPID    C  PRI  NI ADDR  SZ    WCHAN   TTY      TIME       CMD

0 S  1697  10603   10602    0  80   0   -   1203    wait   pts/19   00:00:00   bash

0 R  1697  10964   10603    0  80   0   -   625      -     pts/19   00:00:00   ps

# F 代表这个程序的旗标 (flag) 例如4代表使用者为 superuser

# S 代表这个程序的状态 (STAT)

D代表无法中断的休眠状态(通常IO的进程)、R正在运行、S处于休眠状态、T停止或被追踪、Z僵尸进程

# PID    就是这个程序的 ID,底下的 PPID 则父程序的ID

# C      CPU 使用的资源百分比

# PRI    这个是Priority (优先执行序) 的缩写。

# NI     这个是 Nice值,表示进程可被执行的优先级的修正数值。

# ADDR   这个是kernel function,指出该程序在内存的那个部分。running程序一般是“-”

# SZ     使用掉的内存大小;

# WCHAN  目前这个程序是否正在运作当中,若为 - 表示正在运作;

# TTY    登入者的终端机位置;

# TIME   使用掉的 CPU 时间。

# CMD    所下达的指令为何?

关于父进程和子进程:

The forking process is known as the parent process(父进程)

The created (forked) process is called the child process(子进程)

函数fork( )用来创建一个新的进程,该进程几乎是当前进程的一个完全拷贝。

函数族exec( )用来启动另外的进程以取代当前运行的进程。


 

关于进程状态切换(Foreground and Background Process)

suspended Foreground process:CTRL+Z 挂起进程到后台(CTRL+C)

Display all jobs status:jobs 显示所有任务

Background ->Foreground

-fg [%jobnum]  fg %1

suspended ->Background

-bg [%jobnum] bg %1

 

使用CTRL+Z终止前台进程

使用jobs可以查看进程任务

启动后台进程:(BG) 此时,使用CTRL+Z可以将一个正在前台执行的命令放到后台,并且处于暂停状态。

s12507@Linux:~$ /tmp/loop&正在运行的进程转变为后台

[1]             11706

进程任务号      进程ID

前台进程:(FG),此时,使用Ctrl+C中断进程,不放进后台。

 

 

UNIX Daemons:守护进程

•    A daemon is a system process running in the background

•    Used to offer various types of services to users and handle systemadministration tasks

acpid、mysqld、sshd等均为精灵进程(父进程是init)

 

 

Sequentialand Parallel Execution of Commands:串行和并行运行

串行运行

s12507@Linux:~$ find / -name stdio.h; pwd

并行运行

s12507@Linux:~$ find / -name stdio.h &pwd

 

 

AbnormalTermination of Commands and Processes:异常终止命令和过程

终止进程(均是通过发送信号来终止进程的)

终止前台进程:

<Ctrl-C>

终止后台进程:

s12507@Linux:~$ ps

  PIDTTY          TIME CMD

11750 pts/32   00:00:00 bash

12158 pts/32   00:00:00 loop

12193 pts/32   00:00:00 ps

s12507@Linux:~$ kill 12158

s12507@Linux:~$ ps

  PIDTTY          TIME CMD

11750 pts/32   00:00:00 bash

12198 pts/32   00:00:00 ps

[1]+ Terminated              /tmp/loop> /dev/null

或者通过后台进程转换(fg命令)为前台进程,然后用<Ctrl-C>终止进程。

 

Commonly used signal_numbers:

         1                          Hangup(挂起进程)

         2                          Interrupt(<Ctrl-C>)

         3                          Quit(<Ctrl-\>)

         9                          Sure kill(保证杀死进程)

        15                         Software signal (default signalnumber)

 

s12507@Linux:~$ /tmp/neverdie

^CI won't die

^Z

[1]+ Stopped                /tmp/neverdie

s12507@Linux:~$ bg

[1]+ /tmp/neverdie &

s12507@Linux:~$ jobs -l

[1]+ 12242 Running                 /tmp/neverdie &

s12507@Linux:~$ kill 12242

s12507@Linux:~$ ps

  PIDTTY          TIME CMD

11750 pts/32   00:00:00 bash

12242 pts/32   00:00:00 neverdie

12284 pts/32   00:00:00 ps

 

可以利用信号9来保证杀死进程:

s12507@Linux:~$ kill -9 12242

s12507@Linux:~$ ps

  PIDTTY          TIME CMD

11750 pts/32   00:00:00 bash

12318 pts/32   00:00:00 ps

[1]+ Killed                 /tmp/neverdie

 

nohup

表示退出时仍然保留子进程,如:

s12507@Linux:~$ nohup crawl &

eg:nohup commands[args]  nohup find / -name foo –print1>foo.path

Purpose: Run ‘command and ignore the hangup signal(运行命令并忽略挂起信号)即在注销后使用 nohup 命令运行后台中的程序。

 

 

Scheduling a Process To Execute Later:

One-time jobs useat, recurring jobs use crontab

Create

attime

crontab -e

List

at -l

crontab -l

Details

at -cjobnum

Remove

at -djobnum

crontab -r

Edit

crontab -e

Eg:s12507@Linux:~$ at 08:48 到点执行

warning: commands will be executed using/bin/sh

at>at -l

at>echo hello>he


0 0
原创粉丝点击