CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.

Slides:



Advertisements
Similar presentations
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Advertisements

Lab 9 CIS 370 Umass Dartmouth.  A pipe is typically used as a one-way communications channel which couples one related process to another.  UNIX deals.
© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
4.1 Operating Systems Lecture 11 UNIX Pipes Read Handout "An Introduction to Concurrency..."
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
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 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CSE 451 Section 4 Project 2 Design Considerations.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Today’s topic Inter-process communication with pipes.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
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.
Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
CS 3214 Computer Systems Lecture 13 Godmar Back.
CS 149: Operating Systems January 29 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
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.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
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.
Pipe-Related System Calls COS 431 University of Maine.
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,
Chapter 7 Files By C. Shing ITEC Dept Radford University.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
CSCI 330 UNIX and Network Programming
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
OS Labs 2/25/08 Frans Kaashoek MIT
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
Dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available.
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
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.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Week 3 Redirection, Pipes, and Background
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Protection of System Resources
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
Processes in Unix, Linux, and Windows
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Process Creation Process Termination
Programming Assignment # 2 – Supplementary Discussion
Process Programming Interface
IPC Prof. Ikjun Yeom TA – Hoyoun
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes

System Calls A system call is a controlled entry point into the kernel, allowing a process to request that the kernel do something for the process (API provided by kernels) The set of system calls in an OS is fixed Each system call has a set of arguments that specify information to be transferred to kernel From programmer’s view, a system call looks like a function call. In C, one issues a system call by calling a wrapper function for the call in C library

File Descriptor All system calls refer to open files using a file descriptor, a (usually small) nonnegative integer. Usually 0 refers to standard input, 1 refer to standard output, and 2 to standard error The scope of file descriptors is per process File descriptor can be viewed as index into an array of opened files The maximum number of files descriptor per process is 32 by default but but it can be changed with the command ulimit up to 1024

Some System Calls for File I/O Open a file fd=open(pathname, flags, mode); Read from a file numread = read(fd, buffer, count); Write to a file numread = write(fd, buffer, count); Close a file status = close(fd);

The Open File Table Each process has a File Descriptor Table (maintained by the OS kernel) with all the files that are opened by that process. Process can only affect it using system calls Each entry in the File Descriptor Table contains a pointer to an open file object that contains all the information about the open file. The kernel maintains a Open File Table, which includes Open File Objects for the whole system (shared by all processes).

The System View (from )

Facts About The Open File Table One file (i-node) can have multiple entries in the Open File Table One entry in the Open File Table can be pointed by multiple file descriptors When dup(), dup2(), and fork() are used, entries in Open File Table are shared. When open() is used, another entry in Open File Table is created. File descriptors sharing the same Open File Table entry will share the same offset, which will be updated by read/write.

The Open File Table A Process’ Open File Table Open File Object I-NODE Open Mode Offset Reference Count

Open File Object (or Open File Handle) An Open File Object contains the state of an open file. I-Node – It uniquely identifies a file in the computer. An I-nodes is made of two parts: Major number – Determines the devices Minor number –It determines what file it refers to inside the device. Open Mode – How the file was opened: Read Only Read Write Append

Open File Object (or Open File Handle) Offset – The next read or write operation will start at this offset in the file. Each read/write operation increases the offset by the number of bytes read/written. Reference Count – It is increased by the number of file descriptors that point to this Open File Object. When the reference count reaches 0 the Open File Object is removed. The reference count is initially 1 and it is increased after fork() or calls like dup and dup2.

Default Open Files When a process is created, there are three files opened by default: 0 – Default Standard Input 1 – Default Standard Output 2 – Default Standard Error write(1, “Hello”, 5) Sends Hello to stdout write(2, “Hello”, 5) Sends Hello to stderr Stdin, stdout, and stderr are inherited from the parent process.

The open() system call int open(filename, mode, [permissions]), It opens the file in filename using the permissions in mode. Mode: O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_APPEND, O_TRUNC O_CREAT If the file does not exist, the file is created. Use the permissions argument for initial permissions. Bits: rwx(user) rwx(group) rwx (others) Example: 0555 – Read and execute by user, group and others. (101B==5Octal) O_APPEND. Append at the end of the file. O_TRUNC. Truncate file to length 0. See “man open”

The close() System call void close(int fd) Decrements the count of the open file object pointed by fd If the reference count of the open file object reaches 0, the open file object is removed.

The fork() system call int fork() It is the only way to create a new process in UNIX The OS creates a new child process that is a copy of the parent. ret = fork() returns: ret == 0 in the child process ret == pid > 0 in the parent process. ret < 0 error The memory in the child process is a copy of the parent process’s memory. We will see later that this is optimized by using VM copy- on-write.

The fork() system call The File Descriptor table of the parent is also copied in the child. The Open File Objects of the parent are shared with the child. The reference counters of the Open File Objects are increased.

The fork() system call Open File Object Ref count=1 Open FileTable (parent)_ Ref count=1 Before:

The fork() system call Open File Object Ref count=2 Open FileTable (parent) Ref count=2 After: Open FileTable (child)

The fork() system call Implication of parent and child sharing file objects: By sharing the same open file objects, parent and child or multiple children can communicate with each other. They share the same offset, that is, after one reads it, the offset will be updated We will use this property to be able to make the commands in a pipe line communicate with each other.

