文章目录
-
- [1. 冯诺依曼体系结构](#1. 冯诺依曼体系结构)
- [2. 操作系统(Operator System)](#2. 操作系统(Operator System))
- [3. 进程](#3. 进程)
-
- [3.1 基本概念与基本操作](#3.1 基本概念与基本操作)
1. 冯诺依曼体系结构

2. 操作系统(Operator System)



3. 进程
3.1 基本概念与基本操作


c
[gsm@VM-4-3-centos lesson10]$ ll
total 0
[gsm@VM-4-3-centos lesson10]$ touch myproc.c
[gsm@VM-4-3-centos lesson10]$ ll
total 0
-rw-rw-r-- 1 gsm gsm 0 Oct 22 21:32 myproc.c
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
printf("hello bit!\n");
sleep(1);
}
}
[gsm@VM-4-3-centos lesson10]$ ls > Makefile
[gsm@VM-4-3-centos lesson10]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 18 Oct 22 21:36 Makefile
-rw-rw-r-- 1 gsm gsm 130 Oct 22 21:35 myproc.c
[gsm@VM-4-3-centos lesson10]$ vim Makefile
[gsm@VM-4-3-centos lesson10]$ cat Makefile
myproc:myproc.c
gcc -o $@ $^
.PHONY:clean
clean:
rm -f myproc
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8408 Oct 22 21:39 myproc
-rw-rw-r-- 1 gsm gsm 130 Oct 22 21:35 myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc
c
[gsm@VM-4-3-centos ~]$ ps ajx | head -1
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
[gsm@VM-4-3-centos ~]$ ps ajx | head -1; ps ajx | grep myproc
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
17844 21660 21660 17844 pts/0 21660 S+ 1001 0:00 ./myproc
21788 22396 22395 21788 pts/1 22395 S+ 1001 0:00 grep --color=auto myproc
[gsm@VM-4-3-centos ~]$ ps ajx | head -1 && ps ajx | grep myproc
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
17844 21660 21660 17844 pts/0 21660 S+ 1001 0:00 ./myproc
21788 22766 22765 21788 pts/1 22765 S+ 1001 0:00 grep --color=auto myproc
//结束这个程序后就查不到了
[gsm@VM-4-3-centos ~]$ ps ajx | head -1 && ps ajx | grep myproc
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
21788 22900 22899 21788 pts/1 22899 S+ 1001 0:00 grep --color=auto myproc
[gsm@VM-4-3-centos ~]$ ps ajx | head -1 && ps ajx | grep myproc | grep -v grep
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
//程序运行起来后
[gsm@VM-4-3-centos ~]$ ps ajx | head -1 && ps ajx | grep myproc | grep -v grep
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
17844 24035 24035 17844 pts/0 24035 S+ 1001 0:00 ./myproc
c
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8408 Oct 22 21:39 myproc
-rw-rw-r-- 1 gsm gsm 130 Oct 22 21:35 myproc.c
[gsm@VM-4-3-centos lesson10]$ make clean
rm -f myproc
[gsm@VM-4-3-centos lesson10]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rw-rw-r-- 1 gsm gsm 130 Oct 22 21:35 myproc.c
[gsm@VM-4-3-centos lesson10]$ man getpid
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t id = getpid();
while(1)
{
printf("hello bit!, I am a process, pid:%d\n", id);
sleep(1);
}
}
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8464 Oct 22 22:19 myproc
-rw-rw-r-- 1 gsm gsm 206 Oct 22 22:19 myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc
c
[gsm@VM-4-3-centos ~]$ ps ajx | head -1 && ps ajx | grep myproc
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
17844 29079 29079 17844 pts/0 29079 S+ 1001 0:00 ./myproc
29105 29358 29357 29105 pts/1 29357 S+ 1001 0:00 grep --color=auto myproc
[gsm@VM-4-3-centos ~]$ 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
[gsm@VM-4-3-centos ~]$ kill -9 29079
c
[gsm@VM-4-3-centos ~]$ ls /proc
1 11 1287 1598 17548 19 24 264 29 292 35 417 6 705 978 cpuinfo fb kallsyms loadavg mtrr self sysvipc vmstat
10 1111 1299 16 17587 2 25 26427 29048 298 36 46 65 709 acpi crypto filesystems kcore locks net slabinfo timer_list xpmem
10355 1112 13 1605 17822 20 25273 265 29093 30159 37 48 675 710 buddyinfo devices fs keys mdstat pagetypeinfo softirqs timer_stats zoneinfo
1044 1139 14 1606 17843 200 259 267 291 30984 38 49 697 8 bus diskstats interrupts key-users meminfo partitions stat tty
1046 1140 1524 1646 17844 21 26 27 29104 31183 388 50 698 9 cgroups dma iomem kmsg misc sched_debug swaps uptime
1048 12 1525 17446 18 22 262 276 29105 31262 4 51 7 9411 cmdline driver ioports kpagecount modules schedstat sys version
108 1236 1568 17454 1842 23 263 28 29182 31603 416 511 700 9687 consoles execdomains irq kpageflags mounts scsi sysrq-trigger vmallocinfo
[gsm@VM-4-3-centos ~]$ ls /
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin share_112 srv sys tmp usr var
[gsm@VM-4-3-centos ~]$ ls / -l
total 76
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Jul 8 2024 boot
drwxr-xr-x 2 root root 4096 Nov 5 2019 data
drwxr-xr-x 19 root root 3020 Oct 5 15:16 dev
drwxr-xr-x. 95 root root 12288 Oct 11 21:06 etc
drwxr-xr-x. 4 root root 4096 Oct 9 15:32 home
lrwxrwxrwx. 1 root root 7 Mar 7 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 7 2019 lib64 -> usr/lib64
drwx------. 2 root root 16384 Mar 7 2019 lost+found
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x. 4 root root 4096 Nov 12 2024 opt
dr-xr-xr-x 111 root root 0 Oct 5 15:16 proc
dr-xr-x---. 8 root root 4096 Oct 11 21:06 root
drwxr-xr-x 25 root root 920 Oct 21 09:33 run
lrwxrwxrwx. 1 root root 8 Mar 7 2019 sbin -> usr/sbin
drwxr-xrwt 2 root root 4096 Oct 9 22:18 share_112
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Oct 9 21:54 sys
drwxrwxrwt. 11 root root 4096 Oct 22 22:19 tmp
drwxr-xr-x. 14 root root 4096 Jan 8 2021 usr
drwxr-xr-x. 20 root root 4096 Jan 8 2021 var
[gsm@VM-4-3-centos ~]$ ll /proc
total 0
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1
dr-xr-xr-x 9 root root 0 Oct 5 15:16 10
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1044
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1046
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1048
dr-xr-xr-x 9 root root 0 Oct 5 15:16 108
dr-xr-xr-x 9 root root 0 Oct 5 15:16 11
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1111
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1112
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1139
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1140
dr-xr-xr-x 9 root root 0 Oct 5 15:16 12
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1236
dr-xr-xr-x 9 postfix postfix 0 Oct 5 15:16 1287
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1299
dr-xr-xr-x 9 root root 0 Oct 5 15:16 13
dr-xr-xr-x 9 root root 0 Oct 5 15:16 14
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1524
dr-xr-xr-x 9 root root 0 Oct 5 15:16 1525
dr-xr-xr-x 9 root root 0 Oct 5 15:17 1568
dr-xr-xr-x 9 root root 0 Oct 5 15:17 1598
dr-xr-xr-x 9 root root 0 Oct 5 15:16 16
dr-xr-xr-x 9 root root 0 Oct 5 15:17 1605
dr-xr-xr-x 9 root root 0 Oct 5 15:17 1606
dr-xr-xr-x 9 root root 0 Oct 5 15:17 1646
dr-xr-xr-x 9 gsm gsm 0 Oct 11 20:23 17446
dr-xr-xr-x 9 gsm gsm 0 Oct 11 20:23 17454
dr-xr-xr-x 9 gsm gsm 0 Oct 11 20:23 17548
dr-xr-xr-x 9 gsm gsm 0 Oct 11 20:23 17587
dr-xr-xr-x 9 root root 0 Oct 22 21:29 17822
dr-xr-xr-x 9 gsm gsm 0 Oct 22 21:29 17843
dr-xr-xr-x 9 gsm gsm 0 Oct 22 21:29 17844
dr-xr-xr-x 9 root root 0 Oct 5 15:16 18
dr-xr-xr-x 9 root root 0 Oct 5 15:17 1842
dr-xr-xr-x 9 root root 0 Oct 5 15:16 19
dr-xr-xr-x 9 root root 0 Oct 5 15:16 2
dr-xr-xr-x 9 root root 0 Oct 5 15:16 20
dr-xr-xr-x 9 root root 0 Oct 5 15:16 200
dr-xr-xr-x 9 root root 0 Oct 5 15:16 21
dr-xr-xr-x 9 root root 0 Oct 5 15:16 22
dr-xr-xr-x 9 root root 0 Oct 5 15:16 23
dr-xr-xr-x 9 root root 0 Oct 5 15:16 24
dr-xr-xr-x 9 root root 0 Oct 5 15:16 25
dr-xr-xr-x 9 root root 0 Oct 22 22:03 25273
dr-xr-xr-x 9 root root 0 Oct 5 15:16 259
dr-xr-xr-x 9 root root 0 Oct 5 15:16 26
dr-xr-xr-x 9 root root 0 Oct 5 15:16 262
dr-xr-xr-x 9 root root 0 Oct 5 15:16 263
dr-xr-xr-x 9 root root 0 Oct 5 15:16 264
dr-xr-xr-x 9 root root 0 Oct 20 17:24 26427
dr-xr-xr-x 9 root root 0 Oct 5 15:16 265
dr-xr-xr-x 9 root root 0 Oct 5 15:16 267
dr-xr-xr-x 9 root root 0 Oct 5 15:16 27
dr-xr-xr-x 9 root root 0 Oct 5 15:16 276
dr-xr-xr-x 9 root root 0 Oct 5 15:16 28
dr-xr-xr-x 9 root root 0 Oct 5 15:16 29
dr-xr-xr-x 9 root root 0 Oct 22 22:19 29048
dr-xr-xr-x 9 root root 0 Oct 22 22:19 29093
dr-xr-xr-x 9 root root 0 Oct 5 15:16 291
dr-xr-xr-x 9 gsm gsm 0 Oct 22 22:19 29104
dr-xr-xr-x 9 gsm gsm 0 Oct 22 22:19 29105
dr-xr-xr-x 9 root root 0 Oct 22 22:20 29182
dr-xr-xr-x 9 root root 0 Oct 5 15:16 292
dr-xr-xr-x 9 root root 0 Oct 5 15:16 298
dr-xr-xr-x 9 root root 0 Oct 22 22:24 30159
dr-xr-xr-x 9 gsm gsm 0 Oct 22 22:28 30984
dr-xr-xr-x 9 root root 0 Oct 22 22:29 31183
dr-xr-xr-x 9 root root 0 Oct 22 22:30 31262
dr-xr-xr-x 9 postfix postfix 0 Oct 22 22:33 31924
dr-xr-xr-x 9 gsm gsm 0 Oct 22 22:33 31943
dr-xr-xr-x 9 root root 0 Oct 5 15:16 35
dr-xr-xr-x 9 root root 0 Oct 5 15:16 36
dr-xr-xr-x 9 root root 0 Oct 5 15:16 37
dr-xr-xr-x 9 root root 0 Oct 5 15:16 38
dr-xr-xr-x 9 root root 0 Oct 5 15:16 388
dr-xr-xr-x 9 root root 0 Oct 5 15:16 4
dr-xr-xr-x 9 root root 0 Oct 5 15:16 416
dr-xr-xr-x 9 root root 0 Oct 5 15:16 417
dr-xr-xr-x 9 root root 0 Oct 5 15:16 46
dr-xr-xr-x 9 root root 0 Oct 5 15:16 48
dr-xr-xr-x 9 root root 0 Oct 5 15:16 49
dr-xr-xr-x 9 root root 0 Oct 5 15:16 50
dr-xr-xr-x 9 root root 0 Oct 5 15:16 51
dr-xr-xr-x 9 root root 0 Oct 5 15:16 511
dr-xr-xr-x 9 root root 0 Oct 5 15:16 6
dr-xr-xr-x 9 root root 0 Oct 5 15:16 65
dr-xr-xr-x 9 root root 0 Oct 5 15:16 675
dr-xr-xr-x 9 root root 0 Oct 5 15:16 697
dr-xr-xr-x 9 dbus dbus 0 Oct 5 15:16 698
dr-xr-xr-x 9 root root 0 Oct 5 15:16 7
dr-xr-xr-x 9 libstoragemgmt libstoragemgmt 0 Oct 5 15:16 700
dr-xr-xr-x 9 polkitd polkitd 0 Oct 5 15:16 705
dr-xr-xr-x 9 root root 0 Oct 5 15:16 709
dr-xr-xr-x 9 ntp ntp 0 Oct 5 15:16 710
dr-xr-xr-x 9 root root 0 Oct 5 15:16 8
dr-xr-xr-x 9 root root 0 Oct 5 15:16 9
dr-xr-xr-x 9 root root 0 Oct 11 20:21 9411
dr-xr-xr-x 9 root root 0 Oct 22 20:50 9687
dr-xr-xr-x 9 root root 0 Oct 5 15:16 978
dr-xr-xr-x 2 root root 0 Oct 22 22:33 acpi
-r--r--r-- 1 root root 0 Oct 22 22:33 buddyinfo
dr-xr-xr-x 4 root root 0 Oct 22 22:33 bus
-r--r--r-- 1 root root 0 Oct 22 22:33 cgroups
-r--r--r-- 1 root root 0 Oct 22 22:33 cmdline
-r--r--r-- 1 root root 0 Oct 22 22:33 consoles
-r--r--r-- 1 root root 0 Oct 22 22:33 cpuinfo
-r--r--r-- 1 root root 0 Oct 22 22:33 crypto
-r--r--r-- 1 root root 0 Oct 22 22:33 devices
-r--r--r-- 1 root root 0 Oct 22 22:33 diskstats
-r--r--r-- 1 root root 0 Oct 22 22:33 dma
dr-xr-xr-x 2 root root 0 Oct 22 22:33 driver
-r--r--r-- 1 root root 0 Oct 22 22:33 execdomains
-r--r--r-- 1 root root 0 Oct 22 22:33 fb
-r--r--r-- 1 root root 0 Oct 22 22:33 filesystems
dr-xr-xr-x 5 root root 0 Oct 22 22:33 fs
-r--r--r-- 1 root root 0 Oct 22 22:33 interrupts
-r--r--r-- 1 root root 0 Oct 22 22:33 iomem
-r--r--r-- 1 root root 0 Oct 22 22:33 ioports
dr-xr-xr-x 25 root root 0 Oct 22 22:33 irq
-r--r--r-- 1 root root 0 Oct 22 22:33 kallsyms
-r-------- 1 root root 140737486266368 Oct 22 22:33 kcore
-r--r--r-- 1 root root 0 Oct 22 22:33 keys
-r--r--r-- 1 root root 0 Oct 22 22:33 key-users
-r-------- 1 root root 0 Oct 22 22:33 kmsg
-r-------- 1 root root 0 Oct 22 22:33 kpagecount
-r-------- 1 root root 0 Oct 22 22:33 kpageflags
-r--r--r-- 1 root root 0 Oct 22 22:33 loadavg
-r--r--r-- 1 root root 0 Oct 22 22:33 locks
-r--r--r-- 1 root root 0 Oct 22 22:33 mdstat
-r--r--r-- 1 root root 0 Oct 22 22:33 meminfo
-r--r--r-- 1 root root 0 Oct 22 22:33 misc
-r--r--r-- 1 root root 0 Oct 22 22:33 modules
lrwxrwxrwx 1 root root 11 Oct 22 22:33 mounts -> self/mounts
-rw-r--r-- 1 root root 0 Oct 22 22:33 mtrr
lrwxrwxrwx 1 root root 8 Oct 22 22:33 net -> self/net
-r-------- 1 root root 0 Oct 22 22:33 pagetypeinfo
-r--r--r-- 1 root root 0 Oct 22 22:33 partitions
-r--r--r-- 1 root root 0 Oct 22 22:33 sched_debug
-r--r--r-- 1 root root 0 Oct 22 22:33 schedstat
dr-xr-xr-x 3 root root 0 Oct 22 22:33 scsi
lrwxrwxrwx 1 root root 0 Oct 5 15:16 self -> 31943
-r-------- 1 root root 0 Oct 22 22:33 slabinfo
-r--r--r-- 1 root root 0 Oct 22 22:33 softirqs
-r--r--r-- 1 root root 0 Oct 22 22:33 stat
-r--r--r-- 1 root root 0 Oct 5 15:16 swaps
dr-xr-xr-x 1 root root 0 Oct 5 15:16 sys
--w------- 1 root root 0 Oct 22 22:33 sysrq-trigger
dr-xr-xr-x 2 root root 0 Oct 22 22:33 sysvipc
-r--r--r-- 1 root root 0 Oct 22 22:33 timer_list
-rw-r--r-- 1 root root 0 Oct 22 22:33 timer_stats
dr-xr-xr-x 4 root root 0 Oct 22 22:33 tty
-r--r--r-- 1 root root 0 Oct 22 22:33 uptime
-r--r--r-- 1 root root 0 Oct 22 22:33 version
-r-------- 1 root root 0 Oct 22 22:33 vmallocinfo
-r--r--r-- 1 root root 0 Oct 22 22:33 vmstat
dr-xr-xr-x 2 root root 0 Oct 22 22:33 xpmem
-r--r--r-- 1 root root 0 Oct 22 22:33 zoneinfo
[gsm@VM-4-3-centos ~]$ ls /proc/30984 -ld
dr-xr-xr-x 9 gsm gsm 0 Oct 22 22:28 /proc/30984
//结束进程后
[gsm@VM-4-3-centos ~]$ ls /proc/30984 -ld
ls: cannot access /proc/30984: No such file or directory
[gsm@VM-4-3-centos ~]$ ls /proc
1 1111 1299 16 17587 2 25 26427 29093 31183 388 50 698 8 acpi crypto filesystems kcore locks net slabinfo timer_list xpmem
10 1112 13 1605 17822 20 25273 265 291 31924 4 51 7 865 buddyinfo devices fs keys mdstat pagetypeinfo softirqs timer_stats zoneinfo
1044 1139 14 1606 17843 200 259 267 29104 32193 416 511 700 9 bus diskstats interrupts key-users meminfo partitions stat tty
1046 1140 1524 1646 17844 21 26 27 29105 35 417 6 705 913 cgroups dma iomem kmsg misc sched_debug swaps uptime
1048 12 1525 17446 18 22 262 276 29182 36 46 65 709 9411 cmdline driver ioports kpagecount modules schedstat sys version
108 1236 1568 17454 1842 23 263 28 292 37 48 675 710 9687 consoles execdomains irq kpageflags mounts scsi sysrq-trigger vmallocinfo
11 1287 1598 17548 19 24 264 29 298 38 49 697 772 978 cpuinfo fb kallsyms loadavg mtrr self sysvipc vmstat
c
//程序运行起来后删除
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8464 Oct 22 22:19 myproc
-rw-rw-r-- 1 gsm gsm 206 Oct 22 22:19 myproc.c
[gsm@VM-4-3-centos lesson10]$ make clean
rm -f myproc
[gsm@VM-4-3-centos lesson10]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rw-rw-r-- 1 gsm gsm 206 Oct 22 22:19 myproc.c
c
//删除之前查
[gsm@VM-4-3-centos ~]$ ls /proc/2847 -l
total 0
dr-xr-xr-x 2 gsm gsm 0 Oct 22 22:50 attr
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 autogroup
-r-------- 1 gsm gsm 0 Oct 22 22:50 auxv
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 cgroup
--w------- 1 gsm gsm 0 Oct 22 22:50 clear_refs
-r--r--r-- 1 gsm gsm 0 Oct 22 22:49 cmdline
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 comm
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 coredump_filter
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 cpuset
lrwxrwxrwx 1 gsm gsm 0 Oct 22 22:49 cwd -> /home/gsm/linux/112/lesson10
-r-------- 1 gsm gsm 0 Oct 22 22:49 environ
lrwxrwxrwx 1 gsm gsm 0 Oct 22 22:49 exe -> /home/gsm/linux/112/lesson10/myproc
dr-x------ 2 gsm gsm 0 Oct 22 22:50 fd
dr-x------ 2 gsm gsm 0 Oct 22 22:50 fdinfo
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 gid_map
-r-------- 1 gsm gsm 0 Oct 22 22:50 io
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 limits
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 loginuid
dr-x------ 2 gsm gsm 0 Oct 22 22:50 map_files
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 maps
-rw------- 1 gsm gsm 0 Oct 22 22:50 mem
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 mountinfo
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 mounts
-r-------- 1 gsm gsm 0 Oct 22 22:50 mountstats
dr-xr-xr-x 5 gsm gsm 0 Oct 22 22:50 net
dr-x--x--x 2 gsm gsm 0 Oct 22 22:50 ns
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 numa_maps
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 oom_adj
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 oom_score
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 oom_score_adj
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 pagemap
-r-------- 1 gsm gsm 0 Oct 22 22:50 patch_state
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 personality
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 projid_map
lrwxrwxrwx 1 gsm gsm 0 Oct 22 22:49 root -> /
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 sched
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 schedstat
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 sessionid
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 setgroups
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 smaps
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 stack
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 stat
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 statm
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 status
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 syscall
dr-xr-xr-x 3 gsm gsm 0 Oct 22 22:49 task
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 timers
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 uid_map
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 wchan
//删除之后再查
[gsm@VM-4-3-centos ~]$ ls /proc/2847 -l
total 0
dr-xr-xr-x 2 gsm gsm 0 Oct 22 22:50 attr
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 autogroup
-r-------- 1 gsm gsm 0 Oct 22 22:50 auxv
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 cgroup
--w------- 1 gsm gsm 0 Oct 22 22:50 clear_refs
-r--r--r-- 1 gsm gsm 0 Oct 22 22:49 cmdline
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 comm
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 coredump_filter
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 cpuset
lrwxrwxrwx 1 gsm gsm 0 Oct 22 22:49 cwd -> /home/gsm/linux/112/lesson10
-r-------- 1 gsm gsm 0 Oct 22 22:49 environ
lrwxrwxrwx 1 gsm gsm 0 Oct 22 22:49 exe -> /home/gsm/linux/112/lesson10/myproc (deleted)
dr-x------ 2 gsm gsm 0 Oct 22 22:50 fd
dr-x------ 2 gsm gsm 0 Oct 22 22:50 fdinfo
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 gid_map
-r-------- 1 gsm gsm 0 Oct 22 22:50 io
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 limits
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 loginuid
dr-x------ 2 gsm gsm 0 Oct 22 22:50 map_files
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 maps
-rw------- 1 gsm gsm 0 Oct 22 22:50 mem
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 mountinfo
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 mounts
-r-------- 1 gsm gsm 0 Oct 22 22:50 mountstats
dr-xr-xr-x 5 gsm gsm 0 Oct 22 22:50 net
dr-x--x--x 2 gsm gsm 0 Oct 22 22:50 ns
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 numa_maps
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 oom_adj
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 oom_score
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 oom_score_adj
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 pagemap
-r-------- 1 gsm gsm 0 Oct 22 22:50 patch_state
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 personality
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 projid_map
lrwxrwxrwx 1 gsm gsm 0 Oct 22 22:49 root -> /
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 sched
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 schedstat
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 sessionid
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 setgroups
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 smaps
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 stack
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 stat
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 statm
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 status
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 syscall
dr-xr-xr-x 3 gsm gsm 0 Oct 22 22:49 task
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 timers
-rw-r--r-- 1 gsm gsm 0 Oct 22 22:50 uid_map
-r--r--r-- 1 gsm gsm 0 Oct 22 22:50 wchan
c
[gsm@VM-4-3-centos lesson10]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rw-rw-r-- 1 gsm gsm 206 Oct 22 22:19 myproc.c
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
FILE* fp = fopen("log.txt", "w");
if (fp == NULL)
{}
pid_t id = getpid();
while(1)
{
printf("hello bit!, I am a process, pid:%d\n", id);
sleep(1);
}
}
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc
hello bit!, I am a process, pid:7824
^C
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 0 Oct 23 20:22 log.txt
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8512 Oct 23 20:21 myproc
-rw-rw-r-- 1 gsm gsm 272 Oct 23 20:21 myproc.c
[gsm@VM-4-3-centos lesson10]$ pwd
/home/gsm/linux/112/lesson10
[gsm@VM-4-3-centos lesson10]$ ls
log.txt Makefile myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ rm log.txt
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8512 Oct 23 20:21 myproc
-rw-rw-r-- 1 gsm gsm 272 Oct 23 20:21 myproc.c
[gsm@VM-4-3-centos lesson10]$ man chdir
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
chdir("/");
FILE* fp = fopen("log.txt", "w");
if (fp == NULL)
{
perror("fopen");
return 1;
}
pid_t id = getpid();
while(1)
{
printf("hello bit!, I am a process, pid:%d\n", id);
sleep(1);
}
}
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc
fopen: Permission denied
[gsm@VM-4-3-centos lesson10]$ ls -ld /
dr-xr-xr-x. 20 root root 4096 Oct 23 20:45 /
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
chdir("/home/gsm");
FILE* fp = fopen("log.txt", "w");
if (fp == NULL)
{
perror("fopen");
return 1;
}
pid_t id = getpid();
while(1)
{
printf("hello bit!, I am a process, pid:%d\n", id);
sleep(1);
}
}
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc
c
[gsm@VM-4-3-centos ~]$ ls /proc/14428 -l
total 0
dr-xr-xr-x 2 gsm gsm 0 Oct 23 20:52 attr
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 autogroup
-r-------- 1 gsm gsm 0 Oct 23 20:52 auxv
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 cgroup
--w------- 1 gsm gsm 0 Oct 23 20:52 clear_refs
-r--r--r-- 1 gsm gsm 0 Oct 23 20:51 cmdline
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 comm
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 coredump_filter
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 cpuset
lrwxrwxrwx 1 gsm gsm 0 Oct 23 20:51 cwd -> /home/gsm
-r-------- 1 gsm gsm 0 Oct 23 20:52 environ
lrwxrwxrwx 1 gsm gsm 0 Oct 23 20:51 exe -> /home/gsm/linux/112/lesson10/myproc
dr-x------ 2 gsm gsm 0 Oct 23 20:52 fd
dr-x------ 2 gsm gsm 0 Oct 23 20:52 fdinfo
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 gid_map
-r-------- 1 gsm gsm 0 Oct 23 20:52 io
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 limits
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 loginuid
dr-x------ 2 gsm gsm 0 Oct 23 20:52 map_files
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 maps
-rw------- 1 gsm gsm 0 Oct 23 20:52 mem
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 mountinfo
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 mounts
-r-------- 1 gsm gsm 0 Oct 23 20:52 mountstats
dr-xr-xr-x 5 gsm gsm 0 Oct 23 20:52 net
dr-x--x--x 2 gsm gsm 0 Oct 23 20:52 ns
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 numa_maps
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 oom_adj
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 oom_score
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 oom_score_adj
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 pagemap
-r-------- 1 gsm gsm 0 Oct 23 20:52 patch_state
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 personality
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 projid_map
lrwxrwxrwx 1 gsm gsm 0 Oct 23 20:51 root -> /
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 sched
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 schedstat
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 sessionid
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 setgroups
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 smaps
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 stack
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 stat
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 statm
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 status
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 syscall
dr-xr-xr-x 3 gsm gsm 0 Oct 23 20:52 task
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 timers
-rw-r--r-- 1 gsm gsm 0 Oct 23 20:52 uid_map
-r--r--r-- 1 gsm gsm 0 Oct 23 20:52 wchan
[gsm@VM-4-3-centos ~]$ ls /home/gsm
install.sh linux log.txt test.c

