Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

CS 140 Lecture Notes: Processes and ThreadsSlide 1 UNIX Fork/Exec Example int pid = fork(); if (pid == 0) { exec("foo"); } else { waitpid(pid, &status,
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 Processes and Pipes. 2 "He was below me. I saw his markings, manoeuvred myself behind him and shot him down. If I had known it was Saint-Exupery, I.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
CSE 451 Section 4 Project 2 Design Considerations.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
CS 3204 Operating Systems Lecture 5 Godmar Back.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Object Oriented Programming in C++ Dr. Hammadi Nait-Charif Media School Bournemouth University
Simple Shell Part 1 Due date (75%): April, 2002 Part 2 Due date (25%): Apr 5, 2002.
Exercise #2: Process Creation/Termination and Interprocess Communication J. H. Wang Mar. 30, 2010.
CS162B: Forking Jacob T. Chan. Fork  Forks are:  Implement with two or more prongs that is used for taking up or digging  Division into branches or.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
ITEC 320 C++ Examples.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
System calls for Process management
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Process Management Azzam Mourad COEN 346.
Genesis: From Raw Hardware to Processes Andy Wang Operating Systems COP 4610 / CGS 5765.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
Notes on Processes, Context, Context Switching The following slides contain partially quoted statements from various Wikipedia pages.
CSCI 330 UNIX and Network Programming
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Chapter 3 The Programming Interface Chien-Chung Shen CIS/UD
Command Line Arguments
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Project1: Unix Shell using Multi-Processing
Linux Fork/Exec Example
UNIX Fork/Exec Example
Processes in Unix, Linux, and Windows
Tutorial 3 Tutorial 3.
Unix System Calls and Posix Threads
UNIX Fork/Exec Example
Simple Shell Due date: March 27, 2002.
Tutorial: The Programming Interface
CS150 Introduction to Computer Science 1
Console A presentation by Inti Vincenzo Pizzoni.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Linux Fork/Exec Example
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements

Fall 2002 CS 325 Class Notes Page 2 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( …)

Fall 2002 CS 325 Class Notes Page 3 Exec function Exec comes in many flavors (see man exec) We will use execl –Specify location of new process (1 st arg) –Specify name of new process (2 nd arg) –Specify any arguments passed to new process –Specify NULL to end parameter list (3 rd arg here) #include int main( ) { cout << "hello world" << endl; int pid = fork(); cout<<"PID is "<<pid<<endl; if (pid == 0) execl("/usr/bin/ls","ls",NULL); else cout<<"original proc"<<endl; }

Fall 2002 CS 325 Class Notes Page 4 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() { … int pid = fork( ); if (pid == 0) exec( … ) int main() { // another program // starts at beginning }

Fall 2002 CS 325 Class Notes Page 5 Another example with “exec” Consider the “print” routine shown below –Prints a number N times #include int main(int argc, char *argv[ ]) { // argv[0] is the program name int num = atoi(argv[1]); int loops = atoi(argv[2]); for (int a=0; a<loops; a++) cout << num << " "; } Use “print” in this code #include int main() { cout << "hello world" << endl; int pid = fork(); cout << “PID is " << pid << endl; if (pid == 0) execl("./print", "print", "1", "100", NULL); else execl("./print", "print", "2", "100", NULL); }

Fall 2002 CS 325 Class Notes Page 6 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

Fall 2002 CS 325 Class Notes Page 7 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

Fall 2002 CS 325 Class Notes Page 8 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, num; cout "; cin >> cmd; while (cmd != 0) { int pid = fork(); if (pid == 0) { if (cmd==1) execl("/usr/bin/who", "who", NULL); if (cmd==2) execl("/usr/bin/ls", "ls", NULL); if (cmd==3) execl("/usr/bin/uptime", "uptime",NULL); exit(1); } cin >> cmd; } }

Fall 2002 CS 325 Class Notes Page 9 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

Fall 2002 CS 325 Class Notes Page 10 Programs & Processes We have seen how to write a program that –When executed, could invoke additional instances of itself (fork) –When executed, could then replace these additional instances with other programs – replace the code in the executing process (exec) Unix and Windows handle these basic tasks differently

Fall 2002 CS 325 Class Notes Page 11 Creating processes in Windows No exact Windows equivalent of fork and exec Windows has CreateProcess method –Creates a new process and loads the specified program into that process (one step) –Requires #include –More parameters than fork & exec Most can be NULL Just need to specify program to run and a few flags

Fall 2002 CS 325 Class Notes Page 12 Example using CreateProcess Need two variables to handle basic info – must initialize correctly Specify the location of the new process (our “print” process from last time) Wait for it to finish #include void main( ) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); if ( ! CreateProcess( NULL, “..\\print\\debug\\print.exe 5 10", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) ) cerr << "CreateProcess failed." << endl; WaitForSingleObject( pi.hProcess, INFINITE ); CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); }

Fall 2002 CS 325 Class Notes Page 13 CreateProcess syntax BOOL CreateProcess ( LPCTSTR lpApplicationName, // pointer to executable module LPTSTR lpCommandLine, // pointer to command line string LPSECURITY_ATTRIBUTES lpProcessAttrib, // process security LPSECURITY_ATTRIBUTES lpThreadAttrib, // thread security BOOL bInheritHandles, // handle inheritance flag DWORD dwCreationFlags, // creation flags LPVOID lpEnvironment, // pointer to new environment block LPCTSTR lpCurrentDirectory, // pointer to current dir name LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION );

Fall 2002 CS 325 Class Notes Page 14 Comments on CreateProcess Can specify program in either 1 st or 2 nd args –If in first, give location of program to run –If in second, give the command line to execute Creation flags –If 0, runs in existing window –Also have other flags (combine with | ) CREATE_NEW_CONSOLE probably most useful Specify priority, linkage to parent, etc. Structures pi and si used for process communication (how to start, basic info)

Fall 2002 CS 325 Class Notes Page 15 Class Exercises Write a program (in the Windows VC++ environment) that starts up a copy of notepad as part of its execution –Notepad is located in c:\winnt\ directory –Don’t forget to double the “\” in string literal