0

So i created a c program in unix. This is what it does.

1)It forks a process.

Child Process: Performs execv() on a alarm clock program which prints "Alarm rang" after 'x' seconds

Parent Process: Performs waitPID on child above. Then exits.

Output: So i ran it and i received the "Alarm rang after x seconds"

Everything is working fine. But now what i want is for the child process to run in the background. Meaning i do not want to see its output. but i want it to run and exit.

alk
  • 69,737
  • 10
  • 105
  • 255
RStyle
  • 875
  • 2
  • 10
  • 29
  • Make the forked child fork again and in this grand child do the exec. More here: http://stackoverflow.com/questions/10932592/why-fork-twice – alk Sep 06 '15 at 08:19
  • I made the child fork a grandchild to exec. Then i made the child to return without waiting for the grandchi;d. I still see the grandchild's output =/ – RStyle Sep 06 '15 at 09:24
  • Will you invoke your program from a login shell or at boot time as a system process? A "daemon" (your tag) is one thing, "backgrounding" (your wording) is one of two things, and suppressing or redirecting output (perhaps your need?) is yet another thing. What, precisely, do you want? – pilcrow Sep 06 '15 at 15:24

1 Answers1

0

To make the child process run in the background, you need to make it a daemon process.

There are few steps to follow to make it a daemon process..

  1. Fork off the parent process
  2. Change file mode mask (umask)
  3. Open any logs for writing
  4. Create a unique Session ID (SID)
  5. Change the current working directory to a safe place
  6. Close standard file descriptors

For a better explanation, see this.

Haris
  • 12,120
  • 6
  • 43
  • 70