c
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8616 Oct 23 20:51 myproc
-rw-rw-r-- 1 gsm gsm 359 Oct 23 21:19 myproc.c
[gsm@VM-4-3-centos lesson10]$ man getppid
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
//chdir("/home/gsm");
//FILE* fp = fopen("log.txt", "w");
//if (fp == NULL)
//{
// perror("fopen");
// return 1;
//}
pid_t ppid = getppid();
pid_t id = getpid();
while(1)
{
printf("hello bit!, I am a process, pid:%d, ppid:%d\n", id, ppid);
sleep(1);
}
}
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8520 Oct 23 21:23 myproc
-rw-rw-r-- 1 gsm gsm 402 Oct 23 21:22 myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc
hello bit!, I am a process, pid:21328, ppid:6205
hello bit!, I am a process, pid:21328, ppid:6205
^C
[gsm@VM-4-3-centos lesson10]$ ./myproc
hello bit!, I am a process, pid:21364, ppid:6205
hello bit!, I am a process, pid:21364, ppid:6205
^C
[gsm@VM-4-3-centos lesson10]$ ./myproc
hello bit!, I am a process, pid:21370, ppid:6205
hello bit!, I am a process, pid:21370, ppid:6205
^C
[gsm@VM-4-3-centos lesson10]$ ./myproc
hello bit!, I am a process, pid:21400, ppid:6205
hello bit!, I am a process, pid:21400, ppid:6205
^C
[gsm@VM-4-3-centos lesson10]$ ps ajx | head -1 && ps ajx | grep 6205
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
6204 6205 6205 6205 pts/0 21970 Ss 1001 0:00 -bash
6205 21970 21970 6205 pts/0 21970 R+ 1001 0:00 ps ajx
6205 21971 21970 6205 pts/0 21970 S+ 1001 0:00 grep --color=auto 6205
//这里有两个bash是因为登录了两个Xshell
[gsm@VM-4-3-centos lesson10]$ ps ajx | grep "\-bash"
6204 6205 6205 6205 pts/0 24675 Ss 1001 0:00 -bash
11573 11574 11574 11574 pts/1 11574 Ss+ 1001 0:00 -bash
6205 24676 24675 6205 pts/0 24675 S+ 1001 0:00 grep --color=auto \-bash

