Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)

Similar presentations


Presentation on theme: "Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)"— Presentation transcript:

1

2 Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)

3 Slide 2 COMP 3438 System Programming UNIX Processes Overview of the Subject (COMP 3438) Overview of Unix Sys. Prog. Process/File (HW #1) Overview of Device Driver Development Character Device Driver Development (HW #2) Introduction to Block Device Driver Overview of Complier Design Lexical Analysis (HW #3) Syntax Analysis (HW #4) Part I: Unix System Programming (Device Driver Development) Part II: Compiler Design Course Organization (This Lecture is in red)

4 Slide 3 COMP 3438 System Programming UNIX Processes UNIX processes What is a process? What does a process look like in the system? When is a process created? By whom? How is a process created? In how many ways? When does a process stop? Can we wait for a process to die? What is a process called if it never die?

5 Slide 4 COMP 3438 System Programming UNIX Processes UNIX processes A process is an instance of a program in execution (the execution of the program has started but has not yet terminated). process is dynamic while program is static; A process is the basic unit for competing the resources. In particular, it is the basic active entity to CPU scheduler. When does a program become a process? The program is read into memory; A unique process ID is assigned, Appropriate information is added to the OS kernel structure, and Necessary resources to run the program code is allocated; Have a thread of execution – stream of instructions and a program counter.

6 Slide 5 COMP 3438 System Programming UNIX Processes Process image Each process has been allocated an image (also called address space, or execution environment), which contains the information needed to run the program: its current status, the program's data and stack, its program counter, and other register values; Sample layout of a program image in memory – This view is abstract!

7 Slide 6 COMP 3438 System Programming UNIX Processes Process characteristics A process has an unique ID, a thread of control, and some private data On each computer there is a hierarchy of processes related by parent-child links The process that executes the request for creation of a process is called the parent of that process, and the created process is called the child. A child process inherits all the properties of its parent when it is created

8 Slide 7 COMP 3438 System Programming UNIX Processes Process ID Process ID – integer PID Parent process ID – an integer PPID User process ID – an integer UID In UNIX, each user has a unique user ID. Each process is associated with a particular user called the owner of the process, which executes the program. The owner has certain privileges with respect to the process. Use getpid, getppid and getuid to determine the ID of the child, the parent, and the owner process.

9 Slide 8 COMP 3438 System Programming UNIX Processes Process ID: example The following program prints out its PID, its parent PID and its owner’s user ID. /* Example */ #include void main(void) { printf("Process ID: %ld\n",(long)getpid()); printf("Parent process ID: %ld\n", (long)getppid()); printf("Owner user ID: %ld\n", (long)getuid()); } Note: under POSIX, the type pid_t of the values returned by these system calls may be either an int or a long.

10 Slide 9 COMP 3438 System Programming UNIX Processes Process ID: using ps ps is the short for process status. “ps” lists your current processes PID TTY TIME CMD 1755 pts/1 00:00:17 bash 1981 pts/1 00:00:00 ps “ps –a ” lists more processes, including ones being run by other users and at other terminals (but not include the shells) “ps –l ” prints longer, more information lines, including UID, PID, PPID, process status, etc.

11 Slide 10 COMP 3438 System Programming UNIX Processes Process creation using fork() A traditional method for achieving concurrent execution in UNIX is for a process to create new processes using the fork() system call. The new process receives a copy of the contents of its parent’s address space - UNIX copies the parent’s memory image to child. UNIX kernel does the following: Allocates a new chunk of memory and kernel data structure Copies the original process into the new process Adds the new process to the set of “Ready” processes Returns control back to both processes

12 Slide 11 COMP 3438 System Programming UNIX Processes Example of fork() /* forkdemo1.c – Page 263*/ #include main() { int ret_from_fork, mypid; mypid = getpid(); printf(“Before: my pid is %d\n”, mypid); ret_from_fork = fork(); sleep(1); printf(“After: my pid is %d, fork() said %d\n”, getpid(), ret_from_fork); } Output (a possible one): $ cc forkdemo1.c –o fordemo1 $./forkdemo1 Before: my pid is 4170 After: my pid is 4170, fork() said 4171 $After: my pid is 4171, fork() said 0

13 Slide 12 COMP 3438 System Programming UNIX Processes Process creation using fork() Both parent and child continue execution at the instruction after the fork. But creation of two completely identical processes would not be very useful – we want parent and child execute different code. But how can the parent and child distinguish themselves?

14 Slide 13 COMP 3438 System Programming UNIX Processes Values returned by fork() The value returned by fork() allows the parent and the child to execute different part of the code. The fork returns 0 to the child and returns the child’s PID to the parent: /* After the fork, the parent and child output their process IDs*/ #include if ((childpid = fork()) == 0) { fprintf(stderr, "I am the child, ID = %ld\n", (long)getpid()); /* child code goes here */ } else if (childpid > 0) { fprintf(stderr, "I am the parent, ID = %ld\n", long)getpid()); /* parent code goes here */ }

15 Slide 14 COMP 3438 System Programming UNIX Processes FORK() example-1: creates a chain of n processes /* Example */ #include int i; int n; pid_t childpid; for (i = 1; i < n; ++i) if (childpid = fork() ) break; /* parent breaks out; child continues */ fprintf(stderr,"This is process %ld with parent %ld\n", (long)getpid(),(long)getppid()); 1 2 3 4

16 Slide 15 COMP 3438 System Programming UNIX Processes FORK() example-2 creates a fan of n processes /* Example */ #include int i; int n; pid_t childpid; for (i = 1; i < n; ++i) if ((childpid = fork() ) <= 0) break; /* child breaks out, parent continues */ fprintf(stderr, "This is process %ld with parent %ld\n", (long)getpid(), (long)getppid()); 4 123

17 Slide 16 COMP 3438 System Programming UNIX Processes The wait system call After fork, both parent and child proceed independently. If a parent wants to wait until the child finishes, it executes wait or waitpid. pid_t wait(int *stat); In general, the wait system call causes the caller process to pause until a child terminates or stops or until the caller receives a signal returns right away if the process has no children or if a child has already terminated or stopped but has not yet been waited for. What happen if the parent terminates first without waiting for its children? (the children are still running) - Orphan The stat is a pointer to an integer variable that stores the exit status of the child.

18 Slide 17 COMP 3438 System Programming UNIX Processes Value returned by wait() If wait returns because a child terminated, the return value is positive and is the PID of that child; Otherwise, wait returns -1 and sets errno. errno value = ECHILD indicates that there were no unwaited-for child processes; errno value = EINTR indicates that the call was interrupted by a signal.

19 Slide 18 COMP 3438 System Programming UNIX Processes WAIT() example creates a process chain Only one forked process is a child of the original process. /* Example */ #include int i; int n; pid_t childpid; int status; pid_t waitreturn; for (i = 1; i < n; ++i) if (childpid = fork()) break; while(childpid != (waitreturn = wait(&status))) if ((waitreturn == -1) && (errno != EINTR)) break; fprintf(stderr, "I am process %ld, my parent is %ld\n", (long)getpid(), (long)getppid());  How are the output messages ordered?

20 Slide 19 COMP 3438 System Programming UNIX Processes The exec system call The fork system call creates a copy of the calling process. However, many applications require the child process to execute code different from the parent’s. The exec() family of system calls provides a facility for overlaying the calling process with a new executable module exec loads a new executable into the process image, copies arguments into the process, and calls main(argc, argv). If successful, exec never returns; the calling process is completely overlaid by the new program and is started from its beginning The traditional way to use the fork-exec combination is to have the child execute the new program while the parent continues to execute the original code

21 Slide 20 COMP 3438 System Programming UNIX Processes EXEC() example creates a process to run ls -l /* Example */ #include void main(void) { pid_t childpid; int status; if ((childpid = fork()) == -1) { perror("Error in the fork"); exit(1);} else if (childpid == 0) {/*child code*/ if (execl("/usr/bin/ls", "ls","-l", NULL) < 0) {perror("Exec of ls failed"); exit(1);} } else if (childpid != wait(&status)) /* parent code */ perror("A signal occurred before child exited"); exit(0); } When can a fork call fail?

22 Slide 21 COMP 3438 System Programming UNIX Processes Variations of exec system call Six variations of the exec system call, which can be distinguished by a)the way command-line arguments and environment are passed, and b)whether a pathname has to be given for the executable. The execl calls ( execl, execlp, and execle ) pass the command-line arguments as a list of pointers The execv calls ( execv, execvp, execve ) pass the command-line arguments in an argument array They can also be classified according to (b) In all calls, in addition to the pathname, name of the executable must also be provided, either as a separate parameter or as the first element of the argument array.

23 Slide 22 COMP 3438 System Programming UNIX Processes EXEC() example creates a process to run ls -l /* Example */ #include void main(void) { pid_t childpid; int status; char *arglist[3]; arglist[0] = “ls”; arglist[1] = “-l”; arglist[2] = 0; /* null pointer*/ if ((childpid = fork()) == -1) { perror("Error in the fork"); exit(1);} else if (childpid == 0) {/*child code*/ if (execvp("ls", arglist) < 0) {perror("Exec of ls failed"); exit(1);} } else if (childpid != wait(&status)) /* parent code */ perror("A signal occurred before child exited"); exit(0); }

24 Slide 23 COMP 3438 System Programming UNIX Processes Variations of exec system call All of these functions eventually make a call to execve – the real system call. The exec family tree

25 Slide 24 COMP 3438 System Programming UNIX Processes Process termination Upon termination of a process, the OS de-allocates the resources held by the process, updates the appropriate statistics, and notifies other processes: Canceling pending timers and signals, Releasing virtual memory spaces, Releasing locks, closing open files Notifying the parent in response to a wait system call What happened if the parent of the terminating process is not currently executing a wait()? - Zombie A process can terminate either normally or abnormally.

26 Slide 25 COMP 3438 System Programming UNIX Processes Normal process termination A normal termination occurs if there was a return from main, an implicit return from main, a call to the C function exit, or a call to the _exit system call. exit calls user-defined exit handlers and may provide additional cleanup before it invokes the _ exit system call. exit takes a single, integer argument, called exit status, which will be made available to the parent process (which may be waiting). By convention, a zero means success, some non-zero value means something has gone wrong.

27 Slide 26 COMP 3438 System Programming UNIX Processes Abnormal process termination A process can terminate abnormally by calling abort, causing the SIGABRT signal to be sent to the calling process, or processing a signal that causes termination. A code dump may be produced. User-installed exit handlers will not be called upon abnormal termination.

28 Slide 27 COMP 3438 System Programming UNIX Processes Background processes Recall that the shell is a command interpreter which prompts for commands, reads the commands from standard input, forks children to execute the commands, and waits for the children to finish. A user can terminate execution of a command by ctrl-c. Most shells interpret a command line ending with & as one that should be executed by a background process When a shell creates a background process, it does not wait for the process to complete before issuing a prompt and accepting additional commands A ctrl-c does not terminate a background process. e.g., compare ls -l and ls -l &

29 Slide 28 COMP 3438 System Programming UNIX Processes Daemons A daemon is a background process that normally runs indefinitely (have a infinite loop). Unix relies on many daemon processes to perform routine tasks pageout daemon handling paging, in.rlogind handling remote login requests, the Web server daemon receiving http connection requests ftp daemon, mail daemon,... etc. How does a daemon process work? e.g. an email notification system : The following program, called simplebiff, notifies the user (called "oshacker") of pending mail by sending a ctrl-g character to standard error.

30 Slide 29 COMP 3438 System Programming UNIX Processes Daemons example  The following command starts simplebiff simplebiff & /* Example */ #include #define MAILFILE "/var/mail/oshacker" #define SLEEPTIME 10 void main(void) { int mailfd; for( ; ; ) { if ((mailfd = open(MAILFILE, O_RDONLY)) != -1) { fprintf(stderr,"%s", "\007"); close(mailfd); } sleep(SLEEPTIME); }


Download ppt "Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)"

Similar presentations


Ads by Google