From owner-freebsd-questions@FreeBSD.ORG Sun Jan 15 17:36:29 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 871B81065677 for ; Sun, 15 Jan 2012 17:36:29 +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 4B1A28FC0C for ; Sun, 15 Jan 2012 17:36:28 +0000 (UTC) Received: by iagz16 with SMTP id z16so3618070iag.13 for ; Sun, 15 Jan 2012 09:36:28 -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:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=Pr2Erl936PoO1RUbyHuOc9uGeZqLFxBzVBliLxUm2R4=; b=Kr9HHPcTJwa4vusC69TsWWR7rdbGYTzlkLt6RkBzufXIaRKFs+7HIcIhOg0JTqKUi1 aWtJpyaC6et1e/2/wUT1z0ujWT1wSH0u/PAVqTSRPXgG7eBrnAyCJXT+YPKJlSOS0eqg JJ773PUUCGxWKre6UHzveod0mgWgmWhQ71jEQ= Received: by 10.42.164.71 with SMTP id f7mr7650390icy.49.1326648988666; Sun, 15 Jan 2012 09:36:28 -0800 (PST) Received: from [127.0.0.1] ([223.236.159.52]) by mx.google.com with ESMTPS id bj3sm20243806igb.4.2012.01.15.09.36.25 (version=SSLv3 cipher=OTHER); Sun, 15 Jan 2012 09:36:27 -0800 (PST) Message-ID: <4F130EFD.8070306@gmail.com> Date: Sun, 15 Jan 2012 23:08:05 +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: ss griffon References: <4F12F56B.8030904@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: Re: 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 17:36:29 -0000 Sometimes I do wonder how much stupid I can be. Thanks MJ On 15-Jan-12 22:49, ss griffon wrote: > On Sun, Jan 15, 2012 at 8:48 AM, Manish Jain wrote: >> >> 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 >> >> _______________________________________________ >> freebsd-questions@freebsd.org mailing list >> http://lists.freebsd.org/mailman/listinfo/freebsd-questions >> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" > > > > It looks like you are always reading from the same file descriptor > 'fd'. Instead you should read from fd_naughty[0] or fd_nice[0] based > on your FD_ISSET checks. Also, don't forget to wait() for your child > processes. >