c
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8520 Oct 23 21:23 myproc
-rw-rw-r-- 1 gsm gsm 402 Oct 23 21:22 myproc.c
[gsm@VM-4-3-centos lesson10]$ man fork
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
printf("I am a process, pid:%d, ppid:%d\n", getpid(), getppid());
pid_t id = fork();
(void)id;
printf("I am a 分支!pid: %d, ppid: %d\n", getpid(), getppid());
sleep(1);
//chdir("/home/gsm");
//FILE* fp = fopen("log.txt", "w");
//if (fp == NULL)
//{
// perror("fopen");
// return 1;
//}
//pid_t ppid = getppid();
//pid_t id = getpid();
//while(1)
//{
// printf("hello bit!, I am a process, pid:%d, ppid:%d\n", id, ppid);
// sleep(1);
//}
}
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc
I am a process, pid:15103, ppid:7498
I am a 分支!pid: 15103, ppid: 7498
I am a 分支!pid: 15104, ppid: 15103
[gsm@VM-4-3-centos lesson10]$ ./myproc
I am a process, pid:15175, ppid:7498
I am a 分支!pid: 15175, ppid: 7498
I am a 分支!pid: 15176, ppid: 15175
[gsm@VM-4-3-centos lesson10]$ ./myproc
I am a process, pid:15217, ppid:7498
I am a 分支!pid: 15217, ppid: 7498
I am a 分支!pid: 15218, ppid: 15217
[gsm@VM-4-3-centos lesson10]$ ps ajx | grep 7498
7497 7498 7498 7498 pts/0 16575 Ss 1001 0:00 -bash
7498 16575 16575 7498 pts/0 16575 R+ 1001 0:00 ps ajx
7498 16576 16575 7498 pts/0 16575 S+ 1001 0:00 grep --color=auto 7498

