Operation System Program 1

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CSCC69: Operating Systems
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.
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.
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.
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
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.
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.
Lecture 5 Process, Thread and Task September 22, 2015 Kyu Ho Park.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions.
Creating and Executing Processes
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
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.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
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.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
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” –
Section 8: Processes What is a process Creating processes Fork-Exec
LINUX System : Lecture 8 Programming with Processes
Using Processes.
Unix Process Management
Process Management.
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
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
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)
Chapter 2: The Linux System Part 2
Processes in Unix, Linux, and Windows
Tutorial 3 Tutorial 3.
Processes Prof. Ikjun Yeom TA – Mugyo
Tutorial: The Programming Interface
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Lecture 6: Multiprogramming and Context Switching
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Process Description and Control in Unix
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
System Programming: Process Management
Presentation transcript:

Operation System Program 1 Kernel-Mode Multi-Process Programming 1/2/2019

Introduction In multitasking operating systems, processes (running programs) need a way to create new processes, e.g. to run other programs. Fork and its variants are typically the only way of doing so in Unix-like systems. For a process to start the execution of a different program, it first forks to create a copy of itself. Then, the copy, called the “child process", calls the exec system call to overlay itself with the other program it ceases execution of its former program in favor of the other. 1/2/2019

Child Process Processes are the primitive units for allocation of system resources. Each process has its own address space and one or more thread of control. A process executes a program; you can have multiple processes executing the same program But each process has its own copy of the program within its own address space and executes it independently of the other copies. 1/2/2019

Process Creation Concept Each process is named by a process ID number. A unique process ID is allocated to each process when it is created. The lifetime of a process ends when its termination is reported to its parent process; at that time, all of the process resources, including its process ID, are freed. Processes are created with the fork system call (so the operation of creating a new process is sometimes called forking a process). The child process created by fork is an exact clone of the original parent process, except that it has its own process ID. 1/2/2019

Process Creation Concept After forking a child process, both the parent and child processes continue to execute normally. If you want your program to wait for a child process to finish executing before continuing, you must do this explicitly after the fork operation, by calling wait or waitpid. These functions give you limited information about why the child terminated for example, its exit status code. The fork function is the primitive for creating a process. It is declared in the header file `unistd.h'. Function: pid_t fork (void) 1/2/2019

Executing a File This slides describes the exec family of functions, for executing a file as a process image. You can use these functions to make a child process execute a new program after it has been forked. int execl (const char *filename, const char *arg0, ...) int execve (const char *filename, char *const argv[], char *const env[]) int execle (const char *filename, const char *arg0, char *const env[], ...) int execvp (const char *filename, char *const argv[]) int execlp (const char *filename, const char *arg0, ...) You need to verify the difference. 1/2/2019

Process Completion The functions described in this slide are used to wait for a child process to terminate or stop, and determine its status. These functions are declared in the header file `sys/wait.h'. pid_t waitpid (pid_t pid, int *status_ptr, int options) pid_t wait (int *status_ptr) 1/2/2019

