child process does not do anything
hello!
is there a possibility that a fork() will create the child process but the child process does not do anything? for example: Code:
if((pid = fork()) == 0) Quote:
Quote:
Quote:
i would greatly appreciate your knowledge.. thank you and God bless! |
Did you try to write your code in the way as given in the examples, that is:
Code:
pid = fork(); jlinkels |
new lien
have you tried separating the fork command from if statement ?
pid=fork(); if(pid==0) ... i tried running your program on my computer and both processes always print ,maybe because i got different processing system than you |
Works OK for me too.
The fork line is fine, seperating the statements makes no difference; fork is called, result assigned to pid, then compared to 0. The compiler can't generate a different sequence. Hmmm...., what else it the program doing? The code you posted is not an complete program. |
thanx for all the replies..for the first question, yes, i have tried separating the fork assignment of pid to the if statement. but sometimes the problem still occurs. this one really works a number of times but there will come a point that the problem occurs. due to security reasons, i cant post the real code here. but il give you some pseudo codes on that part of the program. hope this helps.
Code:
typedef struct thrStructS |
The parent may quit before the child has time to run; make the parent 'sleep' for a while, or else test to make sure the child has finished before ending the program.
|
i think the waitpid() did that trick for me..it makes the parent waits until the child has finished executing.
|
man
read: man fork , that can be sometimes useful
|
pthread_atfork
i have encountered about pthread_atfork..will this be useful since im calling a fork inside a thread?..any pointers on these?..i cant find any concrete examples on how to use this..thanx..
|
Quote:
i don't think this function is related to your problem but it explains why the use of pthread_create is more sane than fork when using pthread mechanism i suspect your problem may be with stdout which means the child process is always created but somehow his output to stdout is not shown by the shell. try flush(stdout) or something . or else let child open separate file for writing |
it is in the child process that i send a message through a socket..the message was also not sent to the destination because the other end has not received any message..also when i do "ps -efa", the process is still there since the process is now duplicated..after some minutes or even hours, the process is still there and i know that it is the same process since the pid is the same as before..
i think, though im really not sure, would might just answer why the printing was not printed..but the message was also not sent and the child process was also present for a very long period of time.. any more ideas guys?..till now, ive read some things for this but i still dont have any idea what happened..thanx for helping me out guys.. |
You seriously need to read the fork() manual, then you will understand why you are having the problems which you are having. You can also experiment by setting local and global variables and checking their value in the parent and in the child. Aside from reading the manual, I don't think anyone can give you any useful advice because you're really not posting enough information for people to help.
|
i actually posted the flow of my program in this thread as seen in the 5th entry..i thought that was enough since im asking for ideas on why this is happening on this kind of setup..and i also have read the fork manual before posting this..i also read a couple of times even after..anyway, thanx for all the inputs..
|
Well, "sometimes a problem occurs" really isn't helpful at all - that's like saying "sometimes my car has problems; how do I fix it?"
You are using both 'pthread' and 'fork()'; they have very different behavior, and if you don't take time to understand how they differ, you will not be able to do anything. With fork() for example, any further changes to a variable will be local to the specific 'thread'; you will need to communicate changes via IPC mechanisms and you can't share file descriptors as with pthreads. |
And because they are different in nature, most progs use either fork() if minimal or no data needs to be shared, OR threads if a lot of vars need to be shared. Using both in the same prog is rare and, as you've seen, hard because they work in very different ways.
|
All times are GMT -5. The time now is 05:08 AM. |