c
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8568 Oct 24 20:40 myproc
-rw-rw-r-- 1 gsm gsm 611 Oct 24 20:40 myproc.c
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
printf("I am a process, pid:%d, ppid:%d\n", getpid(), getppid());
pid_t id = fork();
if(id > 0)
{
while(1)
{
printf("我是父进程,pid: %d, ppid: %d, ret id: %d\n", getpid(), getppid(), id);
sleep(1);
}
}
else if(id == 0)
{
while(1)
{
printf("我是子进程,pid: %d, ppid: %d, ret id: %d\n", getpid(), getppid(), id);
sleep(1);
}
}
//(void)id;
//printf("I am a 分支!pid: %d, ppid: %d\n", getpid(), getppid());
//sleep(1);
//chdir("/home/gsm");
//FILE* fp = fopen("log.txt", "w");
//if (fp == NULL)
//{
// perror("fopen");
// return 1;
//}
//pid_t ppid = getppid();
//pid_t id = getpid();
//while(1)
//{
// printf("hello bit!, I am a process, pid:%d, ppid:%d\n", id, ppid);
// sleep(1);
//}
}
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc
c
[gsm@VM-4-3-centos ~]$ ps ajx | head -1 && ps ajx | grep myproc
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
7498 23944 23944 7498 pts/0 23944 S+ 1001 0:00 ./myproc
23944 23945 23944 7498 pts/0 23944 S+ 1001 0:00 ./myproc
23859 24317 24316 23859 pts/1 24316 S+ 1001 0:00 grep --color=auto myproc
c
[gsm@VM-4-3-centos lesson10]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
-rwxrwxr-x 1 gsm gsm 8568 Oct 24 21:21 myproc
-rw-rw-r-- 1 gsm gsm 1001 Oct 24 21:20 myproc.c
[gsm@VM-4-3-centos lesson10]$ vim myproc.c
[gsm@VM-4-3-centos lesson10]$ cat myproc.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int gval = 0;
int main()
{
printf("I am a process, pid:%d, ppid:%d\n", getpid(), getppid());
pid_t id = fork();
if(id > 0)
{
while(1)
{
printf("我是父进程,pid: %d, ppid: %d, ret id: %d, gval: %d\n", getpid(), getppid(), id, gval);
sleep(1);
}
}
else if(id == 0)
{
while(1)
{
printf("我是子进程,pid: %d, ppid: %d, ret id: %d, gval: %d\n", getpid(), getppid(), id, gval);
gval++;
sleep(1);
}
}
//(void)id;
//printf("I am a 分支!pid: %d, ppid: %d\n", getpid(), getppid());
//sleep(1);
//chdir("/home/gsm");
//FILE* fp = fopen("log.txt", "w");
//if (fp == NULL)
//{
// perror("fopen");
// return 1;
//}
//pid_t ppid = getppid();
//pid_t id = getpid();
//while(1)
//{
// printf("hello bit!, I am a process, pid:%d, ppid:%d\n", id, ppid);
// sleep(1);
//}
}
[gsm@VM-4-3-centos lesson10]$ make
gcc -o myproc myproc.c
[gsm@VM-4-3-centos lesson10]$ ./myproc

