Thread: Need Help with Count function

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    31

    Need Help with Count function

    Hi,

    I'm currently trying to write a code using pipes that counts the multiples of 5 that are between numbers 1-10000. The code i've written so far gives me some pretty weird outputs. I think my problem is the count function in my for loop Could someone help me with this? Thanks.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    
    int main()
    {
    	int fd[2]; 
    	int nbytes;
    	pid_t childpid;
    	int array[10001];
    	int a;
    	int count;
    	int m;	
    
    	pipe(fd);
    	
    		if((childpid = fork()) == -1)
    		{
    		perror("fork");
    		exit(1);
    		}
    			if(childpid == 0)
    			{
    			//Child process
    			//close(fd[1]);
    			//Reading arrays
    			//nbytes =read(fd[0],
    			}	
    		else
    		{	
    			//Parent process
    			//close(fd[0]);
    		}
    	for ( m = 0; m < 10001; m++) 
    	{
    	count=0;
    	a=1;
    	while(a<=m)
    	{
    		if((m%5) == 0)
    		count++;	
    		a++;
    		array[0]++;
    	}
    			//if( count == 2000) {
    			printf("%d\n",array[0]); //}
    	}
    
    	//array[m]=m;		
    
    
    			//Write arrays
    	//write(fd[1],
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    To use pipes as a communications channel, redirect I/O of each of the processes involved. That is, if child process is the one reading from the pipe, close its stdout and attach its stdin to one end of the pipe. If parent process is the one writing to the pipe, close its stdin and attach its stdout to other end of the pipe. Don't see that anywhere in the current code which may explain why you're seeing "some pretty weird outputs".

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    I guess I should have explained my problem better. I'm trying to count all the multiples of 5 between 1-10000. The numbers that aren't multiples of 5 are sent into to the child process to search for multiples of 3. After that, the results in the child are sent to the parent will print the count of the multiples of 3 and 5.

    When I run the code I have so far, I get a display of 0,0,0,0,0,0,5,0,0,0,0,10..to 5000. I was expecting the count to increment each time it gets a multiple of 5 and give me the total number of multiples but its not doing that.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The way the program is written there's no connection between the parent and child process and counting multiples of 5 is done in both the child and parent processes. The output is garbled depending on which process is currently executing. In a nutshell the pipe has been created but it is not being used.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Well, I was thinking of having the parent write to the child process and the child will do a read function. Is it easier to use a stdin and stdout?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you post the code you are using. The one you posted earlier is mostly commented out so it's not easy to figure out what statements are being executed. To have the parent be the writer and child the reader; redirect stdout of the parent to the pipe and close its stdin stream. In the child close stdout and attach stdin to the pipe.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    I'm still sort of a novice at using pipes. How do you implement the stdout and stdin? I'm lookin for an example code. Here is what I have so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    
    int main()
    {
    	int fd[2]; 
    	int nbytes;
    	pid_t childpid;
    	int array[10001];
    	int a;
    	int count;
    	int m;	
    
    	pipe(fd);
    	
    		if((childpid = fork()) == -1)
    		{
    		perror("fork");
    		exit(1);
    		}
    			if(childpid == 0)
    			{
    			//Child process
    
    			}	
    		else
    		{	
    			//Parent process
    
    		}
    	for ( m = 0; m < 10001; m++) 
    	{
    	count=0;
    	a=1;
    	while(a<=m)
    	{
    		if((m%5) == 0)
    		count++;	
    		a++;
    		array[0]++;
    	}
    			printf("%d\n",array[0]);
    	}
    
    
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Check out this item on pipes in the FAQ.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MSkiLLz View Post
    Well, I was thinking of having the parent write to the child process and the child will do a read function. Is it easier to use a stdin and stdout?
    It is easier to use stdin and stdout but then there is going to be no channel for communications between the parent and child processes which the pipe() call provides.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MSkiLLz View Post
    ... I'm lookin for an example code.
    Here's one.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main()
    {
        int fd[2]; 
        pid_t childpid;
        int count = 0;
        int m;
        char buf[100];
    
        pipe(fd);
    
        if ((childpid = fork()) == -1) {
            perror("fork");
            exit(1);
        }
        else if (childpid == 0) {
            dup2(fd[0], 0);
            read(fd[0], buf, sizeof buf);
            printf("child process: no. of multiples of 5 is %s\n", buf);
        }
        else if (childpid > 0) {
            close(fd[0]);
            dup2(fd[1], 1);
            for (m = 0; m < 1001; m++)
                if ((m%5) == 0)
                    count++;
            printf("%d", count);
        } 
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Thanks itCbitC. That example helped alot. Now its actually counting multiples instead spitting out random stuff. I thought this program would have to use arrays though since i have to pass the numbers that aren't multiples of 5 to the child process to find multiples of 3. One little note I left out was that I was told the parent is the only one that runs the for loop. How exactly is that possible?

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Sorry for the double post but I've tried editing the code trying to pass the values to the child process to find the multiple of 3 but I don't know if using the write function is the right way.

  13. #13
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    . . . and so why don't you post how you're using the write function?

    Here is the man page for the write function. You pass it three things . . . the file descriptor, the data to write, and the size of the data being written.
    Last edited by Nightowl; 03-19-2009 at 02:44 PM.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    here it is. Sorry if y'all don't understand what i've added since I'm mostly doing trail and error over here.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main()
    {
        int fd[2]; 
        pid_t childpid;
        int count = 0;
        int m;
        char buf[100];
    
        pipe(fd);
    
        if ((childpid = fork()) == -1) {
            perror("fork");
            exit(1);
        }
        else if (childpid == 0) {
            dup2(fd[0], 0);
            read(fd[0], buf, sizeof buf);
            printf("child process: no. of multiples of 5 is %s\n", buf);
    	read(fd[1], buf, sizeof buf);					//F	
    	printf("child process: no. of multiples of 3 is %s\n", buf);	//F
        }
        else if (childpid > 0) {
            close(fd[0]);
            dup2(fd[1], 1);
            for (m = 0; m < 10001; m++)
                if ((m%5) == 0)
                    count++;
            printf("%d", count); }
    	     else if (childpid > 0) {					//F
    	for (m= 0; m < 10001; m++)						//F
    		if ((m%3) == 0)				
    		count++;
    		write(fd[1], buf, sizeof buf);
    	printf("%d", count);
        } 
        return 0;

  15. #15
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Not having used fork() for a while, I can't say what's wrong with it . . .

    But exactly what happens when you run it? . . .

    EDIT: never mind, compiled it myself. I'll look at it in a moment.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM