LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2014, 07:07 AM   #1
one girl
LQ Newbie
 
Registered: Apr 2014
Posts: 20

Rep: Reputation: Disabled
Error in pipes code


I want this program communicate between parent and child ,the parent send signal to child choice the two number randomly, and then parent add that two number choice it by child and print the total .


Code:
#include <stdio.h> /* For printf */
#include <string.h> /* For strlen */
#include <stdlib.h> /* For exit */
 
main(){
    int pid, fd[2], bytes;
    int num1 = rand()%100; 
int num = rand()%100; 
    if (pipe(fd) == -1) { 
        perror("pipe"); 
        exit(1); 
    }
    if ((pid = fork()) == -1) { 
        perror("fork");
printf("Please Enter First number: ");    /*I want a Parent to send a sginal to the child then the child will choice random num between 0 and 100 */

printf("Please Enter First number: ");    /*I want a Parent to send a sginal to the child then the child will choice random num between 0 and 100 */

printf("Total number is : ");    //the parent will add the two random num

        exit(1); 
    }



    if (pid == 0) { 
 
close(fd[0]); 

write(fd[1], &num, sizeof(num));
sleep (3);
 read(fd[0], &num1, sizeof(num1));
       
       
    } 
    else { 
        close(fd[1]); 
        read(fd[0], &num1, sizeof(num1));
        close(fd[0]);  
    }
}

Last edited by one girl; 05-07-2014 at 08:50 AM.
 
Old 05-07-2014, 08:14 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
So the program does work to a degree however you have a typo and there's no such system function named praintf() instead it would be printf().

Note also that in your if statement to test that the fork() failed, you have your printf() statements and they don't make a lot of sense, because you're not taking in any input from the console, by using any of getchar or scanf.

If you compile that using the -ggdb flag and use GDB to step through it, you get a random number and always get the SAME random number, because merely using rand() without selecting a randomized seed, srand() (suggest using time and date for your seed) then you end up getting a deterministic result; hence always that same number.

No process is distinctly sending a signal, the system is doing this because information has been placed into the pipe. The CHILD happens to be the one exciting this, the child is when you get pid of zero, therefore the logic validating that pid is zero is the one the writes to the pipe and the PARENT receives a return from the fork() giving it the child's PID, which is your else case.

The code works. I'd suggest adding a few printf() statements to see what the value of num1 is at the start of the program, see what the descriptor values for fd[2] are; you can also print those out, and then put some printf() statements in the if-else cases to show your progress of reading and writing from/to the pipe, in those cases ALL printf's will show up at your main console, so ensure that you place something distinctive in there such as "PARENT: Read %d from pipe", or "CHILD: Wrote %d to pipe" to clarify things for you.

I have a number of blog entries on these subjects, they are a mile further than what you're doing here, but if you wish to peruse them, they are here:

Using PIPES for Interprocess Communications
Select(2) and why I use it
Creating a daemon to launch and monitor your processes
How to kill those zombies
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in handling pipes concept while passing error and output in two files nagendrar Programming 1 11-06-2009 08:11 AM
examples with 2 pipes+fork+handling error,output in different files nagendrar Programming 4 11-06-2009 06:36 AM
Error in Perl Code : Bad switch statement(Problem in code block)? near ## line # suyog255 Programming 4 02-20-2008 05:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration