CS 3305A Process – Part II Lecture 4 Sept 20, 2017.

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
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
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.
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.
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
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.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Introduction to Processes CS Intoduction to Operating Systems.
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.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
Creating and Executing Processes
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Shell (Addendum). 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.
System calls for Process management
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Operating Systems Process Creation
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
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.
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
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
CSCI 4061 Recitation 2 1.
Robust I/O package Chapter 11 practice problems
Precept 14 : Ish dup() & Signal Handling
Protection of System Resources
Using Processes.
Unix Process Management
Chapter 3: Processes.
CSC 382: Computer Security
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.
IT 344: Operating Systems Module 4 Processes
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
Operating Systems Lecture 6.
Genesis: From Raw Hardware to Processes
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Programming Assignment # 2 – Supplementary Discussion
IPC Prof. Ikjun Yeom TA – Hoyoun
Processes Prof. Ikjun Yeom TA – Mugyo
Lecture 6: Multiprogramming and Context Switching
Processes in Unix, Linux, and Windows
CSE 451: Operating Systems Autumn 2009 Module 4 Processes
IT 344: Operating Systems Winter 2008 Module 4 Processes
CSE 451: Operating Systems Winter 2007 Module 4 Processes
Processes in Unix and Windows
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

CS 3305A Process – Part II Lecture 4 Sept 20, 2017

Topics to be discussed Process File Descriptor Table System File Descriptor Table Fork and Files Systems calls: wait() and exec()

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

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

File info e.g., read offset OS File Structures 1 2 3 4 Process 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 In a fork what gets copied is the file descriptor table; Thus the parent and child point to the same entry in the system file table

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

Fork and Files Open a file before a fork Open a file after a fork The child process gets a copy of its parent's file descriptor table. The child and parent share a file offset pointer because the open came before the fork. Open a file after a fork Assume that parent and child each open a file after the fork 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); }

Question int main() { FILE *fd1, *fd2; char c; pid_t pid; fd1 = fopen(“foo.txt”, “w”); fd2 = fopen(“foo.txt”, “w”); fprintf(fd1, “%s”, “Anwar”); fprintf(fd2, “%s”, “Haque”); }

Question: fopen() before fork int main() { FILE *fd1, *fd2; char c; pid_t pid; fd1 = fopen("foobar.txt", “r”); fd2 = fopen("foobar.txt", “r”); pid = fork (); if (pid > 0){ fscanf(fd1, “%c”, &c); printf(”parent: c = %c\n", c); }else if (pid == 0) { fscanf(fd2, “%c”, &c); printf(”child: c = %c\n", c); }

Question: fopen() before fork int main() { FILE *fd1, *fd2; char c; pid_t pid; fd1 = fopen("foobar.txt", “r”); pid = fork (); if (pid > 0){ fscanf(fd1, “%c”, &c); printf(”parent: c = %c\n", c); }else if (pid == 0) { printf(”child: c = %c\n", c); }

Fork and Files Be careful It is much better to open files after forks.

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

Exec 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 A call to a function from exec loads a binary file into memory (destroying the memory image of the program calling it) The new program starts executing from the beginning (where main begins) On success, exec never returns; on failure, exec returns -1. The different versions are different primarily in the way parameters are passed

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

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

Example if (pid > 0) { wait(NULL); printf("Child Complete"); } else{ if (pid == 0) { execl(“B", NULL); printf("\n You'll never see this line.."); } } #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid_t pid; pid = fork(); if (pid < 0) perror("fork()");