File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -434,9 +434,7 @@ mod impls {
434434 let current = PROCESSOR . get_mut ( ) . current ( ) . unwrap ( ) ;
435435 let ( read_end, write_end) = make_pipe ( ) ;
436436 let read_fd = current. fd_table . len ( ) ;
437- current. fd_table . push ( Some ( Mutex :: new ( read_end) ) ) ;
438- let write_fd = current. fd_table . len ( ) ;
439- current. fd_table . push ( Some ( Mutex :: new ( write_end) ) ) ;
437+ let write_fd = read_fd + 1 ;
440438 // 将 read_fd 写入 pipe[0]
441439 if let Some ( mut ptr) = current
442440 . address_space
@@ -455,6 +453,9 @@ mod impls {
455453 } else {
456454 return -1 ;
457455 }
456+ // 最后添加,避免中途写入异常导致浪费一个 fd
457+ current. fd_table . push ( Some ( Mutex :: new ( read_end) ) ) ;
458+ current. fd_table . push ( Some ( Mutex :: new ( write_end) ) ) ;
458459 0
459460 }
460461 }
Original file line number Diff line number Diff line change @@ -450,9 +450,7 @@ mod impls {
450450 let current = PROCESSOR . get_mut ( ) . get_current_proc ( ) . unwrap ( ) ;
451451 let ( read_end, write_end) = make_pipe ( ) ;
452452 let read_fd = current. fd_table . len ( ) ;
453- current. fd_table . push ( Some ( Mutex :: new ( read_end) ) ) ;
454- let write_fd = current. fd_table . len ( ) ;
455- current. fd_table . push ( Some ( Mutex :: new ( write_end) ) ) ;
453+ let write_fd = read_fd + 1 ;
456454 // 将 read_fd 写入 pipe[0]
457455 if let Some ( mut ptr) = current
458456 . address_space
@@ -471,6 +469,9 @@ mod impls {
471469 } else {
472470 return -1 ;
473471 }
472+ // 最后添加,避免中途写入异常导致浪费一个 fd
473+ current. fd_table . push ( Some ( Mutex :: new ( read_end) ) ) ;
474+ current. fd_table . push ( Some ( Mutex :: new ( write_end) ) ) ;
474475 0
475476 }
476477 }
Original file line number Diff line number Diff line change @@ -17,14 +17,6 @@ enum RingBufferStatus {
1717 Normal ,
1818}
1919
20- /// 缓冲区快照,用于恢复状态
21- #[ derive( Copy , Clone ) ]
22- pub struct BufferSnapshot {
23- head : usize ,
24- tail : usize ,
25- status : RingBufferStatus ,
26- }
27-
2820/// 管道环形缓冲区
2921pub struct PipeRingBuffer {
3022 arr : [ u8 ; RING_BUFFER_SIZE ] ,
@@ -96,22 +88,6 @@ impl PipeRingBuffer {
9688 pub fn all_write_ends_closed ( & self ) -> bool {
9789 self . write_end . as_ref ( ) . unwrap ( ) . upgrade ( ) . is_none ( )
9890 }
99-
100- /// 保存当前状态快照
101- pub fn save_state ( & self ) -> BufferSnapshot {
102- BufferSnapshot {
103- head : self . head ,
104- tail : self . tail ,
105- status : self . status ,
106- }
107- }
108-
109- /// 恢复到之前的状态
110- pub fn restore_state ( & mut self , snapshot : BufferSnapshot ) {
111- self . head = snapshot. head ;
112- self . tail = snapshot. tail ;
113- self . status = snapshot. status ;
114- }
11591}
11692
11793pub trait Pipe {
You can’t perform that action at this time.
0 commit comments