Today - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Today

Description:

Source: http://foldoc.doc.ic.ac.uk/foldoc/ Program: The instructions ... Task manager (CTRL-ALT-DEL) displays processes. Fall 2002 CS 325 Class Notes. Page 4 ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 21
Provided by: janett
Category:
Tags: alt | ctrl | del | today

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 24
  • Today
  • Processes in Unix
  • Announcements
  • Project 5 is due Nov 21 at 900 pm

2
Definitions (dont memorize)
  • Source http//foldoc.doc.ic.ac.uk/foldoc/
  • Program The instructions executed by a computer.
    "Code" is closely related but not exactly the
    same. The noun "program" describes a single,
    complete and more-or-less self-contained list of
    instructions, often stored in a single file,
    whereas "code" and "software" are uncountable
    nouns describing some number of instructions
    which may constitute one or more programs or part
    thereof. Most programs, rely heavily on various
    kinds of operating system software for their
    execution.
  • Process An executing program. A process consists
    of the program code (which may be shared with
    other processes which are executing the same
    program), and some private data. It may have
    other associated resources such as a process
    identifier, open files, CPU time limits, shared
    memory, child processes, and signal handlers. A
    multitasking operating system can run multiple
    processes concurrently or in parallel.

3
Programs Processes
  • Up until now, everything we have done is
  • Write a program
  • Run the program as a single process
  • Both Unix and Windows allow multiple processes to
    be running at the same time
  • Unix multiple users, each has their own
    processes
  • ps ef command lists all processes on the
    system
  • Windows single user, multiple applications
    executing at the same time (browser, VC, etc.)
  • Task manager (CTRL-ALT-DEL) displays processes

4
The ps command
  • Summarize all processes running
  • ps
  • Individually list all processes running
  • ps -ef
  • List all processes of a particular user
  • ps ef grep userid (ps ef grep yessi001)
  • Output
  • USER Proc Parent C STime TTY CPU Name
  • Yessi001 3748 3742 0 165850 pts/17 000 ksh
  • along with all other processes Im running

5
Class Exercises
  • Log onto bama.ua.edu (Unix system)
  • List all the processes currently running
  • ps ef command
  • How many processes are there?
  • Consider piping output to wc routine
  • Are there cases where more than one person is
    running the same program (multiple processes with
    the same name)

6
Programs Processes (cont)
  • Programs to date
  • Write one program
  • It generates a single process
  • Programs can also be written that
  • When executed, create multiple processes
  • Communicate with other processes in the system
  • Future classes (OS, networks, etc.) assume some
    basic understanding of programs and processes

7
Unix Processes
  • In Unix, a standard C program generates a
    single (executable) process
  • Consider code at right
  • Prints hello
  • Invokes fork function
  • Prints goodbye (twice)
  • Fork creates a second (identical) instance of
    this process
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include ltunistd.hgt
  • int main()
  • cout ltlt "hello" ltlt endl
  • fork( )
  • cout ltlt "world" ltlt endl
  • Output
  • hello
  • world
  • world

8
What happens with fork ?
  • Before fork( )
  • One process
  • Arrow indicates current point of execution
  • After fork( )
  • Two separate processes
  • Both at the same point in their execution (both
    just completed fork( ))

int main() cout ltlt "hello" ltlt endl fork(
) cout ltlt "world" ltlt endl
int main() cout ltlt "hello" ltlt endl fork(
) cout ltlt "world" ltlt endl
int main() cout ltlt "hello" ltlt endl fork(
) cout ltlt "world" ltlt endl
9
Class Exercises
  • What is the output?
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include ltunistd.hgt
  • int main()
  • cout ltlt "hello" ltlt endl
  • fork()
  • cout ltlt "world" ltlt endl
  • fork()
  • cout ltlt "goodbye" ltlt endl
  • What is the output?
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include ltunistd.hgt
  • int main() int a 0cout ltlt "hello" ltlt
    endla a 5fork()cout ltlt a ltlt endla a
    5fork()fork()cout ltlt a ltlt endl

10
Are these processes identical?
  • After a fork
  • Both at same point in execution
  • Both have same values for all variables
  • Both have the same file descriptors
  • One difference
  • One is the parent of the other (child) process
  • int pidvar fork()
  • Parent has process id of child, child has 0 in
    pidvar
  • You can tell this via ps command

