ncurses

설치

$apt search libncurses 

$sudo apt install libncurses5-dev libncursesw5-dev

컴파일: gcc -lncurese -o OUT source.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 
  CURHELLO.C
  ==========
  (c) Copyright Paul Griffiths 1999
 
  "Hello, world!", ncurses style.
 
*/
 
 
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>                  /*  for sleep()  */
#include <curses.h>
 
 
int main(void) {
 
    WINDOW * mainwin;
 
    
    /*  Initialize ncurses  */
 
    if ( (mainwin = initscr()) == NULL ) {
    fprintf(stderr, "Error initialising ncurses.\n");
    exit(EXIT_FAILURE);
    }
 
 
    /*  Display "Hello, world!" in the centre of the
    screen, call refresh() to show our changes, and
    sleep() for a few seconds to get the full screen effect  */
 
    mvaddstr(1333"Hello, world!");
    refresh();
    sleep(3);
 
 
    /*  Clean up after ourselves  */
 
    delwin(mainwin);
    endwin();
    refresh();
 
    return EXIT_SUCCESS;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 
  CURHELL2.C
  ==========
  (c) Copyright Paul Griffiths 1999
 
  "Hello, world!", ncurses style (now in colour!)
 
*/
 
 
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>                  /*  for sleep()  */
#include <curses.h>
 
 
int main(void) {
 
    WINDOW * mainwin;
 
 
    /*  Initialize ncurses  */
 
    if ( (mainwin = initscr()) == NULL ) {
    fprintf(stderr, "Error initialising ncurses.\n");
    exit(EXIT_FAILURE);
    }
 
    start_color();                    /*  Initialize colours  */
 
 
    /*  Print message  */
 
    mvaddstr(632" Hello, world! ");
 
 
    /*  Make sure we are able to do what we want. If
    has_colors() returns FALSE, we cannot use colours.
    COLOR_PAIRS is the maximum number of colour pairs
    we can use. We use 13 in this program, so we check
    to make sure we have enough available.               */
 
    if ( has_colors() && COLOR_PAIRS >= 13 ) {
 
    int n = 1;
 
 
    /*  Initialize a bunch of colour pairs, where:
 
            init_pair(pair number, foreground, background);
 
        specifies the pair.                                  */
 
    init_pair(1,  COLOR_RED,     COLOR_BLACK);
    init_pair(2,  COLOR_GREEN,   COLOR_BLACK);
    init_pair(3,  COLOR_YELLOW,  COLOR_BLACK);
    init_pair(4,  COLOR_BLUE,    COLOR_BLACK);
    init_pair(5,  COLOR_MAGENTA, COLOR_BLACK);
    init_pair(6,  COLOR_CYAN,    COLOR_BLACK);
    init_pair(7,  COLOR_BLUE,    COLOR_WHITE);
    init_pair(8,  COLOR_WHITE,   COLOR_RED);
    init_pair(9,  COLOR_BLACK,   COLOR_GREEN);
    init_pair(10, COLOR_BLUE,    COLOR_YELLOW);
    init_pair(11, COLOR_WHITE,   COLOR_BLUE);
    init_pair(12, COLOR_WHITE,   COLOR_MAGENTA);
    init_pair(13, COLOR_BLACK,   COLOR_CYAN);
 
 
    /*  Use them to print of bunch of "Hello, world!"s  */
 
    while ( n <= 13 ) {
        color_set(n, NULL);
        mvaddstr(6 + n, 32" Hello, world! ");
        n++;
    }
    }
 
 
    /*  Refresh the screen and sleep for a
    while to get the full screen effect  */
 
    refresh();
    sleep(3);
    
 
    /*  Clean up after ourselves  */
 
    delwin(mainwin);
    endwin();
    refresh();
 
    return EXIT_SUCCESS;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

worms_game

http://www.paulgriffiths.net/program/c/srcs/curwormsrc.html

 

HTML output

 

www.paulgriffiths.net

 

pipe() 파이프

단방향 데이터 통신에 이용 - 부모 <-> 자식 프로세스 통신, 쉘에서 많이 사용

디스크립터 상속으로 IPC 채널 조인 문제 해결

-생성된 파이프는 프로세스에 종속적이지 않다.

-외부 프로세스와 통신 안됨.

-익명 파이프

#include <unistd.h>

int pipe(int fildes[2])  //성공 시 0, 실패시 -1을 리턴

-fd[0]: 데이터를 읽을 수 있는 파일 디스크립터

-fd[1]: 데이터를 출력할 수 있는 파일 디스크립터

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
#define BUFSIZE 80
 
void errors(char *msg, int code);
 
int main(int argc,char **argv)
{   
    pid_t pid;
    int state;
    int fd[2];
    char buffer[BUFSIZE];
    
    if((state=pipe(fd))==-1//파이프 생성
            errors("pipe() error",1);
            
    if((pid=fork())>0//부모 프로세스
    {
    // sleep(100);
 
    read(fd[0],buffer,BUFSIZE);
    printf("[parent] %s \n",buffer);
    strcpy(buffer,"parent 안녕하세요.");
    wrtie(fd[1],buffer,sizeof buffer);
    sleep(3);
    }
    else if(pid==0)
    {//자식 프로세스
    strcpy(buffer,"child 안녕하세요.");
    write(fd[1],buffer,sizeof buffer);
    sleep(2);
    read(fd[0],buffer,BUFSIZE);
    printf("[child] %s \n",buffer);
    }
    else
    {
        errors("fork() error",2);
    }
     return 0;
//end-of-main
 
void errors(char *msg, int code)
{
    perror(msg);
    exit(code);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs
 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
struct {
    char *abbrev;
    char *fullname;
} days[]={
    "Sun","Sunday",
    "Mon","Monday",
    "Tue","Tuesday",
    "Wed","Wednesday",
    "Thu","Thursday",
    "Fri","Friday",
    "Sat","Saturday",
    0,0
};
 
int main()
{
    pid_t pid;
    int pfd[2];
    int i,status;
    char line[64];
 
    if(pipe(pfd)<0){
        perror("fork");
        return -1;
    }
    if((pid=fork())<0)
    {
        perror("fork");
        return -1;
    }
    if(pid==0)
    {
        dup2(pfd[1],1);
        close(pfd[0]);
        execl("/bin/date","date",0);
        perror("exec");
        _exit(127);
    }
 
     close(pfd[1]);
 
    if(read(pfd[0],line,3)<0)
    {
        perror("read");
        return -1;
    }
 
    for(i=0;days[i].abbrev != NULL; i++)
    {
        if(strncmp(line,days[i].abbrev,10)==0)
            printf("Today is %s.\n",days[i].fullname);
        else
            printf("Today is not %s.\n",days[i].fullname);
    }
 
    close(pfd[0]);
    waitpid(pid,&status,0);
 
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

파이프 지원 라이브러리

#include <stdio.h>

FILE *peopn(char *command, char *type); //호출 성공시 파일포인터를 리턴, 에러시 NULL을 리턴

int pclose(FILE *stream); //자식 프로세스의 종료 상태값을 리턴

popen 함수의 command 인자, 생성할 자식 프로세스를 위한 명령행 라인

popen 함수의 type 인자, "r": 자식 프로세스로부터 데이터를 얻음, "w": 자식 프로세스에게 데이터를 제공

표준 입출력 함수 사용 가능 (버퍼링 문제에 주의)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
 
struct {
    char *abbrev;
    char *fullname;
} days[]={
    "Sun","Sunday",
    "Mon","Monday",
    "Tue","Tuesday",
    "Wed","Wednesday",
    "Thu","Thursday",
    "Fri","Friday",
    "Sat","Saturday",
    0,0
};
 
int main()
{
    int i;
    FILE *pf;
    char line[BUFSIZ];
 
    if((pf=popen("date","r"))==NULL)
    {
        perror("peopn");
        return -1;
    }
 
    if(fgets(line,sizeof(line),pf)==NULL)
    {
        fprintf(stderr,"No output from date command!\n");
        return -1;
    }
 
    for(i=0;days[i].abbrev != NULL; i++)
    {
        if(strncmp(line,days[i].abbrev,3)==0)
            printf("Today is %s.\n",days[i].fullname);
        else
            printf("Today is not %s.\n",days[i].fullname);
    }
 
    pclose(pf);
 
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

FIFO (named pipe)

파이프와 유사한 단방향 통신 메커니즘

FIFO 파일로 IPC 채널 조인 문제 해결

- 부모-자식 관계가 아닌 프로세스들도 통신 가능

- 채널 역할을 하는 FIFO 파일을 생성(FIFO 파일은 버퍼링 용도가 아님에 주의)

- 서버, 클라이언트 프로세스는 각자 동일한 FIFO 파일을 eopn하고, 이 때 얻은 디스크립터로 통신

#include <stdio.h>

int mknode(char *pathname, S_IFIFO | PERMS, int dev);

int mkfifo(char *pathname, mode_t mode); // POSIX.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
#define FIFONAME "myfifo"
 
int main()
{
    int n,fd;
    char buf[1024];
 
    unlink(FIFONAME);
 
    if(mkfifo(FIFONAME,0666)<0)
    {
        perror("mkfifo");
        return -1;
    }
 
    if((fd=open(FIFONAME,O_RDONLY))<0)
    {
        perror("open");
        return -1;
    }
 
    while((n=read(fd,buf,sizeof(buf)))>0)
        write(1,buf,n);
 
    close(fd);
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
#define FIFONAME "myfifo"
 
int main(void)
{
    int n,fd;
    char buf[1024];
 
    if((fd=open(FIFONAME,O_WRONLY))<0)
    {
        perror("open");
        return -1;
    }
 
    while((n=read(0,buf,sizeof(buf)))>0) write(fd,buf,n);
    close(fd);
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

+ Recent posts