LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   child process does not do anything (https://www.linuxquestions.org/questions/programming-9/child-process-does-not-do-anything-655852/)

d_maniger06 07-15-2008 05:23 AM

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!

jlinkels 07-15-2008 06:04 AM

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

yakoub 07-15-2008 06:05 AM

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

smoked kipper 07-15-2008 02:27 PM

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.

d_maniger06 07-15-2008 07:13 PM

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?

pinniped 07-15-2008 07:33 PM

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.

d_maniger06 07-15-2008 08:51 PM

i think the waitpid() did that trick for me..it makes the parent waits until the child has finished executing.

yakoub 07-16-2008 12:03 AM

man
 
read: man fork , that can be sometimes useful

d_maniger06 07-16-2008 01:56 AM

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..

yakoub 07-17-2008 12:27 AM

Quote:

Originally Posted by d_maniger06 (Post 3216103)
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

d_maniger06 07-17-2008 04:12 AM

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..

pinniped 07-17-2008 04:33 AM

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.

d_maniger06 07-17-2008 05:01 AM

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..

pinniped 07-17-2008 05:15 AM

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.

chrism01 07-17-2008 07:59 PM

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.