signal函数和wait函数
时间: 2024-06-13 13:08:02 浏览: 14
signal函数和wait函数是Linux系统编程中常用的两个函数。
signal函数的作用是为指定的信号设置处理函数,其函数原型为:
```c
void (*signal(int signum, void (*handler)(int)))(int);
```
其中,signum表示要设置处理函数的信号编号,handler表示要设置的信号处理函数。signal函数返回的是指向以前的信号处理程序的指针(安装在signum信号上的信号处理函数的指针)。
wait函数的作用是等待子进程结束并获取子进程的终止状态,其函数原型为:
```c
pid_t wait(int *status);
```
其中,status是一个指向整型变量的指针,用于存储子进程的终止状态。wait函数返回的是子进程的进程ID。
在Linux系统编程中,通常会使用signal函数为指定的信号设置处理函数,以便在信号发生时进行相应的处理。而wait函数则常用于父进程等待子进程结束并获取其终止状态的情况下。
相关问题
pthread_cond_signal函数的例子
下面是一个使用pthread_cond_signal函数的例子:
```
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 3
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int count = 0;
void *thread_func(void *arg) {
int *id = (int *) arg;
printf("Thread %d started\n", *id);
pthread_mutex_lock(&mutex);
while (count < 5) {
printf("Thread %d waiting\n", *id);
pthread_cond_wait(&cond, &mutex);
printf("Thread %d continuing\n", *id);
}
pthread_mutex_unlock(&mutex);
printf("Thread %d finished\n", *id);
return NULL;
}
int main() {
pthread_t threads[NUM_THREADS];
int ids[NUM_THREADS] = {0, 1, 2};
for (int i = 0; i < NUM_THREADS; i++) {
pthread_create(&threads[i], NULL, thread_func, &ids[i]);
}
pthread_mutex_lock(&mutex);
for (int i = 0; i < 5; i++) {
count++;
printf("Main thread signalling\n");
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
```
在该例子中,我们创建了三个线程并在主线程中使用pthread_cond_signal函数通知等待线程继续执行。线程等待条件变量cond,当主线程调用pthread_cond_signal函数时,其中一个线程将从等待中醒来并继续执行。注意,我们在使用条件变量时需要使用互斥锁mutex来保护共享数据。
通过signal处理函数回收子进程
通过signal处理函数回收子进程的一种常见方法是使用SIGCHLD信号。当一个子进程终止时,它会向父进程发送SIGCHLD信号。父进程可以通过设置一个信号处理函数来捕捉并处理这个信号。在信号处理函数中,可以调用waitpid函数来回收已经终止的子进程。waitpid函数会阻塞父进程,直到有子进程终止。通过传递一个合适的参数给waitpid函数,父进程可以选择回收特定的子进程或者回收任意子进程。\[1\]\[2\]
下面是一个示例代码,展示了如何使用signal处理函数回收子进程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void sigchld_handler(int signum) {
int status;
pid_t pid;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
// 处理已终止的子进程
if (WIFEXITED(status)) {
printf("子进程 %d 正常终止,退出状态码:%d\n", pid, WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("子进程 %d 被信号终止,信号编号:%d\n", pid, WTERMSIG(status));
}
}
}
int main() {
// 设置SIGCHLD信号的处理函数
signal(SIGCHLD, sigchld_handler);
// 创建子进程
pid_t pid = fork();
if (pid == 0) {
// 子进程执行的代码
printf("子进程开始执行\n");
sleep(2);
printf("子进程结束\n");
exit(0);
} else if (pid > 0) {
// 父进程执行的代码
printf("父进程等待子进程终止\n");
while (1) {
// 父进程继续执行其他任务
sleep(1);
}
} else {
// fork失败
printf("创建子进程失败\n");
return 1;
}
return 0;
}
```
在上面的示例代码中,父进程使用signal函数将SIGCHLD信号的处理函数设置为sigchld_handler。当子进程终止时,它会向父进程发送SIGCHLD信号,父进程会在sigchld_handler函数中处理这个信号。在处理函数中,使用waitpid函数回收已终止的子进程。通过设置WNOHANG选项,waitpid函数可以非阻塞地检查是否有子进程终止。如果有子进程终止,waitpid函数会返回子进程的PID,父进程可以根据返回值进行相应的处理。\[1\]\[2\]
#### 引用[.reference_title]
- *1* *2* *3* [第六章_信号 : signal函数--信号](https://blog.csdn.net/matafeiyanll/article/details/109913745)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)