网站建设未来发展,如何破解网站后台密码,购买天猫店铺网站,网络营销策划方案简介目录
一#xff0c;进程创建
写时拷贝
二#xff0c;进程终止
三#xff0c;进程等待
获取子进程status 一#xff0c;进程创建 命令行启动命令#xff08;程序、指令等#xff09;#xff1b;通过程序自身fork创建#xff1b;
#includeunistd.h
//子进程…目录
一进程创建
写时拷贝
二进程终止
三进程等待
获取子进程status 一进程创建 命令行启动命令程序、指令等通过程序自身fork创建
#includeunistd.h
//子进程返回0父进程返回子进程的ID出错返回-1
pid_t fork(void);进程调用fork当控制转移到内核中的fork代码后内核将会 分配新的内存块和内核数据结构给子进程拷贝父进程部分数据结构内容给子进程添加子进程到系统进程列表中fork返回开始调度器调度 #include stdio.h
#include unistd.h
#include stdlib.hint main()
{pid_t pid;printf(Before: pid is %d\n, getpid());pid fork();if(pid -1){perror(fork);exit(-1);}printf(After: pid is %d, fork return %d\n, getpid(), pid);sleep(1);return 0;
}[wz192 Desktop]$ ./target
Before: pid is 21739
After: pid is 21739, fork return 21740
After: pid is 21740, fork return 0
fork前父进程独立执行fork后父子进程分流执行父子谁先执行完全由调度器决定 写时拷贝 通常父子代码共享数据也共享父子在不写入时当有任意一方写入时便以写实拷贝的方式各自一份副本从而保证父子进程的独立性 二进程终止 代码运行结束结果正确退出码为0代码运行结束结果不正确退出码非0代码异常终止
#include stdio.h
#include unistd.h
#include string.h
int main()
{int i0;for(i;i200;i){printf(%d:%s\n,i,strerror(i));}return 0;
}[wz192 Desktop]$ ./target
0:Success
1:Operation not permitted
2:No such file or directory
3:No such process
4:Interrupted system call
5:Input/output error
6:No such device or address
7:Argument list too long
8:Exec format error
9:Bad file descriptor
10:No child processes
11:Resource temporarily unavailable
12:Cannot allocate memory
13:Permission denied
14:Bad address
15:Block device required
16:Device or resource busy
17:File exists
18:Invalid cross-device link
19:No such device
20:Not a directory
21:Is a directory
22:Invalid argument
23:Too many open files in system
24:Too many open files
25:Inappropriate ioctl for device
26:Text file busy
27:File too large
28:No space left on device
29:Illegal seek
30:Read-only file system
31:Too many links
32:Broken pipe
33:Numerical argument out of domain
34:Numerical result out of range
35:Resource deadlock avoided
36:File name too long
37:No locks available
38:Function not implemented
39:Directory not empty
40:Too many levels of symbolic links
41:Unknown error 41
42:No message of desired type
43:Identifier removed
44:Channel number out of range
45:Level 2 not synchronized
46:Level 3 halted
47:Level 3 reset
48:Link number out of range
49:Protocol driver not attached
50:No CSI structure available
51:Level 2 halted
52:Invalid exchange
53:Invalid request descriptor
54:Exchange full
55:No anode
56:Invalid request code
57:Invalid slot
58:Unknown error 58
59:Bad font file format
60:Device not a stream
61:No data available
62:Timer expired
63:Out of streams resources
64:Machine is not on the network
65:Package not installed
66:Object is remote
67:Link has been severed
68:Advertise error
69:Srmount error
70:Communication error on send
71:Protocol error
72:Multihop attempted
73:RFS specific error
74:Bad message
75:Value too large for defined data type
76:Name not unique on network
77:File descriptor in bad state
78:Remote address changed
79:Can not access a needed shared library
80:Accessing a corrupted shared library
81:.lib section in a.out corrupted
82:Attempting to link in too many shared libraries
83:Cannot exec a shared library directly
84:Invalid or incomplete multibyte or wide character
85:Interrupted system call should be restarted
86:Streams pipe error
87:Too many users
88:Socket operation on non-socket
89:Destination address required
90:Message too long
91:Protocol wrong type for socket
92:Protocol not available
93:Protocol not supported
94:Socket type not supported
95:Operation not supported
96:Protocol family not supported
97:Address family not supported by protocol
98:Address already in use
99:Cannot assign requested address
100:Network is down
101:Network is unreachable
102:Network dropped connection on reset
103:Software caused connection abort
104:Connection reset by peer
105:No buffer space available
106:Transport endpoint is already connected
107:Transport endpoint is not connected
108:Cannot send after transport endpoint shutdown
109:Too many references: cannot splice
110:Connection timed out
111:Connection refused
112:Host is down
113:No route to host
114:Operation already in progress
115:Operation now in progress
116:Stale file handle
117:Structure needs cleaning
118:Not a XENIX named type file
119:No XENIX semaphores available
120:Is a named type file
121:Remote I/O error
122:Disk quota exceeded
123:No medium found
124:Wrong medium type
125:Operation canceled
126:Required key not available
127:Key has expired
128:Key has been revoked
129:Key was rejected by service
130:Owner died
131:State not recoverable
132:Operation not possible due to RF-kill
133:Memory page has hardware error
134:Unknown error 134
135:Unknown error 135
136:Unknown error 136
137:Unknown error 137
138:Unknown error 138进程常见退出方法 正常终止可通过 echo $? 查看最近一次执行程序退出码 main返回return 0代表进程退出0退出码表示成功给系统查看以确认进程是否正确非main函数的return不受终止进程是结束函数exit任意位置调用此函数都会直接终止进程_exit与exit类型但此函数不会刷新缓冲区等处理工作直接终止进程异常退出 ctrl C信号终止 退出码可自定义也可使用系统错误码 #include unistd.h
void exit(int status);
#include unistd.h
void _exit(int status);
//status 定义了进程终止状态父进程通过wait来获取该值
//虽然status为int但仅低8位可被父进程使用_exit(-1)执行echo $?结果为255 #include stdio.h
#include stdlib.h
int main()
{printf(hello);exit(0); return 0;
}[wz192 Desktop]$ ./target
hello[wz192 Desktop]$ #include stdio.h
#include stdlib.h
int main()
{printf(hello);_exit(0); return 0;
}[wz192 Desktop]$ ./target
[wz192 Desktop]$ return退出 一种常见的退出进程方法执行return n等同于执行exit(n)因为调用main的运行时函数会将main的返回值当作exit的参数 进程终止的核心思想就是归还资源 释放曾经为管理进程所维护的所有数据结构对象 不是真的把数据结构对象销毁而是设置为不用状态保存起来“数据结构池”释放程序代码和数据所占的内存空间 不是将代码和数据清空把内存设置为无效即可取消曾经该进程的链接关系 三进程等待 进程等待的必要性
子进程退出父进程如不管不顾就可能造成僵死进程进而造成内存泄露进程进入僵死状态将无能为力即使是kill -9因为无法杀死一个已经死亡的进程父进程交派给子进程的任务完成状况需知道如子进程运行完其结果对错与否是否正常退出父进程通过进程等待的方式回收子进程资源获取子进程退出信息 pid_t wait(int*status) 返回值成功返回被等待进程pid失败返回-1;参数获取子进程退出状态不关心可设置为NULL
#includesys/types.h
#includesys/wait.hpid_t wait(int*status); pid_ t waitpid(pid_t pid, int *status, int options) 返回值 正常返回的时候waitpid返回收集到的子进程的进程PID如设置了选项WNOHANG而调用中waitpid方向没有已退出的子进程可收集返回0如调用中出错则返回-1这时errno会被设置成相应的值以指示错误所在参数 pid-1等待任一个子进程与wait等效0等待其进程ID与pid相等的子进程status WIFEXITED(status)若为正常终止子进程进程返回的状态则为真WEXITSTATUS(status)若WIFEXITED非零提取子进程退出码optionsWNOHANG若pid指定的子进程没有结束则waitpid()函数返回0不予以等待若正常结束则返回该子进程的ID
#includesys/types.h
#includesys/wait.hpid_ t waitpid(pid_t pid, int *status, int options);若子进程已退出调用wait/waitpid时wait/waitpid会立即返回并释放资源获得子进程退出信息如在任意时刻调用wait/waitpid子进程存在且正常运行则进程可能阻塞如不存在该子进程则立即出错返回 获取子进程status wait/waitpid都有一个status参数此参数为输出型参数由操作系统填充如传递NULL表示不关心子进程的退出状态信息否则操作系统会根据该参数将子进程发退出信息反馈给父进程status不能简单的当作整型看待可当作位图