Skip to content

minishell_doc #1

@naranghae

Description

@naranghae

fork

fork 함수를 호출하는 프로세스는 부모 프로세스가 되고 새롭게 생성되는 프로세스는 자식 프로세스가 된다.

fork함수가 실행되면 프로세스가 2개로 나눠진다.
그리고 부모먼저 실행되고 자식이 실행되어 while문이 없지만 2개의 출력이 나오게 된다.

#include<unistd.h>

pid_t fork(void);  // 성공: 부모 프로세스에서는 자식 프로세스의 PID값을 반환 받음
                  //         자식 프로세스에서는 0 값을 반환 받음 
                  // 실패: 음수 값(-1) 반환
#include <unistd.h>
#include <stdio.h>

int main()
{
	pid_t pid;
   
	pid = fork();
	if (pid < 0)
    {
		printf("fork failed!\n");
		return (-1);
	}
	else if (pid == 0)
		printf("child_process: %i\n", getpid());
	else
		printf("parent_process: %i\n", getpid());
	return (0);
}

wait, waitpid

https://codetravel.tistory.com/30?category=993122
https://codetravel.tistory.com/42?category=993122
https://www.joinc.co.kr/w/man/2/waitpid

#include <sys/types.h>
#include <sys/wait.h>

pid_t wait(int *staloc);

pit_t waitpid(pid_t pid, int *staloc, int options);
// (pid == -1): 임의의 자식 프로세스를 기다림
//(pid > 0): 프로세스 ID가 pid인 자식 프로세스를 기다림
//(pid < -1): 프로세스 그룹 ID가 pid의 절댓값과 같은 자식 프로세스를 기다림
//(pid == 0): waitpid를 호출한 프로세스의 프로세스 그룹 PID와 같은 프로세스 그룹 ID를 가진 프로세스를 기다림 

wait

wait 함수는 여러 개의 자식 프로세스 중에 어느 하나라도 종료하면 종료한 자식 프로세스 ID를 반환한다. 만약 종료하는 프로세스가 없으면 블록 상태로 계속 기다리게 되고 종료 상태값은 staloc에 저장된다.
만약 자식 프로세스가 없을 때에는 -1을 리턴한다.

자식 프로세스가 정상적으로 종료
반환값

  • 프로세스 ID

statloc값

  • WIFEXITED(statloc) 매크로가 true를 반환
  • 하위 8비트를 참조하여 자식 프로세스가 exit, _exit, _Exit에 넘겨준 인자값을 얻을 수 있음, WEXITSTATUS(statloc)

자식 프로세스가 비정상적으로 종료
반환값

  • 프로세스 ID

statloc값

  • WIFSIGNALED(statloc) 매크로가 true를 반환
  • 비정상 종료 이유를 WTERMSIG(statloc) 매크로를 사용하여 구할 수 있음

함수 오류
반환값

  • -1

statloc값

  • ECHILD : 호출자의 자식 프로세스가 없는 경우
  • EINTR : 시스템 콜이 인터럽트 되었을 때

wait3, wait4

https://bubble-dev.tistory.com/entry/CC-wait32-wait42
https://www.mkssoftware.com/docs/man3/wait3.3.asp

signal, kill

https://blockdmask.tistory.com/23

exit

https://codetravel.tistory.com/28

getcwd, chdir

#include<unistd.h>

char *getcwd(char *buf, size_t size);

buf : 현재 디렉토리의 절대 경로를 저장할 버퍼 주소
size : 버퍼의 크기
#include<unistd.h>
int chdir(const char *pathname);
pathname: dir 이름

합쳐서 만들어 보면,

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

int main(int ac, char **av)
{
	char buf[1024];

	if (ac != 2)
		return (0);
	printf("dir name: %s\n", av[1]);
	printf("before dir: %s\n", getcwd(buf, 1024));
	if(chdir(av[1]) == -1)
		printf("failed, cd\n");
	else
		printf("after dir: %s\n", getcwd(buf, 1024));
	return (0);
}

stat, lstat, fstat

https://bodamnury.tistory.com/37

execve

https://www.it-note.kr/157
https://pracon.tistory.com/37

exec 함수군은 path나 file에 지정한 명령이나 실행 파일을 실행한다. 이때 arg나 envp로 시작하는 인자를 path나 file에 지정한 파일의 main 함수에 전달한다. 각 함수별로 경로명까지 지정하거나 단순히 실행 파일명만 지정하는 등 차이가 있고 인자를 전달하는 형태에도 차이가 있다.

int execve(const char *path, char *const argv[], char *const envp[]);

// path에 지정한 경로명의 파일을 실행하며 argv, envp를 인자로 전달한다. 
// argv와 envp는 포인터 배열이다. 이 배열의 마지막에는 NULL 문자열을 저장해야 한다.

dup, dup2

https://reakwon.tistory.com/104

pipe

https://codingdog.tistory.com/entry/%EB%A6%AC%EB%88%85%EC%8A%A4-pipe-%ED%95%A8%EC%88%98-%ED%8C%8C%EC%9D%B4%ED%94%84%EB%A5%BC-%EB%A7%8C%EB%93%A0%EB%8B%A4
https://reakwon.tistory.com/80

opendir, readdir, closedir

http://forum.falinux.com/zbxe/index.php?document_srl=408485&mid=C_LIB

strerror, errno

https://modoocode.com/105
https://m.blog.naver.com/PostView.nhn?blogId=sebintiger&logNo=220048362896&proxyReferer=https:%2F%2Fwww.google.com%2F

isatty, ttyname, ttyslot

http://neosrtos.com/docs/posix_api/unistd_isatty.html
https://nxmnpg.lemoda.net/ko/3/ttyname
https://nxmnpg.lemoda.net/ko/3/ttyslot

ioctl

https://wogh8732.tistory.com/306

getenv

https://m.blog.naver.com/PostView.nhn?blogId=hoon2huny&logNo=50005636846&proxyReferer=https:%2F%2Fwww.google.com%2F
https://www.joinc.co.kr/w/man/3/getenv

tcsetattr, tcgetattr

https://m.blog.naver.com/PostView.nhn?blogId=choi125496&logNo=130034222760&proxyReferer=https:%2F%2Fwww.google.com%2F
http://neosrtos.com/docs/posix_api/termios_tcgetattr.html
https://rcode.tistory.com/31

tgetent, tgetflag, tgetnum, tgetstr, tgoto, tputs

https://nxmnpg.lemoda.net/ko/3/tgetent
https://hyeonski.tistory.com/6

초기에 해야할 것들 및 알아두어야할 것

  • cmd로 들어오는 문장을 Tokenize (잘 parsing하는 일)
  • export 명령어 시 lst_add_front로 받아서 앞에다 두어야함, unset으로 env를 지울 수 있다.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions