C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 1st, 2013, 01:54 AM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation 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

Reply With Quote
  #2  
Old March 1st, 2013, 02:47 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old March 1st, 2013, 08:51 PM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
bump

bump

Reply With Quote
  #4  
Old March 1st, 2013, 09:12 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,130 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 43 m 21 sec
Reputation Power: 1949
BUMP: go back and edit your post using code tags and formatted properly. Until we can read it, we won't.

Reply With Quote
  #5  
Old March 1st, 2013, 09:18 PM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
?? can you guys be more specific??? obviously i'm new to here, can i just upload the c file?

Reply With Quote
  #6  
Old March 1st, 2013, 10:15 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,130 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 43 m 21 sec
Reputation Power: 1949
[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.

Reply With Quote
  #7  
Old March 1st, 2013, 10:20 PM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #8  
Old March 2nd, 2013, 12:24 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
> 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
File Type: png Screenshot at 2013-03-02 06:18:48.png (53.0 KB, 42 views)

Reply With Quote
  #9  
Old March 2nd, 2013, 12:39 AM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #10  
Old March 2nd, 2013, 01:00 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
> 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

Reply With Quote
  #11  
Old March 2nd, 2013, 01:18 AM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
Quote:
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)

Reply With Quote
  #12  
Old March 2nd, 2013, 03:27 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
First off, read the manual.
Quote:
#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.
Quote:
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).

Reply With Quote
  #13  
Old March 2nd, 2013, 09:54 AM
maans88 maans88 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 11 maans88 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 53 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Fork and have childs do different tasks

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap