当程序异常存在时,fwrite()缓冲区中的数据是否会被刷新?

时间:2016-06-17 15:15:14

标签: c linux file io

... $select->execute(); $result = $select->fetchall(); // displays products on discount with an Html table style echo '<p style="font-size: 20px; text-align: center;">Active discounts</p> <div class="table"> <div class="thead"> <span class="th">Product</span> <span class="th">Public price</span> <span class="th">Discount price</span> <span class="th">Discount code</span> <span class="th">Description</span> <span class="th">New code</span> <span class="th">&nbsp;</span> <span class="th">&nbsp;</span> </div>'; foreach($result as $row) // loop on Products { $discount_price = $row['product_price'] - ($row['product_price'] * $row['discount_percent'] ); // wrap each "tr like' in a form echo '<form class="tr" action="" method="post"> <span class="td">'.$row['product_name'].'</span> <span class="td">'.$row['product_price'].'</span> <span class="td">'.$discount_price.'</span> <span class="td">'.$row['product_discount'].'</span> <span class="td">'.$row['discount_desc'].'</span>'; // populate discount codes in drop-down $select_d = $connexion -> prepare( "SELECT discount_code FROM Discounts" ); $select_d->execute(); $result_d = $select_d->fetchall(); // build select for each product echo "<span class='td'><select id='discount' name='discount'> <option value='' selected='selected'>-Select-</option>"; foreach($result_d as $row_d) { // loop on Discounts echo "<option value='".$row_d['discount_code']."'>".$row_d['discount_code']."</option>"; } // end select & action buttons echo '</select></span> <span class="td"><button class="btn btn-danger bold" type="submit" name="change" value="'.$row['productID'].'">Change</button></span> <span class="td"><button class="btn btn-danger bold" type="submit" name="reset" value="'.$row['productID'].'">Reset</button></span> </form>'; } // end loop Products echo '</div><br />'; // end display html table style $connexion = null; ... 是一个库调用,它首先将数据缓冲到用户空间缓冲区,然后稍后调用fwrite()系统调用以实际执行写操作。

  

如果程序调用{​​{1}}将某些数据写入文件但存在异常,则write()的缓冲区将被清除刷新,否则缓冲的数据将是留在记忆中?

我正在考虑的操作系统是Linux。

1 个答案:

答案 0 :(得分:8)

如果您的程序异常退出,则不会刷新任何缓冲的数据。操作系统只是说“哦,亲爱的,你把文件描述符打开了,我最好为你关闭”,当进程终止时;它不知道有一些随机数据存在于内存中,程序打算写入磁盘,但没有。