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 ; } }
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.
-
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 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; }
-
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.
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.
-
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; }
errr.... I am only using C, so could you give me some information related to it?
-
errr.... I am only using C, so could you give me some information related to it?
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.