Project 2: User Programs

Slides:



Advertisements
Similar presentations
CS 140 Project 3 Virtual Memory
Advertisements

Christo Wilson Project 2: User Programs in Pintos
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
CSCC69: Operating Systems
1 Pintos Project #3 Virtual Memory The following slides were created by Xiaomo Liu and others for CS 3204 Fall And Modified by Nick Ryan for Spring.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Jaishankar Sundararaman
CS 4284 Systems Capstone Project 2 Hints Slides created by Jaishankar Sundararaman.
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.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Concurrency Sharing of resources in the same time frame Apparent concurrency is sharing the same CPU, memory, or I/O device Real concurrency is sharing.
CSE 451 Section 4 Project 2 Design Considerations.
Christo Wilson Project 3: Virtual Memory in Pintos
Advanced Programming in the UNIX Environment Hop Lee.
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.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Welcome to the World of Nachos CPS 110 Spring 2004 Discussion Session 1.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
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.
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,
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Processes and Virtual Memory
Nachos Lecture 2 Xiaorui Sun. Phase 2 You have got one machine (machine package) You have to implements the incomplete OS (userprog package) Run programs.
Process Management Azzam Mourad COEN 346.
Project 2: User Programs Abdelmounaam Rezgui Acknowledgment: The content of some slides is partly taken from Josh Wiseman’s presentation.
1 Pintos Virtual Memory Management Project (CS3204 Spring 2006 VT) Yi Ma.
Pintos project 3: Virtual Memory Management
Project 2: User Programs Presented by Min Li 26 Feb 2009.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
CS140 Project 4 Due Thursday March 10th Slides adapted from Samir Selman’s Kiyoshi Shikuma.
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.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
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” –
CS 3214 Computer Systems Lecture 9 Godmar Back.
Introduction to Operating Systems
Precept 14 : Ish dup() & Signal Handling
Auburn University COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Part 5: Managing.
Jaishankar Sundararaman
Protection of System Resources
Using Processes.
Unix Process Management
UNIX PROCESSES.
More on UART Interrupts; System Calls
Operation System Program 1
File Handling.
Process Programming Interface
The Environment of Unix Process
Lecture 6: Multiprogramming and Context Switching
Section 2 Processes January 27rd, 2017 Taught by Joshua Don.
Presentation transcript:

Project 2: User Programs Presented by Xiaomo Liu 1 Oct 2009 (update Min Li’s slides)

Till now … All code part of Pintos Kernel Code compiled directly with the kernel This required that the tests call some functions whose interface should remain unmodified From now on, run user programs on top of kernel Freedom to modify the kernel to make the user programs work In project1, all the changes run as part of the kernel but in project 2, it would not be entirely so. In project1, since the code was compiled directly with kernel, it required that we call some kernel specific functions. But in project2, the user program could call various system calls which would then be processed by the kernel When user programs are run, we have some issues like managing the memory and loading different user programs and scheduling them appropriately

Why Project 2 is not Project 1? Timer Interrupts Other IRQs User Programs Kernel int 0x30 Exceptions Project 2 Tests Project 1 lib/user/syscall.c filesystem syscall layer exc handling

Sample User Program in C In C, a user program test.c can pass argument int main(int argc, char* argv[]) { for(int i=0; i<argc; i++) char* arg = argv[i]; } ./test arg1 arg2 … In this project, it requires to interface with the file system. We do not recommend modifying any files in file system related directory. When project4 is underway it may have to be done. Currently the file system has certain limitations which are as listed above. The students could take a look at files like filesys.h and file.h to get some information about the file system basics

Sample User Program in C test.c can call system libraries #include <stdio.h> int main() { FILE* p_file = fopen("myfile.txt","w"); if (p_file != NULL) fputs(“fopen”, p_file); fclose(p_file); } Get fopen, fputs, fclose by system calls Pintos need you to implement Argument passing System calls In this project, it requires to interface with the file system. We do not recommend modifying any files in file system related directory. When project4 is underway it may have to be done. Currently the file system has certain limitations which are as listed above. The students could take a look at files like filesys.h and file.h to get some information about the file system basics

