Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
160
|
第五章
當一個行程結束時,核心會清理它替行程建立的所有資源中不再使用的部分。這包括,
但不僅限於,已分配的記憶體、已開啟的檔案,以及 System V semaphore(旗號)。完
成清理工作後,核心會銷毀行程,以及通知父行程其子行程已死亡的事實。
應用程式可以直接叫用
_exit()
,但是此類行動通常沒有任何意義:多數應用程式會進
行正常結束所需實施的某些清理工作,例如出清
stdout
串流。然而,注意,fork 動作之
後,
vfork()
的用戶應該叫用
_exit()
而不是叫用
exit()
附帶一提,ISO C99 標準中加入了
_Exit()
函式,它的行為如同
_exit()
#include <stdlib.h>
void _Exit (int status);
終止行程的其他方法
結束一個程式的典型方法並非透過明確的系統呼叫,而是從程式的結尾離開。就 C
言的情況來說,這發生在從
main()
函式返回時。然而,這種「從結尾離開」(falling
off the end)的做法,仍會調用一個系統呼叫:編譯器會在自己的「停工程式碼」
shutdown code)之後自動插入一個
_exit()
。經由
exit()
main()
的傳回值,明確傳
回結束狀態是一個好習慣。shell 可以使用此結束值來判斷命令的執行是成功或失敗。注
意,執行成功時,應該使用
exit(0)
,或是由
main()
傳回
0
值。
一個行程也可以被一個信號所終止,如果該信號的預定行為是終止行程。此類信號包括
SIGTERM
SIGKILL ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

深入理解運算原理|從簡單的機器到無所不能的程式

深入理解運算原理|從簡單的機器到無所不能的程式

Tom Stuart
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
算法技术手册(原书第2 版)

算法技术手册(原书第2 版)

George T.Heineman, Gary Pollice, Stanley Selkow

Publisher Resources

ISBN: 9789862769812