11
Class Exercises
  • Run the program at the right, it will fork off a
    new process and then wait for input
  • While it is waiting for input, log on to bama in
    a new window and typeps ef grep userid
  • Find these processes
  • Figure out which is the parent and which is the
    child
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include ltunistd.hgt
  • int main()
  • int a
  • cout ltlt "hello" ltlt endl
  • int pidvar fork()
  • cin gtgt a
  • cout ltlt "world" ltlt endl

12
Class Exercises
  • include
  • int main( )
  • int a 10
  • int anything fork()
  • cout ltlt a ltlt endl
  • coutltlt"PID is "ltltanythingltltendl //assume
    child PID is 18754
  • if (anything 0)
  • cout ltlt this is a child ltlt endl
  • cout ltlt a ltlt endl
  • int anything2fork()
  • if(anything20) cout ltlt this is a child of
    a child ltlt endl
  • else cout ltlt this is a child and a parent
    ltlt endl
  • cout ltlt a ltlt endl
  • else
  • coutltltthis is the parent and a
    grandparent"ltltendl
  • a 5
  • cout ltlt a ltlt endl

13
Creating new processes
  • How do you replace a running process with a new
    process (new set of instructions)
  • Use the exec function
  • First, create a second version of your process
  • via fork( )
  • Next, replace the contents of this new process
    with another program
  • via exec( )

14
Exec function
  • Exec comes in many flavors (see man exec)
  • We will use execl
  • Specify location of new process (1st arg)
  • Specify name of new process (2nd arg)
  • Specify any arguments passed to new process
  • Specify NULL to end parameter list (3rd arg)
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include ltunistd.hgt
  • int main( )
  • cout ltlt "hello world" ltlt endl
  • int pid fork()
  • coutltlt"PID is "ltltpidltltendl
  • if (pid 0)
  • execl("/usr/bin/ls","ls",NULL)
  • else
  • coutltlt"original proc"ltltendl

15
What happens with exec ?
  • Before fork( )
  • After fork( )
  • After exec( )
  • One process still contains the original code we
    started with
  • Other process now has completely different code

int main() int pid fork( ) if (pid
0) exec( )
int main() int pid fork( ) if (pid
0) exec( )
int main() int pid fork( ) if (pid
0) exec( )
int main() // another program // starts
at beginning
int main() int pid fork( ) if (pid
0) exec( )
16
Another example with exec
  • Consider the print routine shown below
  • Prints a number N times
  • include ltiostreamgt
  • include ltstdlib.hgt
  • int main(int argc, char argv )
  • // argv0 is the program name
  • int num atoi(argv1)
  • int loops atoi(argv2)
  • for (int a0 altloops a)
  • cout ltlt num ltlt " "
  • Use print in this code
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include ltunistd.hgt
  • int main() cout ltlt "hello world" ltlt endlint
    pid fork()cout ltlt PID is " ltlt pid ltlt
    endlif (pid 0)
  • execl("./print", "print", "1", "100",
    NULL)else
  • execl("./print", "print", "2", "100",
    NULL)

17
Class Exercises
  • Run the example shown on the previous page
  • Compile print.C (from previous slide) into an
    executable print
  • Compile the main program and run it
  • Try it printing each number
  • 100 times
  • 1000 times
  • 10,000 times
  • 100,000 times

18
Creating new processes
  • Lots of applications where the ability to
    generate a new process comes in handy
  • Simple example command shell in Unix
  • The shell is a C program
  • Basic algorithm
  • Get a command from user
  • Interpret the command
  • Invoke a process to execute this command

19
Implementing a simple shell
  • Overview
  • Prompt user
  • Get command
  • If not time-to-exit
  • Fork new process
  • Replace new process with either who, ls or uptime
  • Read next command
  • int main() int cmd, numcout ltlt "Enter 1who,
    2ls, 3uptime -gt "cin gtgt cmdwhile (cmd ! 0)
    int pid fork() if (pid 0) if
    (cmd1) execl("/usr/bin/who", "who",
    NULL) if (cmd2) execl("/usr/bin/ls", "l
    s", NULL) if (cmd3) execl("/usr/bin/uptime
    ", "uptime",NULL) exit(1) cin gtgt
    cmd

20
Class Exercises
  • Implement the program shown on the previous
    slide. Run it, and watch its execution.
  • What happens when you give an invalid number?
  • Modify the program (add two more commands to the
    shell we are writing), consider
  • date
  • hostname
Write a Comment
User Comments (0)
About PowerShow.com