黑馬《linux系統程式設計》學習筆記(從11到15)
阿新 • • 發佈:2018-12-29
十一. fork複習
無
十二. ps和kill命令
[[email protected]_0_15_centos 5Day]# ls
a.out execlp.c homework orphan.c test_fork.c zombie.c
demo_exec.c fork.c loop_f temp wait.c
execl.c hello loop_fork.c test_f waitpid.c
[[email protected]_0_15_centos 5Day]# ^C
[[email protected] _0_15_centos 5Day]# ^C
[[email protected]_0_15_centos 5Day]# vim heelo.c
gcc: error: unrecognized command line option ‘-0’
[[email protected]_0_15_centos 5Day]# gcc heelo.c -o myhello
[[email protected]_0_15_centos 5Day]# ls
a.out execlp.c hello loop_fork.c temp wait.c
demo_exec.c fork.c homework myhello test_f waitpid.c
execl.c heelo.c loop_f orphan.c test_fork.c zombie.c
[ [email protected]_0_15_centos 5Day]# ./myhello
hello,world
hello,world
hello,world
hello,world
^C
[[email protected]_0_15_centos 5Day]# ps
PID TTY TIME CMD
11892 pts/0 00:00:00 bash
13306 pts/0 00:00:00 ps
[[email protected]_0_15_centos 5Day]# ps a
PID TTY STAT TIME COMMAND
1046 ttyS0 Ss+ 0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt22
1047 tty1 Ss+ 0:00 /sbin/agetty --noclear tty1 linux
11892 pts/0 Ss 0:00 -bash
13354 pts/0 R+ 0:00 ps a
[ [email protected]_0_15_centos 5Day]# ps au
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1046 0.0 0.0 110088 840 ttyS0 Ss+ Dec14 0:00 /sbin/agetty --
root 1047 0.0 0.0 110088 820 tty1 Ss+ Dec14 0:00 /sbin/agetty --
root 11892 0.0 0.1 116804 3596 pts/0 Ss 21:34 0:00 -bash
root 13388 0.0 0.0 151108 1856 pts/0 R+ 21:47 0:00 ps au
[[email protected]_0_15_centos 5Day]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 43252 3032 ? Ss Dec14 0:53 /usr/lib/system
root 2 0.0 0.0 0 0 ? S Dec14 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Dec14 0:09 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Dec14 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Dec14 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S Dec14 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? R Dec14 0:56 [rcu_sched]
root 10 0.0 0.0 0 0 ? S Dec14 0:05 [watchdog/0]
root 12 0.0 0.0 0 0 ? S Dec14 0:00 [kdevtmpfs]
root 13 0.0 0.0 0 0 ? S< Dec14 0:00 [netns]
root 14 0.0 0.0 0 0 ? S Dec14 0:00 [khungtaskd]
root 15 0.0 0.0 0 0 ? S< Dec14 0:00 [writeback]
root 16 0.0 0.0 0 0 ? S< Dec14 0:00 [kintegrityd]
root 17 0.0 0.0 0 0 ? S< Dec14 0:00 [bioset]
root 18 0.0 0.0 0 0 ? S< Dec14 0:00 [kblockd]
root 19 0.0 0.0 0 0 ? S< Dec14 0:00 [md]
root 25 0.0 0.0 0 0 ? S Dec14 0:02 [kswapd0]
root 26 0.0 0.0 0 0 ? SN Dec14 0:00 [ksmd]
root 27 0.0 0.0 0 0 ? SN Dec14 0:02 [khugepaged]
root 28 0.0 0.0 0 0 ? S< Dec14 0:00 [crypto]
root 36 0.0 0.0 0 0 ? S< Dec14 0:00 [kthrotld]
root 38 0.0 0.0 0 0 ? S< Dec14 0:00 [kmpath_rdacd]
root 39 0.0 0.0 0 0 ? S< Dec14 0:00 [kpsmoused]
root 40 0.0 0.0 0 0 ? S< Dec14 0:00 [ipv6_addrconf]
root 59 0.0 0.0 0 0 ? S< Dec14 0:00 [deferwq]
root 98 0.0 0.0 0 0 ? S Dec14 0:02 [kauditd]
root 226 0.0 0.0 0 0 ? S< Dec14 0:00 [ata_sff]
root 230 0.0 0.0 0 0 ? S Dec14 0:00 [scsi_eh_0]
root 232 0.0 0.0 0 0 ? S< Dec14 0:00 [scsi_tmf_0]
root 235 0.0 0.0 0 0 ? S Dec14 0:00 [scsi_eh_1]
root 237 0.0 0.0 0 0 ? S< Dec14 0:00 [scsi_tmf_1]
root 241 0.0 0.0 0 0 ? S< Dec14 0:00 [ttm_swap]
root 258 0.0 0.0 0 0 ? S Dec14 0:21 [jbd2/vda1-8]
root 259 0.0 0.0 0 0 ? S< Dec14 0:00 [ext4-rsv-conve
root 333 0.0 4.6 139536 88236 ? Ss Dec14 1:17 /usr/lib/system
root 355 0.0 0.0 118916 1052 ? Ss Dec14 0:00 /usr/sbin/lvmet
root 359 0.0 0.0 0 0 ? S< Dec14 0:10 [kworker/0:1H]
root 366 0.0 0.0 44084 1672 ? Ss Dec14 0:00 /usr/lib/system
root 450 0.0 0.0 0 0 ? S< Dec14 0:00 [edac-poller]
root 486 0.0 0.0 55496 1076 ? S<sl Dec14 0:15 /sbin/auditd
polkitd 511 0.0 0.5 536252 10576 ? Ssl Dec14 0:20 /usr/lib/polkit
root 513 0.0 2.8 734860 53224 ? Ssl Dec14 1:11 /usr/sbin/rsysl
root 517 0.0 0.0 24248 1584 ? Ss Dec14 0:30 /usr/lib/system
libstor+ 518 0.0 0.0 8576 764 ? Ss Dec14 0:01 /usr/bin/lsmd -
dbus 519 0.0 0.0 26536 1660 ? Ss Dec14 0:55 /bin/dbus-daemo
root 521 0.0 0.0 4384 492 ? Ss Dec14 0:00 /usr/sbin/acpid
ntp 542 0.0 0.1 47252 2216 ? Ss Dec14 0:00 /usr/sbin/ntpd
root 676 0.0 0.0 126280 1600 ? Ss Dec14 0:05 /usr/sbin/crond
root 678 0.0 0.0 25900 940 ? Ss Dec14 0:00 /usr/sbin/atd -
root 818 0.0 0.6 113412 12588 ? Ss Dec14 0:00 /sbin/dhclient
root 898 0.0 0.2 106040 4140 ? Ss Dec14 0:15 /usr/sbin/sshd
root 900 0.0 0.7 562432 13264 ? Ssl Dec14 1:40 /usr/bin/python
mysql 977 0.0 0.0 113308 1520 ? Ss Dec14 0:00 /bin/sh /usr/bi
root 1046 0.0 0.0 110088 840 ttyS0 Ss+ Dec14 0:00 /sbin/agetty --
root 1047 0.0 0.0 110088 820 tty1 Ss+ Dec14 0:00 /sbin/agetty --
mysql 1257 0.0 6.2 961288 117588 ? Sl Dec14 5:23 /usr/sbin/mysql
root 1407 0.0 0.0 97336 1716 ? Sl Dec14 0:07 /usr/local/qclo
root 1419 0.0 0.3 155260 7504 ? S Dec14 0:14 barad_agent
root 1425 0.0 0.4 158160 9028 ? S Dec14 9:40 barad_agent
root 1426 0.3 0.5 610308 11184 ? Sl Dec14 48:24 barad_agent
root 1470 0.0 0.1 100988 2000 ? Ssl Dec14 2:19 /usr/local/qclo
root 11865 0.0 0.2 147832 5260 ? Ss 21:34 0:00 sshd: [email protected]/
root 11892 0.0 0.1 116804 3596 pts/0 Ss 21:34 0:00 -bash
root 12084 0.0 0.0 0 0 ? S 21:36 0:00 [kworker/0:1]
root 12437 0.0 0.0 0 0 ? S 09:50 0:01 [kworker/u2:1]
root 12515 0.0 0.0 0 0 ? S 21:41 0:00 [kworker/0:2]
root 13182 0.0 0.0 0 0 ? R 21:46 0:00 [kworker/0:0]
root 13487 0.0 0.2 106040 4088 ? Ss 21:48 0:00 sshd: [accepted
sshd 13488 0.0 0.1 106040 1924 ? S 21:48 0:00 sshd: [net]
root 13489 0.0 0.2 145628 4804 ? Ss 21:48 0:00 sshd: root [pri
sshd 13490 0.0 0.1 106040 2180 ? S 21:48 0:00 sshd: root [net
root 13491 0.0 0.0 151108 1852 pts/0 R+ 21:48 0:00 ps aux
root 16041 0.0 0.0 0 0 ? S 04:19 0:00 [kworker/u2:2]
root 26009 0.1 0.4 571352 8156 ? Ssl 06:08 1:44 /usr/local/qclo
//這裡用了管道來進行查詢
[[email protected]_0_15_centos 5Day]# ps aux | grep myhello
root 14264 0.0 0.0 112708 976 pts/0 R+ 21:54 0:00 grep --color=auto myhello
kill命令
[[email protected]_0_15_centos 5Day]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
[[email protected]_0_15_centos 5Day]# kill -9
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
kill -9 強殺程序
十三. 程序間資料共享
這裡當我們修改父程序的num時,會額外在實體記憶體中,給父程序的num提供一塊區域
接下來,如果我們在子程序的num裡也進行修改,那麼也會為子程序的num,在實體記憶體裡提供num
也因此各個程序裡的使用者區的資料,在fork後,就完全獨立了。
總結起來,就是“讀時共享,寫時複製”。
十四. 驗證程序間是否共享全域性變數
先寫了一個loop_fork.c的程式
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int counter = 100;
int main(int argc, const char* argv[])
{
pid_t pid;
int i=0;
for(i=0; i<3; i++)
{
pid = fork();
if(pid == 0)
{
break;
}
}
// 父程序
if(i == 3)
{
counter += 100;
printf("parent process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
// sleep(1);
}
// 子程序
else if(i == 0)
{
// 1th
counter += 200;
printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
}
else if(i == 1)
{
// 2th
counter += 300;
printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
}
else if(i == 2)
{
// 3th
counter += 400;
printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
}
return 0;
}
執行結果如下:
[[email protected]_0_15_centos 5Day]# ls
a.out execlp.c hello loop_fork.c temp wait.c
demo_exec.c fork.c homework myhello test_f waitpid.c
execl.c heelo.c loop_f orphan.c test_fork.c zombie.c
[[email protected]_0_15_centos 5Day]# ./loop_f
parent process, pid = 22533, ppid = 22447, 200
[[email protected]_0_15_centos 5Day]# child process, pid = 22534, ppid = 1, 300
child process, pid = 22535, ppid = 1, 400
child process, pid = 22536, ppid = 1, 500
這裡說明了,父子程序之間,並不能使用全域性變數進行通訊
十五. exec函式族功能介紹
exec族的作用