Do You Coding?
[function] unlink ํจ์ ์์๋ณด๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (Linux)
NAME
unlink - delete a name and possibly the file it refers to
: ์ด๋ฆ๊ณผ ํ์ผ์ ์ญ์
SYNOPSIS
#include <unistd.h>
int unlink(const char *pathname);
DESCRIPTION
unlink() deletes a name from the filesystem. If that name was
the last link to a file and no processes have the file open, the
file is deleted and the space it was using is made available for
reuse.
If the name was the last link to a file but any processes still
have the file open, the file will remain in existence until the
last file descriptor referring to it is closed.
If the name referred to a symbolic link, the link is removed.
If the name referred to a socket, FIFO, or device, the name for
it is removed but processes which have the object open may
continue to use it.
: undlink()๋ ํ์ผ ์์คํ
์์ ์ด๋ฆ์ ์ญ์ ํฉ๋๋ค. ํด๋น ์ด๋ฆ์ด ํ์ผ์ ๋ํ ๋ง์ง๋ง ๋งํฌ์ด๊ณ
ํ์ผ์ด ์ด๋ฆฌ์ง ์๋ ํ๋ก์ธ์ค๊ฐ ์๋ ๊ฒฝ์ฐ, ํ์ผ์ ์ญ์ ๋๊ณ ํ์ผ์ด ์ฌ์ฉ ์ค์ด๋ ๊ณต๊ฐ์ด ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ๋ฉ๋๋ค.
์ด๋ฆ์ด ํ์ผ์ ๋ํ ๋ง์ง๋ง ๋งํฌ์์ง๋ง ํ๋ก์ธ์ค์ ํ์ผ์ด ์ด๋ ค ์๋ ๊ฒฝ์ฐ ํด๋น ํ์ผ์ ์ฐธ์กฐํ๋
๋ง์ง๋ง ํ์ผ ์ค๋ช
์๊ฐ ๋ซํ ๋๊น์ง ํ์ผ์ ๊ทธ๋๋ก ์ ์ง๋ฉ๋๋ค.
์ด๋ฆ์ด ๊ธฐํธ ๋งํฌ๋ฅผ ์ฐธ์กฐํ๋ฉด ๋งํฌ๊ฐ ์ ๊ฑฐ๋ฉ๋๋ค.
์ด๋ฆ์ด ์์ผ, FIFO ๋๋ ์ฅ์น๋ฅผ ์ธ๊ธํ ๊ฒฝ์ฐ ํด๋น ์์ผ์ ์ด๋ฆ์ ์ ๊ฑฐ๋์ง๋ง
๊ฐ์ฒด๊ฐ ์ด๋ ค ์๋ ํ๋ก์ธ์ค๋ ๊ณ์ํด์ ์ด ์์ผ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and
errno is set to indicate the error.
: ์ฑ๊ณต ์ 0์ด ๋ฐํ๋๋ฉฐ, ์ค๋ฅ ์ -1์ด ๋ฐํ๋๊ณ errno๊ฐ ์ค์ ๋์ด ์ค๋ฅ๋ฅผ ๋ํ๋ ๋๋ค.
๐ ํจ์ ์ค๋ช
unlink ํจ์๋ ํ์ผ์ ์ญ์ ํ๋ system call ํจ์๋ก์, hard link์ ์ด๋ฆ์ ์ญ์ ํ๊ณ
hard link๊ฐ ์ฐธ์กฐํ๋ count๋ฅผ 1 ๊ฐ์์ํจ๋ค.
์ฌ๊ธฐ์ hard link ๋, '์๋ณธ ํ์ผ๊ณผ ๋์ผํ i-node (ํ์ผ์ ๊ณ ์ ๋ฒํธ, ํ์ผ ์์ฑ์ ์ฃผ์ด์ง.) ๋ฅผ ๊ฐ์ง๋ ๋ณต์ฌ๋ณธ' ์ผ๋ก,
cp๋ก ๋ง๋๋ ์ผ๋ฐ ๋ณต์ฌ๋ณธ๊ณผ ๋ค๋ฅด๊ฒ, ์๋ณธ์ ์์ ํ๋ hard link ๋ณต์ฌ๋ณธ์ ์์ ํ๋ ํจ๊ป ์์ ๋๋ค.
i-node ๋ก 1234๋ฅผ ๊ฐ์ง๊ณ ์๋ ํ์ผ์ด ์์ ๋๋ฉด ๊ฐ์ i-node : 1234๋ฅผ ๊ฐ์ง ๋ชจ๋ ํ์ผ๋ค์ด ๊ฐ์ด ์์ ๋๋ ๊ฒ์ด๋ค.
๋ฐ๋ผ์, ์๋ณธ ํ์ผ์ด ์ง์์ ธ๋ hard link ํ์ผ์ ๋ณด์กด๋์ด ์คํ๋๋ค.
(๋ฐ๋๋๋ ๊ฐ๋ ์ผ๋ก soft link๋ i-node๊ฐ ๋ฌ๋ผ ์๋ณธ์ด ์ง์์ง๋ฉด ๋งํฌ ์ฐ๊ฒฐ์ด ๋๊ธฐ๊ฒ ๋๋ค.)
๊ทธ๋ฆฌํ์ฌ, ์์์ ์ธ๊ธํ unlink๋ฅผ ํตํด hard link๊ฐ ์ฐธ์กฐํ๋ count๊ฐ ์ค์ด๋ค์ด 0์ด ๋๋ฉด,
์๋ณธ๊ณผ hard link ๋ชจ๋ ์ง์์ง ์ํ์ด๋ฏ๋ก, ํด๋น ํ์ผ์ ๋ด์ฉ์ด ์ ์ฅ๋ disk ๊ณต๊ฐ์ freeํ๊ฒ ๋๋ค.
hard link๊ฐ ์๋ ์ผ๋ฐ ํ์ผ์ ๋ฐ๋ก disk ๊ณต๊ฐ์ freeํ์ฌ ์ฌ์ค์ ํ์ผ์ ์ญ์ ํ๋ ๊ฒ์ด๋ค.
ํจ์์ ์ธ์๋ก๋ const char* pathname ์ ๋ฐ๋๋ฐ, ์ด๋ ์ญ์ ํ ํ์ผ๋ช ์ด๋ฉฐ (full path ๋๋ ์๋ path)
return ๊ฐ์ผ๋ก๋ '์ ์์ ์ผ๋ก file ๋๋ link๊ฐ ์ญ์ '๋๋ฉด 0์ ๋ฐํํ๊ณ ,
'์ค๋ฅ๊ฐ ๋ฐ์'ํ๋ฉด -1์ ๋ฐํํ๊ณ ์ค๋ฅ๋ด์ฉ์ด errno์ ์ ์ฅ๋๋ค.
#include <unistd.h>
int main()
{
unlink("testabc");
return (0);
}
์คํ ๊ฒฐ๊ณผ)

