제11장 프로세스 관리.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Operating Systems Chapter 2
1 Logging in to a UNIX System init ( Process ID 1 created by the kernel at bootstrap ) spawns getty for every terminal device invokes our login shell terminal.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Creating and Executing Processes
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Chapter 11 Process Management
Operating Systems Processes 1.
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
제 11 장 프로세스 시스템 프로그래밍 1. Objectives Process Start and Exit Create a child process Execute a new program within a process Signal System boot 2.
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
Signals and Signal Handling. Signals A predefined message sent between two processes or from the kernel to a process, or by a user to a process A software.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
© 숙대 창병모 1 제 10 장 신호 (Signal). © 숙대 창병모 2 Contents 1. Signal Concepts 2. signal() 3. Interrupted System Calls 4. kill() /raise() 5. alarm() pause() 6.
Linux/UNIX Programming APUE (Signals) [Ch. 10] 최미정 강원대학교 컴퓨터과학전공.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
G.Jyostna.
Section 8: Processes What is a process Creating processes Fork-Exec
Precept 14 : Ish dup() & Signal Handling
Linux/UNIX Programming
LINUX System : Lecture 8 Programming with Processes
Using Processes.
Unix Process Management
제11장 프로세스 시스템 프로그래밍 2011 가을 숙명여대 창병모.
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
Linux/UNIX Programming
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
More on UART Interrupts; System Calls
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
Process Creation Process Termination
Process Programming Interface
Advanced Uses of Pointers
The Environment of Unix Process
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Linux/UNIX Programming
EECE.4810/EECE.5730 Operating Systems
System Programming: Process Management
Presentation transcript:

제11장 프로세스 관리

Objectives  create a child process  execute a new program within a process  terminate a process  wait for a child process to terminate

What is a process ? a process is an executing program together with information about the program in some table entries in kernel the program's stack and system stacks the user area which contains information about a process a process image is the layout of a process in memory a process is not a program by itself

Components of Process code area - the executable code of a process data area - static and global variables - heap area stack area - runtime stack to hold activation records including local variables kernel data structures to keep information one process table in kernel a user area per process information like open fds, signal actions, etc

Components of Process main() { … TEXT ... } DATA STACK USER AREA

Kernel processes and user processes process in user mode a system call make it into kernel mode kernel process process in kernel mode the code is linked directly into the kernel

System Calls : Creating a new process int fork( ) create a new process (child process) an almost-exact duplicate of the parent process code, data, stack, open file descriptors, etc. return value success parent process : child process id child process :0 fail -1

System Calls : Creating a new process

System Calls Process id int getpid() int exit (int status) return a process's id int getppid() return a parent process's id int exit (int status) a process may terminate process by executing exit() closes a process's fds, deallocate its code, data, and stack sends its parent a SIGCHLD signal and waiting for its termination code to be accepted (zombie process)

System Calls Orphan process If a parent dies before its child, the child is called an orphan process and is adopted by "init" process, PID 1. it is dangerous for a parent to terminate without waiting for the death of its child.

System Calls int wait(int* status) wait() causes a process to suspend until one of its children terminate a successful call to wait() returns the pid of the child that terminated and places a status code into status

Example : fork and wait #include <stdio.h> main( ) { int pid, status, childPid; printf(“I’m the parent process and my PID is %d\n”, getpid( )); pid = fork( ); if (pid != 0) { printf(“I’m the parent process with PID %d and PPID %d\n” getpid( ), getppid( )); childPid = wait(&status); printf(“A child with PID %d terminated with exit code %d\n” childPid, status>>8); } else { printf(“I’m the child process with PID %d and PPID %d\n”, exit(42); } printf(“PID %d terminates\n”);

System Calls:Exec int execl(char* path, char* arg0, char* arg1, ... , char* argn, NULL) int execv ( char* path, char* argv[]) replace the calling process's code, data, and stack with the executable whose pathname is path and execute the new code. return : a successful exec() never returns failure : -1

Example : fork and exec call exec( ) after calling fork( ) create a new process and execute a program #include <stdio.h> main( ){ int fork( ); if (fork( ) == 0) { execl("/bin/echo", "echo", "this is message one", NULL); perror("exec one faild"); exit(1); } execl("/bin/echo", "echo", "this is message two", NULL); perror("exec two faild"); exit(2); execl("/bin/echo","echo", "this is message three",NULL); perror("exec three faild"); exit(3); printf("Parent program ending\n");

int chdir (char* pathname) Etc Changing directories int chdir (char* pathname) set a process's current working directory tot he directory pathname the process must have execute permission from the directory to succeed returns 0 if successful; otherwise -1 Accessing user and group ids int getuid() int setuid(int id) int geteuid() int seteuid(int id) int getgid() int setgid(int id) int getegid() int setgid(int id) returns the calling process's real and effective user or group ids

Example #include <stdio.h> main( ) { system(“pwd”); chdir(“/”); chdir(“/user/faculty/chang”); }

Example: Background Processing Execute a program in the background using fork( ) and exec( ) #include <stdio.h> main(argc, argv) int argc, argv[ ]; { if (fork( ) == 0) { execvp(argv[1], &argv[1]); fprintf(stderr, “Could not execute %s\n”,argv[1]); }

Example: Redirection %redirect out ls -l duplicate the fd of “out” file to the std. output file(fd=1) execute the command then all standard output will be redirected to the “out” file #include <stdio.h> main(argc, argv) int argc, argv[ ]; { int fd; fd=open(argv[1], O_CREAT|O_TRUNC|O_WRONLY, 0600) dup2(fd, 1); close(fd); execvp(argv[2], &argv[2]); perror(“main”); }

Example: Redirection %command > output 1) The parent shell forks and then waits for the child shell to terminate 2) The child shell opens the file "output“, and 3) duplicates the fd of “output" to the standard output fd, and close the fd of the original file 4) The child shell then exec's the “command”. All of the standard output of the command goes to "output“ 5) When the child shell terminates, the parent resumes

Signals Unexpected events a floating-point error a power failure an alarm clock the death of a child process a termination request from a user (Ctrl-C) a suspend request from a user (Ctrl-Z) Process Signal #8

Predefined signals 31 signals /usr/include/signal.h See the table in page 427 Actions of the default signal handler terminate the process and generate a core(dump) ignores and discards the signal(ignore) suspends the process (suspend) resume the process

Terminal signal Terminal signals Control-C SIGINT signal Control-Z SIGSTP signal Requesting an alarm signal SIGALRM signal Default handler display message “Alarm clock”

Example : Alarm Alarm.c #include <stdio.h> main( ) { alarm(3); printf(“Looping forever …\n”); while (1); printf(“This line should never be executed\n”); }

Handling Signals: signal( ) signal(int sigCode, void (*func)( ))) specify the action for a signal sigCode func func SIG_IGN SIG_DFL user-defined function return the previous func

Handling Signals: Example #include <stdio.h> #include <signal.h> int alarmFlag=0; alarmHandler( ); main( ) { signal(SIGALRM, alarmHandler); alarm(3); printf(“Looping …\n”); while(!alarmFlag) { pause( ); } printf(“Loop ends due to alarm signal \n”); } /* main */ alarmHandler( ) { printf(“An alarm clock signal was received\n”); alarmFlag = 1;

Example: Protecting Critical Code #include <stdio.h> #include <signal.h> main ( ) { int (*oldHandler)( ); printf(“I can be Control-C’ed\n”); sleep(3); oldHandler = signal(SIGINT, SIG_IGN); printf(“I’m proctected from Control-C now\n”); signal(SIGINT, oldHandler); printf(“I can be Control-C’ed again\n”); }

Sending signals: kill() int kill(int pid, int sigCode) send signal with value sigCode to the process with PID pid condition for success the owner of the sending process = the owner of pid the owner of the sending process = super-user

Example: Time Limit #include <stdio.h> #include <signal.h> int delay; childHandler( ); main(argc, argv) int argc; char *argv[ ]; { int pid; signal(SIGCHLD,childHandler); pid = fork(); if (pid == 0) { execvp(argv[2], &argv[2]); perror(“Limit”); } else { sscanf(argv[1], “%d”, &delay); sleep(delay); printf(“Child %d exceeded limit and is being killed\n”, pid); kill(pid, SIGINT); } }

Example: Time Limit Usage %limit 5 ls %limit 4 sleep 100 childHandler( ) /* Executed if the child dies before the parent */ { int childPid, childStatus; childPid = wait(&childStatus); printf(“Child %d terminated within %d seconds\n”, childPid, delay); exit(0); } Usage %limit 5 ls %limit 4 sleep 100

Example: Suspending and Resume #include <signal.h> #include <stdio.h> main( ) { int pid1, pid2; pid1 = fork( ); if (pid1 == 0) { while(1) { printf(“pid1 isalive\n”); sleep(1); } pid2 = fork( ); if (pid2 == 0) { while (1) { printf(“pid2 is alive\n”); sleep(1); } sleep(3); kill (pid1, SIGSTOP); sleep(3); kill(pid1, SIGCONT); sleep(3); kill(pid1, SIGINT); kill(pid2, SIGINT);

Booting Procedure The First Process the first process with pid =0, called sched, is created by UNIX during boot time, and fork/exec twice immediately PID Name 0 sched 1 init 2 pagedaemon init process(/etc/init) is the root process creates processes based upon script files /etc/inittab, /etc/rc login process, mounting file systems, start daemon processes create getty's on each line that a user may login.

The process hierarchy getty process(/etc/getty) login process detects line activity and prompts with login exec /bin/login, after entering a response to the login prompt. login process checks a user's login and password against /etc/passwd exec /bin/sh or /bin/csh set the evnironment variables like HOME, LOGNAME, PATH... shell process initially read commands form start-up files like /etc/.cshrc and $HOME/.cshrc shell starts its job

The process hierarchy