pipe and redirecting sort
I make sort program.
+--------------+ +--------------+
stdin >0 >== pipe to sort ====> |
| in_out | | sort |
stdout <1 <== pipe from sort ==< |
+--------------+ +--------------+
my idea is like that. i did not work well....
i don't know where is problem. i think i do well pipe and redirection.
i can't find my mistakes.
first, i get two pipes.
sencend, fork(input_output and sort)
connected stdin and out to pipes then execl sort
get string and send sort and sorting then return and prnit.
then close pipe and sort dies.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#define oops(m,x) { perror(m); exit(x); }
void be_sort(int in[2], int out[2]);
void in_out(int todc[2], int fromdc[2]);
void fatal(char mess);
int main()
{
int pid, tosort[2], fromsort[2]; /*equipment */
/* make two pipes */
if( pipe(tosort) == -1 || pipe(fromsort) == -1 )
oops("pipe failed", 1);
/* get a process for user interface */
if(( pid = fork()) == -1 )
oops("cannot fork", 2);
if( pid == 0 ) /* child is sort */
be_sort(tosort, fromsort);
else {
in_out(tosort, fromsort); /* parent is ui */
wait(NULL); /* wait for child */
}
return 0;
}
void be_sort(int in[2], int out[2])
/*
* set up stdin and stdout, then execl sort
*/
{
/* setuup stdin from pipein */
if( dup2(in[0], 0) == -1 ) /* copy read end to 0 */
oops("sort: cannot redirect stdin", 3);
close(in[0]); /* moved to fd 0 */
close(in[1]); /* won't write here */
/* setup stdout to pipeout */
if( dup2(out[1], 1) == -1) /* dupe write end to 1 */
oops("sort: cannot redirect stdout", 4);
close(out[1]); /* moved to fd 1 */
close(out[0]); /* won't read from here */
/* now execl sort with the - option */
execlp("sort", "sort", (char *)NULL);
oops("Cannot run sort", 5);
}
void in_out( int tosort[2], int fromsort[2])
{
char operation[BUFSIZ], message[BUFSIZ], *fgets();
FILE *fpout, *fpin, *fdopen();
/* setup */
close(tosort[0]); /* won't read from pipe to sort */
close(fromsort[1]); /* won't write to pipe from sort*/
fpout = fdopen( tosort[1], "w" ); /* convert file descriptors to stream */
fpin = fdopen( fromsort[0], "r" );
if( fpout == NULL || fpin == NULL )
fatal("Error converting pipes to streams");
/* main loop */
while( fgets(message, BUFSIZ, stdin) != NULL )
{
message[strlen(message)-1]=NULL;
printf("%s ,%ldn",message,strlen(message));
if (fprintf(fpout, "%s", *message) == EOF)
fatal("writing");
fflush( fpout );
if (fgets(operation, BUFSIZ, fpin) == NULL)
break;
printf("%s", operation); // stdout
}
fclose(fpout); /* close pipe */
fclose(fpin); /* sort will see EOF */
}
void fatal( char mess )
{
fprintf(stderr, "Error: %sn", mess);
exit(1);
}
redirect pipe fork
add a comment |
I make sort program.
+--------------+ +--------------+
stdin >0 >== pipe to sort ====> |
| in_out | | sort |
stdout <1 <== pipe from sort ==< |
+--------------+ +--------------+
my idea is like that. i did not work well....
i don't know where is problem. i think i do well pipe and redirection.
i can't find my mistakes.
first, i get two pipes.
sencend, fork(input_output and sort)
connected stdin and out to pipes then execl sort
get string and send sort and sorting then return and prnit.
then close pipe and sort dies.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#define oops(m,x) { perror(m); exit(x); }
void be_sort(int in[2], int out[2]);
void in_out(int todc[2], int fromdc[2]);
void fatal(char mess);
int main()
{
int pid, tosort[2], fromsort[2]; /*equipment */
/* make two pipes */
if( pipe(tosort) == -1 || pipe(fromsort) == -1 )
oops("pipe failed", 1);
/* get a process for user interface */
if(( pid = fork()) == -1 )
oops("cannot fork", 2);
if( pid == 0 ) /* child is sort */
be_sort(tosort, fromsort);
else {
in_out(tosort, fromsort); /* parent is ui */
wait(NULL); /* wait for child */
}
return 0;
}
void be_sort(int in[2], int out[2])
/*
* set up stdin and stdout, then execl sort
*/
{
/* setuup stdin from pipein */
if( dup2(in[0], 0) == -1 ) /* copy read end to 0 */
oops("sort: cannot redirect stdin", 3);
close(in[0]); /* moved to fd 0 */
close(in[1]); /* won't write here */
/* setup stdout to pipeout */
if( dup2(out[1], 1) == -1) /* dupe write end to 1 */
oops("sort: cannot redirect stdout", 4);
close(out[1]); /* moved to fd 1 */
close(out[0]); /* won't read from here */
/* now execl sort with the - option */
execlp("sort", "sort", (char *)NULL);
oops("Cannot run sort", 5);
}
void in_out( int tosort[2], int fromsort[2])
{
char operation[BUFSIZ], message[BUFSIZ], *fgets();
FILE *fpout, *fpin, *fdopen();
/* setup */
close(tosort[0]); /* won't read from pipe to sort */
close(fromsort[1]); /* won't write to pipe from sort*/
fpout = fdopen( tosort[1], "w" ); /* convert file descriptors to stream */
fpin = fdopen( fromsort[0], "r" );
if( fpout == NULL || fpin == NULL )
fatal("Error converting pipes to streams");
/* main loop */
while( fgets(message, BUFSIZ, stdin) != NULL )
{
message[strlen(message)-1]=NULL;
printf("%s ,%ldn",message,strlen(message));
if (fprintf(fpout, "%s", *message) == EOF)
fatal("writing");
fflush( fpout );
if (fgets(operation, BUFSIZ, fpin) == NULL)
break;
printf("%s", operation); // stdout
}
fclose(fpout); /* close pipe */
fclose(fpin); /* sort will see EOF */
}
void fatal( char mess )
{
fprintf(stderr, "Error: %sn", mess);
exit(1);
}
redirect pipe fork
add a comment |
I make sort program.
+--------------+ +--------------+
stdin >0 >== pipe to sort ====> |
| in_out | | sort |
stdout <1 <== pipe from sort ==< |
+--------------+ +--------------+
my idea is like that. i did not work well....
i don't know where is problem. i think i do well pipe and redirection.
i can't find my mistakes.
first, i get two pipes.
sencend, fork(input_output and sort)
connected stdin and out to pipes then execl sort
get string and send sort and sorting then return and prnit.
then close pipe and sort dies.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#define oops(m,x) { perror(m); exit(x); }
void be_sort(int in[2], int out[2]);
void in_out(int todc[2], int fromdc[2]);
void fatal(char mess);
int main()
{
int pid, tosort[2], fromsort[2]; /*equipment */
/* make two pipes */
if( pipe(tosort) == -1 || pipe(fromsort) == -1 )
oops("pipe failed", 1);
/* get a process for user interface */
if(( pid = fork()) == -1 )
oops("cannot fork", 2);
if( pid == 0 ) /* child is sort */
be_sort(tosort, fromsort);
else {
in_out(tosort, fromsort); /* parent is ui */
wait(NULL); /* wait for child */
}
return 0;
}
void be_sort(int in[2], int out[2])
/*
* set up stdin and stdout, then execl sort
*/
{
/* setuup stdin from pipein */
if( dup2(in[0], 0) == -1 ) /* copy read end to 0 */
oops("sort: cannot redirect stdin", 3);
close(in[0]); /* moved to fd 0 */
close(in[1]); /* won't write here */
/* setup stdout to pipeout */
if( dup2(out[1], 1) == -1) /* dupe write end to 1 */
oops("sort: cannot redirect stdout", 4);
close(out[1]); /* moved to fd 1 */
close(out[0]); /* won't read from here */
/* now execl sort with the - option */
execlp("sort", "sort", (char *)NULL);
oops("Cannot run sort", 5);
}
void in_out( int tosort[2], int fromsort[2])
{
char operation[BUFSIZ], message[BUFSIZ], *fgets();
FILE *fpout, *fpin, *fdopen();
/* setup */
close(tosort[0]); /* won't read from pipe to sort */
close(fromsort[1]); /* won't write to pipe from sort*/
fpout = fdopen( tosort[1], "w" ); /* convert file descriptors to stream */
fpin = fdopen( fromsort[0], "r" );
if( fpout == NULL || fpin == NULL )
fatal("Error converting pipes to streams");
/* main loop */
while( fgets(message, BUFSIZ, stdin) != NULL )
{
message[strlen(message)-1]=NULL;
printf("%s ,%ldn",message,strlen(message));
if (fprintf(fpout, "%s", *message) == EOF)
fatal("writing");
fflush( fpout );
if (fgets(operation, BUFSIZ, fpin) == NULL)
break;
printf("%s", operation); // stdout
}
fclose(fpout); /* close pipe */
fclose(fpin); /* sort will see EOF */
}
void fatal( char mess )
{
fprintf(stderr, "Error: %sn", mess);
exit(1);
}
redirect pipe fork
I make sort program.
+--------------+ +--------------+
stdin >0 >== pipe to sort ====> |
| in_out | | sort |
stdout <1 <== pipe from sort ==< |
+--------------+ +--------------+
my idea is like that. i did not work well....
i don't know where is problem. i think i do well pipe and redirection.
i can't find my mistakes.
first, i get two pipes.
sencend, fork(input_output and sort)
connected stdin and out to pipes then execl sort
get string and send sort and sorting then return and prnit.
then close pipe and sort dies.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#define oops(m,x) { perror(m); exit(x); }
void be_sort(int in[2], int out[2]);
void in_out(int todc[2], int fromdc[2]);
void fatal(char mess);
int main()
{
int pid, tosort[2], fromsort[2]; /*equipment */
/* make two pipes */
if( pipe(tosort) == -1 || pipe(fromsort) == -1 )
oops("pipe failed", 1);
/* get a process for user interface */
if(( pid = fork()) == -1 )
oops("cannot fork", 2);
if( pid == 0 ) /* child is sort */
be_sort(tosort, fromsort);
else {
in_out(tosort, fromsort); /* parent is ui */
wait(NULL); /* wait for child */
}
return 0;
}
void be_sort(int in[2], int out[2])
/*
* set up stdin and stdout, then execl sort
*/
{
/* setuup stdin from pipein */
if( dup2(in[0], 0) == -1 ) /* copy read end to 0 */
oops("sort: cannot redirect stdin", 3);
close(in[0]); /* moved to fd 0 */
close(in[1]); /* won't write here */
/* setup stdout to pipeout */
if( dup2(out[1], 1) == -1) /* dupe write end to 1 */
oops("sort: cannot redirect stdout", 4);
close(out[1]); /* moved to fd 1 */
close(out[0]); /* won't read from here */
/* now execl sort with the - option */
execlp("sort", "sort", (char *)NULL);
oops("Cannot run sort", 5);
}
void in_out( int tosort[2], int fromsort[2])
{
char operation[BUFSIZ], message[BUFSIZ], *fgets();
FILE *fpout, *fpin, *fdopen();
/* setup */
close(tosort[0]); /* won't read from pipe to sort */
close(fromsort[1]); /* won't write to pipe from sort*/
fpout = fdopen( tosort[1], "w" ); /* convert file descriptors to stream */
fpin = fdopen( fromsort[0], "r" );
if( fpout == NULL || fpin == NULL )
fatal("Error converting pipes to streams");
/* main loop */
while( fgets(message, BUFSIZ, stdin) != NULL )
{
message[strlen(message)-1]=NULL;
printf("%s ,%ldn",message,strlen(message));
if (fprintf(fpout, "%s", *message) == EOF)
fatal("writing");
fflush( fpout );
if (fgets(operation, BUFSIZ, fpin) == NULL)
break;
printf("%s", operation); // stdout
}
fclose(fpout); /* close pipe */
fclose(fpin); /* sort will see EOF */
}
void fatal( char mess )
{
fprintf(stderr, "Error: %sn", mess);
exit(1);
}
redirect pipe fork
redirect pipe fork
asked Nov 23 '18 at 15:45
ジョユンサンジョユンサン
135
135
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449550%2fpipe-and-redirecting-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449550%2fpipe-and-redirecting-sort%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown