#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 15:23:08 浏览: 108
该代码最后一次取了8个字符,是因为管道的缓冲区大小为8,所以最后一次读取时只能读取到8个字符。而循环的问题是因为子进程一直在等待,而父进程没有杀死子进程导致的。可以在父进程中加上kill命令杀死子进程来解决这个问题。同时,在子进程中,可以在sig_int信号处理函数中加上kill命令杀死子进程来确保子进程会被杀死。修改后的代码如下:
相关问题
#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 "); sleep(1); } } else { printf("parent pid: %d ", getpid()); 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); } printf("pid: %d, send SIGINT ", getpid()); kill(pid, SIGINT); sleep(1); printf("pid: %d, send SIGINT again ", getpid()); kill(pid, SIGINT); sleep(1); wait(NULL); exit(EXIT_SUCCESS); } return 0; }的运行结果为
这段代码的运行结果为:
```
parent pid: xxxx ...child is waiting pid: xxxx, sig=2 pid: xxxx, send 26 bytes receive 8 bytes, abcdefgh receive 8 bytes, ijklmnop receive 8 bytes, qrstuvwx receive 2 bytes, yz pid: xxxx, send SIGINT pid: xxxx, sig=2 pid: xxxx, send SIGINT again pid: xxxx, sig=2
```
其中 `xxxx` 是进程的 pid。这段代码创建了一个父子进程,父进程通过管道从子进程接收数据,子进程定时向管道发送数据,并且在接收到 `SIGINT` 信号后退出。父进程在启动子进程后等待一段时间后向子进程发送 `SIGINT` 信号,等待子进程结束后再退出。
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "MQTTClient.h" #include <stdint.h> #include <signal.h> /* sigaction */ #include <errno.h> #include "sqlite3.h" #include "pan_reg.h" #include "spi_func.h" #include "auxiliary.h" #if !defined(WIN32) #include <unistd.h> #else #include <windows.h> #endif #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <math.h> /* modf */ #include "cJSON.h" #include <fcntl.h> #include "uart.h" #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>
这是一段 C 语言的代码,包含了一些头文件的引用,如 pthread.h、stdio.h、stdlib.h、string.h、MQTTClient.h、stdint.h、signal.h、errno.h、sqlite3.h、netdb.h、sys/types.h、sys/socket.h、netinet/in.h、math.h、cJSON.h、fcntl.h 和 uart.h。这段代码中可能包含了一些线程库、网络库、消息队列、JSON 解析等相关的函数。具体实现需要查看代码的具体逻辑。
阅读全文