LinuxUNIX Programming APUE Process Control IT APUE Process










































- Slides: 42

Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT대학 컴퓨터공학과

강의 내용 APUE (Process Control) 프로세스 ID 프로세스 생성 프로세스 종료 레이스 컨디션 프로그램 실행 기타 Page 2 UNIX System Programming by Yang-Sae Moon

Process Identifiers APUE (Process Control) Every process has a unique process ID, a nonnegative integer. (모든 프로세스는 양수의 유일한 식별자(PID)를 가짐) System Processes: 0(or 1) ~ 10000번 이하 System Process 예제 • swapper: scheduler process (it controls time slots for processes) • init: invoked by the kernel at the end of the bootstrap procedure • pagedaemon: supports the paging of the virtual memory system Linux/Unix 버전에 따라 System Process 종류가 상이함 Page 3 UNIX System Programming by Yang-Sae Moon

System Process 예제 (1/2) Page 4 APUE (Process Control) UNIX System Programming by Yang-Sae Moon

System Process 예제 (2/2) Page 5 APUE (Process Control) UNIX System Programming by Yang-Sae Moon

PID 관련 함수 APUE (Process Control) #include <sys/types. h> #include <unistd. h> pid_t uid_t gid_t getpid(void); getppid(void); getuid(void); geteuid(void); getgid(void); getegid(void); // // // returns returns process ID parent process ID real user ID effective user ID real group ID effective group ID None of these functions has an error return. Page 6 UNIX System Programming by Yang-Sae Moon

강의 내용 APUE (Process Control) 프로세스 ID 프로세스 생성 프로세스 종료 레이스 컨디션 프로그램 실행 기타 Page 7 UNIX System Programming by Yang-Sae Moon

fork() (1/3) APUE (Process Control) #include <sys/types. h> #include <unistd. h> pid_t fork(void); fork() is the ONLY way to create a process in Linux/Unix kernel. Child process is the new process created by fork() is called once, but returns twice! • returns 0 in child process. • returns the child process ID in parent process. Page 8 UNIX System Programming by Yang-Sae Moon

fork() (2/3) APUE (Process Control) Child gets a copy of parent’s data space, heap, and stack • Often, read-only text segment is shared Parent and child continue executing instructions following the fork() call Often, fork() is followed by exec(). Page 9 UNIX System Programming by Yang-Sae Moon

fork() (3/3) APUE (Process Control) Page 10 UNIX System Programming by Yang-Sae Moon

예제: fork. c (1/2) APUE (Process Control) Page 11 UNIX System Programming by Yang-Sae Moon

예제: fork. c (2/2) APUE (Process Control) 실행 결과 Page 12 UNIX System Programming by Yang-Sae Moon

Properties Inherited to the Child APUE (Process Control) 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 Page 13 UNIX System Programming by Yang-Sae Moon

Properties NOT Inherited to the Child APUE (Process Control) 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 Page 14 UNIX System Programming by Yang-Sae Moon

강의 내용 APUE (Process Control) 프로세스 ID 프로세스 생성 프로세스 종료 레이스 컨디션 프로그램 실행 기타 Page 15 UNIX System Programming by Yang-Sae Moon

exit() – Process Termination APUE (Process Control) 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 Page 16 UNIX System Programming by Yang-Sae Moon

강의 내용 APUE (Process Control) 프로세스 ID 프로세스 생성 프로세스 종료 레이스 컨디션 프로그램 실행 기타 Page 17 UNIX System Programming by Yang-Sae Moon

Race Conditions (1/2) APUE (Process Control) Multiple processes share some data. Outcome depends on the order of their execution (i. e. RACE) • (Process A) x = 20; • (Process B) x += 10; After fork(), we cannot predict if the parent or the child runs first! The order of execution depends on: • System Load • Kernel’s Scheduling Algorithm Page 18 UNIX System Programming by Yang-Sae Moon

Race Conditions (2/2) APUE (Process Control) For parent to wait for child, • Call wait(), waitpid(), wait 3(), wait 4() • Use signals or other IPC methods For child to wait for parent, • while(getppid() != 1) sleep(1); // Parent가 죽을 때까지 기다림 • Use signals or other IPC methods Page 19 UNIX System Programming by Yang-Sae Moon

예제: race. c (1/2) APUE (Process Control) #include <stdio. h> // race. c #include <sys/types. h> err_sys(char *p) { perror(p); exit(-1); } int main(void) { pid_t pid; if ((pid = fork()) < 0) err_sys("fork error"); else if (pid == 0) charatatime("output from childn"); else charatatime("output from parentn"); exit(0); } charatatime(char *str) { char *ptr; int c; for (ptr = str; c = *ptr++; ) { putc(c, stdout); fflush(stdout); usleep(1); } } Page 20 UNIX System Programming by Yang-Sae Moon

예제: race. c (2/2) APUE (Process Control) 실행 결과 Page 21 UNIX System Programming by Yang-Sae Moon

How to Avoid Race Condition? APUE (Process Control) #include <stdio. h> #include <sys/types. h> int main(void) { pid_t pid; TELL_WAIT(); } if ((pid = fork()) < 0) err_sys("fork error"); else if (pid == 0) { WAIT_PARENT(); // parent goes first charatatime("output from childn"); } else { charatatime("output from parentn"); TELL_CHILD(pid); } exit(0); How to implement TELL_WAIT(), WAIT_PARENT(), and TELL_CHILD()? • Use signals (강의노트 15) • Use IPC methods (강의노트 16) Page 22 UNIX System Programming by Yang-Sae Moon

