From owner-freebsd-questions@FreeBSD.ORG Sun Jan 15 16:18:46 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id DCB131065673 for ; Sun, 15 Jan 2012 16:18:46 +0000 (UTC) (envelope-from invalid.pointer@gmail.com) Received: from mail-iy0-f182.google.com (mail-iy0-f182.google.com [209.85.210.182]) by mx1.freebsd.org (Postfix) with ESMTP id A904D8FC15 for ; Sun, 15 Jan 2012 16:18:46 +0000 (UTC) Received: by iagz16 with SMTP id z16so3516314iag.13 for ; Sun, 15 Jan 2012 08:18:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=sQ/AF6g28hzUJc3mMkq5E2x5HSQXrwux38Wk10qLkjY=; b=CnRLe25s04khl+lIXDWbC/pOiCX8TY6p9TL+xDDEPEeHF6vfAL4F5aWgXtlKVk3URn 1yZtjKhcnQuvyaMoROSrvkPuFJHhZeq469oVOA+Es4XzxW59Y1RI9VuttO+t0+BbP7M+ N0qfCL2G3NzjZ1jIrKy+r93BhzjZ+a29Vhfl0= Received: by 10.50.46.166 with SMTP id w6mr6568218igm.6.1326642441685; Sun, 15 Jan 2012 07:47:21 -0800 (PST) Received: from [127.0.0.1] ([223.236.160.213]) by mx.google.com with ESMTPS id z22sm54730894ibg.5.2012.01.15.07.47.17 (version=SSLv3 cipher=OTHER); Sun, 15 Jan 2012 07:47:20 -0800 (PST) Message-ID: <4F12F56B.8030904@gmail.com> Date: Sun, 15 Jan 2012 21:18:59 +0530 From: Manish Jain User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: freebsd-questions@freebsd.org Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Question on select() : why am I getting absurd output ? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Jan 2012 16:18:46 -0000 Hi All, I was trying to write a small demo code using the select() system call. Here are the sources : #include #include #include #include #include #include int nice_child(int * fd, int * fd_close) { close(fd[0]); close(fd_close[0]); close(fd_close[1]); char buffer[32]; while (1) { sleep(3); strcpy(buffer, "I love my wife !"); write(fd[1], buffer, strlen(buffer) + 1); } return 0; } int naughty_child(int * fd, int * fd_close) { close(fd[0]); close(fd_close[0]); close(fd_close[1]); char buffer[32]; while (1) { sleep(4); strcpy(buffer, "I love your wife !"); write(fd[1], buffer, strlen(buffer) + 1); } return 0; } int main() { int fd_nice[2]; int fd_naughty[2]; pipe(fd_nice); pipe(fd_naughty); if (fork() == 0) { return nice_child(fd_nice, fd_naughty); } else { if (fork() == 0) { return naughty_child(fd_naughty, fd_nice); } } close(fd_nice[1]); close(fd_naughty[1]); fd_set fdset; char buffer[64]; int fd = (*fd_naughty > *fd_nice) ? *fd_naughty : *fd_nice; FD_ZERO(&fdset); FD_SET(fd_nice[0], &fdset); FD_SET(fd_naughty[0], &fdset); while (1) { int result = select(fd + 1, &fdset, 0, 0, 0); assert(result > 0); if (FD_ISSET(fd_nice[0], &fdset)) { int result = read(fd, buffer, sizeof(buffer)); buffer[result] = 0; std::cout << "Nice child sent : " << buffer << std::endl; } if (FD_ISSET(fd_naughty[0], &fdset)) { int result = read(fd, buffer, sizeof(buffer)); buffer[result] = 0; std::cout << "Naughty child sent : " << buffer << std::endl; } } return 0; } I was expecting the output to be like : Nice child sent : I love my wife ! Naughty child sent : I love your wife ! Nice child sent : I love my wife ! But what I actually get is : Nice child sent : I love your wife ! Nice child sent : I love your wife ! Nice child sent : I love your wife ! Nice child sent : I love your wife ! Nice child sent : I love your wife ! Nice child sent : I love your wife ! Can somebody throw some light on what might be wrong ? Thank you & Regards Manish Jain invalid.pointer@gmail.com