process進(jìn)程 and thread
獲取當(dāng)前進(jìn)程getpid(),父進(jìn)程getppid(),當(dāng)前用戶ID,getuid(), geteuid(),組ID,getgid(),getegid(), all need head file <unistd.h>
getlogin()返回登錄用戶名
example:
#include <iostream.h>
#include <unistd.h> //getpid()
getppid()
int main()
{
cout<<getpid()<<endl; //current process ID
cout<<getppid()<<endl; //parent process ID
cout<<getgid()<<endl; //group process ID
cout<<getegid()<<endl; //effective group process ID
cout<<getuid()<<endl; //user process ID
cout<<geteuid()<<endl; //effective user process ID
cout<<getlogin()<<endl; //getlogin()
return the login user name
return 0;
}
獲取登錄用戶的個(gè)人信息,如用戶名,當(dāng)前目錄,用戶ID,組ID等,need
function struct passwd * getpwnam(const char *name)
如下面的例子:
/*
* getname.c - Get login names
*/
#include <stdio.h>
#include <stdlib.h>//exit()
#include <unistd.h>//getlogin()
#include <pwd.h> //getpwnam()
int main(void)
{
char *login;
struct passwd *pentry;
/* Get the login name */
if((login =
getlogin()) == NULL) { /* oops */
perror("getlogin");
exit(EXIT_FAILURE);
}
/* Get the password
entry for login */
if((pentry =
getpwnam(login)) == NULL) {
perror("getpwnam");
exit(EXIT_FAILURE);
}
/* Display the
password entry */
printf("user name: %s\n", pentry->pw_name);
printf("UID : %d\n", pentry->pw_uid);
printf("GID : %d\n", pentry->pw_gid);
printf("gecos : %s\n", pentry->pw_gecos);
printf("home dir : %s\n", pentry->pw_dir);
printf("shell : %s\n", pentry->pw_shell);
exit(EXIT_SUCCESS);
}
system()
如果沒(méi)有找到/bin/sh則返回127,成功返回0,出錯(cuò)返回-1
example:
/*
* system.c -
Demonstrate the system() call
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int retval;
retval = system("ls -l");
if(retval == 127) {
fprintf(stderr, "/bin/sh not available\n");
exit(127);
} else if(retval == -1) {
perror("system");
exit(EXIT_FAILURE);
} else if(retval != 0) {
fprintf(stderr, "command returned %d\n", retval);
perror("ls");
} else {
puts("command successfully executed");
}
exit(EXIT_SUCCESS);
}
fork調(diào)用創(chuàng)建一個(gè)新進(jìn)程
#include <unistd.h>
pid_t fork(void);
Description
fork() creates a child process that differs from the
parent process only in its
PID and PPID,
return value:
On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the
child's thread of
execution. On failure, a -1 will be returned in
the parent's context, no child
process will be created, and errno will be set
appropriately.執(zhí)行成功,就向父進(jìn)程返回子進(jìn)程的PID,并向子進(jìn)程返回0,只調(diào)用一次fork,它會(huì)返回兩次
example:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
pid_t child;
if((child = fork()) == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if(child == 0) {
puts("in child");
printf("\tchild pid = %d\n", getpid());
printf("\tchild ppid = %d\n", getppid());
exit(EXIT_SUCCESS);
} else {
puts("in parent");
printf("\tparent pid = %d\n", getpid());
printf("\tparent ppid = %d\n", getppid());
}
exit(EXIT_SUCCESS);
}
exec()函數(shù)族
exec用被執(zhí)行的程序完全替換了調(diào)用進(jìn)程的映像。exec啟動(dòng)一個(gè)新程序,替換原有的進(jìn)程
execl, execlp, execle, execv, execvp - execute a file
Synopsis
#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,
..., char * const envp[]);
int execve (const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
以上的函數(shù)都必須以NULL結(jié)束
example:
/*
* execve.c - Illustrate execve
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *argv[] = {"/bin/ls", NULL};
if(execve("/bin/ls", argv, NULL) == -1) {
perror("execve");
exit(EXIT_FAILURE);
}
puts("shouldn't get here");
exit(EXIT_SUCCESS);
}
使用wait and waitpid調(diào)用可以收集子進(jìn)程的退出狀態(tài)
Name
wait, waitpid - wait for process to change state
Synopsis
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
int waitid(idtype_t idtype, id_t id ", siginfo_t *" infop ", int
" options );
Description
All of these system calls are used to wait for state changes in a child
of the calling process, and obtain information about the child whose
state has changed. A state change is considered to be: the child
terminated; the child was stopped by a signal; or the child was resumed
by a signal. In the case of a terminated child, performing a wait allows
the system to release the resources associated with the child; if a
wait is not performed, then terminated the child remains in a "zombie" state (see NOTES below).
If a child has already changed state, then these calls return
immediately. Otherwise they block until either a child changes state or a
signal handler interrupts the call (assuming that system calls are not
automatically restarted using the SA_RESTART flag of sigaction(2)). In
the remainder of this page, a child whose state has changed and which
has not yet been waited upon by one of these system calls is termed
waitable.
wait() and waitpid()
The wait() system call suspends execution of the current process until
one of its children terminates. The call wait(&status) is equivalent
to:
waitpid(-1, &status, 0);
The waitpid() system call suspends execution of the current process
until a child specified by pid argument has changed state. By default,
waitpid() waits only for terminated children, but this behaviour is
modifiable via the options argument, as described below.
The value of pid can be:
< -1
meaning wait for any child process whose process group ID is equal
to the absolute value of pid.
-1
meaning wait for any child process.
0
meaning wait for any child process whose process group ID is equal
to that of the calling process.
> 0
meaning wait for the child whose process ID is equal to the value of
pid.
The value of options is an OR of zero or more of the following
constants:
WNOHANG
return immediately if no child has exited.
WUNTRACED
also return if a child has stopped (but not traced via ptrace(2)).
Status for traced children which have stopped is provided even if this
option is not specified.
WCONTINUED
(Since Linux 2.6.10) also return if a stopped child has been resumed
by delivery of SIGCONT.
if you want to more information aboat it, you can reference
http://linux.die.net.
example:
/*
* waiter.c - Simple wait usage
*/
#include <unistd.h>
//#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
pid_t child;
int status;
if((child = fork()) == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if(child == 0) {
puts("in
child");
printf("\tchild
pid = %d\n", getpid());
printf("\tchild
ppid = %d\n", getppid());
exit(EXIT_SUCCESS);
} else {
/* Wait for the child to exit */
waitpid(child, &status, 0);
printf("in
parent\n");
printf("\tparent
pid = %d\n", getpid());
printf("\tparent
ppid = %d\n", getppid());
printf("\tchild
exited with %d\n", status);
}
exit(EXIT_SUCCESS);
}
abort()
#include <stdlib.h>
void abort(void)
kill()
#include <signal.h>
#include <sys/types.h>
int kill(pid_t pid, int sig)
example:
~/*
* killer.c - Killing other processes
*/
#include <sys/types.h>
#include <sys/wait.h>//waitpid()
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>//signal
int main(void)
{
pid_t child;
int status, retval;
if((child = fork()) < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
if(child == 0) {
/* Sleep long enough to be killed */
sleep(1000);
exit(EXIT_SUCCESS);
} else {
/* Use WNOHANG so wait will return */
if((waitpid(child, &status, WNOHANG)) == 0) {
retval = kill(child, SIGKILL);
if(retval) {
/* Kill failed, so wait on child to exit */
puts("kill failed");
perror("kill");
waitpid(child, &status, 0);
} else
printf("%d
killed\n", child);
}
}
exit(EXIT_SUCCESS);
}
~
信
號(hào):
alarm(), pause(),kill(), sigfillset(),sigaction(),sigpending(),all
the functions can find from the site http://linux.die.net
線程
_clone(),
pthread_creat(),
pthread_exit(),pthread_join(),pthread_atfork(),pthread_cancel(), pthread
cleanup宏,pthread_equal()
線程互斥,pthread_mutex_init, pthread_mutex_lock
pthead
中的p display posix
from:
http://blog.chinaunix.net/u1/45689/showart_689217.html
| 只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。 | ||
|
【推薦】100%開(kāi)源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
|
||
|
相關(guān)文章:
|
||
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問(wèn)
Chat2DB
管理
|
||
|
|
| |||||||||
| 日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
|---|---|---|---|---|---|---|---|---|---|
| 28 | 1 | 2 | 3 | 4 | 5 | 6 | |||
| 7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
| 14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
| 21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
| 28 | 29 | 30 | 31 | 1 | 2 | 3 | |||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 | |||
常用鏈接
留言簿(10)
隨筆分類(lèi)(307)
- Algorithm(22)

- apache(1)

- Assembly(1)

- browser(2)

- C++_BASIS(39)

- Compiling Theorem(1)

- CPU(1)

- cvs(3)

- Database(8)

- Designed Patterns(3)

- FileFormat(4)

- FileSystem(1)

- freebsd(1)

- Game(2)

- gdb(1)

- Gossips(12)

- GP_STL(3)

- interview(8)

- java

- life and living(1)

- linux kernel(12)

- Linux_Coding(43)

- Linux_Driver

- Linux_SysAdmin(26)

- makefile(3)

- misce(5)

- MultiCore(1)

- Network(14)

- OS(18)

- RegularExpression(1)

- schedule(1)

- SearchEngine(1)

- security(2)

- Shell(25)

- Socket(18)

- storage(2)

- Template(4)

- VC_MFC(4)

- vi(5)

- website(1)

- windows(7)

隨筆檔案(297)
- 2012年10月 (1)
- 2012年9月 (1)
- 2012年7月 (1)
- 2012年6月 (7)
- 2012年5月 (3)
- 2012年4月 (2)
- 2011年9月 (3)
- 2011年8月 (3)
- 2011年6月 (3)
- 2011年5月 (2)
- 2011年3月 (2)
- 2011年1月 (1)
- 2010年12月 (2)
- 2010年11月 (6)
- 2010年10月 (4)
- 2010年9月 (7)
- 2010年8月 (12)
- 2010年7月 (6)
- 2010年6月 (5)
- 2010年5月 (11)
- 2010年4月 (16)
- 2010年3月 (20)
- 2010年2月 (18)
- 2010年1月 (26)
- 2009年12月 (34)
- 2009年11月 (36)
- 2009年10月 (5)
- 2009年9月 (1)
- 2009年7月 (2)
- 2009年6月 (3)
- 2009年5月 (6)
- 2009年4月 (6)
- 2009年3月 (11)
- 2009年2月 (6)
- 2008年11月 (1)
- 2008年10月 (1)
- 2008年9月 (3)
- 2008年8月 (4)
- 2008年7月 (16)
algorithm
- andytan
- algorithm, linux, os, network,etc
- EXACT STRING MATCHING ALGORITHMS
- httperf -- a web perf test tool
- Java多線程
- 編程夜未眠
- 布薩空間
- 結(jié)構(gòu)之法
- 沈一峰 google技術(shù)博客
- 小兵的窩
Books_Free_Online
C++
- Bjarne Stroustrup's C++ Style and Technique FAQ
- boyplayee column
- C Plus Plus
- CPP Reference
- LearnC++Website
- Welcome to Bjarne Stroustrup's homepage!
database
Linux
Linux shell
linux socket
misce
- cloudward
- 感覺(jué)這個(gè)博客還是不錯(cuò),雖然做的東西和我不大相關(guān),覺(jué)得看看還是有好處的
network
OSS
- Google Android
- Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
- os161 file list
overall
搜索
最新評(píng)論

- 1.?re: memcached完全剖析系列教程《轉(zhuǎn)》
- mark
- --zgpxgame
- 2.?re: 用prctl給線程命名
- 評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
- --none
- 3.?re: 用prctl給線程命名
- 請(qǐng)問(wèn)大俠: 用top命令的時(shí)候可以顯示修改后的線程名么?如何做呢?
- --dhao123@sina.com
- 4.?re: 解決Linux pthread_create內(nèi)存泄漏問(wèn)題
-
我試過(guò),每一種方法有的時(shí)候不行。
第二種是可以的。
- --朱先生
- 5.?re: 著名程序庫(kù)的比較和學(xué)習(xí)經(jīng)驗(yàn)
- 評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
- --buy dissertation
- 6.?re: linux的消息隊(duì)列與共享內(nèi)存編程
- 內(nèi)容選擇得很好,謝謝
- --朱志超
- 7.?re: 著名程序庫(kù)的比較和學(xué)習(xí)經(jīng)驗(yàn)
- 評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
- --LillianHancock
- 8.?re: 解決Linux pthread_create內(nèi)存泄漏問(wèn)題[未登錄](méi)
- 不錯(cuò),支持一個(gè)。
- --jack
- 9.?re: 淺談?dòng)螒蚍?wù)器---功能模塊上來(lái)看[未登錄](méi)
- 不錯(cuò) 好文!! 期待博主繼續(xù)
- --cppexplore
- 10.?re: 全面整理的C++面試題
- 評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
- --chatler
- 11.?re: 微軟面試中簡(jiǎn)單的算法題目(轉(zhuǎn))
- 評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
- --chatler
- 12.?re: Browsers, processes, cookies and session state
- 每個(gè)IE Instance該是不同的進(jìn)程吧,可以獲取進(jìn)程ID,在每個(gè)instance里建一個(gè)名稱(chēng)包含進(jìn)程id的目錄名,就可以分目錄存儲(chǔ)了吧。
- --chatler
- 13.?re: Browsers, processes, cookies and session state
-
文章說(shuō)的很清楚,多謝
我有一個(gè)問(wèn)題:
如何為每個(gè)ie instance ie實(shí)例的 Persistent cookies cookie 指定不同的存儲(chǔ)目錄? - --domolo
- 14.?re: 從一道面試題看指針與數(shù)組的區(qū)別
- 一個(gè)字,強(qiáng)!
- --路過(guò)
- 15.?re: 一個(gè)關(guān)于單向鏈表的面試題
- 評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
- --chatler
閱讀排行榜
- 1.?Windows Socket五種I/O模型(8343)
- 2.?最大公約數(shù)(Gcd)兩種算法(Euclid && Stein)<轉(zhuǎn)>(5500)
- 3.?用prctl給線程命名(5057)
- 4.?Linux core dump file詳解 <轉(zhuǎn)>(4497)
- 5.?算法面試題(3402)
- 6.?64位與32位編程的數(shù)據(jù)類(lèi)型區(qū)別(3235)
- 7.?解決Linux pthread_create內(nèi)存泄漏問(wèn)題(3129)
- 8.?NUMA與英特爾下一代Xeon處理器學(xué)習(xí)心得<轉(zhuǎn)>(2997)
- 9.?c語(yǔ)言抓取網(wǎng)頁(yè)數(shù)據(jù)(2834)
- 10.?CVSNT服務(wù)器配置——添加用戶、解決無(wú)法登陸(2738)
- 11.? pthread_join函數(shù)及l(fā)inux線程(2647)
- 12.?一個(gè)基于Event Poll(epoll)的TCP Server Framework,淺析epoll(2577)
-
13.?為 C/C++ 項(xiàng)目構(gòu)建您自己的內(nèi)存管理器
(2565) - 14.?memcached完全剖析系列教程《轉(zhuǎn)》(2511)
- 15.?G++編譯選項(xiàng)(2406)
- 16.?STL容器 erase的使用陷井<轉(zhuǎn)載>(2196)
- 17.?epoll使用例子(2119)
- 18.?linux的消息隊(duì)列與共享內(nèi)存編程(2072)
- 19.?gdb帶參數(shù)調(diào)試(2064)
- 20.?The Linux Kernel Module Programming Guide(2015)
- 21.?一個(gè)關(guān)于單向鏈表的面試題(1911)
- 22.?c中strncmp與memcmp的區(qū)別(1885)
- 23.?優(yōu)化Derby數(shù)據(jù)庫(kù)技巧(1834)
- 24.?一個(gè)基于完成端口的TCP Server Framework,淺析IOCP(1761)
- 25.?自己整理的指令(1753)
- 26.?autotools制作Makefile 和configure文件(1639)
- 27.?Google C++ Style Guide(1631)
- 28.?linux系統(tǒng)調(diào)用函數(shù)(1625)
- 29.?An In-Depth Look into the Win32 Portable Executable File Format(1610)
- 30.? vim大小寫(xiě)轉(zhuǎn)換(1584)
- 31.?淺談?dòng)螒蚍?wù)器---功能模塊上來(lái)看(1555)
-
32.?MIPS architecture
(1505) - 33.?教你用c實(shí)現(xiàn)http協(xié)議(1504)
- 34.?Aix下查看占用端口的進(jìn)程(1484)
- 35.?史上最強(qiáng)bash函數(shù)庫(kù)(1409)
- 36.?linux trap詳解(1342)
- 37.?ms,google,vmware,nvidia美國(guó)總部面試題(1327)
- 38.?多CPU上的原子操作(1316)
- 39.?power函數(shù)寫(xiě)法《轉(zhuǎn)》(1283)
-
40.?Critical Section
(1256)
評(píng)論排行榜
- 1.?著名程序庫(kù)的比較和學(xué)習(xí)經(jīng)驗(yàn)(3)
- 2.?解決Linux pthread_create內(nèi)存泄漏問(wèn)題(2)
- 3.?用prctl給線程命名(2)
-
4.?Browsers, processes, cookies and session state
(2) - 5.?全面整理的C++面試題(1)
- 6.?linux的消息隊(duì)列與共享內(nèi)存編程(1)
- 7.?一個(gè)關(guān)于單向鏈表的面試題(1)
- 8.?從一道面試題看指針與數(shù)組的區(qū)別 <轉(zhuǎn)>(1)
- 9.?淺談?dòng)螒蚍?wù)器---功能模塊上來(lái)看(1)
- 10.?微軟面試中簡(jiǎn)單的算法題目(轉(zhuǎn))(1)
- 11.?memcached完全剖析系列教程《轉(zhuǎn)》(1)
- 12.?死鎖和活鎖 deadlock and livelock(0)
- 13.?IT公司筆試算法題(0)
- 14.?effective c++(0)
-
15.?為 C/C++ 項(xiàng)目構(gòu)建您自己的內(nèi)存管理器
(0) -
16.?Comparing Two High-Performance I/O Design Patterns
(0) - 17.?boost 庫(kù) enable_shared_from_this 實(shí)現(xiàn)原理分析<轉(zhuǎn)>(0)
-
18.?Which is asymptotically larger: lg(lg* n) or lg*(lg n)$? <
>(0) - 19.?TCMalloc : Thread-Caching Malloc(0)
- 20.?有抗癌效果的神奇食物(0)
- 21.?ubuntu下編譯內(nèi)核(0)
- 22.?MySQL索引背后的數(shù)據(jù)結(jié)構(gòu)及算法原理(0)
- 23.?wget進(jìn)行整站下載<轉(zhuǎn)>(0)
- 24.?What are the difference between DDL, DML and DCL commands?<轉(zhuǎn)載>(0)
- 25.?通過(guò)證書(shū)方式實(shí)現(xiàn)ssh的無(wú)密碼登陸<轉(zhuǎn)>(0)
- 26.?50個(gè)有關(guān)編程的至理名言<轉(zhuǎn)>(0)
- 27.?How I explained Design Patterns to my wife《reprint》(0)
- 28.?How to explain OOD to my wife-code project《轉(zhuǎn)載》(0)
- 29.?ifconf.c《轉(zhuǎn)載》(0)
- 30.?vim configuration(0)
- 31.?<轉(zhuǎn)>how to start a kernel thread(0)
- 32.?Linux系統(tǒng)性能指標(biāo)介紹(0)
- 33.?Linux進(jìn)程的狀態(tài)和調(diào)度(0)
- 34.?關(guān)鍵字的作用 auto static register const volatile extern (0)
- 35.?epoll使用例子(0)
- 36.?Windows Socket五種I/O模型(0)
- 37.?TCP的流量控制 (0)
- 38.?Linux下各類(lèi)TCP網(wǎng)絡(luò)服務(wù)器的實(shí)現(xiàn)源代碼《轉(zhuǎn)》(0)
- 39.?fopen 文本文件與二進(jìn)制文件區(qū)別(0)
- 40.?Linux目錄掃描程序(0)
