LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-15-2008, 05:23 AM   #1
d_maniger06
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Rep: Reputation: 0
Cool 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)
{
    printf("im the child process\n");
    exit( 0 );
}
else if(pid < 0)
{
    printf("error in creating child process\n");
}
else
{
    printf("im the parent process\n");
}
the output of the program is something like this:

Quote:
im the parent process
instead of

Quote:
im the child process
im the parent process
then when i execute ps command, the output would be something like this:

Quote:
root 6940 14040 0 13:47 pts/1 00:00:09 ./sampBinary
root 16190 6940 0 17:44 pts/1 00:00:00 ./sampBinary
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?

i would greatly appreciate your knowledge..

thank you and God bless!
 
Old 07-15-2008, 06:04 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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.

jlinkels
 
Old 07-15-2008, 06:05 AM   #3
yakoub
Member
 
Registered: Oct 2006
Posts: 99

Rep: Reputation: 15
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
 
Old 07-15-2008, 02:27 PM   #4
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
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.
 
Old 07-15-2008, 07:13 PM   #5
d_maniger06
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Cool

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.
 
Old 07-15-2008, 07:33 PM   #6
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.
 
Old 07-15-2008, 08:51 PM   #7
d_maniger06
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
i think the waitpid() did that trick for me..it makes the parent waits until the child has finished executing.
 
Old 07-16-2008, 12:03 AM   #8
yakoub
Member
 
Registered: Oct 2006
Posts: 99

Rep: Reputation: 15
man

read: man fork , that can be sometimes useful
 
Old 07-16-2008, 01:56 AM   #9
d_maniger06
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Talking 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..
 
Old 07-17-2008, 12:27 AM   #10
yakoub
Member
 
Registered: Oct 2006
Posts: 99

Rep: Reputation: 15
Quote:
Originally Posted by d_maniger06 View Post
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
 
Old 07-17-2008, 04:12 AM   #11
d_maniger06
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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..
 
Old 07-17-2008, 04:33 AM   #12
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.
 
Old 07-17-2008, 05:01 AM   #13
d_maniger06
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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..
 
Old 07-17-2008, 05:15 AM   #14
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.
 
Old 07-17-2008, 07:59 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration