1 / 54

UNIT –III PROCESS AND SIGNALS

UNIT –III PROCESS AND SIGNALS. Process: What is a Process? Kernel support for process Process creation Process termination Waiting for a process exec()family Process Data File Descriptors across Processes Special Exit Cases IO Redirection User/Group ID real and effective

jfisk
Download Presentation

UNIT –III PROCESS AND SIGNALS

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. UNIT –III PROCESS AND SIGNALS

  2. Process: • What is a Process? • Kernel support for process • Process creation • Process termination • Waiting for a process • exec()family • Process Data • File Descriptors across Processes • Special Exit Cases • IO Redirection • User/Group ID real and effective • getenv/putenv, ulimit()

  3. What is a Process? • A process is the context (the information/data) maintained for an executing program. • What makes up a Process? • program code • global data( global variables ) • Stack (local variables) • open files (file descriptors)

  4. Process ,Process structure: • Every process has a unique process ID, a non-negative integer. Unique, process IDs are reused. As processes terminate, their IDs become candidates for reuse. • Process ID 0 is usually the scheduler process and is often known as the swapper. • Process ID 1 is usually the init process and is invoked by the kernel at the end of the bootstrap procedure. The init process never dies. • process ID 2 is the page daemon.

  5. In addition to the process ID, there are other identifiers for every process. The following functions return these identifiers. #include <unistd.h> pid_t getpid (void); Returns: process ID of calling process. pid_t getppid (void); Returns: parent process ID of calling process. uid_t getuid (void); Returns: real user ID of calling process.

  6. uid_t geteuid (void); Returns: effective user ID of calling process gid_t getgid (void); Returns: real group ID of calling process. gid_t getegid (void); Returns: effective group ID of calling process. • None of these functions has an error return. • Real User ID ID of user/process which started this process • Effective User ID ID of user who wrote the process’ program

  7. Unix Start Up Processes Diagram OS kernel Process 0 (sched) Process 1 (init) getty getty getty login login csh bash

  8. Kernel Support for Process • A process is the execution of a program • A process is consists of text (machine code), data and stack • Many process can run simultaneously as kernel schedules them for execution • Several processes may be instances of one program • A process reads and writes its data and stack sections, but it cannot read or write the data and stack of other processes • A process communicates with other processes and the rest of the world via systemcalls

  9. Kernel has a process table that keeps tract of all active processes • Each entry in the process table contains pointers to the text, data, stack and the U Area of a process. • All processes in UNIX system, except the very first process (process 0) which is created by the system boot code, are created by the fork system call

  10. Kernel Region Table Kernel Process Table AProcess Per Process Region Table Text File Descriptor Table Data Stack U Area

  11. Process: Region Table • Region table entries describes the attributes of the region, such as whether it contains text or data, whether it is shared or private • The extra level from the per process region table to kernel region table allows independent processes to share regions.

  12. Process: U Area • U Area is the extension of process table entry. • Fields of process table entry: • State field • User ID (UID) • Fields of U Area • Pointer to process table entry • File descriptors of all open files • Current directory and current root • I/O parameters • Process and file size limit • Kernel can directly access fields of the U Area of the executing process but not of the U Area of other processes

  13. Process Context • The context of a process is its state: • Text, data( variable), register • Process region table, U Area, • User stack and kernel stack • When executing a process, the system is said to be executing in the context of the process.

  14. Mode of Process Execution(1) • The UNIX process runs in two modes: • User mode • Can access its own instructions and data, but not kernel instruction and data • Kernel mode • Can access kernel and user instructions and data • When a process executes a system call, the execution mode of the process changes from user mode to kernel mode

  15. Process States Suspended Signal Signal Allocated to CPU Initialize Exit Zombified Idle Runnable Running Event occurs Wait on event Sleeping

  16. Process Creation: fork Function: • An existing process can create a new one by calling the fork function. #include <unistd.h> pid_tfork(void); Returns: 0 in child, process ID of child in parent, -1 on error. The new process created by fork is called the child process. This function is called once but returns twice. The only difference in the returns is that the return value in the child is 0, whereas the return value in the parent is the process ID of the new child.

  17. fork() as a diagram Parent pid = fork() Child Returns a new PID: e.g. pid == 5 pid == 0 SharedProgram DataCopied Data

  18. /* write a c program to create child process and display child , parent PID’s */ #include<stdio.h> #include<fcntl.h> main() { pid_t pid; pid=fork(); if(pid==-1) printf("failed to fork"); else if(pid==0) printf("child process:%d\n",getpid()); else printf("parent process:%d",getpid()); } Output: child process:11815 parent process:11814

  19. uses for fork: • When a process wants to duplicate itself so that the parent and child can each execute different sections of code at the same time. • When a process wants to execute a different program. This is common for shells.

  20. vfork : • Pid_t vfork(void); • The function vfork has the same calling sequence and same return values as fork. • The vfork function creates the new process, just like fork, without copying the address space of the parent into the child, as the child won't reference that address space. • vfork guarantees that the child runs first, until the child calls exec or exit. When the child calls either of these functions, the parent resumes.

  21. Process Termination: exit : A process can terminate normally in five ways: • Executing a return from the main function. • Calling the exit function. • Calling the _exit or _Exit function. • Executing a return from the start routine of the last thread in the process. • Calling the pthread_exit function from the last thread in the process. Once the process terminates, the kernel closes all the open descriptors for the process, releases the memory that it was using.

  22. Exit Functions Three functions terminate a program normally: _exit and _Exit, which return to the kernel immediately, and exit, which performs certain cleanup processing and then returns to the kernel. #include <stdlib.h> void exit( int status); void _Exit( int status); #include <unistd.h> void _exit( int status);

  23. Waiting for a process: wait and waitpid Functions: #include <sys/ wait.h> pid_t wait(int *status_p); pid_t waitpid( pid_t child_pid, int *status_p, int options); Both return: 0 process ID if OK, or -1 on error. • The wait function can block the caller until a child process terminates, whereas waitpid has an option that prevents it from blocking. • The waitpid function doesn't wait for the child that terminates first; it has a number of options that control which process it waits for.

  24. For both functions, the argument status_p is a pointer to an integer. If this argument is not a null pointer, the termination status of the terminated process is stored in the location pointed to by the argument. The interpretation of the pid argument for waitpid depends on its value: pid == -1 Waits for any child process. pid > 0 Waits for the child whose process ID equals pid. pid ==0 Waits for any child whose process group as the parent • pid < 0 Waits for any child whose process group ID equals the absolute value of child_pid(but not -1).

  25. The options constants for waitpid: WCONTINUED WNOHANG WNOTRACED The waitpid function returns the process ID of the child that terminated and stores the child's termination status in the memory location pointed to by status_p.

  26. ZOMBIE PROCESS • When a process terminates and its parent does not wait for it then it is called zombie process. • A zombie process remains in zombie state until it is cleaned up by its parent. • A zombie process does not have any code, data or stack but it still use the system’s fixed-size process table called proc structure. • Proc structure maintains the process ID , termination status of the process and the amount of CPU time taken by the process.

  27. /* Write a C program to create a Zombie process */ #include<stdio.h> main() { int pid; pid=fork(); if(0==pid) { printf("child process %d \n",getpid()); _exit(0); } else { wait(0); sleep(10); printf("parent process \n"); } }

  28. ORPHAN PROCESS • If a parent dies before its child, the child is automatically adopted by the original “init” process, PID 1. /*Write a C program that illustrates how an orphan is created. */ #include<stdio.h> main() { int pid; pid=fork();

  29. if(pid==0) { printf("I am the child process with PID:%d and my parent id is:%d\n\n",getpid(),getppid()); sleep(15); printf("I am the child process with PID:%d and my parent id:%d\n",getpid(),getppid()); } else { printf("I am the parent process with PID:%d\n \n",getpid()); printf("My child's PID is:%d\n\n",pid); sleep(1); } printf("PID %d terminates \n",getpid()); /*Both processes execute this*/ }

  30. exec Functions: • Exec replaces the current process its text, data, heap, and stack segments with a brand new program from disk. • with the exec functions, we can initiate new programs. There are six different exec functions.

  31. #include <unistd.h> int execl (const char* pathname, const char* arg, … ); • int execlp (const char* filename, const char* arg, … ); int execle (const char* pathname, const char* arg, ... , const char** env); • int execv (const char* pathname, const char** argv,…); int execvp (const char* filename, const char** argv); • int execve (const char* pathname, const char** argv, const char** env);

  32. All six return: 1 on error, no return on success. • The first difference in these functions is that the first four take a pathname argument, whereas the last two take a filename argument. When a filename argument is specified • If filename contains a slash, it is taken as a pathname. • Otherwise, the executable file is searched for in the directories specified by the PATH environment variable.

  33. The functions execl, execlp, and execle require each of the command-line arguments to the new program to be specified as separate arguments. We mark the end of the arguments with a null pointer. • For the other three functions (execv, execvp, and execve), we have to build an array of pointers to the arguments, and the address of this array is the argument to these three functions.

  34. Pipe: • The pipe system call creates a communication channel between two related processes. • Specifically, the function creates a pipe device file that serves as a temporary buffer for a calling process to read & write data with another process. • The command pipe “ | ” for connecting the standard output of one process to the standard input of another process. • It is also used in implementation of the popen and pcolse c library functions. • The pipe device file has no assigned name in any file system, thus it is called as unnamed pipe.

  35. #include<unistd.h> int pipe(int fifo[2]); The fifo argument is an array of two integers that are assigned by the pipe API. Data stored in a pipe is accessed sequentially in a first-in-first-out manner. On most unix system, a pipe is unidirectional. the return value of pipe may be 0 if the call succeeds or -1 if it fails. The possible errno values assigned by the API & their meanings are: Errno valueMeaning EFAULT The fifo argument is illegal ENFILE The system file table is full.

  36. Popen and pclose: • The popen function is used to execute a shell command within a user program. FILE* popen(const char* shell_cmd, const char* mode); The first argument shell_cmd is a character string that consist any shell command a user may execute in a Bourne shell. The second argument mode is either “r” or “w”. int pclose(FILE *fstream); pclose returns the exit status of the command being executed if it succeeds or a -1 if it fails.

  37. Signals: • Signals are software interrupts. Signals provide a way of handling asynchronous events. • Every signal has a name. These names all begin with the three characters SIG. These names are all defined by positive integer constants (the signal number) in the header <signal.h>. • No signal has a signal number of 0.

  38. Signal Function: • The simplest interface to the signal features of the UNIX System is the signal function. #include <signal.h> void (*signal(intsigno, void (*handler)(int)))(int); Returns: previous disposition of signal if OK, SIG_ERR on error. • The signo argument is just the name of the signal. • The value of handler is • the constant SIG_IGN, • the constant SIG_DFL, or • the address of a function to be called when the signal occurs.

  39. #define SIG_IGN void (*)(int)1 • #define SIG_DFL void (*)(int)0 • The SIG_IGN specifies a signal is to be ignored, which means that if the signal is generated to the process, it will be discarded without any interruption of the process. • The SIG_DFL specifies to accept the default action of a signal.

  40. Simple program to catch SIGUSR1 & SIGUSR2 static void sig_usr (int); int main(void) { if (signal(SIGUSR1, sig_usr) == SIG_ERR) err_sys("can't catch SIGUSR1"); if (signal(SIGUSR2, sig_usr) == SIG_ERR) err_sys("can't catch SIGUSR2"); for ( ; ; ) pause(); }

  41. static void sig_usr (int signo) { if (signo == SIGUSR1) printf("received SIGUSR1\n"); else if (signo == SIGUSR2) printf("received SIGUSR2\n"); else err_dump("received signal %d\n", signo); }

  42. Unreliable Signals: Unreliable signals mean that signals could get lost: a signal could occur and the process would never know about it. Also, a process had little control over a signal: a process could catch the signal or ignore it.

  43. Interrupted System Calls: • It is a system call within the kernel that is interrupted when a signal is caught. • To support this feature, the system calls are divided into two categories: the "slow" system calls and all the others. • The slow system calls are those that can block forever.

  44. Reads that can block the caller forever if data isn't present with certain file types. • Writes that can block the caller forever if the data can't be accepted immediately by these same file types. • Opens that block until some condition occurs on certain file types. • The pause function and the wait function. • Certain ioctl operations. • Some of the interprocess communication functions .

  45. kill and raise Functions: • The kill function sends a signal to a process or a group of processes. • The raise function allows a process to send a signal to itself. #include <signal.h> int kill( pid_t pid, int signo); int raise(int signo); Both return: 0 if OK,-1 on error.

  46. There are four different conditions for the pid argument to kill. pid > 0 The signal is sent to the process whose process ID is pid. pid == 0 The signal is sent to all processes whose process group ID equals the process group ID of the sender pid < 0 The signal is sent to all processes whose process group ID equals the absolute value of pid • pid == 1 The signal is sent to all processes on the system for which the sender has permission to send the signal.

  47. The super user can send a signal to any process.

  48. alarm function: • The alarm function allows us to set a timer that will expire at a specified time in the future. • When the timer expires, the SIGALRM signal is generated. #include <unistd.h> unsigned int alarm(unsigned int seconds); Returns: 0 or number of seconds until previously set alarm. The seconds value is no.of clock seconds in the future when the signal should be generated.

  49. If, when we call alarm, a previously registered alarm clock for the process has not yet expired, the number of seconds left for that alarm clock is returned as the value of this function. • If a previously registered alarm clock for the process has not yet expired and if the seconds value is 0, the previous alarm clock is canceled. The number of seconds left for that previous alarm clock is still returned as the value of the function.

  50. pause function: • The pause function suspends the calling process until a signal is caught. #include <unistd.h> int pause(void); Returns: 1 with errno set to EINTR • The only time pause returns is if a signal handler is executed and that handler returns.

More Related