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.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
The Process Model.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
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.
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.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
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.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Creating and Executing Processes
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Computer Studies (AL) Operating System Process Management - Process.
System calls for Process management
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.
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,
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)
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
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.
CSCI 4061 Recitation 2 1.
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Protection of System Resources
Using Processes.
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.
System Structure and Process Model
System Structure and Process Model
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Fork and Exec Unix Model
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
System Structure and Process Model
Tutorial 3 Tutorial 3.
Process Programming Interface
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
EECE.4810/EECE.5730 Operating Systems
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

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 processes r Example: Let us say there are two users on gaul m Both run grep on a file m This results in two processes

Process r A process is more than the program code (sometimes known as text section) r Process information includes: m Process stack: Includes temporary data (function parameters, return addresses and local variables) m Data Section: Includes global variables m Heap: Memory that is dynamically allocated during process run-time

Process in Memory

Process State r As a process executes it changes state. r Possible states: m New: The process is created m Running: Instructions are being executed m Waiting: The process is waiting for some event to occur e.g., I/O completion m Terminated: Process has finished execution

Process Control Block r Each process is represented in the operating system by a process control block m Information related to program execution m Context information including: Process Identifier (PID) Process state Program counter and other program related information CPU registers CPU-Scheduling information Memory-Management information I/O status information

Operating System Services r An operating system provides an environment for the execution of programs r Services are provided to programs and users of programs

Operating System Services r OS services (User perspective) m User interface: Command-Line (CLI), Graphics User Interface (GUI) m Program execution: OS needs to load a program into memory, runs that program, end execution, either normally or abnormally (indicating error) m I/O operations: A running program may require I/O, which may involve a file or an I/O device m File-system manipulation: Need to read and write files and directories, create and delete them, search

Operating System Services r OS provides operations for ensuring efficient operation. Examples: m Resource allocation m Accounting m Protection and Security

The Shell r The command interpreter is a program that accepts commands from the user and interprets the commands (type of user interface) r The shell refers to an executing command interpreter m Accepts commands such as “ls” or “ps” m The commands make use of system calls. r A system call allows a command to request a service from the operating system. r A shell is a process m A process e.g., a program being executed We will overuse the term “shell” to refer to the program as well as the process

The Shell r There are different shells that you can use in a Unix-based system including: m bourne shell m C shell m bash shell m tcsh shell m and many more

The Shell r When a user logs in, a shell is started up. r The shell has the terminal as standard input and standard output r The shell starts out by typing the prompt, a character such as a dollar sign or percentage sign e.g., hanan% r User enters a command e.g., date

The Shell r The user can specify that standard output be redirected to a file e.g., date > file r Standard input can be redirected e.g., sort file2 r The output of one program can be used as the input to another program: cat file1 file2 file3 | sort >/dev/lp &

High-Level View of Shell Code while (1) { get a line from the user Execute command found in line } Details Not Highlighted m Making sure that the line from the user is correct m How is shell termination handled? r The execution of a command is done by a separate process (child process) from the shell process r For simple commands, the shell process waits for the child process to terminate so that it can print the prompt r If a child process is put in the background (using &) then the shell process can continue without waiting for the child process to terminate

The Concept of Fork r The Unix system call for process creation is called fork(). r The fork system call creates a child process that is a duplicate of the parent. m Child inherits state from parent process Same program instructions, variables have the same values, same position in the code m Parent and child have separate copies of that state m Child has the same open file descriptors from the parent. Parent and child file descriptors point to a common entry in the system open file descriptor table. More on this later

fork() as a diagram Parent pid = fork() Returns a new PID: e.g. pid == 56 Data Shared Program Data Copied Child pid == 0

Process Creation Using Fork int main () { pid_t pid; int status = 0; pid = fork(); if (pid < 0) perror(“fork()”); if (pid > 0) { /* parent */ printf(“I am parent\n”); wait(0); } else { /* child */ printf(“I am child\n”); } return 0; } pid is zero which indicates a child process The fork system call returns twice: it returns a zero to the child and the child process ID (pid) to the parent. The perror function produces a message on the standard error output describing the last error encountered during a call to a system or library function (man page) The wait function is used to terminate the parent process when the child terminates

Fork System Call r If fork () succeeds it returns the child PID to the parent and returns 0 to the child r If fork() fails, it returns -1 to the parent (no child is created) and sets errno  A program almost always uses this difference to do different things in the parent and child processes. r Failure occurs when the limit of processes that can be created is reached. r pid_t data type represents process identifiers r Other calls: m pid_t getpid() – returns the PID of calling process m Pid_t getppid() – returns the PID of parent process

