problems with fork() inheritating


 
Thread Tools Search this Thread
Top Forums Programming problems with fork() inheritating
# 1  
Old 05-19-2007
problems with fork() inheritating

Hi,
thanks for any ideas...
in main() i wrote this code:

...

for(i=0;i<10;i++){
pid=fork();
switch(pid){
case -1 printf("error");
case 0:child();
default:waitpid(pid,NULL,0);
}
}

...

void child(){
SOMETHING;
}

If SOMETHING is not while(1){} cykle, then all children are made(but ends one after another). If it is while(1){} it blocks everything.

My QUESTION is: What should I do for creating all children and make them try to go to shared memory?(ALL forever...one after another randomly)

I can use the POSIX semaphores...
If not necessary I wouldn't want to use the SYS5 sem., mess. queues or pipes...
# 2  
Old 05-19-2007
Code:
switch(pid){
case -1: printf("error"); break;
case 0:child(); break;
default:waitpid(pid,NULL,0);
}

Correct me if I'm wrong, but shouldn't there be a 'break' statement in the switch? In your case, even when the pid is 0, the default case will always be run, in which case it will enter waitpid, which will wait for the child to end. And since the child is running a long (infinite) while loop, it never ends and your code is always waiting.
# 3  
Old 05-19-2007
Thanks

Thanks,
Well, I tried to test it, but it didn't do anything(despite of the fact that you were right), but i tried to put the default section in comment like this:
default:;//... and I almost couldn't believe, all the children were born and STARTED randomly switch...

I probably don't understad to fork enough, cause I thought, that the default section was caghting the zombie processes(cause the child did'nt do anything and ended as soon they were born...). So it's probably completly diffrent...

If ANYBODY knows why it didn't work before, please ASNWER
# 4  
Old 05-20-2007
the answer

It was caused by the thing that the child was living(I thought it wasn't) and I was waiting while it ends, so it never could have 10 paralel processes...
# 5  
Old 05-20-2007
Could you please give a snippet of the code in the function child and the statements after the default section ?

Basically what is that you are trying to achieve !

Is there any program termination statements with the function child ?

Quote:
if not necessary I wouldn't want to use the SYS5 sem., mess. queues or pipes..
Am sure, its not going to hurt or cost you some more time, if sem are replaced by ' semaphores ' and mess replaced by ' messages ' Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Fork!

I understand that fork create a child but I need very simple example that make child useful.... I mean how will make the program faster anyone explain with code plz using C plz (2 Replies)
Discussion started by: fwrlfo
2 Replies

2. UNIX for Dummies Questions & Answers

fork()

I'm trying to run a simple test on how to use fork(), i'm able to execute the child process first then the parent, but how can I execute parent then child..? Thanks! (1 Reply)
Discussion started by: l flipboi l
1 Replies

3. UNIX for Advanced & Expert Users

Fork and \n

Hi, I wrote a simple program for understanding the fork command. The code is as below int main(void) { fork(); printf("hi 1 \n"); fork(); printf("hi 2 \n"); fork(); printf("hi 3 \n"); } I am getting a variation in the number of times the printf is called if i remove the \n from each of... (1 Reply)
Discussion started by: xyz123456
1 Replies

4. Programming

Problems with child comunicating with parent on fork()

Hello, I'm trying to implement a version of a bucketSort (kinda) server/client, but I'm having a VERY hard time on making the server behave correctly, when talking to the children, after it forks. The server is kinda big (300+ lines), so I won't post it here, but here's what I'm doing. 1)create a... (8 Replies)
Discussion started by: Zarnick
8 Replies

5. Programming

fork() help

Hi everybody, I wanna write a code to understand how fork works. my target -------------- -Parent creates a file(called temp) and writes into this file "1".Then it closes the file. -Then parent creates a child and wait until execution of this child ends. -Then child opens the same... (3 Replies)
Discussion started by: alexicopax
3 Replies

6. Programming

problems with FORK() and WAITPID()

Dear All, I'm trying to write multithreading TCP Daemon which executes external program when new network connection arrives on a socket. After accept() I'm doing fork() for initiating of new child process, in which will be executed external program. After child creation I'm doing fork() again,... (3 Replies)
Discussion started by: Polkovnik
3 Replies

7. Programming

Fork or what?

Hello all. I'm developing a filetransfer application, which is supposed to work sort of like dcc, with multiple transfers etc. Now i wonder what the best way to manage the transfers is. Should i fork() for each new transfer, hogging loads of memory or use pthreads? Maybe I can use select to see... (0 Replies)
Discussion started by: crippe
0 Replies

8. Programming

fork()

#include <stdio.h> #include <string.h> #include <sys/types.h> #define MAX_COUNT 200 #define BUF_SIZE 100 void main(void) { pid_t pid; int i; char buf; fork(); pid = getpid(); for (i = 1; i <= MAX_COUNT; i++) { sprintf(buf,... (2 Replies)
Discussion started by: MKSRaja
2 Replies

9. Programming

fork() fd

I run this code, actually I want to both processes print the message from "data". But only one does. What happens? Anyone can help? #include <stdio.h> main(){ int fd, pid; char x; fd = open("data",0); /* open file "data" */ pid = fork(); if(pid != 0){ wait(0); ... (2 Replies)
Discussion started by: Herman
2 Replies

10. UNIX for Dummies Questions & Answers

Fork

What is a fork? Why would one create a fork? What are the advantages and disadvantages of using a fork? Please advise. Thank You. Deepali (5 Replies)
Discussion started by: Deepali
5 Replies
Login or Register to Ask a Question