LinuxQuestions.org
Visit Jeremy's Blog.
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 08-26-2009, 02:38 AM   #1
nathan
LQ Newbie
 
Registered: Aug 2009
Location: california
Distribution: fedora
Posts: 21

Rep: Reputation: 15
Arrow How to run a function in background in c


Hi All,

I want to run a function in background and continue with executing next statements,

eg:

main()
{

..
....
func(); // want to run in back ground and it is a while(1). executing
// in infinite loop
funct2(); // this should execute normally
..
...

}


kindly suggest me

Thanks in advance
nathan
 
Old 08-26-2009, 02:40 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
I could be wrong, but I think you'd have to look into multi-threading for this.
 
Old 08-26-2009, 02:45 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Yep, look intro threading.
 
Old 08-26-2009, 02:49 AM   #4
nathan
LQ Newbie
 
Registered: Aug 2009
Location: california
Distribution: fedora
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Nylex View Post
I could be wrong, but I think you'd have to look into multi-threading for this.
Thanks for your reply,

if i implement it as thread it will be running always..
but i need a provision to stop this func and start again (like this i may doing many times in my application) .

kindly suggest me how to stop a running thread and restart it again
(syntax)

or
any other methods also ..if any

thanks in advance
nathan
 
Old 08-26-2009, 02:52 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Unfortunately, I can't help you with threading in C as I've never done it (I've only done threading in Java and naturally there's a different API to be used).
 
Old 08-26-2009, 03:08 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by nathan View Post
Thanks for your reply,

if i implement it as thread it will be running always..
but i need a provision to stop this func and start again (like this i may doing many times in my application) .
Threads can be destroyed, created or whatever at a given momment. You could very well fork a new process, but that usually is unnecessary, wastes resources and makes things more difficult.

You should read something about pthreads. Threads can't be explained in a post. For example:

https://computing.llnl.gov/tutorials/pthreads/
 
Old 08-26-2009, 04:38 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-26-2009, 06:40 AM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
If you want to be able to stop the background function asynchronously, don't use POSIX threads. POSIX threads don't handle asynchronous interruption well. Instead, use fork(), kill(), and waitpid().

One complication that might arise if you follow my advice is that you'll have to take additional steps if you want the child process and the parent process to share memory (for variables, for example).

Hope this helps.
 
Old 08-26-2009, 07:37 AM   #9
nathan
LQ Newbie
 
Registered: Aug 2009
Location: california
Distribution: fedora
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by i92guboj View Post
Threads can be destroyed, created or whatever at a given momment. You could very well fork a new process, but that usually is unnecessary, wastes resources and makes things more difficult.

You should read something about pthreads. Threads can't be explained in a post. For example:

https://computing.llnl.gov/tutorials/pthreads/

Hi,

Thanks a lot for your info

kindly go through it

void * func1( void)
{

..
...
}

main()
{
int ret;
pthread_t test1;

ret = pthread_create(&test1,NULL,&func1,NULL);
if(ret == 0)
{
printf("\n successin opening thread \n");
}

pthread_exit(test1); // is it correct way of closing a thread and here i getting following warning:
// warning: passing arg 1 of `pthread_exit' makes pointer from integer without a cast

}


kindly suggest me the correct one.


thanks
Nathan
 
Old 08-26-2009, 04:40 PM   #10
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
To synchronize the threads you need to use mutual exclusion locks and semaphores (mutexes). A complete discussion of pthreads will also include a discussion of mutexes.
 
Old 08-28-2009, 02:14 AM   #11
nathan
LQ Newbie
 
Registered: Aug 2009
Location: california
Distribution: fedora
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jiml8 View Post
To synchronize the threads you need to use mutual exclusion locks and semaphores (mutexes). A complete discussion of pthreads will also include a discussion of mutexes.
Hi All,


finally i implemented it as timer calling the function at a particular interval of time.

Any difference in performance with timer and thread ?

Your comments, suggestions are most welcome

Thnaks in advance
Nathan
 
Old 08-28-2009, 12:01 PM   #12
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
A timer is fine...for things that should be timed.

If you are depending on a timer to synchronize different execution streams, you probably will get bitten sooner or later - and, more than likely, intermittently.
 
  


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
run c function in the background schneidz Programming 4 09-07-2007 07:29 AM
run c function in the background schneidz Programming 1 09-06-2007 02:05 PM
poepn or system function call executed by a background process kshkid Programming 1 08-24-2006 03:04 AM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 12:27 AM
alias or function in .bashrc, which sends command to background ngomong Linux - General 1 04-23-2002 09:50 PM

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

All times are GMT -5. The time now is 01:11 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
Open Source Consulting | Domain Registration