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. Managed C++/CLI
  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 Managed C++/CLI
linuxhelptutorialquestion
2 Posts 2 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 ; } }

    C 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 ; } }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      This is the C++/CLI forum. .NET threads are much easier, if you're using C++/CLI, use those. Otherwise, try the Visual C++ forum.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      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