Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <curses.h>
- #include <time.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- void drawtime(int x, int y)
- {
- // Get current time
- time_t t = time(NULL);
- // Transform to string
- char* s = ctime(&t);
- // Write time to given position of console
- mvaddstr(x,y,s);
- // Refresh output
- refresh();
- sleep(1);
- }
- int kbhit(){
- fd_set rfds;
- struct timeval tv;
- /* Ждем, пока на стандартном вводе (fd 0) что-нибудь появится. */
- FD_ZERO(&rfds);
- FD_SET(0, &rfds);
- tv.tv_sec = 0;
- tv.tv_usec = 100;
- return select(1, &rfds, NULL, NULL, &tv);
- }
- void init_curses()
- {
- // Initialize all curses data structures
- initscr();
- //Set cursor set to 0 - invisible
- curs_set(0);
- // Disable input echo
- noecho();
- refresh();
- }
- int waitForChild(pid_t pid)
- {
- int status;
- pid_t w = waitpid(pid, &status, WCONTINUED | WUNTRACED);
- if ( w == -1 )
- {
- return 0;
- }
- if (WIFEXITED(status) || WIFSIGNALED(status)){
- return 1;
- }
- return 1;
- }
- int main(int argc, char** argv)
- {
- init_curses();
- // Fork current process
- pid_t childpid = fork();
- int childExist = 1;
- if (childpid){ // parent
- while( !kbhit() )
- {
- mvaddstr(0,0,"Parent");
- drawtime(2,0);
- }
- // Parent takes kbhit()
- kill(childpid, SIGTERM); // kill child
- if (!waitForChild(childpid))
- exit(EXIT_FAILURE);
- }
- else{ // child
- while( true )
- {
- mvaddstr(0, 40,"Child");
- drawtime(2 ,40);
- }
- }
- // Exit from curses and restore default settings
- endwin();
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement