1 / 122

Processes and Inter-Process Communication

Processes and Inter-Process Communication. Contents. What is a Process? Process Control Process Relationship Inter- Process Communication. What is a Process?. Program An executable file Process

barney
Download Presentation

Processes and Inter-Process Communication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Processes and Inter-Process Communication

  2. Contents • What is a Process? • Process Control • Process Relationship • Inter- Process Communication

  3. What is a Process? • Program • An executable file • Process • An instance of a program that is being executed by the OS. • Every process has a unique ID (PID).

  4. PID • Every process has a unique ID (PID). • PIDs are 16-bit numbers that are assigned sequentially by Linux as new processes are created.

  5. PID in C/C++ • In C or C++ use the pid_t type • Defined in <sys/types.h> • getpid() : obtain the PID of the process • getppid(): obtain the PID of the parent • Every process has a parent process • Printing the process ID:

  6. Process Management • The Unix OS is a time-sharing system. • Each process is represented by a task_struct data structure. • The task_vector is an array of pointers to every task_struct data structure in the system.

  7. Process Statuses • Running • The process is either running or it is ready to run. • Waiting • The process is waiting for an event or for a resource. • Stopped • The process has been stopped, usually by receiving a signal. • Zombie • This is a halted process which, for some reason, still has a task_struct data structure in the task vector.

  8. Type of Processes • Interactive Process • Initiated from (and controlled by) a shell • Daemon Process • Run in the background until required

  9. Related Commands • ps • Report process status. • pstree • Display a tree of processes. • nice • Run a program with modified scheduling priority. • renice • Alter priority of running process. • kill • Send signal to a process.

  10. Related Commands (Cont.) • top • Display top CPU processes. • jobs • List the active jobs. • bg • Place a job in background (similar to &); • fg • Place a job in foreground. • Ctrl+z • Stopped a process. • Ctrl+c • Terminate a process.

  11. Process Scheduling • Linux schedules the parent and child processes independently • Linux promises that each process will run eventually • Alter execution priority: • By default, every process has a niceness of zero. • A higher niceness value means that the process is given a lesser execution priority; • conversely, a process with a lower (that is, negative) niceness gets more execution time. • $ nice –n value command • renice command to change the niceness of a running process

  12. Viewing active processes • ps displays the processes controlled by the terminal in which ps is invoked. • -e : display all processes running on the system • -o pidd.ppidd,command : what information to show about each process

  13. Contents • What is a Process? • Process Control • Process Relationship • Inter- Process Communication

  14. Process Control • fork and vfork • exit • wait and waitpid • exec • signal • kill

  15. Creating a new process • system: simple but inefficient, has security risks • fork and exec: more complex but provides greater flexibility,speed, and security

  16. Using system • The system function in the standard C library provides an easy way to execute a command from within a program. • In fact, system creates a subprocess running the standard Bourne shell (/bin/sh) and hands the command to that shell for execution. • it’s subject to the features, limitations, and security flaws of the system’s shell

  17. Creating Processes • Using fork and exec • fork: make a child process in an exact copy of it’s parent process • exec: causes a process to become an instance of another program

  18. fork PID2 PID1 fork Child Parent

  19. fork (cont.) • int fork(); • The only way a new process is created by the Unix kernel. • The new process created is called the child process. • The child is a copy of the parent. • The child gets a copy of the parent’s data space, heap and stack. • The parent and child don’t share these portions of memory.

  20. fork (cont.) • This function is called once, but return twice. • The process ID of the new child (to the parent). • A process can have more than one child. • 0 (to the child).

  21. fork Sample main() { int pid; pid = fork(); if (pid < 0) // error else if (pid == 0) //child else //parent }

  22. fork (cont.) • We never know if the child starts executing before the parent or vice versa. • This depends on the scheduling algorithm used by the kernel.

  23. Using fork to Duplicate a Program’s Process

  24. vfork • int = vfork(); • It has the same calling sequence and same return values as fork. • The child doesn’t copy the parent data space. • The child runs in the address space of the parent. • With vfork, child runs first, then parent runs.

  25. Compile: g++ -o VForkTest VForkTest.cpp Run: VForkTest • Child Process: Global variable: 3 Stack variable: 21 Parent Process: Global variable: 3 Stack variable: 21 • The child process executed first, updated the variables which are shared between the processes and NOT unique, and then the parent process executes using variables which the child has updated.

  26. exit • Normal termination • Executing a return from the main function. • Calling the exit function. • Calling the _exit function. • Abnormal termination • Calling abort. • Receives certain signals.

  27. exit (cont.) • void exit (int state); • Sometimes we want the terminating process to be able to notify its parent how it terminated. • For the exit and _exit function this is done by passing an exit status as the argument to these two functions. • The parent of the process can obtain the termination status from either the wait or waitpid function.

  28. exit (cont.)

  29. exit() vs _exit() • The C library function exit() calls the kernel system call _exit() internally • The kernel system call _exit() will cause the kernel to close descriptors, free memory, and perform the kernel terminating process clean-up. • The C library function exit() call will flush I/O buffers and perform additional clean-up before calling _exit() internally.

  30. Termination Conditions • Parent terminate before the child • The init process becomes the parent process of any process whose parent terminated.

  31. Termination Conditions • If child terminate before the parent • If its parent is calling a wait function • the child process vanishes and its termination status is passed to its parent via the wait call

  32. Termination Conditions • Else • The kernel has to keep a certain amount of information for every terminating process. • The process that has terminated, but whose parent has not waited for it, is called zombie. • What happens if the parent does not clean up its children? They stay around in the system, as zombie processes

  33. Termination Conditions • Child terminate before the parent • The child is completely disappeared, but the parent wouldn’t be able to fetch its termination status. • The kernel has to keep a certain amount of information for every terminating process. • The process that has terminated, but whose parent has not waited for it, is called zombie.

  34. Making a zombie process HW2

  35. wait • The parent process will often want to wait until all child processes have been completed. this can be implemented with the wait() function call. • wait(): Blocks calling process until the child process terminates. If child process has already teminated, the wait() call returns immediately. if the calling process has multiple child processes, the function returns when one returns. • waitpid(): Options available to block calling process for a particular child process not the first one.

  36. wait • When a process terminates, the parent is notified by the kernel sending the parent the SIGCHLD signal. • The parent of the process can obtain the termination status from either the wait or waitpid function.

  37. wait (cont.) • The process that calls wait or waitpid can: • Block (if all of its children are still running) • Return immediately with the termination status of a child (if a child has terminated) • Return immediately with an error (if it doesn’t have any child process)

  38. wait and waitpid • int wait (int *statloc); • int waitpid (int pid, int *statloc, int options); • If statloc is not a null pointer, the termination status of the terminated process is stored in this location. • The difference between these two function: • wait can block, while waitpid has an option that prevents it from blocking. • waitpid doesn’t wait for the first child to terminate (it can control which process it waits for)

  39. exec • A process cause another program to be executed by calling one of the exec functions. • When a process calls one of the exec functions, that process is completely replaced by the new program. • The process ID doesn’t change across an exec.

  40. exec functions • int execl (char *path, char *arg0, … /*(char *) 0 */); • int execle (char *path, char *arg0, … /*(char *) 0, char *envp[] */); • int execlp (char *filename, char *arg0, … /*(char *) 0 */); • int execv (char *pathname, char *argv0[]); • int execve (char *pathname, char *argv0[], char *envp[]); • int execvp (char *filename, char *envp[]);

  41. exec functions • Functions that contain the letter p in their names (execvp and execlp) accept a program name and search for a program in the current execution path. Functions that don’t contain p must be given the full path of the program to be executed. • Functions that contain the letter v in their names (execv, execvp, and execve) accept the argument list for the new program as a NULL-terminated array of pointers to strings. • functions that contain the letter l (execl, execlp, and execle) accept the argument list using the C language’s varargs mechanism. • Functions that contain the letter e in their names (execve and execle) accept an additional argument, an array of environment variables. The argument should be a NULL-terminated array of pointers to character strings. Each character string should be of the form “VARIABLE=value”.

  42. Using fork and exec togrther

  43. signal • Signals are software interrupts. • Signals are asynchronous; when a process receives a signal, it processes the signal immediately • For communicating with and manipulating processes in Linux • Signals are defined in /usr/include/bits/signum.h • The name of signals all begin with the three character SIG : SIGABRT • include <signal.h>in your programs

  44. signal (cont.) • When a process receives a signal, it may do one of several things, depending on the signal’s disposition. • default disposition • ignore the signal • call a special signal-handler • The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

  45. signal (cont.) • The Linux system sends signals to processes in response to specific conditions. • Example of signals : • SIGBUS (bus error) • SIGSEGV (segmentation violation) • SIGFPE (floating point exception) • The default disposition for these signals it to terminate the process and produce a core file. • A process may also send a signal to another process. • For example: end another process by sending it a SIGTERM or SIGKILL

More Related