您的位置:首页 > 其它

线程与进程

2016-04-25 21:20 441 查看

进程与线程

进程

函数

getpid

#include <unistd.h>
pid_t getpid(void);
pid_t getppid(void);


返回该进程ID。

返回父进程ID。

fork函数

#include <unistd.h>
pid_t fork(void);


子进程返回0,父进程返回新子进程的进程ID。子进程可以使用getppid得到父进程的进程ID。

利用fork函数创建子进程,子进程获得父进程数据的空间,堆和栈的副本,父子进程并不共享这些存储空间部分。父子进程只共享正文段。

vfork函数

#include <unistd.h>
pid_t vfork(void);


vfork保证子进程先运行,在它调用exec或exit后父进程才可能被调度运行

子进程在父进程的地址空间运行

exit函数

exit();


wait函数

#include <sys/wait.h>
pid_t wait(int *statloc)
pid_t waitpid(pid_t pid, int *statloc, int options);


成功返回0,进程ID。出错返回-1。

1. 如果其所有子进程在运行,则阻塞。

2. 若果一个子进程已终止,正等待父进程获取其终止的状态,则取得该子进程的终止状态立即返回。

3. 如果没有子进程则返回出错。

区别:

在一个子进程终止之前,wait使其调用者阻塞,waitpid有一个选项可以使调用者不阻塞。

waitpid并不等待在其调用之后的第一个终止子进程,它有若干个选项,可以控制它所等待的进程。

exec函数族

#include <unistd.h>
int execl(const char *pathname, const char *arg0, ...)
int execv(const char *pathname, char *const argv[])
int execle(const char *pathname, const char *arg0, ...)
int execve(const char *pathname, char *const argv[], char *const envp[])
int execlp(const char *pathname, const char *arg0, ...)
int execvp(const char *pathname, char *const argv[])


更改用户ID和组ID

#include <unistd.h>
int setuid(uid_t uid)
int setgid(gid_t gid)


线程

未完……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: