今天在写父子进程用两个单向管道通信时,出现了错误:
Segmentation fault (core dumped)
打开core文件发现:
附上源码:
1 #include <stdlib.h> 2 #include <unistd.h> 3 #include <stdio.h> 4 #include <sys/wait.h> 5 #include <errno.h> 6 #include <string.h> 7 8 void erreur(const char *msg) 9 { 10 perror(msg); 11 } 12 13 #define NBCAR 256 14 15 int main(void) 16 { 17 pid_t pid; 18 int tube[2]; 19 int tube2[2]; 20 int ret_out, ret_in; 21 char *buffer,*buffer2; 22 23 if (pipe(tube) == -1) {//from parent to son 24 erreur("Erreur de creation du pipe"); 25 exit(EXIT_FAILURE); 26 } 27 if (pipe(tube2) == -1) {//from son to parent 28 erreur("Erreur de creation du pipe"); 29 exit(EXIT_FAILURE); 30 } 31 buffer = (char *) malloc(NBCAR * sizeof(char)); 32 buffer2 = (char *) malloc(NBCAR * sizeof(char)); 33 switch (pid = fork()) { 34 case (pid_t) -1: 35 erreur("fork"); 36 case (pid_t) 0: 37 close(tube[1]); 38 close(tube2[0]); 39 if (printf("Je suis le fils de PID %d write dans le tube : %s Nbr Caracteres lus: %d ",getpid(), buffer, ret_in =write(tube2[1], buffer="tube1 s to p", NBCAR - 1)) == -1) { 40 erreur(" Pb Lecture "); 41 exit(EXIT_FAILURE); 42 } 43 if (printf("Je suis le fils de PID %d Lecture dans le tube : %s Nbr Caracteres lus: %d ",getpid(), buffer2, ret_in =read(tube[0], buffer2, NBCAR - 1)) == -1) { 44 erreur(" Pb Lecture "); 45 exit(EXIT_FAILURE); 46 } 47 48 exit(0); 49 break; 50 default: 51 close(tube[0]); 52 close(tube2[1]); 53 if (printf("Je suis le parent de PID %d Lecture dans le tube : %s Nbr Caracteres lus: %d ",getpid(), buffer, ret_in =read(tube2[0], buffer, NBCAR - 1)) == -1) { 54 erreur(" Pb Lecture "); 55 exit(EXIT_FAILURE);} 56 if (printf("Je suis le parent de PID %d write dans le tube : %s Nbr Caracteres lus: %d ",getpid(), buffer2, ret_in =write(tube[1], buffer2="tube2 p to s", NBCAR - 1)) == -1) { 57 erreur(" Pb Lecture "); 58 exit(EXIT_FAILURE);} 59 60 wait(NULL); 61 break; 62 63 } 64 free(buffer); 65 free(buffer2); 66 return EXIT_SUCCESS; 67 }
关于SIGSEGV错误
SIGSEGV --- Segment Fault. The possible cases of your encountering this error are:
1.buffer overflow --- usually caused by a pointer reference out of range.
2.stack overflow --- please keep in mind that the default stack size is 8192K.
3.illegal file access --- file operations are forbidden on our judge system.
后来通过邮件问老师,发现是buffer出现内存泄漏。
先梳理一下C语言中char和字符型、字符串型、单引号、双引号、字符串截止符号的概念:
首先C中没有专门的字符串变量(没有C++中的String类),单个的char就代表一个字符,赋值时应该是:char c = 'a'; 故而单引号表示单个字符。如果用char表示字符串,需要定义char的数组,并有两种主要赋值方法:
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '