The Environment of Unix Process

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Chapter 7 Process Environment Chien-Chung Shen CIS, UD
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Advanced Programming in the UNIX Environment Hop Lee.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
1 Logging in to a UNIX System init ( Process ID 1 created by the kernel at bootstrap ) spawns getty for every terminal device invokes our login shell terminal.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
System calls for Process management Process creation, termination, waiting.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Section 8: Processes What is a process Creating processes Fork-Exec
Protection of System Resources
Using Processes.
Unix Process Management
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
System Structure and Process Model
System Structure and Process Model
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
Unix Process Course code: 10CS62 Prepared by : Department of CSE.
Chapter 14 - Advanced C Topics
More examples How many processes does this piece of code create?
Processes in Unix, Linux, and Windows
System Structure and Process Model
Operating Systems Lecture 6.
Memory Allocation CS 217.
Process Creation Process Termination
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Lecture 6: Multiprogramming and Context Switching
CSCI 380: Operating Systems William Killian
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

The Environment of Unix Process 2/19/2019 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 2/19/2019 Nittida Nuansri

The Environment of Unix Process 2/19/2019 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) 2/19/2019 Nittida Nuansri

The Environment of Unix Process: 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) 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

The Environment of Unix Process: Process Termination 2/19/2019 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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

Example of exit handlers $ a.out main is done first exit handler second exit handler 2/19/2019 Nittida Nuansri

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? 2/19/2019 Nittida Nuansri

The Environment of Unix Process Environment List Each program is also passed an environment list 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; 2/19/2019 Nittida Nuansri

The Environment of Unix Process 2/19/2019 The Environment of Unix Process Environment List Environment list Environment Strings NULL SHELL=/bin/sh\0 Environment Strings NULL Environment list Environment Strings NULL USER=xx\0 Environment Strings NULL Environment Strings NULL Environment list Environment Strings NULL Environment pointer environ: HOME=/home/xx\0 PATH=:/bin:/sbin\0 extern char **environ; 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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?” 2/19/2019 Nittida Nuansri

The Environment of Unix Process: Memory Allcoation Three functions for memory allocation malloc() calloc() realloc() Self-study !!!! 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

Process Control Process Control Provided by UNIX Creation of new processes 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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) 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 When global => external variable in initialised data var => automatic variable on the stack 2/19/2019 Nittida Nuansri

Process Control Run the program several times Then run the program as 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 2/19/2019 Nittida Nuansri

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 from fork AND explain your diagram in words. 30 mins. 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri

2/19/2019 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() 2/19/2019 Nittida Nuansri

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

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 2/19/2019 Nittida Nuansri

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) 2/19/2019 Nittida Nuansri

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) 2/19/2019 Nittida Nuansri

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) 2/19/2019 Nittida Nuansri

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 2/19/2019 Nittida Nuansri