프로세스의 생성

Timo·2022년 7월 22일
0
post-thumbnail

3.3 Operations Processes

fork()

  • UNIX 계열의 운영체제에서 새로운 프로세스를 생성할 때 fork()라는 시스템 콜을 사용한다.
  • 부모 프로세스에서는 자식 프로세스의 PID값이 fork()의 반환 값으로 전달됨.
  • 자식 프로세스에서는 fork()의 반환 값이 0
  • 부모 프로세스가 wait() system call을 호출하면 wait queue에 대기하게 됨
    * 자식 프로세스가 종료되면 wait queue에 interrupt 발생 부모 프로세스는 ready queue로 이동함

Exercise 3.1

int value = 5; 

int main() { 
	pid_t pid;
	pid = fork();

	if (pid == 0) { // child process
		value += 15;
		return 0; 
	} 
	else if (pid > 0) { // parent process
		wait(NULL);
		printf("Parent: value = %d\n", value); // LINE A
	}
} 

결과

Parent: value = 5

Exercise 3.2

#include <stdio.h>
#include <unistd.h>
#include <wait.h>

/*
 * How many processes are created? 
 */ 
int main() { 
	fork(); // fork a child process
	fork(); // fork another child process
	fork(); // and fork another
	return 0; 
} 

-> 총 8개 프로세스 생성됨

[image:A9B64F20-DB6E-4684-91C3-0135EBCA1BF5-10634-000000D353062135/IMG_7988.jpg]

Exercise 3.11

#include <stdio.h> 
#include <unistd.h>
/*
 * How many processes are created?
 */
int main() { 
	int i;

	for (i = 0; i < 4; i++)
		fork();

	return 0;
}

-> fork() 4번 실행했으므로 16개의 프로세스가 생성됨

Exercise 3.12

int main() { 
	pid_t pid;
	pid = fork();

	if (pid == 0) { // child process 
		execlp("/bin/ls", "ls", NULL); 
		printf("LINE J\n"); 
	}
	else if (pid > 0) { // parent process
		wait(NULL); 
		printf("Child Complete\n");
	}

	return 0; 
} 

-> execlp() 시스템 콜이 명령어를 완전히 덮어 쓰기 때문에 LINE J는 출력되지 않는다.

Exercise 3.13

int main() { 
	pid_t pid, pid1;
	pid = fork();

	if (pid == 0) { // child process
		pid1 = getpid();printf("child: pid = %d\n", pid); 
		printf("child: pid1 = %d\n", pid1); // B 
	}else if (pid > 0) { // parent process 
		pid1 = getpid();
		printf("parent: pid = %d\n", pid);   // C
		printf("parent: pid1 = %d\n", pid1); // D
		wait(NULL);
	}
 
	return 0; 
} 

결과

parent: pid = 3645 		//자식 PID
parent: pid1 = 3644 		//자신의 PID
child: pid = 0 			//fork()로 리턴된 PID
child: pid1 = 3645 		//자신의 PID

Exercise 3.16

#define SIZE 5 
int nums[SIZE] = {0, 1, 2, 3, 4};

int main() { 
	pid_t pid;
	int i;
	pid = fork();

	if (pid == 0) { // child process 
		for (i = 0; i < SIZE; i++) { 
        nums[i] *= i;
        printf("CHILD: %d \n", nums[i]); // LINE X
		} 
	} 
	else if (pid > 0) { // parent process
		wait(NULL);
		for (i = 0; i < SIZE; i++) {
			printf("PARENT: %d \n", nums[i]); // LINE X
		} 
	} 

	return 0; 
}

결과

CHILD: 0, 1, 4, 9, 16
PARENT: 0, 1, 2, 3, 4

CPU Scheduling 을 통해 순서가 보장되지 않을 수 있다.

-> 그래서 Synchronization 동기화가 필요함.


퀴즈

  1. 다음 프로그램에서 LINE X 라고 표기된 위치의 실행 순서로 올바른 것은?
int main() {
	pid_t pid = fork(); 

	if (pid > 0) { 
		wait(NULL); 
		// LINE A 

	} else { 
		pid = fork(); 

		if (pid == 0) { 
			// LINE B 

		} else { 
			wait(NULL); 
			// LINE C 
		} 
	} 

	// LINE D 
} 

1) B-C-A-D-D-D
2) B-C-D-A-D-D
3) B-A-D-C-D-D
4) B-D-C-D-A-D

  1. 다음 프로그램의 출력 결과로 올바른 것은?
int x = 10; 

int main() {
	pid_t pid = fork(); 

	if (pid == 0) { 
		x += 10; 

	} else { 
		wait(NULL); 
		pid = fork(); 
		x += 10;

		if (pid > 0) { 
			wait(NULL); 

		} else { 
			x += 10; 
		} 
	}
 
	printf(%d “, x); 
} 

1) 20 20 30
2) 20 30 20
3) 20 30 30
4) 20 20 20

profile
나는 매일 성장하는 사람

0개의 댓글