The execvp() system call int execvp(progname, argv[]) Loads a program in the current process. The old program is overwritten. progname is the name of the executable to load. argv is the array with the arguments. argv[0] is the progname itself. The entry after the last argument should be a NULL so execvp() can determine where the argument list ends. If successful, execvp() will not return.

The execvp() system call void main() { // Create a new process int ret = fork(); if (ret == 0) { // Child process. // Execute “ls –al” const char *argv[3]; argv[0]=“ls”; argv[1]=“-al”; argv[2] = NULL; execvp(argv[0], argv); // There was an error perror(“execvp”); _exit(1); } else if (ret < 0) { // There was an error in fork perror(“fork”); exit(2); } else { // This is the parent process // ret is the pid of the child // Wait until the child exits waitpid(ret, NULL); } // end if }// end main  Example: Run “ls –al” from a program.

The dup2() system call int dup2(fd1, fd2) After dup2(fd1, fd2), fd2 will refer to the same open file object that fd1 refers to. The open file object that fd2 refered to before is closed. The reference counter of the open file object that fd1 refers to is increased. dup2() will be useful to redirect stdin, stdout, and also stderr.

The dup2() system call Open File Object Shell Console Ref count=3 File “myout” Ref count=1 Before: Example: redirecting stdout to file “myfile” previously created. What should we use to redirect stdout to file “myout”?

The dup2() system call Open File Object Shell Console Ref count=2 File “myout” Ref count=2 After dup2(3,1); Now every printf will go to file “myout”.

Example: Redirecting stdout int main(int argc,char**argv) { // Create a new file int fd = open(“myoutput.txt”, O_CREAT|O_WRONLY|O_TRUNC, 0664); if (fd < 0) { perror(“open”); exit(1); } // Redirect stdout to file dup2(fd,1); // Now printf that prints // to stdout, will write to // myoutput.txt printf(“Hello world\n”); } A program that redirects stdout to a file myoutput.txt

The dup() system call fd2=dup(fd1) dup(fd1) will return a new file descriptor that will point to the same file object that fd1 is pointing to. The reference counter of the open file object that fd1 refers to is increased. This will be useful to “save” the stdin, stdout, stderr, so the shell process can restore it after doing the redirection.

The dup() system call Open File Object Shell Console Ref count=3 Before:

The dup() system call Open File Object Shell Console Ref count=4 After fd2 = dup(1) fd2 == 3

The pipe system call int pipe(fdpipe[2]) fdpipe[2] is an array of int with two elements. After calling pipe, fdpipe will contain two file descriptors that point to two open file objects that are interconnected. What is written into fdpipe[1] can be read from fdpipe[0]. In some Unix systems like Solaris pipes are bidirectional but in Linux they are unidirectional.

The pipe system call Open File Objects Shell Console Ref count=3 Before:

The pipe system call Open File Objects Shell Console Ref count=3 After running: int fdpipe[2]; pipe(fdpipe); pipe0 Ref count=1 Pipe1 Ref count=1 fdpipe[0]==3 fdpipe[1]==4 What is written in fdpipe[1] can be read from fdpipe[0].

After pipe() and fork() (from notes/node28.html)

Example of pipes and redirection A program “lsgrep” that runs “ls –al | grep arg1 > arg2”. Example: “lsgrep aa myout” lists all files that contain “aa” and puts output in file myout. int main(int argc,char**argv) { if (argc < 3) { fprintf(stderr, "usage:” “lsgrep arg1 arg2\n"); exit(1); } // Strategy: parent does the // redirection before fork() //save stdin/stdout int tempin = dup(0); int tempout = dup(1); //create pipe int fdpipe[2]; pipe(fdpipe); //redirect stdout for "ls“ dup2(fdpipe[1],1); close(fdpipe[1]);

Example of pipes and redirection // fork for "ls” int ret= fork(); if(ret==0) { // close file descriptors // as soon as are not // needed close(fdpipe[0]); char * args[3]; args[0]="ls"; args[1]=“-al"; args[2]=NULL; execvp(args[0], args); // error in execvp perror("execvp"); _exit(1); } //redirection for "grep“ //redirect stdin dup2(fdpipe[0], 0); close(fdpipe[0]); //create outfile int fd=open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600); if (fd < 0){ perror("open"); exit(1); } //redirect stdout dup2(fd,1); close(fd);

Example of pipes and redirection // fork for “grep” ret= fork(); if(ret==0) { char * args[3]; args[0]=“grep"; args[1]=argv[1]; args[2]=NULL; execvp(args[0], args); // error in execvp perror("execvp"); _exit(1); } // Restore stdin/stdout dup2(tempin,0); dup2(tempout,1); // Parent waits for grep // process waitpid(ret,NULL); printf(“All done!!\n”); } // main

Two Implementations of lsgrep Read the first implementation at ourses/252_Spring15/code/lsgrep/lsgrep.c Parent process does redirection And the second implementation at ourses/252_Spring15/code/lsgrep/lsgrep2.c Child process does redirection

Closing Unused Pipes Must close unused write end of the pipe, otherwise processes keep waiting for more to be written to the pipe, and won’t exit. Only when all write ends are closed to the pipe, is EOF sent to the readers. Good practice to close unused read end of the pipe as well, so writer process gets an error signal when writing to the pipe Otherwise a writer process may keep writing, until pipe is full and process is blocked

Review: What is an open file object? What information does it include? What is the semantics for pipe, dup, dup2, especially how they affect the creation/deletion and reference count of open file object? How to implement pipe and redirection using pipe, dup, dup2? (Not required for mid-term 1)