The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Fork and have childs do different tasks
Discuss Fork and have childs do different tasks in the C Programming forum on Dev Shed. Fork and have childs do different tasks C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 1st, 2013, 01:54 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 11
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
|

March 1st, 2013, 02:47 AM
|
 |
Contributed User
|
|
|
|
|
Perhaps you could investigate the use of [code][/code] tags as opposed to some barely readable colour in some barely readable font.
|

March 1st, 2013, 08:51 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 11
Time spent in forums: 1 h 12 m 53 sec
Reputation Power: 0
|
|
|
bump
bump
|

March 1st, 2013, 09:12 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
BUMP: go back and edit your post using code tags and formatted properly. Until we can read it, we won't.
|

March 1st, 2013, 09:18 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 11
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?
|

March 1st, 2013, 10:15 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
[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.
|

March 1st, 2013, 10:20 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 11
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.
|

March 2nd, 2013, 12:24 AM
|
 |
Contributed User
|
|
|
|
> 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.
|

March 2nd, 2013, 12:39 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 11
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?
|

March 2nd, 2013, 01:00 AM
|
 |
Contributed User
|
|
|
|
> 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
|

March 2nd, 2013, 01:18 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 11
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)
|

March 2nd, 2013, 03:27 AM
|
 |
Contributed User
|
|
|
|
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).
|

March 2nd, 2013, 09:54 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 11
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 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|