Do You Coding?

[function] exit ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[function] exit ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ

๐Ÿ“Œ ๋งค๋‰ด์–ผ (Linux)

๋”๋ณด๊ธฐ

NAME

       exit() - End Program

: ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•˜๊ธฐ

 

SYNOPSIS

       #include <stdlib.h>

       void exit(int status);

 

DESCRIPTION

       The exit() function returns control to the host environment from the program. It first calls all functions that are

       registered with the atexit() function, in reverse order; that is, the last one that is registered is the first one

       called. It deletes all buffers and closes all open files before ending the program.

 

       The argument status can have a value from 0 to 255 inclusive, or be one of

       the macros EXIT_SUCCESS or EXIT_FAILURE. A status value of EXIT_SUCCESS

       or 0 indicates a normal exit;

       otherwise, another status value is returned.

: exit() ํ•จ์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ ํ˜ธ์ŠคํŠธ ํ™˜๊ฒฝ์œผ๋กœ ์ œ์–ด๊ถŒ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € atexit() ํ•จ์ˆ˜์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ

์—ญ์ˆœ์œผ๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋งˆ์ง€๋ง‰์œผ๋กœ ๋“ฑ๋ก๋œ ํ•จ์ˆ˜๋ฅผ ์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๊ธฐ ์ „์—

๋ชจ๋“  ๋ฒ„ํผ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์—ด๋ฆฐ ํŒŒ์ผ์„ ๋ชจ๋‘ ๋‹ซ์Šต๋‹ˆ๋‹ค.

์ธ์ˆ˜ ์ƒํƒœ๋Š” 0์—์„œ 255 ์‚ฌ์ด์˜ ๊ฐ’์„ ํฌํ•จํ•˜๊ฑฐ๋‚˜ ๋งคํฌ๋กœ EXIT_SUCCESS ๋˜๋Š” EXIT_FAILUE ์ค‘

ํ•˜๋‚˜์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EXIT_SUCCESS ๋˜๋Š” 0์˜ ์ƒํƒœ ๊ฐ’์€ ์ •์ƒ์ ์ธ ์ข…๋ฃŒ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค;
๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋‹ค๋ฅธ ์ƒํƒœ ๊ฐ’์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

 

RETURN VALUE

       The exit() function returns both control and the value of status to the operating system.

: exit() ํ•จ์ˆ˜๋Š” ์ปจํŠธ๋กค๊ณผ ์ƒํƒœ ๊ฐ’์„ ๋ชจ๋‘ ์šด์˜ ์ฒด์ œ์— ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ํ•จ์ˆ˜ ์„ค๋ช…

exit ํ•จ์ˆ˜๋Š” atexit()ํ•จ์ˆ˜๋กœ ๋“ฑ๋ก๋œ ์ข…๋ฃŒ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ํ‘œ์ค€ ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋‹ซ์€ ํ›„, ์ปค๋„์˜ ์ข…๋ฃŒ ์ž‘์—…์„ ์‹คํ–‰ํ•œ๋‹ค.

๋‹จ์ˆœํ•˜๊ฒŒ ๋งํ•˜์ž๋ฉด, ์„ค์ •ํ•ด๋‘” ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉฐ ์ข…๋ฃŒ๋ฅผ ์‹œ์ผœ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

ํŠน์ง•์ ์ธ ๋ถ€๋ถ„์œผ๋กœ๋Š”, ๋ชจ๋“  ์—ด๋ ค์ง„ ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ๋‹ซ์œผ๋ฉฐ, ์ถœ๋ ฅ ๋ฒ„ํผ ์† ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ์“ฐ๊ธฐ ์™„๋ฃŒ ์‹œํ‚จ๋‹ค.

์ž…๋ ฅ ์ธ์ž์ธ status๋Š” ์šด์˜์ฒด์ œ๋กœ ์ „๋‹ฌ๋˜๋ฉฐ return์˜ ์ „๋‹ฌ๊ฐ’๊ณผ ๊ฐ™์€ ์—ญํ• ์ด ๋œ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด, exit ํ•จ์ˆ˜์˜ ์ธ์ž์ธ status๋Š” ์–ด๋–ค๊ฑธ๊นŒ?

stdlib.h์— ํ•ด๋‹น status ์ƒ์ˆ˜๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

์ •์˜๋œ ์ƒ์ˆ˜๋ช… ๊ฐ’
EXIT_SUCCESS 0
EXIT_FAILURE 1

 

์œ„์™€ ๊ฐ™์ด, status์— ๊ฐ’์ด 0์ด ๋“ค์–ด๊ฐ€๋ฉด, ์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋œ ์ƒํ™ฉ์œผ๋กœ ๊ฐ„์ฃผํ•˜๊ณ ,

0์ด ์•„๋‹Œ ์ˆซ์ž(๋Œ€์ฒด์ ์œผ๋กœ 1์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ด์™ธ์˜ ์ˆซ์ž๋„ ๋Œ€์ž… ๊ฐ€๋Šฅ) ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ์–ด๋–ค ์˜ค๋ฅ˜์— ์˜ํ•ด ์ข…๋ฃŒ๋œ ์ƒํ™ฉ์œผ๋กœ ๋ณธ๋‹ค.

 

main๋ฌธ์—์„œ์˜ return(0); ์™€ exit(0)์€ ๋™์ผํ•˜์ง€๋งŒ,

exit์€ ์šด์˜์ฒด์ œ์—๊ฒŒ ๊ถŒํ•œ์„ ์ฃผ๋ฉฐ ๋ฐ”๋กœ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•˜๊ณ , return์€ ๋’ค ๋ฌธ์žฅ์„ ์‹คํ–‰ํ•˜๋ฉฐ ํ”„๋กœ๊ทธ๋žจ ์ „์ฒด๋ฅผ ์ค‘์ง€์‹œํ‚จ๋‹ค.

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i;
	printf("์–‘์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
	scanf("%d", &i);
	if(i <= 0)
		exit(1);
	exit(0);
}

 

์ž…๋ ฅ ๋ฐ ๊ฒฐ๊ณผ - fish shell)

์ผ๋ฐ˜ shell์—์„œ ํ™•์ธํ•˜๋ฉด ๋˜‘๊ฐ™์ด ์ข…๋ฃŒ๋œ ๊ฒƒ๋งŒ ์•Œ ์ˆ˜ ์žˆ๊ณ ,

fish shell ์—์„œ ํ™•์ธํ•˜๋ฉด exit(1)์ด ์‹คํ–‰๋œ ๊ฒƒ์„ [1]๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋งŒ์•ฝ exit(1)์ž๋ฆฌ์— exit(5)๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด, [1]์ด [5]๋กœ ๋ฐ”๋€Œ๊ณ ,

์ง€์ •ํ•œ status๋ฅผ ์šด์˜์ฒด์ œ๋กœ ์ „๋‹ฌํ•œ ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น exit(1)์ž๋ฆฌ๊ฐ€ return(1)์œผ๋กœ ๋ฐ”๋€Œ์–ด๋„ ๋™์ผํ•˜๋‹ค.