ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
so it would seem that the child process was actualy created but did not do anything. this is not always the case. there are just times that this is what happens. what may be the cause of the problem?
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Did you try to write your code in the way as given in the examples, that is:
Code:
pid = fork();
if (pid == 0){
printf ("I am the child");
}
The first thing which comes to my mind is that if you use both fork() and the comparison in one statement, the compiler might do some funny things in the ordering of the code. While it is essential that right after forking the child process is cloned and should start running exactly at the return of the fork() function, and that is the comparison. If the compiler for some reason makes something else out of it, the comparsion in the child may fail.
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
{
int socket;
} thrStruct;
typedef struct Message
{
int msg;
} message;
void func1(int sd);
void *func2(void *thrParam);
int main()
{
int sd = -1;
while( 1 )
{
//something here receives a connection and sd is the socket for
that connection
// the select statement here including all other networking
statements which listens to a port, etc.
// once message is detected send socket to func1
func1(sd);
}
return 0;
}
void func1(int sd)
{
thrStruct *thrParam = NULL;
int status = -1;
// the thread attributes thr, thrAttr are all initialized here
thrParam->socket = sd;
status = pthread_create(thr, thrAttr, func2,(void *)thrParam);
if(status != 0)
{
printf("thread was not created successfully");
}
}
void *func2(void *thrParam)
{
thrStruct *param = NULL;
int socket = -1;
int pid = -1;
int status = -1;
message msg;
param = (thrStruct *) thrParam;
if(param != NULL)
{
socket = param->socket;
}
if(socket != -1)
{
msg.msg = 1;
if((pid = fork()) == 0)
{
printf("im the child process\n");
status = send(socket, &msg , sizeof(message), 0);
close(socket);
exit( 0 );
}
else if(pid < 0)
{
printf("error in creating child process\n");
}
else
{
printf("im the parent process\n");
close(socket);
if(waitpid(pid, &status, 0) != pid)
{
printf("wait error\n");
}
else
{
if(WIFSIGNALED(status) != 0)
{
printf( "signal %d received by child\n",
WTERMSIG(status));
}
else if(WIFEXITED(status) != 0)
{
printf("child ended with status = %d\n",
WEXITSTATUS(status));
}
else
{
printf("child did not end normally\n");
}
}
}
}
return( NULL );
}
as ive said, this code works, but there will come a point that the problem occurs. any ideas?
Last edited by d_maniger06; 07-16-2008 at 04:38 AM.
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 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..
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..
have you tried man pthread_atfork ? the manual tells it all .
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.