fork() Example 1 #include int main() { pid_t pid; int i; sum = 0; pid = fork(); if( pid > 0 ) { /* parent */ for( i=0; i < 10; i++ ) sum = sum + i; printf(“parent: sum is %\n”,sum); wait(0); } else { /* child */ for( i=0; i < 10; i++ ) sum = sum - i; printf(“child: sum is %d\n”,sum); } return 0; }

fork() Example 1 r What is the value of sum in the parent and child processes after pid = fork()? m0m0 r What is the value of sum in the parent and child processes at the print statements? m parent: sum is 45 m child: sum is -45

fork() Example 1 r Remember that sum was 0 before the fork took place r When the fork took place the process was duplicated which means that a copy is made of each variable; sum was duplicated r Since sum was 0 just before the fork then sum is 0 right after the fork in both the child and parent processes

fork() Example 2 #include int main() { pid_t pid; int i; pid = fork(); if( pid > 0 ) { /* parent */ for( i=0; i < 1000; i++ ) printf(“\t\t\tPARENT %d\n”, i); wait(0); } else { /* child */ for( i=0; i < 1000; i++ ) { printf( “CHILD %d\n”, i ); } return 0; } What is the possible output?

fork () Example 2:Possible Output PARENT 0 PARENT 1 PARENT 2 PARENT 3 PARENT 4 PARENT 5 PARENT 6 PARENT 7 PARENT 8 PARENT 9 CHILD 0 CHILD 1 CHILD 2 CHILD 3 CHILD 4 CHILD 5 CHILD 6 CHILD 7 CHILD 8 CHILD 9

fork () Example 2:Possible Output PARENT 0 PARENT 1 PARENT 2 PARENT 3 PARENT 4 PARENT 5 PARENT 6 CHILD 0 CHILD 1 CHILD 2 PARENT 7 PARENT 8 PARENT 9 CHILD 3 CHILD 4 CHILD 5 CHILD 6 CHILD 7 CHILD 8 CHILD 9 Lots of possible outputs!!

Execution r Processes get a share of the CPU before giving it up to give another process a turn r The switching between the parent and child depends on many factors: m machine load, system process scheduling r Output interleaving is nondeterministic m Cannot determine output by looking at code

How many Processes are Created by this Program? #include int main() { fork(); }

Process Structure? r What does the process structure look like? r Is this a tree or a chain of processes pid_t childpid = 0; for (i=1;i<n;i++) if (childpid = fork()) == 0) break;

Process Structure? r What does the process structure look like? r Is this a tree or a chain of processes? pid_t childpid = 0; for (i=1;i<n;i++) if ((childpid = fork()) <= 0) break;

Process File Descriptor Table r Every process has a process file descriptor table r Each entry represents something that can be read from or written to e.g., m file m Screen m pipe (later)

System File Descriptor Table r The OS maintains a system file descriptor table in the OS's memory space. m Every open file in the system has an entry in this table. m One file can be associated with many entries in this table (if opened by many processes). m These entries contain among other things: the file descriptor's permissions # links the file offset which is moved by read and write. m An entry is removed when 0 links point to it.

OS File Structures Parent File Descriptor table stdin stdout stderr in_file System file table File info e.g., read offset Assume that there was something like this in a program FILE *in_file; in_file = fopen("list.txt", "r");

Fork and Files r In a fork what gets copied is the file descriptor table; r Thus the parent and child point to the same entry in the system file table

Fork and Files Parent File Descriptor table stdin stdout stderr in_file System file table File info e.g., read offset stdin stdout stderr in_file Child File Descriptor table

Fork and Files r Open a file before a fork m The child process gets a copy of its parent's file descriptor table. m The child and parent share a file offset pointer because the open came before the fork. r Open a file after a fork m Assume that parent and child each open a file after the fork m They get their own entries in the System File Descriptor table This implies that the file position information is different

Question  Suppose that foobar.txt consists of the 6 ASCII characters foobar. Then what is the output of the following program? int main() { FILE *fd1, *fd2; char c; fd1 = fopen("foobar.txt", “r”); fd2 = fopen("foobar.txt", “r”); fscanf(fd1, “%c”, &c); fscanf(fd2, “%c”, &c); printf("c = %c\n", c); }

Answer  The descriptors fd1 and fd2 each have their own open file table entry, so each descriptor has its own file position for foobar.txt. Thus, the read from fd2 reads the first byte of foobar.txt, and the output is c = f and not c = o

