LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2012, 07:27 AM   #1
sowpra
LQ Newbie
 
Registered: Feb 2012
Posts: 23
Blog Entries: 1

Rep: Reputation: Disabled
Unhappy Command or function in C to make the process run in background


hi all...
we are trying to make the process run in background....like '&' is used in shell programming...is there any command or a function in 'C Programming' that makes a process run in background????

Pleeeeeeease help us

trying from more than a week

Last edited by sowpra; 04-30-2012 at 08:01 AM.
 
Old 04-30-2012, 08:42 AM   #2
wildwizard
Member
 
Registered: Apr 2009
Location: Oz
Distribution: slackware64-14.0
Posts: 865

Rep: Reputation: 264Reputation: 264Reputation: 264
This should do the trick :-
man 3 daemon
 
Old 05-02-2012, 01:45 AM   #3
sowpra
LQ Newbie
 
Registered: Feb 2012
Posts: 23
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by wildwizard View Post
This should do the trick :-
man 3 daemon
Hi..!

just take a look at this code...!

in shell programming...

it was something like...

!#bin/bash
while(true)
do
echo "Done"
done &

pid = $!
sleep 60 && kill TERM pid

i just tried converting a part of it to C....and i have a problem with that &...!


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <signal.h>
int kill( pid_t pid, int sig );
int main()
{
while(1)
{
printf("\nDone");
}
sleep(60) && kill(getpid(),15);
}

and u told daemon will do that job...is it like within daemon function..this while should be put???

Not getting it
 
Old 05-02-2012, 01:54 AM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,275

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Not quite ... read the man page for daemon. The function will fork the program and detach it from the controlling terminal. At that point, the process is and will remain in the background. Anything it does will happen in the background (i.e. you should redirect output and error to files, as you'll no longer have a controlling tty). You might find this page along with the man pages of fork and setsid helpful. This is a bit confusing in C at first (not as easy as in the shell, although behind the scenes the shell is doing the same stuff), so you really need to understand how fork works. In other words, there's no getting around reading some documentation.
 
Old 05-02-2012, 01:56 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Look at the fork() function. If it returns 0, you are the child. Otherwise it returns the PID of the child.

http://www.gnu.org/software/libc/man...a-Process.html

A daemon is a system program that runs in the background such as sshd. I dont think that is what you are asking about.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Problem with a function having a background process inside a command substitution rylphs Linux - Newbie 2 10-05-2010 11:36 AM
[SOLVED] How does '&' make a process run in background ? TheIndependentAquarius Programming 22 12-22-2009 11:58 AM
run c function in the background schneidz Programming 1 09-06-2007 03:05 PM
poepn or system function call executed by a background process kshkid Programming 1 08-24-2006 04:04 AM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 01:27 AM


All times are GMT -5. The time now is 10:30 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration