Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 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

2 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…

3 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.

4 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 116432 Oct 10 17:34 myfile  by executing the seteuid(uid_t) function

5 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.

6 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

7 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

8 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

9 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

10 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

11 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”); }

12 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!

13 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

14 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?

15 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?

16 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

17 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,...);

18 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[]);

19 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

20 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);

21 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

22 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


Download ppt "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,"

Similar presentations


Ads by Google