Unix System Programming Overview Outline What Is a Process? What Is

Unix System Programming Overview Outline What Is a Process? What Is

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() Maria Hybinette, UGA 1 Maria Hybinette, UGA 2 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, ● Special Exit Cases – word processing process – 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 Maria Hybinette, UGA 3 Maria Hybinette, UGA 4 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: » cat file1 file2 & ● Process reads, and writes (or updates) ● Two processes started on the command line. machine resources » ls | wc -l » e.g., CPU time (CPU carries out the instructions), ● 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 0 1 2 … OS (schedules process) Maria Hybinette, UGA 5 Maria Hybinette, UGA 6 What Makes up a Process? Formal Process Definition size? ● Program code (text) User Mode 3GB » Compiled version of the text Address Space ● Data (cannot be shared) A process is a program in execution, a stack sequential execution characterized by trace. It » global variables – Uninitialized (BSS segment) sometimes has a context (the information or data) and listed separately. – Initialized routine1 this context is maintained as the process var1 ● Process stack (scopes) var2 progresses through the system. » function parameters heap » return addresses arrayB[10] ; » local variables and functions data arrayA[10] = {0} ● <<Shared Libraries >> ● Heap: Dynamic memory (alloc) main ● OS Resources, environment routine1 text » open files, sockets routine2 » Credential for security 0x0 ● Registers address space are the shared resources of a(ll) thread(s) in a » program counter, stack pointer Maria Hybinette, UGA 7 Maria Hybinette, UGA program 8 Info about a process (running and foot print) 0xFFFFFFFF 4GB ● ● longsleepHelloW (binary with long sleep) Example: Process with Kernel Space 4GB Virtual Address Virtual Addresses ● size a.out. (foot print) Space (32 bit ● ps ! architectures) 0xC0000000 3GB » 61542 pts/5 00:00:00 longsleepHelloW! ● User Space (focused on ● cat /proc/61542/status! earlier, lower address ● space) User Space cat /proc/61542/maps! Virtual Addresses ● Kernel Space ● ppp 0x0 0x0 Maria Hybinette, UGA 9 Maria Hybinette, UGA 10 What is needed to keep track of a Process? Process Representation Process P2 Information System Memory ● Memory information: Memory Limits » Pointer to memory segments needed to run a process, i.e., pointers to the Page tables Memory base Kernel Process Table address space -- text, data, stack Initial P0 segments. Process Number Program counter P0 : HW state: resources ● Process management information: Program Counter Process P3 … » Process state, ID Registers P1 : HW state: resources » Content of registers: Process P P : HW state: resources – Program counter, stack pointer, Process State 2 2 process state, priority, process ID, CPU time used List of opened files P3 : HW state: resources Memory mappings ● File management & I/O information: I/O Devices allocated » Working directory, file descriptors … open, I/O devices allocated Accounting Pending requests Process P1 ● Accounting: amount of CPU used. Process control … Block (PCB) Maria Hybinette, UGA 11 Maria Hybinette, UGA 12 System Control: OS View: Process Control Block Process Attributes (PCB) ● How does an OS keep track of the state of a ps and top command can be used to look at process? current processes » Keep track of some information in a structure. – 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 Maria Hybinette, UGA 13 Maria Hybinette, UGA 14 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 ● ps –ef # every process ● File descriptor table ● ps -p 77851 # particular process ● Environment – top interative » In a C program: int p = getpid(); // more later ● Pointer to program code, data stack and heap ● Execution priority ● Signal information Maria Hybinette, UGA 15 Maria Hybinette, UGA 16 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 – background (&) [no user input] ● PID 0 – initiated an controlled terminal session » is usually the scheduler process (swapper), a system process – can accept input form user as it runs and output to the terminal (does not correspond to a program stored on disk, the grandmother of all processes). Daemons – server processes running in the background (e.g., listening to a port) ● init - Mother of all user processes, init is started at – Not associated with the terminal boot time (at end of the boot strap procedure) and is – typically started by init process at boot time responsible for starting other processes – Examples: ftpd, httpd, …, mail » It is a user process with PID 1 – 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 ● – recurrent tasks scheduled to run from a queue getty - login process that manages login sessions – periodic, recurrent tasks run when system usage is low, cron-jobs (administered by the daemon crond). – Examples: backups, experimental runs. » ZombiesMaria Hybinette, UGA… don’t count. 17 Maria Hybinette, UGA 18 Hierarchical Processes Tree on a (historical) UNIX System Display Process Hierarchy mother of all user processes OS Kernel pstree (processes) ● Syntax: pstree | more (all process) Process 0 Process 1 Process 2 (BSD) ● Syntax: pstree <PID> (sched - ATT, swapper - BSD) (init) pagedaemon ● Syntax: pstree <username> tree (directory) deamon (e.g. httpd) getty getty ● -d (directories), -a (hidden), -s (size), -p (permissions) ● tree –H . login login bash ksh Maria Hybinette, UGA 19 Maria Hybinette, UGA 20 Daemon Processes PID and Parentage ● Print out status information of various processes in the system: ps -axj (BSD) , ps -efjc (SVR4) , switches / flags varies ● A process ID or PID is a positive integer that uniquely identifies a running process and is stored in a variable ● process status (ps) of type pid_t ● Daemons (d) run with root privileges, no controlling terminal, parent process is init ● Example: print the process PID and parents PID {atlas:maria} ps -efjc | sort -k 2 -n | more // solaris below UID PID PPID PGID SID CLS PRI STIME TTY TIME CMD {saffron} print-pid root 0 0 0 0 SYS 96 Mar 03 ? 0:01 sched #include <sys/types.h> My PID is 3891 root 1 0 0 0 TS 59 Mar 03 ? 1:13 /etc/init -r #include <unistd.h> MY PPID is 3794 root 2 0 0 0 SYS 98 Mar 03 ? 0:00 pageout root 3 0 0 0 SYS 60 Mar 03 ? 4786:00 fsflush #include <stdio.h> PID COMMAND %CPU TIME root 61 1 61 61 TS 59 Mar 03 ? 0:00 /usr/lib/sysevent/syseventd int main(void) root 64 1 64 64 TS 59 Mar 03 ? 0:08 devfsadmd 3891 print-ids 0.0% 0:00.00 root 73 1 73 73 TS 59 Mar 03 ? 30:29 /usr/lib/picl/picld { 3874 top 13.6% 0:19.71 3794 ksh 0.0% 0:00.04 root 256 1 256 256 TS 59 Mar 03 ? 2:56 /usr/sbin/rpcbind pid_t pid, ppid; root 259 1 259 259 TS 59 Mar 03 ? 2:05 /usr/sbin/keyserv root 284 1 284 284 TS 59 Mar 03 ? 0:38 /usr/sbin/inetd -s printf( My PID is %d\n, (pid = getpid()) ); daemon 300 1 300 300 TS 59 Mar 03 ? 0:02 /usr/lib/nfs/statd printf( My PPID is %d\n\n, (pid = getppid()) ); root 302 1 302 302 TS 59 Mar 03 ? 0:05 /usr/lib/nfs/lockd root 308 1 308 308 TS 59 Mar 03 ? 377:42 /usr/lib/autofs/automountd } rootMaria Hybinette, 319 UGA 1 319 319 TS 59 Mar 03 ? 6:33 /usr/sbin/syslogd 21 Maria Hybinette, UGA 22 ● Linux processes ● pstree ● ps -efjc | sort -k 2 -n | more ● pidstat {nike:maria:125} ps -efjc | sort -k 2 -n | more # linux Oct 2014 below UID PID PPID PGID SID CLS PRI STIME TTY TIME CMD ● top, htop root 1 0 1 1 TS 19 Oct01 ? 00:02:11 /sbin/init root 2 0 0 0 TS 19 Oct01 ? 00:00:04 [kthreadd] ● mpstat root 3 2 0 0 FF 139 Oct01 ? 01:23:55 [migration/0] root 4 2 0 0 TS 19 Oct01 ? 00:01:10 [ksoftirqd/0] ● jobs root 5 2

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    13 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us