一、在linux平台下,每個線程可有專用數據:
#include
}
二、摸擬shell
通過execlp函數來實現 ,execlp函數用於執行文件
其參數與說明為:
#includeint execlp( const char * file, const char * arg0, const char * arg1, … const char * argn, NULL );
wait for process termination
#include
#include
pid_t waitpid(pid_t pid, int *stat_loc, int options);
The waitpid() function lets the calling process obtain status information about one of its child processes. If status information is available for two or more child processes, the order in which their status is reported is unspecified. If more than one thread is suspended in waitpid() awaiting termination of the same process, exactly one thread returns the process status at the time of the target child process termination. The other threads return -1, with errno set to ECHILD.
If the calling process sets SIGCHLD to SIG_IGN, and the process has no unwaited for children that were transformed into zombie processes, the calling thread blocks until all of the children of the process terminate, at which time waitpid() returns -1 with errno set to ECHILD.
If the parent process terminates without waiting for all of its child processes to terminate, the remaining child processes are assigned a new parent process ID corresponding to a system-level process.
Specifies a set of child processes for which the status is requested:
Specifies the location to which the child process' exit status is stored. If NULL is passed, no exit status is returned. Otherwise, the following macros defined in
Evaluates to a non-zero value if status was returned for a child process that exited normally.
If the value of WIFEXITED(s) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to exit() or _exit(), or to the value that the child process returned from main().
Evaluates to a non-zero value if status was returned for a child process that terminated due to receipt of a signal that was not caught.
If the value of WIFSIGNALED(s) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process.
Evaluates to a non-zero value if status was returned for a child process that terminated due to receipt of a signal that was not caught, and whose default action is to dump core.
Evaluates to a non-zero value if status was returned for a child process that terminated due to receipt of a signal that was not caught, and whose default action is to dump core.
If the value of WIFCORED(s) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process.
Evaluates to a non-zero value if status was returned for a child process that is currently stopped.
If the value of WIFSTOPPED(s) is non-zero, this macro evaluates to the number of the signal that caused the child process to stop.
Is the bitwise inclusive-OR of zero or more of the following flags, defined in
The waitpid() function does not suspend execution of the calling thread if status is not immediately available for one of the child processes specified by pid.
The status of any child processes specified by pid that are stopped, and whose status has not yet been reported since they stopped, is also reported to the requesting thread. This value is currently not supported, and is ignored.
If waitpid() was invoked with WNOHANG set in options, and there are children specified by pid for which status is not available, waitpid() returns 0. If WNOHANG was not set,waitpid() returns the process ID of a child when the status of that child is available. Otherwise, it returns -1 and sets errno to one of the following values:
The process or process group specified by pid does not exist or is not a child of the calling process.
stat_loc is not a writable address.
The function was interrupted by a signal. The value of the location pointed to by stat_loc is undefined.
The options argument is not valid.
pid specifies a process group (0 or less than -1), which is not currently supported.