Understanding Process Management in Computer Systems

Slide Note
Embed
Share

Exploring the concepts of process management in computer systems, including process creation, termination, running programs, synchronization between processes, and the components involved in managing processes such as memory, CPU, files, and devices. The importance of processes in sharing system resources, process isolation, and interaction is highlighted. Examination of processes in Unix through commands like ps, examining CPU and memory usage statistics, and understanding the structure and elements of a process are also covered in this comprehensive overview.


Uploaded on Oct 01, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Process Management Process management related system calls Process creation Process termination Running another program in a process Synchronization between parent/child processes Readings APUE 7.6, 8.3, 8.5, 8.6, 8.10 UNP 4.7 1

  2. Computer Systems Overview Processes User Space System Call Interface OS File system CPU Memory Mgmt Device Mgmt scheduling Network Stack Hardware: CPU, Memory, Disk, Keyboard, Monitor 2

  3. Users view Program1 Program2 ProgramX CPU Memory Disk Keyboard Monitor CPU Memory Disk Keyboard Monitor CPU Memory Disk Keyboard Monitor Each program owns its own (virtual) computer. The execution of a program does not affect one another.

  4. Process Informal definition A process is a program in execution Process is not the same as a program. Program is a passive entity stored in disk Program (code) is just one part of the process. 4

  5. What else in a Process? Process context: Memory space (static, dynamic) Procedure call stack Open files, connections Registers and counters : Program counter, stack pointer, general purpose registers 5

  6. Memory Layout of a Process Text Machine instructions Data Global variables Heap Dynamically allocated memories (malloc, new, etc) Stack Local variables Function calls stack heap data text 6

  7. Why Process? Allowing multiple processes (users) to share the system resources. Process isolation (the illusion that each process is the only one on the machine). Process interaction (synchronization, inter-process communication). Which is more important? 7

  8. Examining Processes in Unix ps command Standard process attributes Try ps ef /proc directory All Linux distributions, some other OSes to provide hardware, system, and process information /proc/cpuinfo, /proc/memory, /proc/sys, /proc/####(PID) etc man proc to get more information on /proc top, vmstat command Examining CPU and memory usage statistics. 8

  9. Creating a New Process - fork() UNIX convention: -1 is the failure code for all UNIX system calls pid = fork(); if (pid == -1) { fprintf(stderr, "fork failed\n"); exit(1); } if (pid == 0) { printf( This is the child\n"); exit(0); } if (pid > 0) { printf( This is parent. The child is %d\n", pid); exit(0); } 9

  10. Points to Note fork() is called once but it returns twice!! Once in the parent and Once in the child See example1.c fork() basically duplicates the parent process image Both processes are exactly the same after the fork() call. Are there any dependence between the two processes? Provide a way to distinguish the parent and the child Parent and child share text segment (machine instructions) Parent and child do not share other memory including data space, stack, and heap 10

  11. Points to Note How to distinguish parent and child?? Return value of fork() in child = 0 Return value in parent = process id of child See example2.c What about the data in the program? See example6.c, example6b.c Return value of -1 indicates error in all UNIX system calls. Is it true: All processes are created by fork() in UNIX? 11

  12. Running New Program in a Process Exec() family of functions Completely replace current process with a new program Various forms int execl(char * pathname, char * arg0, , (char *)0); int execv(char * pathname, char * argv[]); int execle(char * pathname, char * arg0, , (char *)0, char envp[]); int execlp(char * filename, char * arg0, , (char *)0); int execvp(char * filename, char * argv[]); Notation convention l: list of arguments v: vector of arguments e: environment variables p: searching PATH to locate filename, unless filename contains / We can use any of them, but normally only one of them is system call (execve), others are library functions man execve 12

  13. execv int execv(char * pathname, char * argv[]); Example: to run /bin/ls l a / pathname: file path for the executable char *argv[]: must be exactly the same as the C/C++ command line argument. argv[0] = /bin/ls ; argv[1] = -l ; argv[2] = -a ; argv[3] = / ; argv[4] = NULL; See example3f.c, example3d.c, example3e.c

  14. Examples Running one command example3a.c Run all commands in the command line argument example3b.c How does a parent know if a child process has run successfully? 14

  15. Properties of exec() Replaces current process image with new program image. If successful, everything after the exec() call will NOT be executed. Will exec() return anything other than -1 (on error)? 15

  16. Running a Command without Killing the Process Fork( ) Child Parent Exec( ) New program image in execution 16

  17. Terminating a Process exit (int status) Clean up the process (e.g close all files) Tell its parent processes that he is dying (SIGCHLD) Tell child processes that he is dying (SIGHUP) Exit status can be accessed by the parent process. See example3.c 17

  18. Parent/Child Synchronization Parent created a child, it has the responsibility to see the child through: check if the child is done. wait, waitpid. Both block if no child process changes state waitpid has option to return immediately if no child has exited. check the exit status of the child pid_t wait(int *stat_loc), see example4.c (and example4a.c) Macros related to exit status WIFEXITED(status), WEXISTSTATUS(status) WIFSIGNALED(status), WIFSTOPPED(status), WIFCONTINUED(status) A child has no responsibility for the parent 18

  19. Zombie and Orphan Processes What happens when a child process terminates but the parent does not wait or has not waited for it? The child process becomes a zombie process (see example12.c) System recycles most of its resources, including process image, and closes all files opened by the process System does maintain certain information, including process ID, exit status, and CPU time, until it is waited What happens when a parent terminates without waiting for child processes to terminate? The child processes become orphan process (normally only mean the child processes that are still running) They will be adopted by a special process (normally init, with process ID 1) Init waits for each (adopted) child process, so no zombie What happens to a zombie process when the parent process terminates? They will also be adopted by init (orphaned zombie) And they are removed (waited) by init 19

  20. Process ID Processes are identified by a process id (pid) getpid(): find your own pid getppid(): find the pid of the parent See example5.c for the time for system calls versus regular routine calls. Linux getpid() is highly optimized Try run example5.c on program.cs.fsu.edu 20

  21. Questions to Consider How to implement the system routine? How to prevent a child process to become long-term zombies? Note that a parent process may run for a long time before it terminates, or it will not terminate under normal conditions (for example, a web server) 21

  22. Summary/Review Why processes? What is process context? How to check processes in UNIX? What does fork() do? What is its return value? Does fork() create all processes in a UNIX system? What does execv() do? What is its return value? How to run a command in a program without getting killed? Does exit completely clean up a process? Can a parent process tell if its child process terminate normally? Can a child process tell if its parent process terminate normally?

Related


More Related Content