LinuxForum.Fans
Тема

разные выводы

Автор: trung_bui 3 сообщений 974 просмотров
19.05.2017 20:06 #423152

запустил код на сайте ideone.com

text

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main() { int fd[2], result; size_t size; char resstring[14];

if(pipe(fd) < 0) { printf("Can\'t create pipe\n"); exit(-1); }

result = fork(); if(result < 0) { printf("Can\'t fork child\n"); exit(-1); } else if (result > 0) { /* процесс-предок*/ close(fd[0]); printf("Father is going to send a letter to his beloved son...\n"); size = write(fd[1], "Hello, world!", 14); if(size != 14) { printf("Can\'t write all string\n"); exit(-1); } close(fd[1]); printf("Father wants his letter to reach the final destination.\n"); printf("Parent exit\n"); } else { /* процесс - потомок*/ close(fd[1]); printf("Son decides to check his inbox...\n"); size = read(fd[0], resstring, 14); if(size < 0) { printf("Can\'t read string\n"); exit(-1); } printf("Son has just recieved a nice letter from his father.\n"); printf("Here it is:\n"); printf("\"%s\"\n",resstring); close(fd[0]); } getchar(); return 0; }

Результат:

Father is going to send a letter to his beloved son...
Father wants his letter to reach the final destination.
Parent exit
Son decides to check his inbox...
Son has just recieved a nice letter from his father.
Here it is:
"Hello, world!"


а когда запустил код под линуксе
такой результат:
Father is going to send a letter to his beloved son...
Son decides to check his inbox...
Father wants his letter to reach the final destination.
Parent exit
Son has just recieved a nice letter from his father.
Here it is:
"Hello, world!"


В чем же проблем??
Мне надо чтобы код запустил под линуксом выдал результат как на сайте ideone.com
т.е
Father is going to send a letter to his beloved son...
Father wants his letter to reach the final destination.
Parent exit
Son decides to check his inbox...
Son has just recieved a nice letter from his father.
Here it is:
"Hello, world!"
Исправьте,плиз

19.05.2017 21:17 #423154

Опять же. Не используйте printf. Он работает через буферезированный выхлоп stdout. Юзайте

text

fprintf (stderr, "My string\n");
19.05.2017 21:47 #423156

Не сработал.Все равно такой же результат

Войдите или зарегистрируйтесь, чтобы ответить.