Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to keep on this program executing when I put it at the backgound?

how to keep on this program executing when I put it at the backgound?

Scheduled Pinned Locked Moved C / C++ / MFC
linuxhelptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bloodwinner
    wrote on last edited by
    #1

    the scenario is I am counting down 5, if no 'y' has been input during this time, then it ends at one way, else it ends at another way. Because I dont expect there must be a 'y' input, I write this as multi-thread, one for counting down, one for checking the 'y' input (could be unnecessary). this program works fine when I put it at the foreground(using linux), but when put it at background, error happens, it will stop and wait for getchar() input, and m doesnot count down. As long as I use shell command "fg" to bring it back to foreground, the count down resumes, but that is not what I want. what I want is just: put the program execution at background, while waiting for a 'y' input during 5 seconds count down time. void *countdown( ); void *waitinput( ); static int m=5; static int k=1; int main(int argc, char **argv) { pthread_t thread1=0, thread2=0; int iret1=0, iret2=0; /* Create independent threads each of which will execute function */ iret1 = pthread_create( &thread1, NULL, countdown, NULL); iret2 = pthread_create( &thread2, NULL, waitinput, NULL); /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); // pthread_join( thread2, NULL); pthread_cancel(thread1); pthread_cancel(thread2); printf("pthread been cancelled\n\n"); if(k=1){...} else {...} return 0; } void *countdown() { while(m>0) { printf("%d seconds left\n",m--); sleep(1); printf("m is %d\n",m); } } void *waitinput() { char c='n'; while(m) { if((c=getchar())=='y') { printf("congratulations. you entered y\n"); sleep(1); m=0;k=0; } else ; } }

    R I 2 Replies Last reply
    0
    • B bloodwinner

      the scenario is I am counting down 5, if no 'y' has been input during this time, then it ends at one way, else it ends at another way. Because I dont expect there must be a 'y' input, I write this as multi-thread, one for counting down, one for checking the 'y' input (could be unnecessary). this program works fine when I put it at the foreground(using linux), but when put it at background, error happens, it will stop and wait for getchar() input, and m doesnot count down. As long as I use shell command "fg" to bring it back to foreground, the count down resumes, but that is not what I want. what I want is just: put the program execution at background, while waiting for a 'y' input during 5 seconds count down time. void *countdown( ); void *waitinput( ); static int m=5; static int k=1; int main(int argc, char **argv) { pthread_t thread1=0, thread2=0; int iret1=0, iret2=0; /* Create independent threads each of which will execute function */ iret1 = pthread_create( &thread1, NULL, countdown, NULL); iret2 = pthread_create( &thread2, NULL, waitinput, NULL); /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); // pthread_join( thread2, NULL); pthread_cancel(thread1); pthread_cancel(thread2); printf("pthread been cancelled\n\n"); if(k=1){...} else {...} return 0; } void *countdown() { while(m>0) { printf("%d seconds left\n",m--); sleep(1); printf("m is %d\n",m); } } void *waitinput() { char c='n'; while(m) { if((c=getchar())=='y') { printf("congratulations. you entered y\n"); sleep(1); m=0;k=0; } else ; } }

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      bloodwinner wrote:

      pthread_create( &thread1, NULL, countdown, NULL);

      A stab in the dark, since I do not know anything about Linux, but maybe you need to set one of these to something else than just NULL to get it to act independently of having or not the focus.

      http://www.readytogiveup.com/[^] - Do something special today.

      B 1 Reply Last reply
      0
      • B bloodwinner

        the scenario is I am counting down 5, if no 'y' has been input during this time, then it ends at one way, else it ends at another way. Because I dont expect there must be a 'y' input, I write this as multi-thread, one for counting down, one for checking the 'y' input (could be unnecessary). this program works fine when I put it at the foreground(using linux), but when put it at background, error happens, it will stop and wait for getchar() input, and m doesnot count down. As long as I use shell command "fg" to bring it back to foreground, the count down resumes, but that is not what I want. what I want is just: put the program execution at background, while waiting for a 'y' input during 5 seconds count down time. void *countdown( ); void *waitinput( ); static int m=5; static int k=1; int main(int argc, char **argv) { pthread_t thread1=0, thread2=0; int iret1=0, iret2=0; /* Create independent threads each of which will execute function */ iret1 = pthread_create( &thread1, NULL, countdown, NULL); iret2 = pthread_create( &thread2, NULL, waitinput, NULL); /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); // pthread_join( thread2, NULL); pthread_cancel(thread1); pthread_cancel(thread2); printf("pthread been cancelled\n\n"); if(k=1){...} else {...} return 0; } void *countdown() { while(m>0) { printf("%d seconds left\n",m--); sleep(1); printf("m is %d\n",m); } } void *waitinput() { char c='n'; while(m) { if((c=getchar())=='y') { printf("congratulations. you entered y\n"); sleep(1); m=0;k=0; } else ; } }

        I Offline
        I Offline
        InOut NET
        wrote on last edited by
        #3

        I think that you should bring in another class that handles your threads - one that holds back the return of the main. main(...) { ClassThreads = new clsClassThreads(); do { existCondition = ClassThreads->Start(); if (existCondition) { ClassThreads->getResults(); //do something with thread results. ShowMessage("Thread class done. Exiting loop and Main"); }else{ ShowMessage("Thread class not done yet. Restarting threads"); } }while(!exitCondition) delete ClassThreads; return 0; }

        B 1 Reply Last reply
        0
        • R Rage

          bloodwinner wrote:

          pthread_create( &thread1, NULL, countdown, NULL);

          A stab in the dark, since I do not know anything about Linux, but maybe you need to set one of these to something else than just NULL to get it to act independently of having or not the focus.

          http://www.readytogiveup.com/[^] - Do something special today.

          B Offline
          B Offline
          bloodwinner
          wrote on last edited by
          #4

          thank you for reply. 1.I guess it does not have much thing to do with the platform. 2.Regarding to the parameters of pthread, I took reference from here http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#BASICS it looks working fine, at least as I said when executing it at the foreground, but because I can open only one terminal, probably have to do several things, so I can only put it running at the background, there problems occurs.

          1 Reply Last reply
          0
          • I InOut NET

            I think that you should bring in another class that handles your threads - one that holds back the return of the main. main(...) { ClassThreads = new clsClassThreads(); do { existCondition = ClassThreads->Start(); if (existCondition) { ClassThreads->getResults(); //do something with thread results. ShowMessage("Thread class done. Exiting loop and Main"); }else{ ShowMessage("Thread class not done yet. Restarting threads"); } }while(!exitCondition) delete ClassThreads; return 0; }

            B Offline
            B Offline
            bloodwinner
            wrote on last edited by
            #5

            errr.... I am only using C, so could you give me some information related to it?

            I 1 Reply Last reply
            0
            • B bloodwinner

              errr.... I am only using C, so could you give me some information related to it?

              I Offline
              I Offline
              InOut NET
              wrote on last edited by
              #6

              Sorry - Im was wrong - the fact that it works in foreground i missed. Ignore my posting. I thought you had a code design issue. Well, other guy suggested the os. I would say the same. And i think it is something you'll find in Linux articles. I don't know C threads and linux in the detail you might need.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups