CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.

Slides:



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

15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
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.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
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.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
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.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
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.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Operating Systems Chapter 2
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Creating and Executing Processes
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
CE Operating Systems Lecture 10 Processes and process management in Linux.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
Operating Systems Processes 1.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
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,
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (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.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
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.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
CSCI 4061 Recitation 2 1.
Section 8: Processes What is a process Creating processes Fork-Exec
Unix Process Management
Processes in Unix, Linux, and Windows
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder

Today's Topic SMP0 problems Topics needed for SMP1 Process Fork Wait Exec

About SMP0 Part 2, Task 5 overlay_overlay(char *s1, char *s2, char *s3, char *s4)‏ Part 3 triple_three_step_step_step(char *first, char *second, char *third)‏ the_end(void *orange, void *blue)‏

About SMP0 (contd.)‏ overlay_overlay(char *s1, char *s2, char *s3, char *s4)‏ Find the overlay between s1 and s2 = o1 Find the overlay between s3 and s4 = o2 Find the overlay between o1 and o2 = result How to find the overlay between “father” and “mother”? Possible approach: generate all possible combination of “mother” strstr() with “father” to find the largest match

About SMP0 (contd.)‏ void triple_three_step_step_step(char *first, char *second, char *third) { if (third == second+2 && second == first+2 && *third == *second+4 && *second == *first+4 && third[3] == second[2]+8 && second[2] == first[1]+8) printf("10: Illinois\n"); } firstsecondthird

About SMP0 (contd.)‏ void triple_three_step_step_step(char *first, char *second, char *third) { if (third == second+2 && second == first+2 && *third == *second+4 && *second == *first+4 && third[3] == second[2]+8 && second[2] == first[1]+8) printf("10: Illinois\n"); } 04 firstsecondthird 8

About SMP0 (contd.)‏ void triple_three_step_step_step(char *first, char *second, char *third) { if (third == second+2 && second == first+2 && *third == *second+4 && *second == *first+4 && third[3] == second[2]+8 && second[2] == first[1]+8) printf("10: Illinois\n"); } 04 firstsecondthird 8 first[1] second[2] third[3]

About SMP0 (contd.)‏ void triple_three_step_step_step(char *first, char *second, char *third) { if (third == second+2 && second == first+2 && *third == *second+4 && *second == *first+4 && third[3] == second[2]+8 && second[2] == first[1]+8) printf("10: Illinois\n"); } 004 firstsecondthird 8 first[1] second[2] third[3] 16

About SMP0 (contd.)‏ void the_end(void *orange, void *blue){ if (orange != NULL && orange == blue && ((char *)blue)[0] == 1 && *((int *)orange) % 3 == 0) printf("12: Illinois\n"); } 1 blue[0] orange blue[1] blue[2] blue[3]

About SMP0 (contd.)‏ void the_end(void *orange, void *blue){ if (orange != NULL && orange == blue && ((char *)blue)[0] == 1 && *((int *)orange) % 3 == 0) printf("12: Illinois\n"); } 1001 blue[0] orange blue[1] blue[2] blue[3] *orange = ? ( )‏ =257

SMP1 Simple Unix shell Non built-in commands Built-in commands Change Directory Termination History Error handling Concepts needed: process, fork, exec, wait

Processes A process is an instance of a running program A process contains: Instructions (i.e. the program)‏ Resources (variables, buffers, links, etc.)‏ State (identity, ready/running/locked, etc.)‏ Processes can create other processes

fork() creates a new process: Process Creation with fork()‏ env stack free heap static code  The new (child) process is identical to the old (parent) process, except… env stack free heap static code fork()‏ creates Parent Child

Differences between parent and child Process ID ( getpid() )‏ Parent ID ( getppid() )‏ Return value of fork()‏ In parent, fork() returns child pid In child, fork() returns 0

fork() Example 1 What does this do? fprintf(stdout,”%d\n”,fork()); Try it!

fork() example 2 #include int main(int argc, char** argv) { pid_t child_pid = fork(); if (child_pid < 0) {// error code perror(“Fork Failed”); return –1; } fprintf(stdout, “I am process %d\n”,getpid()); if (child_pid == 0) {// child code fprintf(stdout,”I’m the child process.\n”); } else {// parent code fprintf(stdout,”I’m the parent of child process %d.\n”, child_pid); } return 0; }

Example 2 cont’d This exits too quickly; let’s slow it down: if (child_pid == 0) {// child code sleep(15); fprintf(stdout,”I’m the child process.\n”); } else {// parent code sleep(20); fprintf(stdout,”I’m the parent of child process %d.\n”, child_pid); }

Example 2 cont’d In a second window, run ps –a, and look for the pids from the program output. Periodically run ps –a again, as the program in the first window executes. What happens when the program runs? What happens when the child finishes? What happens when the parent finishes? What happens when you switch the parent and child sleep statements?

Orphans and Zombies When a process finishes, it becomes a zombie until its parent cleans up after it. If its parent finishes first, the process becomes an orphan, and the init process (id 1) adopts it.

How can a parent know when its children are done?

Solution: wait(…) wait() allows a parent to wait for its child process, and save its return value pid= wait(&status, options); pid= waitpid(pid, &status,options); wait() waits for any child; waitpid() waits for a specific child.

wait cont’d wait() blocks until child finishes wait() does not block if the option WNOHANG is included. When would we want to use this? The child’s return value is stored in *status

wait(...) macros WIFEXITED(status) is true iff the process terminated normally. WEXITSTATUS(status) gives the last 8 bits of the process’s return value (assuming normal exit)‏

Example 3: wait #include … // add this to parent code if (waitpid(child_pid, &result, 0) == -1) { perror(”Wait failed”); return -1; } if( WIFEXITED(result)) { fprintf(stdout, ”child %d returned %d\n”, child_pid, WEXITSTATUS(result));

exec exec replaces the current process image(code, variables, etc.) with that of a new program: env stack free heap static code env* New Program exec * The program may choose to change the environment BeforeAfter

exec variations There are 6 different ways of calling exec. Which one to use depends on three conditions: 1.How arguments are passed 2.How the path is specified 3.Whether a new environment is used

exec variations: passing parameters exec can have parameters passed to it two different ways: List of parameters: execl(“/usr/bin/ls”, ”ls”, ”-l”, NULL); Argument Vector (like argv ): execv(argv[1],argv+1); Q: When would you use execl ? When would you use execv ?

exec variations: command path Adding a “ p ” to an exec call tells the system to look for the command in the environment’s path. Compare:  execl(“/usr/bin/ls”, ”ls”, ”-l”, NULL);  execlp(“ls”, ”ls”, ”-l”, NULL); The difference is similar for execv and execvp.

exec variations (cont’d)‏ By default, the new program inherits the old program’s environment. Adding an “ e ” to the exec call allows the new program to run with a new environment **environ execve and execle allow the user to specify the environment. The others inherit the old environment.

fork + exec If exec succeeds, it does not return, as it overwrites the program. No checking return values, executing multiple commands, monitoring results, etc. Solution: fork a new process, and have the child run exec

Example 4: exec int main() { char command[10]; while(fscanf(stdin, “%s”, command)) { pid_t child_pid = fork(); if (child_pid < 0) { perror("Fork failed"); return -1; } else if (child_pid == 0) { // child code // execute command // check errors } else { // parent code // What belongs here? } return 0; }