Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

  2. #2
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    EOF

  3. #3
    Just Joined! djap's Avatar
    Join Date
    Jul 2005
    Location
    Not so sure anymore...
    Posts
    97
    Or use pthread.

    Depends a litle what you want to do with this function that is running background.

  4. #4
    Linux Guru Rubberman's Avatar
    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!

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    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
    -------------------

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...