Using the File system May need to interact with file system Do not modify the file system! Certain limitations (till Project 4) No internal synchronization Fixed file size No subdirectories File names limited to 14 chars System crash might corrupt the file system Files to take a look at: ‘filesys.h’ & ‘file.h’ In this project, it requires to interface with the file system. We do not recommend modifying any files in file system related directory. When project4 is underway it may have to be done. Currently the file system has certain limitations which are as listed above. The students could take a look at files like filesys.h and file.h to get some information about the file system basics

Some commands Creating a simulated disk Formatting the disk pintos-mkdisk filesys.dsk --filesys-size=2 Formatting the disk pintos -f –q This will only work after your kernel is built ! Copying the program into the disk pintos -p ../../examples/echo -a echo -- -q Running the program pintos -q run ’echo x’ Single command: pintos --fs-disk=2 -p ../../examples/echo -a echo -- -f -q run ’echo x’ $ make check – Builds the disk automatically Copy&paste the commands make check does! The slide lists some of the commands which may have to be executed for making user programs to get running

Various directories Few user programs: Relevant files: Other files: src/examples Relevant files: userprog/ Other files: threads/, filesys/ Examples directory contain some of the examples which could be run and seen Userprog contain those files which may need to be modified for this project Threads contain some files which may have to be modified to get project2 working

Requirements Process Termination Messages Argument Passing System calls Deny writes to executables The slide lists some of the requirements/tasks for project0

Process Termination Process Terminates Do not print any other message! printf ("%s: exit(%d)\n",...); for eg: args-single: exit(0) Do not print any other message! Return Code Program name Every user program which exits should print the program name which is invoked and the exit code before exiting. Care should be taken that no other messages are printed in kernel since it may confuse the grading scripts and may lead to marks getting reduced

Argument Passing Pintos currently lacks argument passing. You Implement it! Change *esp = PHYS_BASE to *esp = PHYS_BASE – 12 in setup_stack() to get started Change process_execute() in process.c to process multiple arguments Could limit the arguments to fit in a page(4 kb) String Parsing: strtok_r() in lib/string.h pgm.c main(int argc, char *argv[]) { … } $ pintos run ‘pgm alpha beta’ argc = 3 argv[0] = “pgm” argv[1] = “alpha” argv[2] = “beta” The sample program gives a way in which a user program might be passing command line arguments to the program run under pintos Remember to initially change the *Esp value to get programs working till argument passing is implemented Then change process_execute() such that arguments are first separated into tokens, then given to the child process to be invoked. These arguments should be properly pushed into stack ie stack should be setup before the program is invoked, else the kernel might crash The function to tokenize is present in lib/string.h and is named strtok_r() Example taken from Abdelmounaam Rezgui’s presentation

Memory layout PHYS_BASE = 3GB The slides shows the memory layout. The shaded region shows the kernel virtual memory. The user programs could access the user virtual memory. The kernel can access user virtual memory as well as kernel virtual memory. The PHYS_BASE marks the end of the user virtual memory and beginning of kernel virtual memory. This is a very important value which might be of use when getting pointers from user space to be processed by kernel space. Figure taken from Abdelmounaam Rezgui’s presentation

Setting up the Stack How to setup the stack for the program: /bin/ls –l foo bar The slide shows the way in which a stack might have to be setup to load the user program with command line.

Setting up the Stack… Contd bffffffc0 00 00 00 00 | ....| bffffffd0 04 00 00 00 d8 ff ff bf-ed ff ff bf f5 ff ff bf |................| bffffffe0 f8 ff ff bf fc ff ff bf-00 00 00 00 00 2f 62 69 |............./bi| bfffffff0 6e 2f 6c 73 00 2d 6c 00-66 6f 6f 00 62 61 72 00 |n/ls.-l.foo.bar.|

Synchronization Synchronization between parent and children processes Ensuring child process Loading new executables successfully

