LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Fork() problem (https://www.linuxquestions.org/questions/programming-9/fork-problem-241079/)

MrBrain 10-10-2004 04:03 PM

Fork() problem
 
Hello,
I'm writing an daemon C-program for linux but I got a strange problem with fork(). When I call an function located in another c-file the program just dissappears...

This is the way I've done it:
  • in main.c I fork() and if PID > 0 I exit
  • after some system-logging and SID recieving i start a while(1) loop
  • in the loop I call for action(&value) which is in action.c
  • the action() is ran but when it should return to the main while(1) loop it just dies, like it is running exit(0) instead of return(0) :scratch:

I define prototypes of all functions in a common header file (header.h) which I include in all c-files. If i comment the action(&value) part, it will run on and on in the while(1) loop until I kill it with a SIGKILL.

Am I using fork wrong here or is fork messing with me? As far as I understand with my scills in C this should work just fine since I'm including this header file with prototypes an link the compiled files as supposed to. The compiler is not complaining about anything...

I cannot find anything about this kind of problems anywhere :study:

itsme86 10-10-2004 04:29 PM

What exactly does action() do? Can you post the code?

The reason I ask is because weird things seem to happen with I/O in fork()'d processes. Like this:
Code:

itsme@itsme:~/C$ cat fork.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

void action(int *num)
{
  int ch;

  printf("Call #%d. Press ENTER to continue.\n", *num);
  ch = getchar();
  if(ch == EOF)
  {
    printf("Error! %s\n", strerror(errno));
    *num = -1;
  }
  else
    (*num)++;
}

int main(void)
{
  int ctr = 0;

  if(fork())
    exit(EXIT_SUCCESS);

  while(1)
  {
    action(&ctr);
    if(ctr == -1)
      break;
  }

  return EXIT_SUCCESS;
}

This is what I get when I run it:
Code:

itsme@itsme:~/C$ ./fork
Call #0. Press ENTER to continue.
itsme@itsme:~/C$ Call #1. Press ENTER to continue.
Error! Input/output error

I press ENTER after the Call #0 line and then the Call #1 line comes up but then displays that error right away. However, if I comment out the 2 lines that deal with fork()'ing in main() the loop keeps happening as expected.

Hko 10-10-2004 04:43 PM

Instead of this:
Code:

if (fork() >0) {
  exit();
}

Try this:
Code:

if (daemon(0,0) < 0) {
    perror("daemon: ");
    exit(1);
}

Or this:
Code:

if ((fork()) != 0)
        exit(0);
setsid();
if ((fork()) != 0)
    exit(0);


MrBrain 10-10-2004 04:52 PM

Thanx for the reply, I was going to post the code when I noticed an declaration I hade

Code:

FILE *fp;
and later in the code
Code:

fclose(fp);
Some old garbage that had left there when I copy-pasted the structure of my function from an old program :tisk:

My bad :)

I would have expected some complains from the gcc though..


All times are GMT -5. The time now is 01:09 AM.