#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Fork and have childs do different tasks


    hey guys, i'm not sure where i'm going wrong.

    my program generates random numbers and stores them in a file for example
    the 3 and 4 are given by the user, so 3 would be three lines and each line has four numbers. but that's not where the problem is

    3 4
    1 2 8 2
    5 6 4 3
    3 3 4 4

    my problem is i want to make the program fork and then have each child reads a different lines, so I'll fork to make childs as many lines as i have.

    now i know this has to deal with cursor positioning, but i'm not sure how to implement it.

    **


    here is a sample output
    ftell: 3
    Child1:
    My position BEFORE scan 6
    My position AFTER scan 7
    I am child: 1096. My parent is: 1095. The number i scanded is: 2
    Child1:
    My position BEFORE scan 7
    My position AFTER scan 9
    I am child: 1096. My parent is: 1095. The number i scanded is: 8
    Child1:
    My position BEFORE scan 9
    My position AFTER scan 11
    I am child: 1096. My parent is: 1095. The number i scanded is: 2
    Child1:
    My position BEFORE scan 11
    My position AFTER scan 13
    I am child: 1096. My parent is: 1095. The number i scanded is: 5
    TOTAL IS: 17
    I am parent 1095
    Child2:
    My position BEFORE scan 6
    My position AFTER scan 7
    I am child: 1097. My parent is: 1095. The number i scanded is: 2
    Child2:
    My position BEFORE scan 7
    My position AFTER scan 9
    I am child: 1097. My parent is: 1095. The number i scanded is: 8
    Child2:
    My position BEFORE scan 9
    My position AFTER scan 11
    I am child: 1097. My parent is: 1095. The number i scanded is: 2
    Child2:
    My position BEFORE scan 11
    My position AFTER scan 13
    I am child: 1097. My parent is: 1095. The number i scanded is: 5
    TOTAL IS: 17
    I am parent 1095
    Child3:
    My position BEFORE scan 6
    My position AFTER scan 7
    I am child: 1098. My parent is: 1095. The number i scanded is: 2
    Child3:
    My position BEFORE scan 7
    My position AFTER scan 9
    I am child: 1098. My parent is: 1095. The number i scanded is: 8
    Child3:
    My position BEFORE scan 9
    My position AFTER scan 11
    I am child: 1098. My parent is: 1095. The number i scanded is: 2
    Child3:
    My position BEFORE scan 11
    My position AFTER scan 13
    I am child: 1098. My parent is: 1095. The number i scanded is: 5
    TOTAL IS: 17
    I am parent 1095
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Perhaps you could investigate the use of [code][/code] tags as opposed to some barely readable colour in some barely readable font.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    bump


    bump
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    BUMP: go back and edit your post using code tags and formatted properly. Until we can read it, we won't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0
    ?? can you guys be more specific??? obviously i'm new to here, can i just upload the c file?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    [code] paste your code here [/code]

    HTML strips out extra white space, like leading blanks. Code tags keep that from happening.

    Just make sure that you do a proper job of indenting. I see that you use K&R format which hides open braces at the end of lines and even in the middle of lines. K&R absolutely depends on proper and consistent indentation. If you screw up the indentation with K&R, it's worse than no indentation at all.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by dwise1_aol
    [code] paste your code here [/code]

    HTML strips out extra white space, like leading blanks. Code tags keep that from happening.

    Just make sure that you do a proper job of indenting. I see that you use K&R format which hides open braces at the end of lines and even in the middle of lines. K&R absolutely depends on proper and consistent indentation. If you screw up the indentation with K&R, it's worse than no indentation at all.
    I know what you mean, but I'm not sure how to fix it.
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > I know what you mean, but I'm not sure how to fix it.
    Code:
    int main ( ) {
        // code goes here
    }
    Like this

    1a Copy the code from your editor, so that indentation is present (we can only hope).
    1b Highlight all the code.
    2. Press the # looking icon which displays wrap code tags around the selection.

    Boy, this is too much like hard work.
    Attached Images
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by salem
    > I know what you mean, but I'm not sure how to fix it.
    Code:
    int main ( ) {
        // code goes here
    }
    Like this

    1a Copy the code from your editor, so that indentation is present (we can only hope).
    1b Highlight all the code.
    2. Press the # looking icon which displays wrap code tags around the selection.

    Boy, this is too much like hard work.
    appreciate it. Now i resolved the problem each child is reading the line they are supposed to be reading,
    but when i run the program and check the process i see <defunct> i'm guessing that means i have a zombie, but i'm not sure how to fix that. any ideas?
  18. #10
  19. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > but when i run the program and check the process i see <defunct> i'm guessing that means i have a zombie, but i'm not sure how to fix that. any ideas?
    For each child, you need to wait() for it in the parent.

    waitpid(-1, &status, WNOHANG);
    is particularly useful if there are a lot of child processes and you don't know all the PID's
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by salem
    > but when i run the program and check the process i see <defunct> i'm guessing that means i have a zombie, but i'm not sure how to fix that. any ideas?
    For each child, you need to wait() for it in the parent.

    waitpid(-1, &status, WNOHANG);
    is particularly useful if there are a lot of child processes and you don't know all the PID's
    how do I wait for each child in the parent?

    i tried this by it self but same thing "waitpid(-1,&status,WNOHANG)
  22. #12
  23. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    First off, read the manual.
    #include <sys/types.h>
    #include <sys/wait.h>

    pid_t wait(int *status);
    Notice that wait() has only ONE parameter, and you're calling it with three!

    Which leads me to - warnings and error messages.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:55:7: warning: implicit declaration of function ‘wait’ [-Wimplicit-function-declaration]
    foo.c:13:12: warning: unused variable ‘pos’ [-Wunused-variable]
    foo.c:11:9: warning: unused variable ‘end’ [-Wunused-variable]
    foo.c:61:1: warning: control reaches end of non-void function [-Wreturn-type]
    Compile with as many as you can find, and do NOT ignore them.

    When I include the right header files, the situation is clearer.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:57:7: error: incompatible type for argument 1 of ‘wait’
    /usr/include/x86_64-linux-gnu/sys/wait.h:116:16: note: expected ‘__WAIT_STATUS’ but argument is of type ‘int’
    foo.c:57:7: error: too many arguments to function ‘wait’
    /usr/include/x86_64-linux-gnu/sys/wait.h:116:16: note: declared here
    foo.c:15:12: warning: unused variable ‘pos’ [-Wunused-variable]
    foo.c:13:9: warning: unused variable ‘end’ [-Wunused-variable]
    foo.c:63:1: warning: control reaches end of non-void function [-Wreturn-type]
    Fixing this, results in.
    Code:
    $ ./a.out 
    I am child 4034 and my parent is 4033
    Parent done waiting for 4034, found 4034
    I am child 4035 and my parent is 4033
    Parent done waiting for 4035, found 4035
    I am child 4036 and my parent is 4033
    Parent done waiting for 4036, found 4036
    I am child 4037 and my parent is 4033
    Parent done waiting for 4037, found 4037
    I am child 4038 and my parent is 4033
    Parent done waiting for 4038, found 4038
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main(){
      long int i, j;
      int pid;
      int rows, columns;
      FILE *input;
      int number;
      pid_t end;
      int temp;
      int total = 0;
      FILE *output;
    
      rows = 5;
      columns = 5;
      output = fopen("input.dat", "w");
      fprintf(output,"%d %d\n", rows, columns);
      for (i = 0; i < rows; i++){
        for (j = 0; j < columns; j++){
          fprintf(output, "%d ", 1 + rand() % 9);
        }
        fprintf(output, "\n");
      }
      fclose(output);
    
      input = fopen("input.dat", "r");
      fscanf(input,"%d %d", &rows, &columns);
    
      for (i = 0; i < rows; i++){
        pid = fork();
        if (pid == 0){
          printf("I am child %d and my parent is %d\n", getpid(), getppid());
          fseek(input, i*columns*2, SEEK_CUR);
          for (j = 0; j < columns; j++){
            fscanf(input, "%d", &number);
            total = total + number;
          }
          output = fopen("output.dat", "a+");
          fprintf(output,"Child %ld sum is = %d\n", i+1,total);
          fclose(output);
          exit(0);
          sleep(1);
        }
    
        else if (pid == -1){
          perror("Fork error");
        }
    
        else {
          sleep(1);
          end = waitpid(-1, &temp,0);
          printf("Parent done waiting for %d, found %d\n", pid, end );
        }
      }
    
      fclose(input);
      return 0;
    }
    > fseek(input, i*columns*2, SEEK_CUR);
    By the way, this does NOT work on text files.

    Sure you can seek, but you can't reliably find the start of any given line because in general, you don't know how \n is treated by the environment.
    Originally Posted by c99 on fseek for text files
    4 For a text stream, either offset shall be zero, or offset shall be a value returned by
    an earlier successful call to the ftell function on a stream associated with the same file
    and whence shall be SEEK_SET.
    You don't appear to account for it, even when you do know it's only a single char (as in this case).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0
    alright thanks man i really appreciate it.

    i deleted the code from the first message, if you can do the same that would be great. thank you :D

IMN logo majestic logo threadwatch logo seochat tools logo