g++安装命令:
sudo yum install -y gcc-c++
c
[gsm@VM-4-3-centos lesson10]$ mkdir multprocess
[gsm@VM-4-3-centos lesson10]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 64 Oct 22 21:39 Makefile
drwxrwxr-x 2 gsm gsm 4096 Oct 24 22:15 multprocess
-rw-rw-r-- 1 gsm gsm 1121 Oct 24 22:14 myproc.c
[gsm@VM-4-3-centos lesson10]$ cd multprocess/
[gsm@VM-4-3-centos multprocess]$ touch myprocess.cc
[gsm@VM-4-3-centos multprocess]$ cp ../Makefile .
[gsm@VM-4-3-centos multprocess]$ vim Makefile
[gsm@VM-4-3-centos multprocess]$ cat Makefile
myprocess:myprocess.cc
g++ -o $@ $^ -std=c++11
.PHONY:clean
clean:
rm -f myprocess
[gsm@VM-4-3-centos multprocess]$ vim myprocess.cc
[gsm@VM-4-3-centos multprocess]$ cat myprocess.cc
#include <iostream>
#include <vector>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
const int num = 10;
void SubProcessRun()
{
while (true)
{
cout << "I am sub process, pid: " << getpid() << ", ppid: "<< getppid() << endl;
sleep(5);
}
}
int main()
{
vector<pid_t> allchild;
for (int i = 0; i < num; i++)
{
pid_t id = fork();
if (id == 0)
{
// 子进程
SubProcessRun();
}
// 这里谁执行?父进程一个人
allchild.push_back(id);
}
// 父进程
cout << "我的所有的孩子是:";
for (auto child : allchild)
{
cout << child << " ";
}
cout << endl;
sleep(10);
while (true)
{
cout << "我是父进程, pid: " << getpid() << endl;
sleep(1);
}
return 0;
}
[gsm@VM-4-3-centos multprocess]$ make
g++ -o myprocess myprocess.cc -std=c++11
[gsm@VM-4-3-centos multprocess]$ ./myprocess
c
[gsm@VM-4-3-centos ~]$ ps ajx | grep myprocess
7498 12379 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12380 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12381 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12382 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12383 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12384 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12385 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12386 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12387 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12388 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
12379 12389 12379 7498 pts/0 12379 S+ 1001 0:00 ./myprocess
23859 12559 12558 23859 pts/1 12558 S+ 1001 0:00 grep --color=auto myprocess
