Problem-solving process through the pipe communication between Linux garbled

Process communication section of pipe between the code is shown below:

First to find out the reason garbled character codes in a computer are represented by ascll, some ascll code indicates character determination, such as 65 capital letter 'A', 97 represents a lower case letter 'a', 48 represents the number '0 ', while some ascll code indicates a number of other characters, these other characters is the cause of garbled. At first declare an array of characters, each element of the array of characters is uncertain, that is ascll code is random, then read about these characters show, became garbled on the screen.

In the C language, reading from a character array is read character '\ 0' on the end, we can declare a character array are using memset function to initialize it again, '\ 0' is ascll code is 0, so we add memset (outpipe, 0, sizeof (outpipe)), memset (inpipe, 0, sizeof (inpipe) can.

Another inpipe distortion is caused by a small array of open, such as open inpipe 15 bytes, 20 bytes are read out from the pipe, then the UNIX kernel will read 20 bytes to dump elsewhere memory, leading to read the entire contents would have wanted to read, but followed by a bunch of gibberish. The system also prompted a "discarded (core dumped)." As shown below:

This garbled solution is to inpipe array larger open. 

Published 72 original articles · won praise 203 · views 90000 +

Guess you like

Origin blog.csdn.net/weixin_41676881/article/details/102890516
Recommended