Thread: Need Help with Count function

  1. #16
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MSkiLLz View Post
    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.
    State the problem clearly; is the parent going to count the multiples of 5 and the child process count the multiples of 3? and you said that the child can't use a for loop. Correct?

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by itCbitC View Post
    State the problem clearly; is the parent going to count the multiples of 5 and the child process count the multiples of 3? and you said that the child can't use a for loop. Correct?
    Yes. The parent counts the multiples of 5 and the values that aren't multiples are passed to the child to count the multiples of 3. After that the parent displays both the count values of child and parent. The child doesn't use a for loop.

  3. #18
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Made some modifications to your program but ain't sure if it cuts it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(void)
    {
        pid_t childpid;
        int count[2];
        int fd[2];
        int m;
    
        pipe(fd);
    
        if ((childpid = fork()) == -1) {
            perror("fork");
            exit(1);
        }
        else if (childpid == 0) {
            dup2(fd[0], 0);
            read(fd[0], count, sizeof count);
            printf("child process: no. of multiples of 5 are %d"
                " and those of 3 are %d\n", count[0], count[1]);
        }
        else if (childpid > 0) {
            close(fd[0]);
            dup2(fd[1], 1);
            for (m = 0; m < 10001; m++) {
                if ((m%5) == 0)
                    count[0]++;
                if ((m%3) == 0)
                    count[1]++;
            }
            write(fd[1], count, sizeof count);
        }
        return 0;
    }
    Last edited by itCbitC; 03-19-2009 at 06:42 PM.

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    The outputs I get are "1075122321 and those of 3 are 1073836614". I was wondering if having a larger buffer would make this program work right but I noticed that you took it off. What could be making this number so large? its pretty much the same weird output I was talking about initially.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MSkiLLz View Post
    The outputs I get are "1075122321 and those of 3 are 1073836614". I was wondering if having a larger buffer would make this program work right but I noticed that you took it off. What could be making this number so large? its pretty much the same weird output I was talking about initially.
    Is that from the code posted here??

    Edit: I get this output "child process: no. of multiples of 5 are 2001 and those of 3 are 3334"
    Last edited by itCbitC; 03-19-2009 at 06:49 PM.

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by itCbitC View Post
    Is that from the code posted here??

    Edit: I get this output "child process: no. of multiples of 5 are 2001 and those of 3 are 3334"
    Yeah. I'm using the same code. Maybe its my compiler? I'm using PuTTy.

  7. #22
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Perhaps something got lost in the copy 'n paste. What compiler are you using? Post the code you're compiling.

  8. #23
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by itCbitC View Post
    Perhaps something got lost in the copy 'n paste. What compiler are you using? Post the code you're compiling.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(void)
    {
        pid_t childpid;
        int count[2];
        int fd[2];
        int m;
    
        pipe(fd);
    
        if ((childpid = fork()) == -1) {
            perror("fork");
            exit(1);
        }
        else if (childpid == 0) {
            dup2(fd[0], 0);
            read(fd[0], count, sizeof count);
            printf("child process: no. of multiples of 5 are %d"
                " and those of 3 are %d\n", count[0], count[1]);
        }
        else if (childpid > 0) {
            close(fd[0]);
            dup2(fd[1], 1);
            for (m = 0; m < 10001; m++) {
                if ((m%5) == 0)
                    count[0]++;
                if ((m%3) == 0)
                    count[1]++;
            }
            write(fd[1], count, sizeof count);
        }
        return 0;
    }
    This is the code. I'm using PuTTy.

  9. #24
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    PuTTy is not a compiler but a software for terminal emulation.

  10. #25
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Oh. I guess I should ask you what you used and try that out.

  11. #26
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What platform and OS are you on?

  12. #27
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Windows Vista.

  13. #28
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Sorry for the double post but can someone tell me why the outputs I get are extremely large for the last code posted?

  14. #29
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What command are you issuing to compile your C program, and please learn the difference between a compiler and a terminal emulator.
    *PuTTY is not a compiler*

  15. #30
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    I'm using the gcc command and executing it with ./a.out.

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