Systems Programming/ C and UNIX Alice E. Fischer Lecture 6: Processes October 9, 2017 Alice E. FischerLecture 6: Processes Lecture 5: Processes... 1/26 October 9, 2017 1 / 26
Outline 1 Processes 2 Process Creation 3 Shell Commands Create Processes 4 A Process Can Create Another Process Alice E. FischerLecture 6: Processes Lecture 5: Processes... 2/26 October 9, 2017 2 / 26
Processes Processes What is a Process? Parts of a Unix Process Seeing the Processes Alice E. FischerLecture 6: Processes Lecture 5: Processes... 3/26 October 9, 2017 3 / 26
Processes What is a Process? The representation, at run time, of the activity of one user doing one task or one concurrent part of that task. The evolution of the job state through time. Something that is handled by the job scheduling part of the OS. A data structure that contains all the necessary state information for the job to run or to resume running. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 4/26 October 9, 2017 4 / 26
Processes Parts of a UNIX Process A process ID (pid): unique among all running processes A parent-process ID (ppid) Security attributes: owner ID, group ID A block of executable code and static literals and variables A stack and a heap (for dynamic allocation) A file table, umask, and working directory The process status (running, blocked, ready, terminated, etc.) Registers (when running) and register-images, (when not running) Status of signals and interrupts An environment (a null-terminated list of pairs of strings) An argument list (a null-terminated list of strings) Optionally, semaphores and locks for inter-process communication. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 5/26 October 9, 2017 5 / 26
Processes The Environment Each process has an environment, which is a list of variables with assigned values. Bash syntax from a command shell: PATH="$PATH:." Tcsh syntax from a command shell: set WINDOW_NAME=Lila The variables have upper-case names. Examples: PATH USER LOGNAME HOSTTYPE OSTYPE SHELL To see your environment type: printenv Initially, your environment comes from the /etc/profile file, modified by the login script in your bashrc or cshrc file. Things may be in different places on a Linux system. Read the man page on your shell to see where this file lives. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 6/26 October 9, 2017 6 / 26
Processes Viewing the Processes [alice] jodi:~~> ps PID TTY TIME CMD 59843 ttys000 0:00.03 -tcsh 59955 ttys001 0:00.03 -tcsh ps shows you a list of your own processes, attached to your own terminal. You can add several options: The a option shows you everyone s processes. The x option also shows you the daemons also. The w gives a wide listing: it does not truncate the display at the edge of the window. The u gives more usage information Alice E. FischerLecture 6: Processes Lecture 5: Processes... 7/26 October 9, 2017 7 / 26
Processes Getting More Process Information [alice] jodi:~~> ps PID TTY TIME CMD 59843 ttys000 0:00.03 -tcsh 59955 ttys001 0:00.03 -tcsh [alice] jodi:~~> ps a PID TT STAT TIME COMMAND 59842 s000 Ss 0:00.01 login -pf alice 59843 s000 S 0:00.03 -tcsh 60388 s000 R+ 0:00.00 ps a 59954 s001 Ss 0:00.01 login -pf alice 59955 s001 S+ 0:00.03 -tcsh Note: these commands give the same information in different formats: [alice] jodi:~~> ps a [alice] jodi:~~> ps -a Alice E. FischerLecture 6: Processes Lecture 5: Processes... 8/26 October 9, 2017 8 / 26
Processes Getting More Process Information On my system, ps u gives me 11 columns of information: [alice] jodi:~~> ps u USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND ps ux gives me the same information about more processes And ps uxw gives me the entire command that caused the process to be created, not just the beginning of that command. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 9/26 October 9, 2017 9 / 26
Process Creation Process Creation Initial Process Creation User Process Creation Process Creation Demo Alice E. FischerLecture 6: Processes Lecture 5: Processes... 10/26 October 9, 2017 10 / 26
Process Creation Initial Process Creation The first process, systemd, is created when you boot the computer. Subsequent processes are created by executing a fork instruction in an existing process. During boot-up, systemd creates many processes, called daemons, that run in the background and are not attached to a command shell. Examples of daemons: mail, ssh, your web server, your desktop,... Your system preferences determine which daemons are started. systemd also creates one user process connected to your shell window (ttys000). It s owner ID, privileges, and environment are taken from the settings in your profile and your.bashrc file. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 11/26 October 9, 2017 11 / 26
Process Creation User Process Creation After boot-up, I have one process owned by me and attached to my tty. I can create more processes by: Clicking on an application icon. This creates a GUI window and a daemon process. Logging in remotely. This creates a login shell and a shell process. Creating a new tab in my tcsh window. This calls the login command. Double-clicking on an executable file in my finder listing. This creates a console window with a process running in it. Entering a command into a shell. This forks off another process in the same shell. Executing a fork instruction in my own code. This creates another process connected to the same shell. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 12/26 October 9, 2017 12 / 26
Process Creation Process Creation Demo I open a console window by clicking on my console icon and look at my processes: Last login: Mon Apr 30 12:59:42 on ttys001 Terminal? (next33, xterm, vt100, other) [xterm] [alice] jodi:~~> ps ux USER PID TT STAT STARTED TIME COMMAND root 59842 s000 Ss 1:07PM 0:00.01 login -pf a alice 59843 s000 S 1:07PM 0:00.02 -tcsh... many daemon processes are omitted here. root 59862 s000 R+ 1:07PM 0:00.00 ps ux alice 112?? Ss 4Apr12 1:15.14 /sbin/launc The ps process will die when it is done executing. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 13/26 October 9, 2017 13 / 26
Process Creation Opening a Shell Creates two Processes Now use the command-t keystroke to open a new tab on my console window. I have two new processes: one owned by root, the other by me. USER PID TT STAT STARTED TIME COMMAND root 59842 s000 Ss 1:07PM 0:00.01 login -pf alic alice 59843 s000 S 1:07PM 0:00.02 -tcsh root 59954 s001 Ss 1:14PM 0:00.01 login -pf alice alice 59955 s001 S+ 1:14PM 0:00.01 -tcsh... many daemon processes are omitted here. root 59961 s000 R+ 1:14PM 0:00.00 ps ux alice 112?? Ss 4Apr12 1:15.15 /sbin/launchd Alice E. FischerLecture 6: Processes Lecture 5: Processes... 14/26 October 9, 2017 14 / 26
Shell Commands Create Processes Shell Commands Create Processes Built-in Commands Login and Remote Login Secure Remote Login Alice E. FischerLecture 6: Processes Lecture 5: Processes... 15/26 October 9, 2017 15 / 26
Shell Commands Create Processes Built-In Commands and Others Some commands are built into the shell when it is programmed. This increases efficiency. Example on a former OS-X version: [alice] jodi: > which cd cd: shell built-in command. Other commands are scripted in separate files: Example: [alice] jodi: > which man /usr/bin/man If a command is not built in, executing it creates a process or several. USER PID TT STAT STARTED TIME COMMAND alice 60190 s000 S+ 1:32PM 0:00.01 man ps alice 60193 s000 S+ 1:32PM 0:00.00 sh -c (cd /usr/share alice 60194 s000 S+ 1:32PM 0:00.00 sh -c (cd /usr/share alice 60198 s000 S+ 1:32PM 0:00.00 sh -c (cd /usr/share alice 60200 s000 S+ 1:32PM 0:00.01 /usr/bin/less -is Alice E. FischerLecture 6: Processes Lecture 5: Processes... 16/26 October 9, 2017 16 / 26
Shell Commands Create Processes Sidenote: searching a man page. On Linux, the man command starts up the less command to display the information. USER PID TT STAT STARTED TIME COMMAND alice 60190 s000 S+ 1:32PM 0:00.01 man ps alice 60200 s000 S+ 1:32PM 0:00.01 /usr/bin/less -is At the end of each page, the display stops and you see a : (colon). Press the space bar to see the next page. less lets you search the man page, including parts that are not currently on the screen, for a keyword. At the colon, type a a slash followed by the word you want to find (no space after /). Example: To search for the word stat, type /stat My system is not case sensitive. My husband s Linux system is. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 17/26 October 9, 2017 17 / 26
Shell Commands Create Processes Logging in login: Supplying a valid user name and password gives access to your files and to the use of the system. rlogin: Remote login: antiquated and not to be used. slogin: Secure remote login: passwords are encrypted before transmission. Recently compromised. ssh: New version of secure remote login: passwords are encrypted before transmission. Example: ssh alice@eliza.newhaven.edu The response will be to ask for your password on the remote machine. If you have the same username on both ends, you can use a shorter login command: ssh eliza.newhaven.edu Alice E. FischerLecture 6: Processes Lecture 5: Processes... 18/26 October 9, 2017 18 / 26
Shell Commands Create Processes Servers and Clients All Unix systems are servers. Each can participate in a connection as either the client or the server. Each has (or can have) an ssh daemon that is always running and waiting for contact on a standard welcome port. That port is defined in the file /etc/services and is usually port 22. It is possible to change the port to some other port. We have done this on our home gateway server to avoid harassment by hackers. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 19/26 October 9, 2017 19 / 26
Shell Commands Create Processes Secure Remote Login - 1 Suppose I am sitting at Lila and want to log into Eliza. On Lila: ssh eliza.newhaven.edu contacts the welcome port on Eliza to request a connection. Suppose you changed the welcome port to 4422. The login command would be: ssh -p 4422 eliza.newhaven.edu Eliza s daemon creates a process to handle the new connection (owner _sshd on a Mac, root on Linux). This process frees the daemon to watch for more connections on the welcome port. This process on Eliza talks to Lila s shell process using strong encryption to negotiate a symmetric session key, which is used to encrypt all further communication. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 20/26 October 9, 2017 20 / 26
Shell Commands Create Processes Secure Remote Login - 2 Now the root process can complete the login process safely: Eliza requests your password or passphrase, which will be encrypted by Lila, then sent. Eliza decrypts the password and validates it. If valid, Eliza will open a shell process for you (owned by you). The stdin and stdout for this shell process are attached to pipes that deliver all communication through the encrypted channel to Lila. You sit at Lila and use the remote shell process. All activity is visible on Lila (but not on Eliza). Shell commands control the remote machine. When you log out of Eliza, your remote shell and your ssh shell die, and control reverts to your local shell. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 21/26 October 9, 2017 21 / 26
Shell Commands Create Processes The Newborn Process A new process gets its own: Process ID and PPID. A stack, that is a copy of the parent s stack, but with a different return value. for the fork function that created the new process. A new process inherits copies of these things from its parent: User and group IDs and a set of permissions. Command-line arguments, an environment, variables. The file table and the list of open files. A program counter that points to the next instruction to be run. This is the same for parent and child. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 22/26 October 9, 2017 22 / 26
A Process Can Create Another Process A Process Can Create Another Process Booting creates the first process. All other processes are created by forking. fork results in a parent process and a child. exec tells a process what code to execute. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 23/26 October 9, 2017 23 / 26
A Process Can Create Another Process A New User Process Processes are created by pid_t fork( void ); (from unistd.h) The new process will be the daughter of the original process. Its ppid will be the pid of the parent process or thread. The daughter s owner, code, and data will be almost identical copies of the parent s. The parent process or thread will receive the daughter process s pid as the return value from fork(). (Or -1 if there was a system error such as too many processes.) The daughter process will receive a return value of 0. The differing return values must be used by parent and child to figure out which is which. Usually, the parent forks more processes and waits for all to complete. Usually, a daughter will immediately call command. execve(), or another exec Alice E. FischerLecture 6: Processes Lecture 5: Processes... 24/26 October 9, 2017 24 / 26
A Process Can Create Another Process Demo: sayit The sayit demo executes the very simplest kind of fork. Everything is in the main function. There is one variable, k, in main. Each process ends up with an independent copy of k: the child process is given new virtual memory as soon as it tries to store into k. On line 10, the process forks. The parent process or thread will receive the daughter process s pid as the return value from fork(). The daughter process will receive a return value of 0. The differing return values must be used by parent (line 13) and child (line 27) to figure out which is which. Both of them display messages to the screen. This lets us observe the way processes are scheduled on the host system. Executing ps au or top -o cpu in a second shell lets us monitor resource consumption. wait() suspends its calling process until its child terminates. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 25/26 October 9, 2017 25 / 26
A Process Can Create Another Process Do This Mini-lab Yourself Create a demo directory. Put the 2 tools files and a copy of sayit.c in it. Compile sayit.c, with the tools, to produce an executable sayit1. Now edit sayit.c to remove k<200 from both loops. Compile sayit.c again to produce a sayit2 that will run forever. Open two console windows (or one with two tabs.) In the first, type the command ps au and be ready to hit enter. In the second console, type./sayit1 and hit enter. Then hit enter in console-1. Do this several times. Observe the number of processes running and how the output from the two sayit1 processes is interleaved on the screen. Repeat with sayit2. This time, execute the ps command repeatedly, noting how the execution time increases. Close console-2 to stop these runaway processes! Otherwise you may need to reboot. Alice E. FischerLecture 6: Processes Lecture 5: Processes... 26/26 October 9, 2017 26 / 26