Systems Programming/ C and UNIX

Similar documents
Unix Processes. What is a Process?

High Performance Computing Lecture 11. Matthew Jacob Indian Institute of Science

Linux System Administration

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Operating System Structure

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Systems Programming/ C and UNIX

Simplest version of DayOfYear

Operating Systems. Lecture 05

Processes. CS3026 Operating Systems Lecture 05

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

Operating Systems. II. Processes

3. Process Management in xv6

CPSC 457 OPERATING SYSTEMS MIDTERM EXAM SOLUTION

Review of Fundamentals

Implementation of a simple shell, xssh

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Operating Systems. Copyleft 2005, Binnur Kurt

Getting Started. Logging In and Out. Adapted from Practical Unix and Programming Hunter College

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Announcements Processes: Part II. Operating Systems. Autumn CS4023

Creating a Shell or Command Interperter Program CSCI411 Lab

5/20/2007. Touring Essential Programs

COMP 3430 Robert Guderian

Shells and Processes. Bryce Boe 2012/08/08 CS32, Summer 2012 B

Unix Tutorial Haverford Astronomy 2014/2015

Fall 2015 COMP Operating Systems. Lab #3

Linux Command Line Interface. December 27, 2017

CS 300 Data Structures

Unix Basics. Systems Programming Concepts

Mon Sep 17, 2007 Lecture 3: Process Management

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Processes and Threads

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

1/13/2019 Operating Systems. file:///volumes/users/rasit/desktop/comp3430/coursematerial/slides/03_processes/index.html?

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

Lab 1: Accessing the Linux Operating System Spring 2009

CS61 Scribe Notes Lecture 18 11/6/14 Fork, Advanced Virtual Memory

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Introduction: What is Unix?

Process Management! Goals of this Lecture!

518 Lecture Notes Week 3

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Removing files and directories, finding files and directories, controlling programs

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

CPSC 457 OPERATING SYSTEMS MIDTERM EXAM

Introduction to Linux

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

Most of the work is done in the context of the process rather than handled separately by the kernel

Processes. Dr. Yingwu Zhu

Chap 4, 5: Process. Dongkun Shin, SKKU

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

CSC 1600 Unix Processes. Goals of This Lecture

The Unix Shell & Shell Scripts

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Process Management 1

Essential Unix and Linux! Perl for Bioinformatics, ! F. Pineda

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

CS Fundamentals of Programming II Fall Very Basic UNIX

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

CS370 Operating Systems

Processes. slide 1 kv, gm. Silberschatz chapter 3

IBM AIX Operating System Courses

Process concepts. Processes. Process in Memory. Process Creation. definition of a process (sometimes called a Task) aprogram in execution

Operating System Interaction via bash

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Study Guide Processes & Job Control

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CS 326: Operating Systems. Process Execution. Lecture 5

SOFTWARE ARCHITECTURE 3. SHELL

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Binghamton University. CS-220 Spring Sharing Resources. Computer Systems Chapter 8.2, 8.4

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Operating systems and concurrency - B03

Week 5 Lesson 5 02/28/18

Lecture Topics. Announcements. Today: Operating System Overview (Stallings, chapter , ) Next: Processes (Stallings, chapter

Introduction to Process in Computing Systems SEEM

UNIX Kernel. UNIX History

Process Management forks, bombs, zombies, and daemons! Lecture 5, Hands-On Unix System Administration DeCal

COSC243 Part 2: Operating Systems

Perl and R Scripting for Biologists

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

CSE506: Operating Systems CSE 506: Operating Systems

Design Overview of the FreeBSD Kernel CIS 657

Operating System Labs. Yuanbin Wu

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

Processes. CS439: Principles of Computer Systems January 24, 2018

Midterm Exam CPS 210: Operating Systems Spring 2013

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Transcription:

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