Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
4
Добавлен:
26.05.2014
Размер:
872 б
Скачать
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

void do_child(int data_pipe[]) {
int c;
int rc;
FILE *f;
close(1);
f=open("res.txt",O_WRONLY|O_CREAT|O_TRUNC);
close(data_pipe[1]);
while ((rc = read(data_pipe[0], &c, 1)) > 0) {
fprintf(stdout,"%c",c);
}
exit(0);
}

void do_parent(int data_pipe[])
{
int c;
int rc;
FILE *q;
q=fopen("f.dat","r");
close(data_pipe[0]);
while ((c = fgetc(q)) > 0) {
rc = write(data_pipe[1], &c, 1);
if (rc == -1) { 
perror("Parent: write");
close(data_pipe[1]);
exit(1);
}
}
close(data_pipe[1]);
exit(0);
}

int main(int argc, char* argv[])
{
int data_pipe[2]; 
int pid;
int rc;
rc = pipe(data_pipe);
if (rc == -1) {
perror("pipe");
exit(1);
}
pid = fork();
switch (pid) {
case -1:
perror("fork faild");
exit(1);
case 0:
do_child(data_pipe);
default:
do_parent(data_pipe);
}
return 0;
}
Соседние файлы в папке 2
  • #
    26.05.201412.14 Кб4a.out
  • #
    26.05.201415 б4f.dat
  • #
    26.05.2014872 б4q.c
  • #
    26.05.201415 б4res.txt