ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

High-level API

struct info
{
    char ch[10];
    int digit;
};

int write_info(struct info *i)
{
   FILE *fp = NULL;
    
    if (!(fp = fopen("/test", "w")))
    	return -1;

	if (fwrite(i, sizeof(struct info), 1 fp) != 1) {
    	fclose(fp);
        return -1;
    }
    
    fclose(fp);
    return 0;
}

int read_info()
{
    FILE *fp;
    struct info i;
    
    if (!(fp = fopen("/test", "r")))
    	return -1;
        
    if (fread(i, sizeof(struct info), 1) != 1) {
    	fclose(fp);
        return -1;
    }
    
    fclose(fp);
    
    printf("%s, %d\n", i.ch, i.digit);
}

 ์œ„๋Š” ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๊ธฐ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ด๋‹ค.

  • ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ผ์ธ 15, 33๊ณผ ๊ฐ™์ด fopen์„ ํ†ตํ•ด ์ฝ๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์žˆ๋Š” FILE ํฌ์ธํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
  • ํŒŒ์ผ์„ ์“ฐ๊ณ  ์ฝ์„ ๋•Œ๋Š” Text, Binary ๋ชจ๋“œ๋กœ ๋‚˜๋‰˜๊ฒŒ ๋œ๋‹ค.
    • ์œˆ๋„์šฐ์˜ ๊ฒฝ์šฐ ํ•จ์ˆ˜์— ์ด์™€ ๊ฐ™์€ ๊ฒƒ์„ ๋ช…์‹œ์ ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์ง€๋งŒ ๋ฆฌ๋ˆ…์Šค๋Š” ํ•จ์ˆ˜์— ๋”ฐ๋ผ ๊ตฌ๋ถ„๋œ๋‹ค.
    • Text ๋ชจ๋“œ
      • Read : fgetc, fgets, fscanf
      • Write : fputc, fputs, fprintf
    • Binary ๋ชจ๋“œ
      • fread, fwrite
  • fopen์„ ํ†ตํ•ด FILE ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค๋ฉด, ์‚ฌ์šฉ ํ›„ fclose๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    • fclose๋ฅผ ํ•˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ fopen์„ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ’์ด ๊ธฐ๋ก๋˜์ง€ ์•Š๊ฑฐ๋‚˜, ์ด์ƒํ•œ ๊ฐ’์ด ์ฝํžŒ๋‹ค.
  • ftell, fseek๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ํŒŒ์ผ ํฌ์ง€์…˜์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

Low-level API

struct info
{
    char ch[10];
    int digit;
};

int write_data(struct info *i)
{
    int fd;
    ssize_t ret;
    
    fd = (open("/test", O_CRET | O_WRONLY | O_APPEND, 0644);
    
    if (fd == -1)
    	return -1;
    
    ret = write(fd, i, sizeof(struct info));
    
    if (ret == -1) {
    	close(fd);
       	return -1;
    } else if (ret != sizeof(struct info)) {
    	close(fd);
        return -1;
    }
    
    close(fd);
    return 0;
}

int read_data()
{
    int fd;
    ssize_t ret;
    struct info i;
    
    fd = open("/test", O_RDONLY);
    
    if (fd == -1)
    	return -1;
        
    ret = write(fd, &i, sizeof(struct info));
    
    if (ret == -1) {
    	close(fd);
        return -1;
    } else if (ret != sizeof(struct info)) {
    	close(fd);
        return -1;
    }
    
    printf("%s %d", i.ch, i.digit);
    
    close(fd);
    return 0;
}

 High level API์˜ ๊ฒฝ์šฐ `foepn`์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์„ ์—ฌ๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ, Low level API๋Š” `open`์ด๋ผ๋Š” ์ €์ˆ˜์ค€ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ปค๋„๋กœ ์›ํ•˜๋Š” ๋™์ž‘์„ ์ „๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค.

  • High level API๋ณด๋‹ค ๋‹ค์–‘ํ•œ ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜๋‰œ๋‹ค.
    • Read
      • read, pread
    • Write
      • write, pwrite, dprintf
  • ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ํŒŒ์ผ ํฌ์ธํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ๋ฐ˜๋Œ€์˜ ์ž‘์—…๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
    • fd → fp
      • `FILE *fdopen(int fd, const char *mode)`
    • fp → fd
      • `int fileno(FILE *stream);`

 

Memory Mapping

struct info
{
    char ch[10];
    int digit;
};

int read_data()
{
    int fd;
    struct stat sp;
    struct info *i;
    
    fd = open("/test", O_RDONLY);
    
    if (fd == -1)
    	return -1;
        
    if (fstat(fd, &sp) == -1) {
    	close(fd);
        return -1;
    }
    
    i = mmap(NULL, sp.st_size, PROT_READ, MAP_SHARED, fd, 0);
    
    if (i == MAP_FAILED) {
    	close(fd);
       	return -1;
    }
    
    close(fd);
    return 0;
}

 ์ผ๋ฐ˜์ ์€ read, write์˜ ๊ฒฝ์šฐ system call์„ ํ†ตํ•ด ์ปค๋„๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์ด ์ง€์†์ ์œผ๋กœ ํ•„์š”ํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ Memory Mapping์ด ๋ณด๋‹ค ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค.

  • mmap์€ ๋””๋ฐ”์ด์Šค ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋งตํ•‘ ํ›„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.
  • ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— ๋งตํ•‘ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.
    • ๋ณ„๋„์˜ context switch๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.
  • mmap์€ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ์‚ฌ์šฉํ•  ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ NULL์„ ์ž…๋ ฅํ•˜๋ฉด ์ปค๋„์ด ์ ์ ˆํ•œ ์ฃผ์†Œ๋ฅผ ํ• ๋‹นํ•œ๋‹ค.
  • open์„ ํ†ตํ•ด fd๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๊ณ , ํŒŒ์ผ ํฌ๊ธฐ๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” fstat๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ์ปค๋„ ๋ ˆ๋ฒจ์—์„œ๋Š” ioremap์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

'๐Ÿ™‹โ€โ™‚๏ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด > C' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

C: IPC  (0) 2021.06.13
C: Signal  (0) 2021.06.11
C: thread safe and reentrant  (0) 2021.01.26
C: Memory Layout of C Programs  (0) 2021.01.26
C: Storage class specifier  (0) 2020.11.27
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€