#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #define BUF_SIZE 26 int pipefd[2]; void sig_alrm(int signo){ char buf[BUF_SIZE]; int cnt = 0; printf("pid: %d, sig=%d ", getpid(), signo); for (char c = 'a'; c <= 'z'; c++) buf[cnt++] = c; write(pipefd[1], buf, cnt); printf("pid: %d, send %d bytes ", getpid(), cnt); } void sig_int(int signo){ printf("pid: %d, sig=%d ", getpid(), signo); signal(SIGINT, SIG_DFL); alarm(2); } int main(){ pid_t pid; char buf[BUF_SIZE]; int cnt = 0; if (pipe(pipefd) < 0) { perror("pipe error"); exit(EXIT_FAILURE); } if ((pid = fork()) < 0) { perror("fork error"); exit(EXIT_FAILURE); } else if (pid == 0) { signal(SIGALRM, sig_alrm); signal(SIGINT, sig_int); alarm(2); while (1) { printf("...child is waiting "); fflush(stdout); sleep(1); } } else { printf("parent pid: %d ", getpid()); fflush(stdout); close(pipefd[1]); while (1) { int n = read(pipefd[0], buf, 8); if (n <= 0) break; cnt += n; printf("receive %d bytes, %s ",n, buf); fflush(stdout); } printf("pid: %d, send SIGINT ", getpid()); fflush(stdout); kill(pid, SIGINT); sleep(1); printf("pid: %d, send SIGINT again ", getpid()); fflush(stdout); kill(pid, SIGINT); sleep(1); wait(NULL); printf("...child is waiting "); fflush(stdout); exit(EXIT_SUCCESS); } return 0; } 为什么该代码运行结果最后一次两个字符却取了八个字符,并且...child is waiting一直循环不止,该如何更改代码才能得到parentpid:221...childiswaiting...childiswaitingpid:222,sig=14send26bytesreceive8bytes,abcdefgh...childiswaitingreceive8bytes,ijklmnopreceive8bytes,qrstuvwxreceive2bytes,yzpid:222,sig=2...childiswaiting的运行结果。
时间: 2024-04-27 19:23:08 浏览: 12
该代码最后一次取了8个字符,是因为管道的缓冲区大小为8,所以最后一次读取时只能读取到8个字符。而循环的问题是因为子进程一直在等待,而父进程没有杀死子进程导致的。可以在父进程中加上kill命令杀死子进程来解决这个问题。同时,在子进程中,可以在sig_int信号处理函数中加上kill命令杀死子进程来确保子进程会被杀死。修改后的代码如下:
相关问题
前6行程序输出。 C/C++ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <signal.h>
这段程序是C/C++代码,包含了5个头文件。第一个头文件<stdio.h>提供了标准输入输出函数,第二个头文件<stdlib.h>提供了一些常用的函数和类型定义,第三个头文件<sys/types.h>定义了系统调用所需的数据类型,第四个头文件<unistd.h>提供了一些通用的系统服务函数,最后一个头文件<signal.h>提供了信号处理的相关函数和数据类型。
这段代码并没有输出语句,所以程序不会有任何输出。
用signal函数改写下面的程序,使闹钟时间到后输出“ling ling ling”,然后接着执行pause()后的语句。 提示:定时器时间到后向进程发出SIGALRM信号。 #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { /* 调用alarm定时器函数 */ int ret = alarm(5); pause(); printf("I have been waken up.\n"); return 0; }
下面是使用signal函数改写后的程序:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void alarm_handler(int sig) {
printf("ling ling ling\n");
}
int main() {
// 注册SIGALRM信号处理函数
signal(SIGALRM, alarm_handler);
// 设置定时器
alarm(5);
// 暂停进程,等待信号到来
pause();
printf("I have been waken up.\n");
return 0;
}
```
程序中使用了signal函数注册了SIGALRM信号的处理函数alarm_handler,当定时器时间到达时,操作系统会向进程发送SIGALRM信号,进程就会执行alarm_handler函数来处理该信号。在alarm_handler函数中输出"ling ling ling",然后程序继续执行pause()后的语句。
相关推荐
![application/x-bzip2](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)