IT

20141029 시스템 프로그래밍. curses.

kio467 2014. 10. 29. 15:53

#include <stdio.h>

#include <curses.h> // curses를 하기 위해 헤더파일을 인클루드 해줍니다. 

// 컴파일 할때 gcc hello.c -o hello -lcurses 해줘야됩니다.

main()

{

initscr(); // curses를 켭니다.

clear(); // 화면을 비우고

move(10,20); // 왼쪽 상단이 0,0이니 그 기준으로 10로우(열),20컬럼(행) 움직여서

addstr("hell o world"); // 헬 오 월드 를 출력합니다.

move(LINES-1,0); // 커서를 제일 왼쪽으로 이동합니다.


refresh();

getch();


endwin();

}



#include <stdio.h>

#include <curses.h>


main()

{

int i;


initscr();

clear();

for(i=0;i<LINES; i++)

{

move(i, i+1); // 한칸씩, 한 줄씩 옮기면서

if(i%2 == 1) // 홀수번째 줄이면

standout(); // 스탠드아웃 모드

addstr("hell o world");

if(i%2==1)

standend(); // 스탠드 아웃 모드 종료

}

refresh();

sleep(5); // 오초간 슬립하고 

endwin(); // 종료

}



main()

{

int i;


initscr();

clear();

for(i=0;i<LINES; i++)

{

move(i, i+1);

if(i%2 == 1)

standout();

addstr("hell o world");

if(i%2==1)

standend();

sleep(1); // 슬립과 리프레시의 위치를 반복문 안으로 옮겨

refresh(); // 한 줄씩 화면에 나타나게 하였음.

}

endwin();

}


main()

{

int i;


initscr();

clear();

for(i=0;i<LINES; i++)

{

move(i, i+1);

if(i%2 == 1)

standout();

addstr("hell o world");

if(i%2==1)

standend();

sleep(1);

refresh();


move(i,i+1); // 커서를 맨 앞으로 옮기고

addstr("             "); // 빈 칸 삽입 = 헬 오 월드를 지우는 효과

}

endwin();

}


#define LEFTEDGE 10

#define RIGHTEDGE 30

#define ROW 10


main()

{

char message[] = " hell o ";

char blank[] = " ";

int dir = +1;

int pos = LEFTEDGE;


initscr();

clear();

while(1)

{

move(ROW, pos); // 커서 이동

addstr( message ); // 메시지 출력

move( LINES-1, COLS-1);  // 커서를 화면 우측 하단으로 이동

refresh();

sleep(1);

move(ROW,pos);  // 커서 이동

addstr(blank); // 빈 칸 출력 = 메시지 지우기

pos += dir;

if(pos >= RIGHTEDGE)

dir = -1;

if(pos <= LEFTEDGE)

dir = +1;

}

}





#include <stdio.h>

#include <signal.h>


main()

{

void wakeup(int);

printf("4 sec\n");

signal(SIGALRM, wakeup); // alarm signal을 처리할 겁니다. alarm signal 오면 wakeup function 실행합니다.

alarm(4); // 4초후 alarm

pause(); // 기다립니다...

printf("wake up\n");

}


void wakeup(int signum)

{

printf("alarm received from kernel\n");

}



#include <stdio.h>

#include <sys/time.h>

#include <signal.h>


int main()

{

void countdown(int);

signal(SIGALRM, countdown);

if (set_ticker(500) == -1)

perror("set ticker");

else

while(1)

pause();

return 0;

}


void countdown(int signum)

{

static int num = 10;

printf("%d ..", num --);

fflush(stdout);

if (num < 0)

{

printf("DONE\n");

exit(0);

}

}


int set_ticker(int sec)

{

struct itimerval timer;

long nsec, usec;


nsec = sec / 1000;

usec = (sec % 1000) * 1000L;


timer.it_interval.tv_sec = nsec;

timer.it_interval.tv_usec = usec;

timer.it_value.tv_sec = nsec;

timer.it_value.tv_usec = usec;


return setitimer(ITIMER_REAL, &timer, NULL);

}