1 / 34

Process in Unix OS

Process in Unix OS. Program Vs Process. Program is just a file containing instructions and data These instructions while in execution constitute a process.

dean
Download Presentation

Process in Unix OS

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. Process in Unix OS

  2. Program Vs Process • Program is just a file containing instructions and data • These instructions while in execution constitute a process. • A process is an execution environment that consists of instructions, user-data and system-data segments as well as resources acquired at run time.

  3. Process • Each process in Unix is identified by a unique process id called pid. • Process IDs are usually 16-bit numbers that are assigned sequentially by UNIX as new processes are created. • Every process has a parent process which creates that process. • All the processes in the system are arranged in a tree like structure having init process at the root. • The parent process id (ppid) is the process id of the parent process.

  4. getpid() & getppid() system calls • To obtain current process id, use getpid() system call. • To obtain parent process id, use getppid() system call.

  5. #include <stdio.h> #include <unistd.h> #include<sys/types.h> int main() { pid_t pid,ppid; pid = getpid(); ppid = getppid(); printf("\nProcess id: %d",pid); printf("\nParent Process id: %d\n",ppid); return 0; }

  6. Run on the same window [Mayuri@localhost processes]$ ./a.out Process id: 6590 Parent Process id: 6570 [Mayuri@localhost processes]$ ./a.out Process id: 6598 Parent Process id: 6570 Run on different window [Mayuri@localhost processes]$ ./a.out Process id: 6617 Parent Process id: 6603 [Mayuri@localhost processes]$

  7. Vewing Active Processes: ps command [Mayuri@localhost processes]$ ps PID TTY TIME CMD 6603 pts/2 00:00:00 bash 6624 pts/2 00:00:00 ps

  8. Context of a process • Many processes are running in the Unix system. • Memory can not hold all the processes at the same time. • Hence the system needs to replace one process from main memory and introduce another process in its place. • The replacement of one process with another is called context-switching. • The context of a process includes all the information that the OS needs to restart a process after a context switch. Typically, this includes PC, stack, registers, executable code etc

  9. Fork() and exec() system calls • Fork() • It creates a new process which is an identical copy of an existing process. • The newly created process will contain all the instructions and data of its parent process. • Hence it executes the same parent process. • Exec() • This on the other hand re-initializes the existing process with some other designated program. • It does not create a new process. • It merely flushes the current context of a program and loads a new context (new program). • exec() call is the only way to execute programs in UNIX. In fact, the kernel boots itself using the exec() call. • fork() is the only way to create new processes in UNIX

  10. System() function call • The System() call creates a new process that will execute a designated program and context of the new process is loaded. • It is used to execute a unix command from C program • The syntax is #include <stdlib.h> int system(const char *command); • System() function is a C library function which in turn uses Unix system call to do its job. • It creates a new process for the command to be executed by using fork() system call. • Then it calls exec() system call which executes the unix command mentioned in the system() function.

  11. The following program gives the output of the command “ls –l” #include <stdlib.h> int main () { int return_value; return_value = system (“ls -l /”); return return_value; }

  12. Program Components: Arguments and Environment • An executing Unix program always receives two collection of data from the process that invoked it. • Arguments (command line arguments) • It holds user data • Environment • It holds system data • In C programs, these are in the form of array of character pointers. • The count of arguments is stored in variable “argc” and pointers to character array (arguments) are stored in argv[]. This is terminated by NULL pointer. • In addition, a global variable called “environ” that points to array of environment strings is also passed to main() function. extern char **environ; /* Environment Array */

  13. Program to see the current execution environment: it gives listing of all the environment variables # include<stdio.h> extern char **environ; int main(int argc, char *argv[]) { int i; printf("Arguments to this Program\n"); for(i=0;i<argc;i++) { printf("%s\n",argv[i]); } printf("Environment Listing of this program\n"); for(i=0;environ[i]!=NULL;i++) { printf("%s\n",environ[i]); } return 0; }

  14. Output: $./a.out 1 2 3 4 5 Arguments to this Program ./a.out 1 2 3 4 5 Environment Listing of this program ORBIT_SOCKETDIR=/tmp/orbit-Mayuri HOSTNAME=localhost.localdomain IMSETTINGS_INTEGRATE_DESKTOP=yes GPG_AGENT_INFO=/tmp/keyring-jWyvqj/gpg:0:1 TERM=xterm SHELL=/bin/bash XDG_SESSION_COOKIE=267e035611a62169555037150000000e-1315307637.293523-1578328767 HISTSIZE=1000 WINDOWID=71303171 GNOME_KEYRING_CONTROL=/tmp/keyring-jWyvqj IMSETTINGS_MODULE=none USER=Mayuri ... ... ...i

  15. getenv() function: int main(void) { char *s; s = getenv(“LOGNAME”); if(s==NULL) printf(“Variable Not Found\n”); else printf(“Value is %s \n”, s); return 0; }

  16. Exec Family • Exec system calls are a set six system call of the form execAB • execl • execv • execlp • execvp • execle • execve

  17. execl() system Call • It execute file with arguments explicitly in call. • Syntax int execl ( const char *path,/* Complete Program pathname */ const char *arg0,/* First Argument(filename) */ const char *arg1, /* Second Argument(optional) */ … /* Remaining Arguments (if any) */ (char *) NULL /* Arg list terminator */ ); /* Returns -1 on error (sets errno) */

  18. execl() system call • After the call to execl() the context of the process is overwritten. • Previous code is replaced by the code/instructions of the executable in ‘path’. • User data is also replaced with the data of the program in ‘path’ thereby reinitializing the stack. • And the new program begins to execute from its main function. • New program accesses the arguments of new program which are mentioned in execl() through its ‘argc’ and ‘argv’ arguments of the main function. • Environment pointed to by ‘environ’ is also passed to the new program.

  19. Return of execl() system call • Recall that the return address of any function is saved in the stack. • The return address is popped from the stack while a function returns. • But here the stack is reinitialized with the data of the new program and the old program’s data is lost. • So there is no way to pop the return address and hence there is no way to return from execl() call if the call is successful.

  20. execl() example to invoke user executable #include<stdio.h> #include<stdlib.h> #include<string.h> int main(int argc,char *argv[]) { int sum=0; int i; if(argc != 4) { printf("invalid argument\n"); exit(0); } for(i=0;i<argc;i++) sum = sum + atoi(argv[i]); printf("sum = %d\n",sum); } #include<stdio.h> #include<unistd.h> int main() { execl("./sum","sum","100","200","300",(char *)NULL); printf("execl call unsuccessful\n"); }

  21. execl() Example to invoke UNIX commands # include<stdio.h> # include<unistd.h> int main(int argc, char ** argv){ printf("Hello World!"); execl("/bin/echo","echo","Print","from","execl",(char *)NULL); return 0; } In the above program “Hello World!” is not printed Reason: Printf() function in C does not immediately prints the data on stdout but it buffers it till the next printf() statement or program exit.

  22. Forcefully flush the data from the buffer to stdout in the following way: int main(int argc, char ** argv){ printf("Hello World!"); fflush(stdout); execl("/bin/echo","echo","Print","from","execl",(char *)NULL); return 0; }

  23. Other exec system calls • The other exec calls are very similar to execl(). They provide the following three features that are not available in execl(). • Arguments can be put into a vector/array instead of explicitly listing them in the exec call. This feature is useful if the arguments are not known at compile time. • Searching for an executable using the value of the PATH environment variable. When this feature is used we don’t have to specify the complete path in the exec call. • Manually passing an explicit environment pointer instead of automatically using environ.

  24. Exec Family • Exec system calls are a set six system call of the form execAB. • Where ‘A’ is either ‘l’ (the arguments are directly in the call (list)) or ‘v’ (the arguments are in an array (vector)). • ‘B’ if present is either ‘p’ or ‘e’. • ‘p’ indicates that the PATH environment variable should be used to find the program to be executed. • ‘e’ indicates that a particular environment should be used which is passed as an argument.

  25. The six exec system calls • execl: execute file with arguments explicitly in call • execv : execute file with argument vector • execlp: execute file with arguments explicitly in call and PATH search • execvp: execute file with argument vector and PATH search • execle: execute file with argument list and manually passed environment pointer • execve: execute file with argument vector and manually passed environment pointer

  26. int execv ( const char *path, /* Program pathname */ char* const argv[] /* Argument vector */ ); int main(int argc,char *argv[]) { execv("./sum",argv); printf("execl call unsuccessful\n"); }

  27. int execvp ( • const char *file, /* Program filename */ • char* const argv[] /* Argument vector */ • ); • int execve ( • const char *path, /* Program pathname */ • char *const argv[], /* Argument vector */ • char *const envv[] /* Environment vector */ • );

  28. int execlp ( const char *file, /* Program filename */ const char *arg0, /*First Argument(filename) */ const char *arg1, … (char *) NULL /* Arg list terminator */ ); int execle ( const char *path, /* Program pathname */ const char *arg0, /*First Argument(filename) */ const char *arg1, … (char *) NULL, /* Arg list terminator */ char *const envv[] /* Environment vector */ );

  29. fork() system call • fork() call creates a “new” process. • The child process’ context will be the same as the parent process. • After a fork() call, two copies of the same context exist, one belonging to the child and another to the parent. • Contrast this to exec(), where a single context will exist because of child context over-writing the parent. • # include<unistd.h> • int fork(void); • /*Returns child process-ID and 0 on success and -1 on error */

  30. Return value of fork() system call • After fork() returns both the child and the parent receive the return value. • The child receives a 0 as return value from fork() and the parent receives the process-ID of the child.

  31. Program to demonstrate simple fork() usage 1# include<stdio.h> 2int main(void){ 3 printf(“************ Before Fork************\n”); 4 system(“ps”); 5 6 fork(); 7 8 printf(“************ After Fork *************\n”; 9 system(“ps”); 10 return 0; 11}

  32. output ************ Before Fork************ PID TTY TIME CMD 2967 pts/0 00:00:00 bash 2990 pts/0 00:00:00 a.out 2991 pts/0 00:00:00 ps ************ After Fork ************* ************ After Fork ************* PID TTY TIME CMD PID TTY TIME CMD 2967 pts/0 00:00:00 bash 2967 pts/0 00:00:00 bash 2990 pts/0 00:00:00 a.out 2990 pts/0 00:00:00 a.out 2992 pts/0 00:00:00 a.out 2992 pts/0 00:00:00 a.out 2993 pts/0 00:00:00 ps 2993 pts/0 00:00:00 ps 2994 pts/0 00:00:00 ps 2994 pts/0 00:00:00 ps

  33. Modified code 1# include<stdio.h> 2int main(void){ 3 int ret; 4 printf(“************ Before Fork************\n”); 5 system(“ps”); 6 7 ret=fork(); 8 if(ret==0){ 9 printf(“************ After Fork *************\n”; 10 system(“ps”); 11 } 12 else if(ret>0) 13 wait(); 14 return 0; 15}

  34. output ************ Before Fork************ PID TTY TIME CMD 2967 pts/0 00:00:00 bash 3005 pts/0 00:00:00 a.out 3006 pts/0 00:00:00 ps ************ After Fork ************* PID TTY TIME CMD 2967 pts/0 00:00:00 bash 3005 pts/0 00:00:00 a.out 3007 pts/0 00:00:00 a.out 3008 pts/0 00:00:00 ps

More Related