1 컴퓨터 컴퓨터 특강 특강 (UNIX System Programming) (UNIX System Programming) APUE(Process APUE(Process Control) [Ch. 8] Control) [Ch. 8] 2006 2006년 봄학기 봄학기 문양세 문양세 강원대학교 강원대학교 컴퓨터과학과 컴퓨터과학과 Page 2 UNIX System Programming by Yang-Sae Moon 강의 강의 내용 내용 프로세스 ID 프로세스 생성 프로세스 종료 레이스 컨디션 프로그램 실행 기타 APUE (Process Control)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
컴퓨터컴퓨터 특강특강 (UNIX System Programming)(UNIX System Programming)
Process Identifiers (1/2)Process Identifiers (1/2)
Every process has a unique process ID, a nonnegative integer.(모든 프로세스는 양수의 유일한 식별자(PID)를 가짐)
Process ID 0: swapper
• scheduler process (it controls time slots for processes)
• system process
• part of the kernel
• no program on disk corresponds to this process
APUE (Process Control)
Page 4UNIX System Programmingby Yang-Sae Moon
Process Identifiers (2/2)Process Identifiers (2/2)
Process ID 1: init process
• invoked by the kernel at the end of the bootstrap procedure
• /etc/init or /sbin/init
• reads the system-dependent initialization files (/etc/rc*)
• brings the system to a certain state (multi-user)
• a normal user process but runs with super-user privileges
• PID 1 is immortal
Process ID 2: pagedaemon
• supports the paging of the virtual memory system
• kernel process
APUE (Process Control)
3
Page 5UNIX System Programmingby Yang-Sae Moon
System Process System Process 예제예제APUE (Process Control)
Page 6UNIX System Programmingby Yang-Sae Moon
PID PID 관련관련 함수함수
None of these functions has an error return.
APUE (Process Control)
#include <sys/types.h>#include <unistd.h>
pid_t getpid(void); // returns process IDpid_t getppid(void); // returns parent process IDuid_t getuid(void); // returns real user IDuid_t geteuid(void); // returns effective user IDgid_t getgid(void); // returns real group IDgid_t getegid(void); // returns effective group ID
File Sharing after fork()File Sharing after fork()APUE (Process Control)
Parent and child share the same file descriptors.Parent and child share the same file offset.Therefore, intermixed output will occur from parent and child.
parent process table entry
file descriptorsfd 0:fd 1:fd 2:
. . . .
fd flags ptr
file tablefd status flagscurrent file offsetv-node ptr
fd status flagscurrent file offsetv-node ptr
v-node tablev-node
informationi-node
information
current file size
file descriptorsfd 0:fd 1:fd 2:
. . . .
fd flags ptr
child process table entryfd status flagscurrent file offsetv-node ptr
v-nodeinformation
i-nodeinformation
current file size
v-nodeinformation
i-nodeinformation
current file size
Page 14UNIX System Programmingby Yang-Sae Moon
Properties Inherited to the ChildProperties Inherited to the Child
real user and group ID, effective user and group ID
supplementary group IDs
process group ID, session ID
set-user-ID and set-group ID flags
current working directory
root directory
file mode creation mask
signal mask and dispositions
the close-on-exec flag for any open file descriptors
environment
attached shared memory segments
resource limits
APUE (Process Control)
8
Page 15UNIX System Programmingby Yang-Sae Moon
Properties NOT Inherited to the ChildProperties NOT Inherited to the Child
the return value from fork()
the process IDs are different
file locks
pending alarms are cleared for the child
the set of pending signals for the child is set to the empty set
APUE (Process Control)
Page 16UNIX System Programmingby Yang-Sae Moon
강의강의 내용내용
프로세스 ID
프로세스 생성
프로세스 종료
레이스 컨디션
프로그램 실행
기타
APUE (Process Control)
9
Page 17UNIX System Programmingby Yang-Sae Moon
exit() exit() –– Process TerminationProcess Termination
A process can terminate in 5 ways:
Normal Termination
• return from main()
• exit() w/ cleanup procedure
• _exit() w/o cleanup procedure
Abnormal Termination
• calling abort() (generates SIGABRT signal)
• process receives signals
APUE (Process Control)
Page 18UNIX System Programmingby Yang-Sae Moon
exit() exit() –– Termination StatusTermination Status
Exit Status:
• argument of exit(), _exit()
• the return value from main()
Termination Status:
• Normal termination: Exit status Termination status
• Abnormal termination: kernel indicates reason Termination status
Parent can obtain the termination status of the child process.
• by wait() or waitpid()
What if parent terminates before child?
• init(PID = 1) becomes the parent of the child process
system() is implemented by calling fork, exec, and waitpid.
Return values:
• -1 with errno: fork or waitpid fails
• 127: exec fails
• Termination status of shell: all 3 functions succeed
Page 42UNIX System Programmingby Yang-Sae Moon
예제예제: : system.csystem.c
#include <sys/types.h> // system.c#include <sys/wait.h>#include <errno.h>#include <unistd.h>int system(const char *cmdstring) /* version without signal handling */{pid_t pid; int status;
if(cmdstring == NULL)return(1); /* always a command processor with Unix */
if((pid = fork()) < 0) {status = -1; /* probably out of processes */
clock_t times(struct tms *buf);Returns: elapsed wall clock time in clock ticks if OK, -1 on error
struct tms (clock_t tms_utime; /* user cpu time */clock_t tms_stime; /* system cpu time */clock_t tms_cutime; /* child user cpu time */clock_t tms_cstime; /* child system cpu time */
}
Wall clock time: the amount of time the process takes to run anddepends on the system loads. (실제 수행된 시간)
User CPU time: attributed to user instructions(사용자 코드에 의해 CPU를 점유한 시간)
System CPU time: attributed to the kernel, when it executes on behalf of the process (시스템 코드에 의해 CPU를 점유한 시간)