Architecture Pipe/dup2 - Chad Salinas ::: Data Scientist
Life and times of Chad Salinas
Chad Salinas, golf caddy, data scientist, chad rPubs, recovering chess addict, daddy caddy
934
post-template-default,single,single-post,postid-934,single-format-standard,qode-listing-1.0.1,qode-social-login-1.0,qode-news-1.0,qode-quick-links-1.0,qode-restaurant-1.0,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,qode-theme-ver-12.0.1,qode-theme-bridge,bridge,wpb-js-composer js-comp-ver-5.4.2,vc_responsive

Architecture Pipe/dup2

Architecture Pipe/dup2

Attribution:  I thank Jerry Cain at Stanford for inspiring the following content
Audience:  Folks with a strong foundation in computer science who want a starting point to direct their investigation into topical areas.
pipe
Goal: allow a forked process to communicate with text to a parent process and vice versa
File descriptors are the raw to source and sink bytes.
int fds[2]
parent and child can both read and write to fds[0] and fds[1] respectively.  So, we need to close the read or write end of the pipe to achieve the behavior we desire.  For example, if we only ever intend to write from the parent, we would immediately upon forking, close the read end of the pipe in the parent.  Both ends of the piple are duplicated across fork boundaries so it is cleaner to programatically close file descriptors that you don’t need to use just so the intent is clear.
dup2
dup2(fds[0], 0);  // running sort in the child, but sort wants to read from stdin, i.e. fd 0, so dup2 detaches the read end of the pipe at fds[0] and re-associates it to fd 0.

Any programming problem can be solved by adding a level of indirection.

– David J. Wheeler
No Comments

Sorry, the comment form is closed at this time.