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,

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CSSE Operating Systems
CSE 451 Section 4 Project 2 Design Considerations.
Advanced Programming in the UNIX Environment Hop Lee.
Unix Processes operating systems. The Process ID Unix identifies each process with a unique integer called a process ID. The process that executes the.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Operating systems Lab 04 System Call, Nested Fork(),Exec(),Pipe()
Some Example C Programs. These programs show how to use the exec function.
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.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,
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.
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Creating and Executing Processes
CE Operating Systems Lecture 10 Processes and process management in Linux.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
Lecture 5 Systems Programming: Unix Processes Creation: fork & exec Process Communication: Pipes.
System calls for Process management
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
Operating Systems Process Creation
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
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.
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.
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
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
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.
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
CSCI 4061 Recitation 2 1.
Section 8: Processes What is a process Creating processes Fork-Exec
Using Processes.
Unix Process Management
Processes in Unix, Linux, and Windows
UNIX 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
Tutorial 3 Tutorial 3.
Chien-Chung Shen CIS/UD
Process Programming Interface
Processes Prof. Ikjun Yeom TA – Mugyo
Lecture 6: Multiprogramming and Context Switching
Processes in Unix, Linux, and Windows
Process Description and Control in Unix
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

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, offering memory resources, access to I/O, and access to kernel services. u In Unix, when a new process is created, a copy of the parent process’ environment variables is provided as a default to the new process

Unix Processes u Every process has the following attributes:  a process id (a small integer)  a user id (a small integer)  a group id (a small integer)  a current working directory  a chunk of memory that hold name/value pairs as text strings (the environment variables).  a bunch of other things…

Identification (uid, gid) u Each process has an associated real uid, but also an effective uid (euid) u The euid is used in determining the access permissions of a given process u getuid() and geteuid() will return the current real and effective user ids.

Effective uids u A process can change its effective id in two ways  by setting the setuid bit on a file in the filesystem  chmod u+s myfile  ls –l myfile: -rwsrwxr-x 1 root root Oct 10 17:34 myfile  by executing the seteuid(uid_t) function

Creating a Process  To create a new process make a fork() system call.  fork() splits the current process in to 2 processes, one is called the parent and the other is called the child.

Parent and Child Processes u The child process is a copy of the parent process  It is running the same program  It's program counter is in the same place  It has its own process ID u The child process inherits many attributes from the parent, including:  current working directory  user id  group id

Command Line Arguments u It is possible for a program to detect which options were used when it is invoked  Like "ls –l" or "cp one.txt../two.txt" u The text of the command that comes after the command line is composed of command line arguments u Your program can access the arguments with a specially structured main() function

Command Line Arguments int main(int argc, char** argv){ u The shell puts the number of command line tokens in argc; the program name is the first of these u Each command line token is stored in the new process memory  the shell creates an array and stores a pointer to each token in the array  argv is a pointer to this array

Command Line Arguments #include int main(int argc, char** argv){ cout << endl << "ARG COUNT= " << argc << endl; for (int i = 0; i < argc; i++) cout << "ARG" << i << ": " << argv[i] << endl; } u argv can be treated as an array of strings u Each command line token is stored in the new process memory  the shell creates an array and stores a pointer to each token in the array  argv is a pointer to this array

The fork() system call #include pid_t fork(void); //prototype for fork  fork() returns a process id (a small integer)  fork() returns twice!  In the parent – fork returns the id of the child process  In the child – fork returns a 0

Example #include void main(void) { if (fork()) //0 = false, all else true printf(“I am the parent\n”); else printf(“I am the child\n”); printf(“I am the walrus\n”); }

Bad Example (don’t try this!) #include void main(void) { while (!fork()) printf("I am the child %d\n“, getpid()); printf("I am the parent %d\n“, getpid()); } fork bomb!

Switching Programs  fork() creates a new process u This would be almost useless if there was not a way to switch which program is associated with the new process  The exec() system call is used to load a new program into an existing process

The exec() Functions: Out with the old, in with the new u The exec() functions all replace the current running program in the process with another program u bring up an xterm:  exec sleep 5#what happens?

The exec() Functions: Out with the old, in with the new u There are two families of exec() functions, the “l” family (list), and the “v” family (vector) u Each exec() call can choose different ways of finding the executable and whether the environment is delivered in a list or an array (vector) u The environment, open file handles, etc. are passed into the exec’d program u What is the return value of a successful exec() call?

The exec family u When you call a member of the exec family you give it the pathname of the executable file that you want to run, and its command line arguments u If all goes well, exec will never return! u The process becomes the new program

The execl... functions u int execl(const char * path, const char * arg0,...);  executes the command at path, passing it the environments arg0... argn  thus, the execl family breaks down argv into its individual constituents, and then passes them as a list to the execl? function u int execlp(const char * path, const char * arg0,...);  same as execl, but uses $PATH resolution for locating the program in path u int execle(const char * path, const char * arg0,...);

The execv... functions u int execv(const char * path, char *const argv[]);  executes the command at path, passing it the environment contained in argv[] u int execvp(const char * path, char *const argv[]);  same as execv, but uses $PATH resolution for locating the program in path u int execve(const char * path, char *const argv[], char * const envp[]);

Death and Destruction u All processes usually end at some time during runtime (with the exception of init) u Processes may end either by:  executing a return from the main function  calling the exit(int) function  calling the _exit(int) function  calling the abort(void) function u When a process exits, the OS delivers a termination status to the parent process of the recently deceased process

Waiting u Parent processes often wait for their child processes to end u Parent processes do that via a wait() call  pid_t wait(int * status);  pid_t waitpid(pid_t pid, int * status, int options);

waitpid() pid_t waitpid( pid_t pid, int * status, int options); u pid can be any of 4 values: < -1:wait for any child whose pgid is the same as pid == -1:waits for any child to terminate == 0:waits for a child in the same process group as the current process  0:waits for process pid to exit

Problem Children: Orphans and Zombies u If a child process exits before it’s parent has called wait(), it would be inefficient to keep the entire child process around, since all the parent is going to want to know about is the exit status:  A zombie is an exited child process that holds nothing but the child’s exit status u If a parent dies before it’s child, the child becomes an orphan  An orphan is “adopted” by the init process (pid == 1), who will call wait() on behalf of the desceased parent when the child dies