date
とwc
をCentOSのパイプで使用しようとしています。私はprintf
には私がparent
またはchild
にいることができません。どんな助けもありがとうございます。フォーク後に親と子でPrintfが印刷されない
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
/* pipe1.c - send information through pipe. */
void syserr(char* msg)
{
printf("%s", msg);
}
void child(int pfd[]){
dup2(pfd[1],1);
execl("/bin/date", "date", 0);
}
void main()
{
int pfd[2], i, pid;
char str[] = "Hello World!\n";
if (pipe(pfd) == -1)
syserr("pipe");
printf("pfd[0] = %d, pfd[1] = %d\n", pfd[0], pfd[1]);
pid=fork();
switch(pid) {
case -1:
syserr("fork");
case 0:
{
printf("I'm child'");
child(pfd);
}
default:{ /* parent only */
if(pid!=0)
{
printf("I'm parent'");
dup2(pfd[0],0); //input
execl("/bin/wc", "wc", 0);
}/*default*/
} /*switch*/
}
}
'popen'を使用することを検討してください。そして、あなたは 'date'をフォークする必要はありません。 [time(7)](http://man7.org/linux/man-pages/man7/time.7.html)を読み、[time(2)](http://man7.org/linux/man -pages/man2/time.2.html)、[localtime_r(3)](http://man7.org/linux/man-pages/man3/localtime_r.3.html)、[strftime(3)](http ://man7.org/linux/man-pages/man3/strftime.3.html)(これは 'date'が使用しています) –
' syserr'を 'perror'で置き換えます。 –