Results 1 to 6 of 6
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 ...
- 08-26-2009 #1Just Joined!
- Join Date
- Jul 2009
- Posts
- 16
How to run a function in background using 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
john
- 08-26-2009 #2Linux Newbie
- Join Date
- Nov 2007
- Location
- Planet Earth
- Posts
- 152
- 08-26-2009 #3
Or use pthread.
Depends a litle what you want to do with this function that is running background.
- 08-27-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
You need to fork() the process in order to run the processes in parallel. As noted, threads can be used for this purpose as well, though with more complexity.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 08-27-2009 #5
I would also suggest fork().
check
man 2 fork- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-27-2009 #6
So, as said, there are two options for doing this: forking and threading. Forking creates a new _process_, threading creates a new thread within the same process.
Which of these is better for you depends on what you want to do. If your func() and funct2() interact in any way, then threading is better, because threads in the same process can share variables and other process state. Separate processes, on the other hand, can communicate, but they have their own separate copies of all variables.
If you give us some more details, we may be able to help you further.DISTRO=Arch
Registered Linux User #388732


Reply With Quote