Fork and Files r Be careful r It is much better to open files after forks. r Even so you need to be careful if there is writing m This often requires that a process has mutual exclusive access during the write process (more later in the course)

Wait r Parents waits for a child (system call) m Blocks until a child terminates m Returns pid of the child process m Returns -1 if no child process exists (already exited) m status #include pid_t wait(int *status) r Parent waits for a specific child to terminate pid_t waitpid(pid_t pid, int *status, int options)

Process Creation Using Fork int main () { pid_t pid; int status = 0; pid = fork(); if (pid < 0) perror(“fork()”); if (pid > 0) { /* parent */ printf(“I am parent\n”); pid = wait(&status); } else { /* child */ printf(“I am child\n”); exit(status); } Parent uses wait to sleep until the child exits; wait returns child pid and status. Wait variants allow wait on a specific child, or notification of stops and other signals. The fork syscall returns twice: it returns a zero to the child and the child process ID (pid) to the parent.

More about Process Operations r In Unix-based systems, a hierarchy of processes is formed r In Unix, we can obtain a listing of processes by using the ps command r ps –el will list complete information for all processes

Exec r The term exec refers to a family of functions where each of the functions replace a process’s program (the one calling one of the exec functions) with a new loaded program r A call to a function from exec loads a binary file into memory (destroying the memory image of the program calling it) r The new program starts executing from the beginning (where main begins) r On success, exec never return; on failure, exec returns -1. r The different versions are different primarily in the way parameters are passed

Exec r The exec family consists of these functions: execvp, execlp, execv, execve, execl, execle m Functions with p in their name (execvp, execlp) search for the program in the path indicated by the PATH environment variable; functions without p must be given full path. m Functions with v in their name (execv, execvp, execve) differ from functions with l (execl, execlp, execle) in the way arguments are passed m Functions with e accept array of environment variables

Versions of exec r Versions of exec offered by C library: int execl( const char *path, const char *arg,... ); int execlp( const char *file, const char *arg,... ); int execle( const char *path, const char *arg,..., char *const envp[] ); int execv( const char *path, char *const argv[] ); int execvp( const char *file, char *const argv[] ); int execve( const char *filename, char *const argv [], char *const envp[] );

Exec Example Program A: int i = 5; printf(“%d\n”,i); execl(“B”, “B”, NULL); printf(“%d\n”,i); Program B: main() { printf(“hello\n”); } r What is the output of program A? 5 hello r Why is it not this? 5 hello 5 r The execl command replaces the instructions in the process with instructions for program B. It starts at the first instruction (starts at main)

Exec Example Program A: int i = 5; prog_argv[0] = "B"; prog_argv[1] = NULL; printf(“%d\n”,i); execv(prog_argv[0], prog_argv); printf(“%d\n”,i); Program B: main() { printf(“hello\n”); } r Same functionality as the program on the previous slide r Used execv instead of execl r execv uses an array to pass arguments r execl uses a list to pass arguments

Exec Example int main(int argc, char *argv[]) { int i = 5; int status; status = execlp(“ls”, “ls”, “-l”, “a.c”, NULL); if (status !=0) { perror(“you goofed\n"); printf(“errno is %d\n”,errno); } printf("%d\n",i); } r In this example, note that the command is ls –l a.c r Each argument is in the list r Question: m What would cause the perror function to be executed?

Exec Example int main(int argc, char *argv[]) { char *prog1_argv[4]; int i = 5; prog1_argv[0] = "ls"; prog1_argv[1] = "-l"; prog1_argv[2] = "a.c"; prog1_argv[3] = NULL; execvp(prog1_argv[0], prog1_argv); … printf("%d\n",i); } r Same example as that on the previous side but execvp is used which requires an array

Fork and Exec r Child process may choose to execute some other program than the parent by using one of the exec calls. r Exec overlays a new program on the existing process. r Child will not return to the old program unless exec fails. This is an important point to remember. r File descriptors are preserved

fork() and execv() execv(new_program, argv[ ]) New Copy of Parent Initial process fork Original process Continues Returns a new PID new_Program (replacement) execv(new_program) fork() returns pid=0 and runs as a cloned parent until execv is called

Example #include int main() { pid_t pid; pid = fork(); if (pid < 0) perror("fork()"); if (pid > 0) { wait(NULL); printf("Child Complete"); } else{ if (pid == 0) execlp("ls","ls", “-l, “a.c”, NULL); }

Exec and Shell r As we can see from the example on the previous slide a process, which can be a shell, can fork a child and the child can execute a command using exec