Notice
Recent Posts
Recent Comments
Link
celina의 이것저것
03. 프로세스 API 본문
반응형
API
사용자가 OS를 통해서 어떤 하드웨어 리소스들을 접근하거나, 실행을 하거나, 제어를 하려고 하는 것들, 그런 함수들
1. fork
자식 프로세스를 생성하고 부모 프로세스 자원의 사본을 할당한다.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc,char * argv[]){
int rc = 0
printf("hello world (pid:%d)\n",(int)getpid());
int rc = fork();
if(rc < 0){
// fork failed; exit
fprintf(stderr,"fork failed\n");
exit(1);
}
else if(rc == 0){
// child (new process)
printf("hello, i am child (pid) : %d\n",(int)getpid());
}
else{
// parent goes down this path (original process)
printf("hello, i am parent of %d (pid:%d)",rc,(int)getpid());
}
}


2. wait
자식 프로세스들이 종료되기까지 호출하는 프로세스가 블록되게 한다.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
printf("hello world (pid:%d)\n", (int)getpid());
int rc = fork();
if (rc < 0) {
// fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) {
// child (new process)
printf("hello, I am child (pid:%d)\n", (int)getpid());
sleep(1)
} else {
//parent goes down this path (original process)
int wc = wait(NULL);
printf("hello. I am parent of %d (wc:%d) (pid:%d)\n",
wc, rc, (int)getpid());
}
return 0;
}
3. exec
파일에 있는 프로세스의 명령어들과 데이터를 그 주소 공간에 로드한다.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
printf("hello world (pid:%d)\n", (int)getpid());
int rc = fork();
if (rc < 0) {
// fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) {
// child (new process)
printf("hello, I am child (pid:%d)\n", (int)getpid());
char *myargs[3];
myargs[0] = strdup("wc"); //program: "wc" (word count)
myargs[1] = strdup("p3.c"); //argument: file to count
myargs[2] = NULL; // marks end of array
execvp(myargs[0], myargs); //runs word count
printf("this shouldn't, print out");
} else {
// parent goes down this path (original process)
int wc = wait(NULL);
printf("hello. I am parent of %d (wc:%d) (pid:%d)\n",
wc, rc, (int)getpid());
}
return 0;
}
fork(), exec() 를 분리해야 하는 이유?
=> fork(), exec()를 분리해놓음으로써 새 program이 실행되기 전에 다양한 setting과 I/P redirection등을 할 수 있다
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
printf("hello world (pid:%d)\n", (int)getpid());
int rc = fork();
if (rc < 0) {
// fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) {
// child: redirect standard output to a file
close(STDOUT_FILENO); // standard output을 닫고
open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU); //이 파일을 연다(생성)->io redirection
// now exec "wc" ...
char *myargs[3];
myargs[0] = strdup("wc"); //program: "wc" (word count)
myargs[1] = strdup("p3.c"); //argument: file to count
myargs[2] = NULL; // marks end of array
execvp(myargs[0], myargs); //runs word count
printf("this shouldn't, print out");
} else {
// parent goes down this path (original process)
int wc = wait(NULL);
assert (wc >= 0);
}
return 0;
}
I/O redirection흐름 이해
shell에선 commands와 argument를 토대로 fork()와 exec()을 이용해 program을 생성하고 수행할 것임
shell에서 fork()를 호출하고, 이어서 exec("wc", "p3.c")를 호출한다
이때 exec 호출 이전에 shell은 STDOUT을 닫고 ./p4.output.txt 파일을 open한다
출력
>
'대학생활 > 운영체제' 카테고리의 다른 글
운영체제 과제 (0) | 2023.10.18 |
---|---|
04. LDE 제한적 직접 실행 (0) | 2023.10.10 |
02. 프로세스 (0) | 2023.09.17 |
01. 하드웨어 소프트웨어 (0) | 2023.09.13 |
00. CPU 가상화 / 메모리 가상화 (0) | 2023.09.13 |