1 / 38

The Environment of Unix Process

The Environment of Unix Process. The environment of a single process How the main function is called How command-line arguments are passed to the new program What the typical memory layout looks like How to allocate additional memory How the process can use environment variables

vivienne
Download Presentation

The Environment of Unix Process

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. The Environment of Unix Process • The environment of a single process • How the main function is called • How command-line arguments are passed to the new program • What the typical memory layout looks like • How to allocate additional memory • How the process can use environment variables • Different ways of process to terminate Nittida Nuansri

  2. The Environment of Unix Process • main function int main(int argc, char *argv[]); argc -- the number of command-line arguments argv -- an array of pointers to the arguments • When a C program is started by the kernel • a special start-up routine is called before the main function is called • the start-up routine takes values from the kernel • the command-line arguments • the environment • the start-up routine sets things up (and also clear things up) Nittida Nuansri

  3. The Environment of Unix Process:Process Termination • Process Termination • Normal Termination • return from main • calling exit() • calling _exit() • Abnormal Termination • calling abort() • terminated by a signal • The start-up routine calls the exit() function when the main function returns (clear things up) Nittida Nuansri

  4. The Environment of Unix Process:Process Termination • Process Termination :: exit() and _exit() functions #include <stdlib.h> void exit(int status); #include <unistd.h> void _exit(int status); • _exit() returns to the kernel immediately • exit() performs certain cleanup processing and then returns to the kernel • fclose() is called for all open streams • caused all buffered output data to be flushed Nittida Nuansri

  5. The Environment of Unix Process:Process Termination • atexit Function • A process can register up to 32 functions that are automatically called by exit() • These are called exit handlers and are registered by calling the atexit() function #include <stdlib.h> int atexit(void (* func)(void); Returns: 0 if ok, nonzero on error • The exit() function calls these functions in reverse order of their registration • Each function is called as many times as it was registered Nittida Nuansri

  6. The Environment of Unix Process:Process Termination user process user function exit handler _exit return call exit return call main function exit handler return exit _exit call call exit function return call call standard I/O cleanup return C start-up routine exit _exit exec kernel Nittida Nuansri

  7. The Environment of Unix Process • Note: • The only way a program is executed by the kernel is when one of the exec functions is called Nittida Nuansri

  8. Example of exit handlers $ a.out main is done first exit handler first exit handler second exit handler Nittida Nuansri

  9. The Environment of Unix Process • Command-Line Arguments • When a program is executed, the process that does the exec can pass command-line arguments to the new program • TO DO : Write your own program to demonstrate how to pass command-line arguments…. • And display how many arguments are passed each time • When is it useful? Nittida Nuansri

  10. The Environment of Unix Process • Environment List • Each program is also passed an environmentlist which is an array of char pointers, • Each pointer containing the address of a null-terminated string • The address of the array of pointers is contained in the global variable environ extern char **environ; Nittida Nuansri

  11. EnvironmentStrings EnvironmentStrings EnvironmentStrings EnvironmentStrings EnvironmentStrings EnvironmentStrings Environment list Environment list Environment list SHELL=/bin/sh\0 USER=xx\0 NULL NULL NULL NULL NULL NULL The Environment of Unix Process • Environment List Environment pointer environ: HOME=/home/xx\0 PATH=:/bin:/sbin\0 extern char **environ; Nittida Nuansri

  12. The Environment of Unix Process • Environment Variables #include <stdlib.h> char *getenv(char *name);’ Returns: pointer to value associated with name, NULL if not found int putenv(char *str); int setenv(char *name, char *value, int rewrite); Returns: 0 if OK, nonzero on error Nittida Nuansri

  13. The Environment of Unix Process • Memory Layout of a C Program • Historically a C program has been composed of • Text segment • Initialised data segment • Uninitialised data segment • Stack • Heap Nittida Nuansri

  14. The Environment of Unix Process: Memory Layout • Text segment • machine instructions, executed by the CPU • sharable, only a single copy needs to be in the memory for frequently executed program • read-only, to prevent a program from being accidently modifying its instructions • Initialised data segment (or data segment) • contains variables that are specifically initialised in the program e.g. int max = 99; appearing outside any function -- stored in the initialised data segment Nittida Nuansri

  15. The Environment of Unix Process: Memory Layout • Unintialised data segment • often called “bss” segment -- block started by symbol • data is initialised by the kernel to be 0 before the program starts executing e.g. long sum[100]; appear outside any function ==> stored in uninitialised data segment • Stack • stores automatic variables + information saved each time a function is called • returned address • caller’s environment information Nittida Nuansri

  16. The Environment of Unix Process: Memory Layout • Heap • Dynamic memory allocation usually takes place on the heap • Normally located between the uniintialised data segment and the stack • Write a Program with all types of variables to be stored in each of these segments Nittida Nuansri

  17. The Environment of Unix Process: Memory Layout Typical Memory Management high address command-line arguments and environment variables stack heap initialised to zero by exec uninitialised data uninitialised data read from prog file by exec low address text Nittida Nuansri

  18. The Environment of Unix Process: Memory Layout • The size(1) command reports size of the text data, bss segments, e.g $ size /bin/cc /bin/sh text data bss dec hex 81920 16384 664 98968 18298 /bin/cc 90112 16384 0 106496 1a000 /bin/sh Nittida Nuansri

  19. The Environment of Unix Process: Shared Library • A single copy of library routine is used by many processes • Reduce the size of each executable file • The library function can be replaced with new versions without having to re-link every program that uses the library • Disadvantage : may add some run-time overhead, either • when the program is first execed, or • when the library function is called Nittida Nuansri

  20. The Environment of Unix Process: Shared Library • TO DO • Write a “classic hello.c” program, and compile it • normally (cc hello.c) • using a shared library option • What are the differences, in term or size measures • Explain “why?” Nittida Nuansri

  21. The Environment of Unix Process: Memory Allcoation • Three functions for memory allocation • malloc() • calloc() • realloc() • Self-study !!!! Nittida Nuansri

  22. The Environment of Unix Process: setjmp and longjmp Functions • goto statement • Scope? • setjmp() and longjmp() • useful for handling error conditions that occur in a deeply nested function call #include <setjmp.h> int setjmp(jmp_buf env); void longjmp(jmp_buf env, int val); More Nittida Nuansri

  23. Process Control • Process Control Provided by UNIX • Creation of new processes Nittida Nuansri

  24. Process Control • Process Identifiers -- Process ID • Unique nonnegative integer • Some special processes, such as • swapper -- process ID 0 • part of the kernel known as a system process • init -- process ID 1 • a normal user process, runs with superuser privileges • bringing up a Unix system • read the system-dependent initialisation files • never dies • pagedaemon -- process ID 2 • supporting the paging of the virtual memory system Nittida Nuansri

  25. Process Control • Process Identifiers -- Other IDs • parent process ID getppid() • real user ID getuid() • effective user ID geteuid() • real group ID getgid() • effective group ID getegid) Nittida Nuansri

  26. Process Control • fork Function pid_t fork(void) • creates a new process -- child process • called once, but returns twice • the return value in the child is 0 • the return value in the parent is the process ID of the the child WHY the return values are 0 and the child PID ? (given all functions related to processes are as from the previous page)’ • both the child and parent continue executing with the instruction that follos the call to fork • the child is a copy of the parent • gets a copy of the parent’s data space, heap, and stack Nittida Nuansri

  27. Process Control • Write a program to demonstrate the fork function…. when executed, the result should be as follows $ a.out a write to standout out --- > by “write()” before fork --- > by “printf()” pid = XXX, global variable = XX, var = XX pid = XXX, global variable = XX, var = XX • When global => external variable in initialised data var => automatic variable on the stack Nittida Nuansri

  28. Process Control • Run the program several times • What is the order of the execution -- from the parent or child process first? • Why? -- reasons to support your answer • Then run the program as • $ a.out > temp.out • Study the result • Explain your result Nittida Nuansri

  29. Process Control • File Sharing -- for parent & child processes • All descriptors that are opened in the parent are duplicated in the child • Consider a process that has three different files opened for standard input, standard output and standard error. Write a diagram of file descriptors arrangement (process table, file table, etc.) on return fromfork AND explain your diagram in words. • 30 mins. Nittida Nuansri

  30. Process Control • File offset sharing and Synchronisation issue • It is important that the child and parent share the same file offset • E.g. --- assume that the parent and child write to stdout • a parent forks a child, then wait for a child to complete • if the parent has its stdout redirected (by a shell), it is essential that the parent’s file offset must be updated by the child • the child can write to stdout while the parent is waiting • the parent continue writing on the completing of the child -- its output must be appended to whaever the child wrote Nittida Nuansri

  31. Process Control • Two normal cases for handling the descriptors after a fork • The parent waits for the child to complete • parent does not need to do anything with its descriptors • when the child terminated, file offsets are updated accordingly • The parent and child each go their own way • after the fork, the parent closes the descriptors that does not need • the child does the same thing -- > not interferes with the other’s open fd • often the case with network servers Nittida Nuansri

  32. Process Control • Study the process characteristics/properties and answer the following questions • What are the properties of the parent that that are inherited by the child • What are the differences between the parent and the child • Study vfork() function, what is the different between fork() and vfork() Nittida Nuansri

  33. Process Control • Process Termination • By • return() • exit(), atexit(), _exit() • abort() • when a process receives certain signals, such as devided by zero.. Nittida Nuansri

  34. Process Control • Process Termination • The same code in the kernel is executed • closes all open filederscriptors • release memory occupied by the process • Notify the parent process, by • for nomal termination : passing an exit status (if exit and _exit is used) to the parent • for abnormal termination : the kernel generate a termination status to indicate the reason for the abnormal termiation Nittida Nuansri

  35. Process Control • Process Termination • The parent process can obtain this status from • wait() or waitpid() • If the child terminates (before the parent) and does not send a status to the parent • the kernel keeps information about all terminating processes • this information is available for the parent process (when wait() or waitpid() is called) Nittida Nuansri

  36. Process Control • wait() and waitpid() Functions • When a process is terminated • the kernel sends SIGCHLD signal to the parent • it is a synchronous notification signal • the parent can choose to • ignore, or • provide a signal handler function (is called when a signal occurs) Nittida Nuansri

  37. Process Control • wait() and waitpid() Functions • If a process calls wait() or waitpid(), it can • block (if all of its child are still running), or • return immediately with the termination status of a child (if a child has terminated and is waiting for its status to be fetched), or • return immediately with an error (if it doesn’t have any child processes) Nittida Nuansri

  38. Process Control • wait() and waitpid() Functions #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *statloc); pid_t waitpid(pid_t pid, int *statloc, int options); • wait() can block the caller until a child process terminates • waitpid() has an option that prevents it from blocking Nittida Nuansri

More Related