Overview Last Week: ● How to program with directories ● Brief introduction to the UNIX file system Unix System Programming This Week: ● How to program UNIX processes (Chapters 7-9) Processes » Follow the flow of Ch 8. Process control sprinkled with reflections from Ch 7 (e.g., exit, process/program memory layout). ● fork() and exec() 1 2 Maria Hybinette, UGA Maria Hybinette, UGA Outline What is a Process? ● What is a process? ● Review: A process is a program in execution (an active ● fork() entity, i.e., it is a running program , this was also on the ● exec() » Basic unit of work on a computer ● wait() » Examples: ● Process Data – compilation process, – word processing process ● Special Exit Cases – a.out process ● Process Ids – Shell process ● I/O Redirection – (we just need to make sure the program is running) ● User & Group ID real and effective (revisit) ● getenv & putenv ● ulimit 3 4 Maria Hybinette, UGA Maria Hybinette, UGA What is a Process? What is a Process? ● It has both time, and space ● Each user can run many processes at once (e.g., by using &) » A container of instructions with some resources ● A process: ● Process reads, and writes (or updates) » cat file1 file2 & machine resources ● Two processes started on the command line. » e.g., CPU time (CPU carries out the instructions), » ls | wc -l ● A time sharing system (such as UNIX) run several processes » memory, by multiplexing between them » files, Processes » I/O devices (monitor, printer) to accomplish its task n-1 … OS (schedules process) 5 6 Maria Hybinette, UGA Maria Hybinette, UGA
What Makes up a Process? size ? Formal Process Definition ● Program code (text) 3GB User Mode » Compiled version of the text Address Space A process is a � program in execution � , a ● Data (cannot be shared) stack » global variables sequential execution characterized by trace. It – Uninitialized (BSS segment) sometimes has a context (the information or data) and listed separately. – Initialized this � context � is maintained as the process routine1 var1 ● Process stack (scopes) progresses through the system. var2 » function parameters heap » return addresses arrayB[10] ; data » local variables and functions arrayA[10] = {0} ● <<Shared Libraries >> ● Heap: Dynamic memory (alloc) main ● OS Resources, environment routine1 text routine2 » open files, sockets » Credential for security 0x0 ● Registers address space are the shared resources of a(ll) thread(s) in a » program counter, stack pointer 7 8 program Maria Hybinette, UGA Maria Hybinette, UGA Info about a process (running and foot print) 0xFFFFFFFF 4GB ● Example: Process with ● longsleepHelloW (binary with long sleep) Kernel Space 4GB Virtual Address Virtual Addresses ● size a.out. (foot print) Space (32 bit 0xC0000000 3GB ● ps ! architectures) » 61542 pts/5 00:00:00 longsleepHelloW ! ● User Space (focused on ● cat /proc/61542/status ! earlier, lower address space) ● cat /proc/61542/maps ! User Space Virtual Addresses ● Kernel Space ● ppp 0x0 0x0 9 10 Maria Hybinette, UGA Maria Hybinette, UGA What is needed to keep track of a Process? Process Representation Process P 2 Information System Memory ● Memory information: Memory Limits » Pointer to memory segments needed to run a process, i.e., pointers to the Page tables Kernel Process Table Memory base address space -- text, data, stack Initial P 0 Process Number segments. Program counter P 0 : HW state: resources ● Process management information: Program Counter Process P 3 … » Process state, ID P 1 : HW state: resources Registers » Content of registers: Process P 2 P 2 : HW state: resources – Program counter, stack pointer, Process State process state, priority, process ID, P 3 : HW state: resources CPU time used List of opened files Memory mappings ● File management & I/O information: I/O Devices allocated … » Working directory, file descriptors Pending requests open, I/O devices allocated Accounting Process P 1 ● Accounting: amount of CPU used. … Process control Block (PCB) 11 12 Maria Hybinette, UGA Maria Hybinette, UGA
System Control: OS View: Process Control Block Process Attributes (PCB) ● How does an OS keep track of the state of a process? ps and top command can be used to look at » Keep track of � some information � in a structure. current processes – Example: In Linux a process � information is kept in a ● PID - process ID: each process has a unique ID structure called struct task_struct declared in #include linux/sched.h ! ● PPID - parent process ID: The process that – What is in the structure? � forked � to start the (child) process struct task_struct ● nice value - priority (-20 highest to 19 lowest) pid_t pid; /* process identifier */ long state; /* state for the process */ ● TTY associated with terminal (TTY teletype unsigned int time_slice /* scheduling information */ terminal) struct mm_struct *mm /* address space of this process */ – Where is it defined: ● not in /usr/include/linux – only user level code ● usr/src/kernel/2.6.32-431.29.2.elf6.x86_64/include/linux 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Back to user-level Other Process Attributes ● Finding PIDs ● Real user ID » At the shell prompt ● Effective user ID – ps u, ps, ps aux, ● Current directory ● ps no args # your process ● File descriptor table ● ps –ef # every process ● ps -p 77851 # particular process ● Environment – top interative ● Pointer to program code, data stack and heap » In a C program: int p = getpid(); // more later ● Execution priority ● Signal information 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Process ID conventions, and the 3 General Process Types in UNIX Process Life Cycle Interactive – foreground (shell must wait until complete [takes user input], or ● PID 0 – background (&) [no user input] » is usually the scheduler process ( swapper ), a system process – initiated an controlled terminal session (does not correspond to a program stored on disk, the – can accept input form user as it runs and output to the terminal grandmother of all processes). Daemons ● init - Mother of all user processes, init is started at – server processes running in the background (e.g., listening to a port) boot time (at end of the boot strap procedure) and is – Not associated with the terminal responsible for starting other processes – typically started by init process at boot time » It is a user process with PID 1 – Examples: ftpd , httpd , … , mail – If user wants to creates one, detach it from the terminal, kill its parent. (init adopts) » init uses file inittab and directory /etc/rc?.d Batch (at, cron, batch) » brings the user to a certain specified state (e.g. multiuser) – Jobs that are queued and processed one after another ● getty - login process that manages login sessions – recurrent tasks scheduled to run from a queue – periodic, recurrent tasks run when system usage is low, cron-jobs (administered by the daemon crond). – Examples: backups, experimental runs. 17 18 » Zombies … don’t count. Maria Hybinette, UGA Maria Hybinette, UGA
Recommend
More recommend