how to keep on this program executing when I put it at the backgound?
-
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 ; } }
-
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 ; } }
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 )