2010年3月24日 星期三

Processes and Signals

Starting New Processes

#include stdlib.h
int system (const char* string);
/* not recommened for use */

==========================================================

Replacing a Process Image
#include unistd.h
exec family

[example]
/* argument list vector version */
char* const ps_argv[] = { "ps", "ax", 0 };
/* environment variable vector */
char* const ps_envp[] = { "PATH=/bin:/usr/bin", "TERM=console", 0 };

-------End with null pointer-----------
execl ( "/bin/ps", "ps", "ax", 0 );
execlp ( "ps", "ps", "ax", 0 ); /* assumes /bin is in PATH */
execle ( "/bin/ps", "ps", "ax", 0, ps_envp ); /* passes own environment */
-----------vector version--------------
execv ( "/bin/ps", ps_argv );
execvp ( "ps", ps_argv );
execve ( "/bin/ps", ps_argv, ps_envp );
---------------------------------------

l 代表 list [ 表示在exec內的命令列參數直接以字串 list寫入 ]
v 代表 vector [ 表示以陣列方式將命令列參數傳入exec函數 ]
p 代表 path [ 表示 command 可以不需要標明絕對路徑 ]
e 代表 environment [ 表示以陣列方式將環境變數傳入exec函數 ]

=========================================================

Duplicating a Process Image
#include sys/types.h
#include unistd.h
pid_t fork( void );

open file descriptors are preserved across calls to fork and exec

Waiting for a Process
#include sys/types.h
#include sys/wait.h
pid_t wait( int* stat_val );
pid_t waitpid( pid_t pid, int* stat_val, int options );

WIFEXITED( stat_val )
WEXITSTATUS( stat_val )

for parent process to release zombie child processes

=========================================================

Sinals
#include signal.h
void (*signal (int sig, void (*func)(int) ) ) (int);
/* not recommened for use */

#include sys/types.h
#include signal.h
int kill ( pid_t pid, int sig );

#include unistd.h
unsigned int alarm ( unsigned int seconds );

#include signal.h
int sigaction( int sig, const struct sigaction* act, struct sigaction* oldact );


Functions that are safe to call inside a signal handler,
either to be re-entrant or not to raise signals themselves.


[Reference]
Begining Linux Programming 4th edition
Chapter 11 Processes and Signals

沒有留言:

張貼留言