-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add comments to process module and minor refactoring #64
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,72 @@ | ||
use std::{io::Read, time::Duration}; | ||
use std::io::Read; | ||
|
||
use super::{MAX_EVENTS, WAIT_FOR_CHILD}; | ||
use crate::process::message::Message; | ||
use anyhow::{bail, Result}; | ||
use mio::unix::pipe; | ||
use mio::unix::pipe::{Receiver, Sender}; | ||
use mio::{Events, Interest, Poll, Token}; | ||
|
||
use crate::process::message::Message; | ||
|
||
// Token is used to identify which socket generated an event | ||
const PARENT: Token = Token(0); | ||
|
||
/// Contains receiving end of pipe to child process and a poller for that. | ||
pub struct ParentProcess { | ||
receiver: Receiver, | ||
poll: Poll, | ||
} | ||
|
||
// Poll is used to register and listen for various events | ||
// by registering it with an event source such as receiving end of a pipe | ||
impl ParentProcess { | ||
/// Create new Parent process structure | ||
pub fn new() -> Result<(Self, Sender)> { | ||
// create a new pipe | ||
let (sender, mut receiver) = pipe::new()?; | ||
// create a new poll, and register the receiving end of pipe to it | ||
// This will poll for the read events, so when data is written to sending end of the pipe, | ||
// the receiving end will be readable and poll wil notify | ||
let poll = Poll::new()?; | ||
poll.registry() | ||
.register(&mut receiver, PARENT, Interest::READABLE)?; | ||
Ok((Self { receiver, poll }, sender)) | ||
} | ||
|
||
/// Waits for associated child process to send ready message | ||
/// and return the pid of init process which is forked by child process | ||
pub fn wait_for_child_ready(&mut self) -> Result<i32> { | ||
let mut events = Events::with_capacity(128); | ||
self.poll.poll(&mut events, Some(Duration::from_secs(5)))?; | ||
// Create collection with capacity to store up to MAX_EVENTS events | ||
let mut events = Events::with_capacity(MAX_EVENTS); | ||
|
||
// poll the receiving end of pipe created for WAIT_FOR_CHILD duration for an event | ||
self.poll.poll(&mut events, Some(WAIT_FOR_CHILD))?; | ||
for event in events.iter() { | ||
// check if the event token in PARENT | ||
// note that this does not assign anything to PARENT, but instead compares PARENT and event.token() | ||
// check http://patshaughnessy.net/2018/1/18/learning-rust-if-let-vs--match for a bit more detailed explanation | ||
if let PARENT = event.token() { | ||
// read data from pipe | ||
let mut buf = [0; 1]; | ||
self.receiver.read_exact(&mut buf)?; | ||
// convert to Message wrapper | ||
match Message::from(u8::from_be_bytes(buf)) { | ||
Message::ChildReady => { | ||
// read pid of init process forked by child, 4 bytes as the type is i32 | ||
let mut buf = [0; 4]; | ||
self.receiver.read_exact(&mut buf)?; | ||
return Ok(i32::from_be_bytes(buf)); | ||
} | ||
msg => bail!("receive unexpected message {:?} in parent process", msg), | ||
} | ||
} else { | ||
// as the poll is registered with only parent token | ||
unreachable!() | ||
} | ||
} | ||
bail!("unexpected message.") | ||
// should not reach here, as there should be a ready event from child within WAIT_FOR_CHILD duration | ||
unreachable!( | ||
"No message received from child process within {} seconds", | ||
WAIT_FOR_CHILD.as_secs() | ||
); | ||
Comment on lines
+67
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