一、进程补充
1.函数回收
(1)wait
原型:pid_t wait(int *wstatus);
功能:
回收子进程空间
参数:
wstatus:存放子进程结束状态空间的首地址
返回值:
成功返回回收到的子进程的PID
失败返回-1
WIFEXITED(wstatus):测试进程是否正常结束
WEXITSTATUS(wstatus):获得进程退出的值
WIFSIGNALED(wstatus):测试进程是否被杀死
WTERMSIG(wstatus):获得杀死该进程的信号
注意:
wait具有阻塞等待功能,等到有子进程结束才会回收子进程继续向下执行
wait可以实现父子进程任务的同步
(2)waitpid
原型:pid_t waitpid(pid_t pid, int *wstatus, int options);
功能:
回收指定的子进程空间
参数:
pid:要回收的进程的PID(-1表示回收任意子进程)
wstatus:存放子进程结束状态空间首地址
options:
0 阻塞回收
WNOHANG 非阻塞回收
返回值:
成功返回回收到的子进程PID
失败返回-1
指定的子进程空间没结束,返回0
waitpid(-1, NULL, 0) 等价于 wait(NULL
注意:
waitpid可以非阻塞回收子进程空间
waitpid可以回收指定子进程空间
2.exec函数族
(1)exec函数族
利用进程空间执行另一份代码
exec常搭配fork使用,fork负责创建新的子进程,exec负责让子进程执行自己的代码
extern char **environ;
int execl(const char *path, const char *arg, .../* (char *) NULL
*/);
int execlp(const char *file, const char *arg, .../* (char *) NULL
*/);
int execle(const char *path, const char *arg, .../*, (char *) NULL,
char * const envp[] */);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const
envp[]);
l:参数以列表的形式传递
p:在系统变量PATH对应的目录下查找文件
v:参数以指针数组的形式传递
e:执行新代码是更新环境变量
(2)主函数传参
主函数形式
void main(void);
int main(void);
int main(int argc, const char *argv[]);
int main(int argc, const char **argv);
argc:传入参数的个数
argv:存放每一个传入参数指针的数组名
argv[0] = "./a.out"
argv[1] = "hello"
argv[2] = "world"
argv[3] = "how"
argv[...] = ...
argv[argc] = ...
注:通过在终端传入值来传参,第一个参数一般为./a.out
(3)system函数
原型:int system(const char *command);
功能:
运行command命令
参数:
command:shell命令字符串首地址
返回值:
成功返回0
失败返回-1
二、线程
1.基本概念
线程是一个轻量级的进程,线程本质就是一个进程
线程和进程不完全一致,轻量指空间,进程空间和线程空间管理方法不同
2.进程和线程区别
(1)线程本质是进程,线程是任务创建、调度、回收的过程
(2)进程空间:文本段 + 数据段 + 系统数据段
(3)线程空间:
线程必须位于进程空间内部,没有进程,线程无法独立存在
一个进程中的所有线程共享文本段+数据段+堆区,独享栈区
线程独享的栈区默认为8M
一个进程中的多个线程切换调度任务时,资源开销比较小
(4)进程和线程区别:
线程是CPU任务调度的最小单元
进程是操作系统资源分配的最小单元

3.多进程和多线程的优缺点
| 场景 | 多进程 | 多线程 | 对比 |
| 效率 | 多进程切换需要重新映射物理地址,占用资源开销较大 | 多线程在同一进程空间内部切换任务,占用资源开销较小 | 多线程 > 多进 程 |
| 通信 | 多进程没有共享空间,需要使用进程间通信的方法来完成通信 | 多线程有共享空间,只需定义共享空间变量完成数据交换即可实现通信 | 多线程 > 多进 程 |
|
资源 竞争 | 多进程没有共享空间,不存在资源竞争 | 多线程使用共享空间通信,需保证资源使用的互斥性,防止多线程对共享资源产生 竞争 | 多进程 > 多线 程 |
| 安全 | 多进程空间独立,一个进程的崩溃不会影响其余进程 | 多线程共用同一个进程空间,一个线程异常崩溃,可能引发进程异常退出,导致其 余线程也无法执行 | 多进程 > 多线 程 |
4.线程的调度
与进程调度保持一致
宏观并行,微观串行
5.线程的消亡
线程结束需要回收线程空间,否则产生僵尸线程
6.线程的函数接口
(1)pthread_create
原型:int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
功能:
在进程中创建一个线程
参数:
thread:存放线程ID空间的首地址
attr:线程的属性,默认属性NULL
start_routine:线程函数的入口
arg:线程传入的参数
返回值:
成功返回0
失败返回错误
(2)pthread_self
原型:pthread_t pthread_self(void);
功能:
获得调用该函数的线程的ID
(3)pthread_exit
原型:void pthread_exit(void *retval);
功能:
结束当前线程任务
参数:
retval:线程结束的值
(5)pthread_join
原型:int pthread_join(pthread_t thread, void **retval);
功能:
回收线程空间
参数:
thread:要回收的线程的ID
retval:存放线程结束状态空间的首地址
返回值:
成功返回0
失败返回错误
注意:
tid对应的线程只要不退出,pthread_join阻塞等待结束回收线程空间
pthread_join具备同步功能
e.g.
#include "head.h"
void *thread1(void *arg)
{
printf("线程1(TID:%#lx)开始执行\n", pthread_self());
pthread_exit("线程1退出");
return NULL;
}
void *thread2(void *arg)
{
printf("线程2(TID:%#lx)开始执行\n", pthread_self());
pthread_exit("线程2退出");
return NULL;
}
void *thread3(void *arg)
{
printf("线程3(TID:%#lx)开始执行\n", pthread_self());
pthread_exit("线程3退出");
return NULL;
}
int main(void)
{
int ret;
pthread_t tid[3];
int i = 0;
void *pret = NULL;
void *(*p[3])(void *) = {thread1, thread2, thread3};
for(i = 0; i < 3; i++)
{
pthread_create(&tid[i], NULL, p[i], NULL);
if(ret != 0)
{
perror("fail to create");
return -1;
}
}
for(i = 0; i < 3; i++)
{
pthread_join(tid[i], &pret);
printf("线程退出状态:%s\n", (char *)pret);
}
#if 0
ret1 = pthread_create(&tid1, NULL, thread1, NULL);
if(ret1 != 0)
{
perror("fail to ohread_create\n");
return -1;
}
ret2 = pthread_create(&tid2, NULL, thread2, NULL);
if(ret2 != 0)
{
perror("fail to ohread_create\n");
return -1;
}
ret3 = pthread_create(&tid3, NULL, thread3, NULL);
if(ret3 != 0)
{
perror("fail to ohread_create\n");
return -1;
}
#endif
while(1)
{
}
return 0;
}
1675

被折叠的 条评论
为什么被折叠?



