Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
I am trying to run my function in the background but can't seem to do it. I can run my app in background like this ; ./myapp & but I ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    9

    Running function in the background in C

    I am trying to run my function in the background but can't seem to do it.

    I can run my app in background like this ;

    ./myapp &

    but I want to run it in the background when a specific event occurs.

    main() {

    int a=0;

    scanf(" %d ", &a);

    if(a==5)

    printf("a is 5 \n");

    else

    run in background??

    }

    I mean when a is not 5 I want my app to exit from console control but run in bnackground.

    Any help would be appriciated.

  2. #2
    Just Joined! djap's Avatar
    Join Date
    Jul 2005
    Location
    Not so sure anymore...
    Posts
    97
    sounds like you need fork()
    fork

  3. #3
    Just Joined!
    Join Date
    Jan 2010
    Posts
    9
    Quote Originally Posted by djap View Post
    sounds like you need fork()
    well, not really.
    actually the exact thing I want to do is ;

    main() {

    int a=0;

    scanf(" %d ", &a);

    if(a==5)

    printf("a is 5 \n");

    else

    *continue the program flow in background

    }

    fork will create a child process and main will wait for fork to complete.
    this is not I want, I want the app to continue in the background when it reaches *, ready to accept input from the same console.

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    you need to create and use a thread, I think for c/c++ on linux you would want to use pthread library

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by coopstah13 View Post
    you need to create and use a thread, I think for c/c++ on linux you would want to use pthread library
    Sorry for interrupting - you can do this...start a thread and then run it in the background while the main process runs in the foreground?
    Make mine Arch Linux

  6. #6
    Just Joined! djap's Avatar
    Join Date
    Jul 2005
    Location
    Not so sure anymore...
    Posts
    97
    Quote Originally Posted by n00b13 View Post
    well, not really.
    actually the exact thing I want to do is ;

    main() {

    int a=0;

    scanf(" %d ", &a);

    if(a==5)

    printf("a is 5 \n");

    else

    *continue the program flow in background

    }

    fork will create a child process and main will wait for fork to complete.
    this is not I want, I want the app to continue in the background when it reaches *, ready to accept input from the same console.
    ... and the fork will complete as soon as child process is created, it doesn't wait till the child process is finished so I think something like
    Code:
    if(fork()==0)
         *continue program flow
    else
         return 0;
    should do it, or maybe I just understood wrong.

    EDIT: Wait... so you want the app exit from the consoles control but still be able to take input from console?

  7. #7
    Just Joined!
    Join Date
    Jan 2010
    Posts
    9
    Quote Originally Posted by djap View Post
    ... and the fork will complete as soon as child process is created, it doesn't wait till the child process is finished so I think something like
    Code:
    if(fork()==0)
         *continue program flow
    else
         return 0;
    should do it, or maybe I just understood wrong.

    EDIT: Wait... so you want the app exit from the consoles control but still be able to take input from console?
    no I do not want it to get input from console ;

    main() {

    int a=0;

    scanf(" %d ", &a);

    if(a==5)

    printf("a is 5 \n");

    else

    *continue the program flow in background

    }

    when the program flow comes to *, it will go background but not end the application.

    after * there is no user interaction, code will continue working but exit from console like I started the application like ./myapp &

    I do not think fork is the solution here becasue if I fork it I will be able to run the child in the background but parent will wait for child to complete so parent will still run normally.

    if I couldn't express my goal clearly, let me try to clarify it better ;

    user@linuxbox#./myapp
    5
    a is 5
    user@linuxbox#./myapp
    4
    user@linuxbox# << program running

    exactly like this but program will be working in the background, still has a pid and is a main process.

  8. #8
    Just Joined! djap's Avatar
    Join Date
    Jul 2005
    Location
    Not so sure anymore...
    Posts
    97
    Ok, if I still got it wrong I realise I'm getting pretty annoying for everyone so this will be my last post in this thread ...but lets see...
    Quote Originally Posted by n00b13 View Post
    I do not think fork is the solution here becasue if I fork it I will be able to run the child in the background but parent will wait for child to complete so parent will still run normally.
    Ok, so this where we disagree. My opinion is that the parent will NOT wait for the child to complete.

    Quote Originally Posted by n00b13 View Post
    if I couldn't express my goal clearly, let me try to clarify it better ;

    user@linuxbox#./myapp
    5
    a is 5
    user@linuxbox#./myapp
    4
    user@linuxbox# << program running

    exactly like this but program will be working in the background, still has a pid and is a main process.
    Let's see following code:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void) {
    int a=0;
    scanf("%d", &a);
    if(a==5)
      printf("a is 5 \n");
    else
    {
      if(fork()==0)
         while(1);	//keep running forever
      else
      {
        puts("parent finishing...");
         return 0;
      }
    }
    return 0;
    }
    Now if I run this and input lets say 4 for example the output will be:
    djap@djap-laptop:~/koodi$ ./a.out
    4
    parent finishing...
    djap@djap-laptop:~/koodi$ <<the process is still running

  9. #9
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    djap is correct. fork()'s behaviour is explicitly to return to the parent as soon as the child process is created. From the fork man page:
    Code:
    RETURN VALUE
           On success, the PID of the child process is returned in the parent, and
           0  is returned in the child.  On failure, -1 is returned in the parent,
           no child process is created, and errno is set appropriately.
    fork() returns at the same time from both the parent and child.

    If you wanted the parent to wait for the child, you must explicitly use one of the wait() family of functions. It is very common to see a construct like the following:
    Code:
    pid_t childpid = fork();
    
    if(childpid < 0)
    {
        perror("fork()");
        return 1;
    }
    else if(childpid == 0)
    {
        // child
    
        // do your processing
        // ...
    
        exit(0);
    }
    else
    {
        // parent
    
        // wait for child
        waitpid(childpid, NULL, 0);
    }
    
    // at this point, the child has ended and the parent continues
    In your case, you may simply fork() and then exit the parent, and the child will continue in the background.
    DISTRO=Arch
    Registered Linux User #388732

  10. #10
    Just Joined!
    Join Date
    Jan 2010
    Posts
    7
    Quote Originally Posted by n00b13 View Post
    well, not really.
    actually the exact thing I want to do is ;

    main() {

    int a=0;

    scanf(" %d ", &a);

    if(a==5)

    printf("a is 5 \n");

    else

    *continue the program flow in background

    }

    fork will create a child process and main will wait for fork to complete.
    this is not I want, I want the app to continue in the background when it reaches *, ready to accept input from the same console.
    I'm a little rusty with my linux programming but if i'm not mistaking once the app is in background it's tty isnt the same with the console's anymore so it cant accept anymore input from console. When it enters background the only way to communicate with it is through shared memory or pipes or signals or message queues. Correct me if i'm wrong...

Page 1 of 2 1 2 LastLast

Posting Permissions

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