Chapter 12: System and Process Information
==========================================
Exercise 12-1
-------------
**Question**
Write a program that lists the porcess ID and command name for all
processes being run by the user named in the program's command-line
argument. (You may find the userIdFromName() function from Listing
8-1, on page 159, useful.) This can be done by inspecting the Name:
and Uid: lines of all of the /proc/PID/status files on the system.
Walking through all of the /proc/PID directories on the system
requires the use of readdir(3), which is described in Section 18.8.
Make sure your porgram correctly handles the possibility that a
/proc/PID directory disappears between the time that the porgram
determines that the directory exists and the time that it tries to
open the corresponding /proc/PID/status file.
**Answer**
See proclist.c. Example Output:
```
[posborne@pobox-lin:~/Projects/linux-programming-interface-exercises/ch12 git:master] $ ./prog_proclist posborne
22548: prog_proclist
1105: chrome
1115: chrome
1325: chrome
1658: mate-session
1693: ssh-agent
1696: dbus-launch
1697: dbus-daemon
1704: mateconfd-2
1708: mate-keyring-da
1716: mate-settings-d
1720: gvfsd
1722: gvfs-fuse-daemo
1733: gvfs-gdu-volume
1743: gvfs-afc-volume
1746: gvfs-gphoto2-vo
1751: mate-panel
1754: caja
1758: mate-power-mana
1759: deja-dup-monito
1762: zeitgeist-datah
1767: matecomponent-a
1769: mate-bluetooth-
1775: polkit-mate-aut
1776: nm-applet
1787: zeitgeist-daemo
1790: mate-settings-d
1791: gnome-do
1793: mate-settings-d
1798: update-notifier
1805: zeitgeist-fts
1813: applet.py
1820: wnck-applet
1822: pulseaudio
1830: mate-screensave
1833: clock-applet
1835: multiload-apple
1837: notification-ar
1855: gnome-do
2023: cat
2026: gvfsd-trash
2033: gconfd-2
2054: gconf-helper
2085: gvfsd-metadata
2105: chrome
2112: chrome
2113: chrome-sandbox
2114: chrome
2116: nacl_helper_boo
2117: chrome
2153: chrome
2204: chrome
2217: chrome
2316: at-spi-bus-laun
2592: mixer_applet2
2704: chrome
2811: chrome
2835: chrome
2845: chrome
2869: chrome
2892: chrome
2908: chrome
2932: emacs
2936: chrome
2949: chrome
2961: chrome
2968: chrome
2989: chrome
3202: notify-osd
3210: dconf-service
7241: chrome
7833: chrome
10392: eclipse
10463: java
12447: chrome
14552: firefox
14587: plugin-containe
14721: mate-terminal
14727: mate-terminal
14728: bash
19678: marco
19691: chrome
19794: chrome
19898: chrome
19909: chrome
19939: chrome
19960: chrome
19979: chrome
20035: chrome
20107: chrome
20519: chrome
20706: emacsclient
22548: prog_proclist
```
Exercise 12-2
-------------
**Question**
Write a program that draws a tree showing the hierarchical
parent-child relationships of all processes on the system, going all
the way back to init. For each process, the program should display
the process ID and the command being executed. The output of the
program should be similar to that produced by pstree(1), although it
does need not to be as sophisticated. The parent of each process on
the system can be found by inspecing the PPid: line of all of the
/proc/PID/status files on the system. Be careful to handle the
possibilty that a process's parent (and thus its /proc/PID directory)
disappears during the scan of all /proc/PID directories.
**Answer**
See pstree.c. Example output:
```
[posborne@pobox-lin:~/Projects/linux-programming-interface-exercises/ch12
git:master] $ ./prog_pstree
kthreadd (pid: 2, ppid: 0)
kworker/0 (pid: 23815, ppid: 2)
kworker/3 (pid: 23695, ppid: 2)
kworker/0 (pid: 23603, ppid: 2)
kworker/1 (pid: 23453, ppid: 2)
krfcommd (pid: 1054, ppid: 2)
flush-8 (pid: 944, ppid: 2)
hd-audio1 (pid: 805, ppid: 2)
hd-audio0 (pid: 801, ppid: 2)
edac-poller (pid: 746, ppid: 2)
ext4-dio-unwrit (pid: 312, ppid: 2)
jbd2/sdb5-8 (pid: 311, ppid: 2)
scsi_eh_5 (pid: 292, ppid: 2)
scsi_eh_4 (pid: 290, ppid: 2)
firewire (pid: 270, ppid: 2)
devfreq_wq (pid: 121, ppid: 2)
kworker/u (pid: 92, ppid: 2)
kworker/u (pid: 90, ppid: 2)
scsi_eh_3 (pid: 88, ppid: 2)
scsi_eh_2 (pid: 87, ppid: 2)
scsi_eh_1 (pid: 86, ppid: 2)
scsi_eh_0 (pid: 85, ppid: 2)
kthrotld (pid: 77, ppid: 2)
crypto (pid: 69, ppid: 2)
ecryptfs-kthrea (pid: 68, ppid: 2)
fsnotify_mark (pid: 67, ppid: 2)
khugepaged (pid: 66, ppid: 2)
ksmd (pid: 65, ppid: 2)
kswapd0 (pid: 64, ppid: 2)
khungtaskd (pid: 63, ppid: 2)
md (pid: 32, ppid: 2)
khubd (pid: 31, ppid: 2)
ata_sff (pid: 30, ppid: 2)
kblockd (pid: 29, ppid: 2)
kintegrityd (pid: 28, ppid: 2)
bdi-default (pid: 27, ppid: 2)
sync_supers (pid: 26, ppid: 2)
netns (pid: 24, ppid: 2)
kdevtmpfs (pid: 23, ppid: 2)
khelper (pid: 22, ppid: 2)
cpuset (pid: 21, ppid: 2)
watchdog/3 (pid: 20, ppid: 2)
ksoftirqd/3 (pid: 19, ppid: 2)
kworker/3 (pid: 18, ppid: 2)
migration/3 (pid: 17, ppid: 2)
watchdog/2 (pid: 16, ppid: 2)
ksoftirqd/2 (pid: 15, ppid: 2)
migration/2 (pid: 13, ppid: 2)
watchdog/1 (pid: 12, ppid: 2)
ksoftirqd/1 (pid: 10, ppid: 2)
migration/1 (pid: 8, ppid: 2)
watchdog/0 (pid: 7, ppid: 2)
migration/0 (pid: 6, ppid: 2)
ksoftirqd/0 (pid: 3, ppid: 2)
init (pid: 1, ppid: 0)
mate-terminal (pid: 14721, ppid: 1)
mate-terminal (pid: 14727, ppid: 14721)
firefox (pid: 14552, ppid: 1)
eclipse (pid: 10392, ppid: 1)
dconf-service (pid: 3210, ppid: 1)
notify-osd (pid: 3202, ppid: 1)
emacs (pid: 2932, ppid: 1)
mixer_applet2 (pid: 2592, ppid: 1)
hald (pid: 2454, ppid: 1)
at-spi-bus-laun (pid: 2316, ppid: 1)
chrome (pid: 2105, ppid: 1)
chrome-sandbox (pid: 2113, ppid: 2105)
chrome (pid: 2112, ppid: 2105)
system-service- (pid: 2087, ppid: 1)
gvfsd-metadata (pid: 2085, ppid: 1)
SystemToolsBack (pid: 2070, ppid: 1)
system-tools-ba (pid: 2044, ppid: 1)
gconfd-2 (pid: 2033, ppid: 1)
gvfsd-trash (pid: 2026, ppid: 1)
notification-ar (pid: 1837, ppid: 1)
multiload-apple (pid: 1835, ppid: 1)
clock-applet (pid: 1833, ppid: 1)
mate-screensave (pid: 1830, ppid: 1)
upowerd (pid: 1829, ppid: 1)
rtkit-daemon (pid: 1824, ppid: 1)
pulseaudio (pid: 1822, ppid: 1)
wnck-applet (pid: 1820, ppid: 1)
zeitgeist-fts (pid: 1805, ppid: 1)
zeitgeist-daemo (pid: 1787, ppid: 1)
matecomponent-a (pid: 1767, ppid: 1)
gvfs-gphoto2-vo (pid: 1746, ppid: 1)
gvfs-afc-volume (pid: 1743, ppid: 1)
udisks-daemon (pid: 1735, ppid: 1)
gvfs-gdu-volume (pid: 1733, ppid: 1)
gvfs-fuse-daemo (pid: 1722, ppid: 1)
gvfsd (pid: 1720, ppid: 1)
mate-keyring-da (pid: 1708, ppid: 1)
mateconfd-2 (pid: 1704, ppid: 1)
dbus-daemon (pid: 1697, ppid: 1)
dbus-launch (pid: 1696, ppid: 1)
console-kit-dae (pid: 1583, ppid: 1)
accounts-daemon (pid: 1560, ppid: 1)
getty (pid: 1515, ppid: 1)
hddtemp (pid: 1280, ppid: 1)
whoopsie (pid: 1132, ppid: 1)
polkitd (pid: 1118, ppid: 1)
atd (pid: 1114, ppid: 1)
irqbalance (pid: 1112, ppid: 1)
cron (pid: 1111, ppid: 1)
lightdm (pid: 1102, ppid: 1)
lightdm (pid: 22010, ppid: 1102)
lightdm (pid: 21029, ppid: 1102)
lightdm (pid: 1555, ppid: 1102)
Xorg (pid: 1146, ppid: 1102)
avahi-daemon (pid: 1100, ppid: 1)
acpid (pid: 1099, ppid: 1)
getty (pid: 1094, ppid: 1)
getty (pid: 1092, ppid: 1)
getty (pid: 1091, ppid: 1)
colord (pid: 1089, ppid: 1)
getty (pid: 1075, ppid: 1)
NetworkManager (pid: 1070, ppid: 1)
dhclient (pid: 1148, ppid: 1070)
getty (pid: 1061, ppid: 1)
bluetoothd (pid: 1039, ppid: 1)
cupsd (pid: 1023, ppid: 1)
modem-manager (pid: 996, ppid: 1)
dbus-daemon (pid: 970, ppid: 1)
rsyslogd (pid: 923, ppid: 1)
mount.ntfs (pid: 870, ppid: 1)
mount.ntfs (pid: 833, ppid: 1)
upstart-socket- (pid: 824, ppid: 1)
udevd (pid: 515, ppid: 1)
udevd (pid: 1499, ppid: 515)
upstart-udev-br (pid: 50
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Linux/UNIX系统编程手册课后习题代码 (561个子文件)
fork_whos_on_first.count.awk 813B
longest_line.awk 77B
BUILDING 5KB
acl_update.c 14KB
chattr.c 11KB
po_malloc_free.c 8KB
i_fcntl_locking.c 7KB
demo_clone.c 7KB
inet_sockets.c 6KB
inet_sockets.c 6KB
execlp.c 6KB
vector_fileio.c 6KB
benchmarkwrites.c 6KB
t_mount.c 6KB
test_tty_functions.c 6KB
error_functions.c 6KB
myenviron.c 6KB
t_sigsuspend.c 5KB
check_password_caps.c 5KB
self_pipe.c 5KB
thread_multijoin.c 5KB
select_mq.c 5KB
is_seqnum_sv.c 5KB
scm_cred_recv.c 5KB
daemon_SIGHUP.c 5KB
scm_cred_send.c 5KB
setfattr.c 5KB
ptmr_sigev_thread.c 5KB
real_timer.c 5KB
svmsg_file_server.c 5KB
ttyname.c 5KB
scm_rights_recv.c 4KB
pstree.c 4KB
is_echo_v2_sv.c 4KB
svsem_op.c 4KB
acct_v3_view.c 4KB
acl_view.c 4KB
script.c 4KB
pty_fork.c 4KB
pty_fork.c 4KB
catch_rtsigs.c 4KB
chiflag.c 4KB
t_stat.c 4KB
dnotify.c 4KB
dup.c 4KB
unbuffer.c 4KB
epoll_input.c 4KB
job_mon.c 4KB
is_echo_sv.c 4KB
multi_SIGCHLD.c 4KB
demo_inotify.c 4KB
seek_io.c 4KB
procfs_user_exe.c 4KB
svsem_good_init.c 4KB
unix_sockets.c 4KB
unix_sockets.c 4KB
exercise3_tstat.c 4KB
acct_view.c 4KB
orphaned_pgrp_SIGHUP.c 4KB
mq_notify_sigwaitinfo.c 4KB
nftw_dir_tree.c 4KB
utmpx_login.c 4KB
fifo_seqnum_server.c 4KB
ptmr_sigev_signal.c 4KB
system.c 4KB
svshm_xfr_writer.c 4KB
scm_rights_send.c 4KB
popen_glob.c 3KB
get_num.c 3KB
create_pid_file.c 3KB
create_pid_file.c 3KB
t_clock_nanosleep.c 3KB
demo_sched_fifo.c 3KB
svshm_create.c 3KB
svmsg_file_client.c 3KB
prod_condvar.c 3KB
proclist.c 3KB
svsem_create.c 3KB
is_seqnum_v2_sv.c 3KB
change_case.c 3KB
t_select.c 3KB
ignore_pending_sig.c 3KB
memlock.c 3KB
t_clone.c 3KB
pipe_sync.c 3KB
thread_cleanup.c 3KB
is_seqnum_cl.c 3KB
tty_functions.c 3KB
tty_functions.c 3KB
sherlock.c 3KB
is_echo_cl.c 3KB
free_and_sbrk_7_1.c 3KB
poll_pipes.c 3KB
svmsg_create.c 3KB
demo_SIGFPE.c 3KB
sig_speed_sigsuspend.c 3KB
list_files_readdir_r.c 3KB
svmsg_receive.c 3KB
check_password.c 3KB
fork_sig_sync.c 3KB
共 561 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
- zgr9402142023-02-07资源质量不错,和资源描述一致,内容详细,对我很有用。
- 大桑树保安队2023-09-30资源质量不错,和资源描述一致,内容详细,对我很有用。
- Megan___2022-10-20总算找到了想要的资源,搞定遇到的大问题,赞赞赞!
- campuspuzzle2023-03-22资源很实用,内容详细,值得借鉴的内容很多,感谢分享。
weixin_39711936
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功