๋จ์ํ testabc๋ผ๋ ํ์ผ์ unlink ํ์ฌ ์ญ์ ํด์ฃผ๋ ์ฝ๋์ด๋ค. ์คํ ์, ํด๋น ํ์ผ์ด ์ญ์ ๋ ๋ชจ์ต์ ๋ณผ ์ ์๋ค.
ํด๋น ํ์ผ์ hard link๊ฐ ์กด์ฌํ์ง ์์ผ๋ฏ๋ก ๋ฐ๋ก disk ๊ณต๊ฐ์ freeํด์ค ์ํ์ด๋ค.
ํน์ํ ์ํฉ์ด ์๋๋ฐ, ์ง์ฐ๋ ค๋ ํ์ผ์ด ๋ ์ด์ ์ฐ๊ฒฐ๋ hard link๊ฐ ์์ง๋ง, open๋์ด ์๋ ๊ฒฝ์ฐ์๋
ํ์ผ์ด close๋ ๋๊น์ง ํ์ผ์ remain, ์ ์ง ์์ผ์ค๋ค.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd = 0;
fd = open("test_unlink", O_WRONLY | O_CREAT | O_TRUNC);
unlink("test_unlink");
char buf[10] = "good";
int word_len = write(fd, buf, 10);
if (word_len == 10)
printf("write O\n");
else
printf("write X\n");
close(fd);
return (0);
}
๊ฒฐ๊ณผ - std_output)
| write O |
open์ ํ๊ณ , ๋ฐ๋ก unlinkํ ์ํฉ์ธ๋ฐ, ํด๋น ํ์ผ์ด close๊ฐ ๋๊ธฐ ์ ๊น์ง๋ ํ์ผ์ ์ ์ง์์ผ์ฃผ๋ ํน์ง์ด ์๋ค.
๋ฐ๋ผ์ ์ดํ์ write๋ฅผ ํ๋ฉด, ์ ์์ ์ผ๋ก write๊ฐ ๋๋ ๋ชจ์ต์ ๋ณผ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ , close๋ฅผ ํตํด ๋ง์ง๋ง์ ๋ซ๊ณ ํด๋น ํ์ผ์ ์ง์์ง๊ฒ ๋๋ค.
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [function] waitpid ํจ์ ์์๋ณด๊ธฐ (2) | 2024.06.13 |
|---|---|
| [function] wait ํจ์ ์์๋ณด๊ธฐ (0) | 2024.06.09 |
| [function] pipe ํจ์ ์์๋ณด๊ธฐ (0) | 2024.06.06 |
| [function] fork ํจ์ ์์๋ณด๊ธฐ (2) | 2024.06.04 |
| [function] exit ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.31 |