tonykang22 / study

0 stars 0 forks source link

[리눅스 개발환경] 10. 프로세스 #81

Open tonykang22 opened 2 years ago

tonykang22 commented 2 years ago

10. 프로세스

프로세스 개요

프로세스란?


프로세스 ID


부모/자식 프로세스

예시

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

pid_t getpid (void); 
pid_t getppid (void);



프로세스 생성

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

pid_t fork (void);


pid_t pid;
pid = fork(); // fork() 시스템 콜을 호출한 시점에 두 개의 프로세스가 작동
if (pid > 0) { /* parent process */
    printf(“Parent process – child’s PID(%d)\n”, pid) 
} else if (pid == 0) { /* child process */
    printf(“Child process\n”); 
} else if (pid == -1) { /* error */
    perror(“fork error: “);
}



프로세스 실행

#include <unistd.h>

int execl (const char *path, const char *arg, ...);


int execl (const char path, const char arg, ...); int execlp (const char file, const char arg, ...); int execle (const char path, const char arg, ..., char * const envp[]);

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


- l : 인자를 리스트로 제공
- v : 인자를 벡터로 제공
- p : file 인자 값을 사용자의 실행 경로 환경 변수에서 찾게 됨
    * 환경 변수와 관련된 것은 보안상의 취약성이 있어 잘 사용하지 않는다.
    * Path의 절대 경로를 써주면 사실 실행 경로를 찾아 사용할 필요가 없다.
- e : 새롭게 생성될 프로세스를 위한 새로운 환경 제공
    * e(nvironment) : 환경 변수 포인터인 envp를 받는다.

<br>

- 예는 다음과 같다.
``` c
const char *args[] = { “test_program”, “test.file”, NULL }; 
int ret;

ret = execv(“/home/bin/test_program”, args); 
if (ret == -1)
    perror(“execv error: “);


#define _XOPEN_SOURCE /* WEXITSTATUS 등을 사용할 경우 */
#include <stdlib.h>

int system (const char *command);
int ret;
ret = system(“touch empty.file”); 
if (ret == -1) {
    printf(“system error”); 
} else {
    printf(“system result: %d\n”, WEXITSTATUS(ret));
}



프로세스 종료

#include <stdlib.h> 

void exit (int status);


#include <stdlib.h>

int atexit (void (*function)(void));
// exit을 만나는 시점에 function을 수행하게 된다.


void final(void) { printf(“atexit() succeeded!\n”); }

int main(void) { if (atexit(final)) fprintf(stderr, “atexit() failed!\n”); return 0; // 여기 다음 exit가 묵시적으로 호출된다는 의미 -> final()을 수행한다. }


<br><br>

### 자식 프로세스 종료
``` c
#include <sys/types.h> 
#include <sys/wait.h>

pid_t wait (int *status);


#include <sys/wait.h>

int WIFEXITED (status);
int WIFSIGNALED (status);
int WIFSTOPPED (status);
int WIFCONFTINUED (status);

int WEXITSTATUS (status); 
int WTERMSIG (status);
int WCOREDUMP (status);



자식 프로세스 종료 예시

int main(void) { int status; pid_t pid;

if (fork() == 0) /* child process */ 
    return 1;

pid = wait(&status); 
if (pid == -1)
    perror("wait error: ");

printf("pid = %d\n", pid);

if (WIFEXITED(status)) {
    printf("child process terminated with exit status (%d)\n",
        WEXITSTATUS(status)); 
    }

if (WIFSIGNALED(status)) {
    printf("child process killed by signal (%d)\n", WTERMSIG(status)); 
    if (WCOREDUMP(status)) { /* signal received SIGSEGV, etc */
        printf("child process dumped core\n"); 
    }
}

if (WIFSTOPPED(status))
    printf("child process stopped by signal (%d)\n", WSTOPSIG(status));

if (WIFCONTINUED(status)) 
    printf("child process continued\n");

return 0; 

}



<br>

- 결과
![image](https://user-images.githubusercontent.com/86760744/180778195-05fed4c2-58cd-4bd6-86a0-9e5d06a60834.png)