Requirements Process Termination Messages Argument Passing System calls Deny writes to executables The slide lists some of the requirements/tasks for project0

System Calls Pintos lacks support for system calls currently! Syscall handler currently … Pintos lacks support for system calls currently! Implement the system call handler in userprog/syscall.c System call numbers defined in lib/syscall-nr.h Process Control: exit, exec, wait File system: create, remove, open, filesize, read, write, seek, tell, close Others: halt static void syscall_handler (struct intr_frame *f UNUSED) { printf ("system call!\n"); thread_exit (); }

Continued… System calls that return a value () must modify f->eax A system call has: System call number (possibly) arguments When syscall_handler() gets control: The slide shows how a system call might have to be processed by a system call handler System calls that return a value () must modify f->eax Figure taken from Abdelmounaam Rezgui’s presentation

System calls – File system Decide on how to implement the file descriptors O(n) data structures… perfectly fine! Access granularity is the entire file system Have 1 global lock! write() – fd 1 writes to console use putbuf() to write entire buffer to console read() – fd 0 reads from console use input_getc() to get input from keyboard Implement the rest of the system calls

System calls – Process Control wait(pid) – Waits for process pid to die and returns the status pid returned from exit Returns -1 if pid was terminated by the kernel pid does not refer to child of the calling thread wait() has already been called for the given pid exec(cmd) – runs the executable whose name is given in command line returns -1 if the program cannot be loaded exit(status) – terminates the current program, returns status status of 0 indicates success, non zero otherwise Parent: exec() Parent process executes Child process executes Child process exits The slide explains the description of the various system calls. Parent: wait() OS notifies Figure taken and modified from Dr. Back’s lecture – CS3204 - Fall 2006

Process Control: wait int process_wait (tid_t child_tid UNUSED) { return -1; } Implement process_wait() in process.c Then, implement wait() using process_wait() Cond variables and/or semaphores will help Think about what semaphores may be used for and how they must be initialized Some Conditions to take care! Parent may or may not wait for its child Parent may call wait() after child terminates! main() { int i; pid_t p; p = exec(“pgm a b”); // i = wait (p); }

Memory Access System calls can have memory access In open-bad-ptr.c e.g open(), read(), write() have a look at tests cases *-bad-ptr.c In open-bad-ptr.c void test_main (void) { msg ("open(0x20101234): %d”, open ((char *) 0x20101234)); fail ("should have called exit(-1)"); }

Memory Access (contd’) Invalid pointers must be rejected. Why? Kernel has access to all of physical memory including that of other processes Kernel like user process would fault when it tries to access unmapped addresses User process cannot access kernel virtual memory User Process after it has entered the kernel can access kernel virtual memory and user virtual memory How to handle invalid memory access?

Memory Access (contd’) Two methods to handle invalid memory access Verify the validity of user provided pointer and then dereference it Look at functions in userprog/pagedir.c, threads/vaddr.h Strongly recommended! Check if user pointer is below PHYS_BASE and dereference it Could cause page fault Handle the page fault by modifying the page_fault() code in userprog/exception.c Make sure that resources are not leaked There could be 2 methods to handle invalid memory access. We usually recommend method 1, but if method 2 is done, care must be taken to handle the page fault and return the properly to the system call handler.

Some Issues to look at… Check the validity of the system call parameters Every single location should be checked for validity before accessing it. For e.g. not only f->esp, but also f->esp +1, f->esp+2 and `f->esp+3 should be checked Read system call parameters into kernel memory (except for long buffers) copy_in function recommended!

Denying writes to Executables Use file_deny_write() to prevent writes to an open file Use file_allow_write() to re enable write Closing a file will automatically re enable writes

Suggested Order of Implementation Change *esp = PHYS_BASE to *esp = PHYS_BASE – 12 to get started Implement the system call infrastructure Change process_wait() to a infinite loop to prevent pintos getting powered off before the process gets executed Implement exit system call Implement write system call Start making other changes

Misc Deadline: 19 Oct, 11:59 pm Do not forget the design document Must be done individually Good Luck!