Process Completion Status If the exit status value of the child process is zero, then the status value reported by waitpid or wait is also zero. You can test for other kinds of information encoded in the returned status value using the following macros int WIFEXITED (int status) int WIFSIGNALED (int status) int WIFSTOPPED (int status) For more information check the header file `sys/wait.h'. 1/2/2019

Standard signals Linux supports the standard signals listed below. SIGQUIT 3 SIGKILL 9 SIGTERM 15 …etc Several signal numbers are architecture-dependent. For more information , please check the header file ‘signal.h’ 1/2/2019

Program 1-1 In this program, you have to write a user mode process monitor that oversees its own child process. First this monitor forks a child process to execute the test program When child process finish executing , the child process will enter zombie status. And the parent process halt and receive(block receiving) the SIGCHLD signal by wait() function. 1/2/2019

Program 1-1 After receiving the SIGCHLD signal , you will know exit state of child process by checking the SIGCHLD signal If normal termination, print normal termination If not normal termination, print the situation of child process Hint: checking the WEXITSTATUS and WTERMSIG …etc You have to print the parent process id and following error message on the screen. 1/2/2019

Process state diagram 1/2/2019

Wait until child process finish monitor fork Child process Wait until child process finish Execute Check the return signal Test program Finish Print normal termination or the following error message Return status signal 1/2/2019

DEMO Path of test program. YOU can see that the child process was terminated normally. 1/2/2019

DEMO Segmentation fault detected!! 1/2/2019

Program 1-2 In this program, we also want you to implement a fork function and wrap your program into a kernel object (.ko file) that insert into your OS kernel In next slide, we will show you how to insert an kernel object into your own OS kernel. 1/2/2019

Program 1-2 We will give you an template, you need to create an kernel thread and run a function name call my_fork to implement your fork function. We will give you a makefile to compile your program to kernel object. $make (to compile your program) $make clean(to delete the files that generated by makefile) 1/2/2019

Kernel Object A loadable kernel module (or LKM) is an object file that contains code to extend the running kernel, or so-called base kernel LKMs are typically used to add support for new hardware and/or filesystems, or for adding system calls. Most current Unix-like systems support loadable kernel modules, although they might use a different name for them, such as kernel loadable module (kld), kernel extension (kext) in and kernel- mode driver 1/2/2019

1/2/2019

Modify here to match your program name 1/2/2019

How to insert module Before insert the kermel object, you have to sign in the root account. $ sudo su And type the command to insert your module $insmod MODULE_NAME.ko How to list all the module you insert $lsmod This command will remove your module $rmmod MODULE_NAME.ko Always remember to remove your module!!! 1/2/2019

Create Kernel thread The first step in creating a kernel thread is to define a "thread function" which will contain the code to be executed; int thread_function(void *data); A kernel thread is created with: struct task_struct *kthread_create(int (*threadfn)(void *data), void *data, const char *namefmt, ...); The data argument will simply be passed to the thread function. The thread will not start running immediately; to get the thread to runpass the task_struct pointer returned by kthread_create() to wake_up_process(). 1/2/2019

Create Kernel thread There is also a convenience function which creates and starts the thread: struct task_struct *kthread_run(int (*threadfn)(void *data), void *data, const char *namefmt, ...); 1/2/2019

Steps in fork Calls dup_task_struct(), which creates a new kernel stack, thread_info structure, and task_struct for the new process. calls get_pid() to assign an available PID to the new task. copy_process() then either duplicates or shares open files, filesystem information, signal handlers, process address space, and namespace. For more details http://www.makelinux.net/books/lkd2/ch03lev1sec2 (fork steps) http://lxr.free-electrons.com/source/kernel/fork.c (do_fork) 1/2/2019

Steps to execute program First you have to understand data structure struct linux_binprm. http://lxr.free-electrons.com/source/include/linux/binfmts.h And use prepare_bprm_creds API to initialize the struct. Open your program file and assign corresponding values into struct linux_binprm And use the bprm_mm_init and prepare_binprm to prepare execute your binary program 1/2/2019

Steps to execute program Read the file content and copy corresponding information into your struct linux_binprm. Search_binary_handler to execute your binary program For more detail information: Binary format header http://lxr.free-electrons.com/source/include/linux/binfmts.h Execve implementation in linux kernel http://lxr.free-electrons.com/source/fs/exec.c 1/2/2019

Catch the signal After execve succeeded, you have to call acct_update_integrals and free the binary program by calling free_brpm And check the signal struct which is one of the member in task struct. 1/2/2019

Bonus Create the executable file, named myfork Execute “./myfork testprogram” will show the signal message that child process execute testprogram. We can add many executable files as the argument of myfork, such as “./myfork testprogram1 testprogram2 testprogram3”, the testprogram2 is the child of testprogram1, the testprogram3 is the child of testprogram2, and so on.

Bonus Create the executable file, named watproc Execute “./watproc myfork” after myfork execute that show the message of process state of myfork process and its descendants. For example : 1 -> 2 -> 3 (number is the pid of each process) 1/2/2019

Grading policy User mode monitor : 8 Kernel mode my_fork : 10 Bonus : 11 1/2/2019

Reference Process API http://www.cs.utah.edu/dept/old/texinfo/glibc-manual- 0.02/library_23.html http://man7.org/linux/man-pages/man7/signal.7.html Unix process state diagram http://www.ksvali.com/wp- content/uploads/2009/09/processstate.gif 1/2/2019

Reference How to compile .ko file http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html Kernel thread http://tuxthink.blogspot.tw/2011/02/kernel-thread-creation-1.html Insert Kernel module http://www.tldp.org/HOWTO/Module-HOWTO/x197.html 1/2/2019

Reference Fork implementation http://www.makelinux.net/books/lkd2/ch03lev1sec2 http://lxr.free-electrons.com/source/kernel/fork.c Process Descriptor and the Task Structure http://www.makelinux.net/books/lkd2/ch03lev1sec1 1/2/2019