강의 내용 APUE (Process Control) 프로세스 ID 프로세스 생성 프로세스 종료 레이스 컨디션 프로그램 실행 기타 Page 23 UNIX System Programming by Yang-Sae Moon

Program Execution: exec() (1/2) APUE (Process Control) When a process calls one of the exec() functions • that process is completely replaced by the new program (새로운 프로그램으로 대체) (text, data, heap, and stack segments) • and the new program starts at its main function 함수 exec()를 호출하여 완전히 다른 프로그램으로 실행된다. Page 24 UNIX System Programming by Yang-Sae Moon

Program Execution: exec() (2/2) Page 25 APUE (Process Control) UNIX System Programming by Yang-Sae Moon

exec() Functions APUE (Process Control) #include <unistd. h> int execl(const char *pathname, const char *arg 0, … , (char *)0); int execv(const char *pathname, const char *argv[]); int execle(const char *pathname, const char *arg 0, … /* (char*) 0, char *const envp[] */); int execve(const char *pathname, const char *argv[], char *const envp[]); int execlp(const char *filename, const char *arg 0, … , (char *)0); int execvp(const char *filename, const char *argv[]); All six return: -1 on error, no return on success exec? (p, l, v, e) • p: filename (not pathname) • l: takes a list of arguments (the last argument should be a null pointer) • v: takes argv[] vector • e: takes envp[] array without ‘e’, the environment variables of the calling process are copied Page 26 UNIX System Programming by Yang-Sae Moon

Properties inherited to the new program APUE (Process Control) same ID • process ID, parent process ID, real user ID, real group ID, supplementary group IDs, process group ID, session ID controlling terminal time left until alarm clock current working directory root directory file mode creation mask file locks process signal mask pending signals resource limits, … Page 27 UNIX System Programming by Yang-Sae Moon

예제: nexec. c, echoall. c (1/5) Page 28 APUE (Process Control) UNIX System Programming by Yang-Sae Moon

예제: nexec. c, echoall. c (2/5) Page 29 APUE (Process Control) UNIX System Programming by Yang-Sae Moon

예제: nexec. c, echoall. c (3/5) Page 30 APUE (Process Control) UNIX System Programming by Yang-Sae Moon

예제: nexec. c, echoall. c (4/5) Page 31 APUE (Process Control) UNIX System Programming by Yang-Sae Moon

예제: nexec. c, echoall. c (5/5) APUE (Process Control) 실행 결과 Page 32 UNIX System Programming by Yang-Sae Moon

강의 내용 APUE (Process Control) 프로세스 ID 프로세스 생성 프로세스 종료 레이스 컨디션 프로그램 실행 기타 Page 33 UNIX System Programming by Yang-Sae Moon

system() APUE (Process Control) #include <stdlib. h> int system(const char *cmdstring); 주어진 스트링(cmdstring)을 Shell 상에서 수행시킨다. • e. g. ) system(“date > file”); 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 34 UNIX System Programming by Yang-Sae Moon

예제: myls. c (1/2) APUE (Process Control) #include <stdio. h> // myls. c main(int ac, char *av[]) { int i; char cmdstr[1024]; strcpy(cmdstr, "/bin/ls "); for(i=1; i < ac; i++) { strcat(cmdstr, av[i]); strcat(cmdstr, " "); } fprintf(stdout, "cmdstr = "%s"n", cmdstr); system(cmdstr); exit(0); } Page 35 UNIX System Programming by Yang-Sae Moon

예제: myls. c (2/2) APUE (Process Control) 실행 결과 Page 36 UNIX System Programming by Yang-Sae Moon

Process Times APUE (Process Control) #include <sys/times. h> 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 and depends 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를 점유한 시간) Page 37 UNIX System Programming by Yang-Sae Moon

예제: cmd_time. c (1/4) APUE (Process Control) 소스 코드 Page 38 UNIX System Programming by Yang-Sae Moon

예제: cmd_time. c (2/4) APUE (Process Control) 소스 코드 Page 39 UNIX System Programming by Yang-Sae Moon

예제: cmd_time. c (3/4) APUE (Process Control) 소스 코드 Page 40 UNIX System Programming by Yang-Sae Moon

예제: cmd_time. c (4/4) APUE (Process Control) 실행 결과 Page 41 UNIX System Programming by Yang-Sae Moon

Homework #10 APUE (Process Control) Page 42 UNIX System Programming by Yang-Sae Moon
Unix
Apue
Apue
Apue
Pthread example
Perbedaan linear programming dan integer programming
Greedy algorithm vs dynamic programming
System programming definition
Linear vs integer programming
Programing adalah
Product and process control
Control structures in c programming
Literal table stores
Planning programming budgeting and execution ppbe process
Flowgorit
Programming process steps
Programming process steps
Programming process steps
Primary control vs secondary control
Control volume vs control surface
Stock control e flow control
Control volume vs control surface
Negative vs positive control
What is a negative control in an experiment
Hdlc adalah
Control de flujo parada y espera
Negative control vs positive control examples
Error control and flow control
Scalar control vs vector control
Komponen ltspice
The control process begins with establishing standards
Statistical process control ppt
Systemactio
Procurement management pmi
Project control and process instrumentation
Explain the process control block
Unix process state transition diagram
Spc/fdc/rms/apc
Process control instrumentation technology
Process design and control design should always be in
Process control block pcb
Pfmea and control plan example
What are threads in os