Unix Process Management
Unix Process Management
Abstract
UNIX is a multi-tasking, multi-user operating system. This means that lots of people can run lots of tasks on the same machine, at the same time. Problems can arise if processes are very computationally expensive, and hog the Central Processing Unit (CPU). If this happens, the CPU will be constantly busy, and the system will slow down. Luckily, most tasks tend to spend a lot of time waiting for other information, so this situation does not arise that often. Therefore, UNIX processes must be managed by using a complex set of system functions and capabilities provided by the UNIX OS. Behind the scenes, as we shall see, a tangled web of events are required to insure processes synchronize their actions.
Stack segment -function temporary variables and a return address are saved here each time a function is called. Heap segment -Dynamic Memory Allocation - memory allocation requests ( i.e. malloc(), realloc(), linked lists and so on ).
Data segment -contains global variables that are initialized in the program ( i.e. int maxcount = 99 ; ) outside of any functions and those initialized by the kernel as null pointers or 0 ( i.e. Array Declarations long sum[1000]; ). Text segment -Text Segment is the read-only machine instructions that are executed by the CPU. It is the piece shared by heavyweight processes ( HWPs ).
fork()
It is used, by an existing process, to create a new process. The new process created by the fork() function is called the child process. The child gets a copy of the parents data space, heap and stack at that time. This is a COPY for the child - the parent and child do not share these portions of memory unless the COW ( copy-on-write ) mechanism is present. If it is, a full copy is not made. If either process tries to modify segment regions, the kernel makes a copy of that piece of memory only. Parent and Child do share the text ( or program instruction ) segment. Once the fork() has been executed, the parent and child go on and continue executing their responsible pieces of code with the instruction following the fork.
exec()
Following a fork() call, the child can call the exec() system function when it wants to execute a different program. Exec() actually replaces the current (child) process (its text, data, heap and stack) with a brand new program from disk that starts executing at its main() function.
wait()
is used when the parent process waits for one or multiple children to terminate before accepting another caller.
exit()
When a process terminates, either abnormally or normally, the parent is notified following the exit by the kernel.
2. when a process wants to execute a different program; it uses the exec() call. User-written Shells and scripts do this quite often.
Process Synchronization
IPC - InterProcess Communication Call Facility
UNIX provides IPC mechanisms for processes to communicate and to synchronize their actions.
Pipes
One of the oldest form of UNIX IPC are Pipes. A pipe is usually an ordinary file implemented to permit byte stream communication between two processes ( parent and child ). Normally, a pipe is created by a process via the pipe system call to implement the pipe. Secondly, that same process in turn calls the fork() system, thus allowing communication between the parent and child process.
Semaphores
A Semaphore is a IPC synchronization tool used to provide access to a single resource among multiple processes. The resource, is usually a segment of code called a processs critical section, which could be in the middle of changing variables, updating a table and so on. The critical section is put under the control of the semaphore, which uses a counter to determine its availability for access to other processors.
Structures:
1. Kernel semid_ds structure The kernel maintains a special internal data structure for each semaphore set which exists within its addressing space. This structure is of type semid_ds, and is defined in linux/sem.h as follows: /* One semid data structure for each set of semaphores in the system. */ struct semid_ds { struct ipc_perm sem_perm; /* permissions .. see ipc.h */ time_t sem_otime; /* last semop time */ time_t sem_ctime; /* last change time */ struct sem sem_base; struct wait_queue *eventn; struct wait_queue *eventz; struct sem_undo undo; ushort sem_nsems; }; /* ptr to first semaphore in array */
2. Kernel sem structure In the semid_ds structure, there exists a pointer to the base of the semaphore array itself. Each array member is of the sem structure type. It is also defined in linux/sem.h:
/* One semaphore structure for each semaphore in the system. */ struct sem { short sempid; /* pid of last operation */ ushort semval; /* current value */ ushort semncnt; /* num procs awaiting increase in semval */ ushort semzcnt; /* num procs awaiting semval = 0 */ };
Process Deadlocks
Process deadlocks are centered around the events of getting and releasing a resource; such as locking region of a file. A deadlock can occur when 2 processes are each waiting for that desired resource that the other has locked. This is why process synchronization ( i.e. semaphores ) routines are made available. They make a process wait for other processes to obtain and release their locks. When a deadlock is detected, the kernel has to choose one process to receive the error return. This can vary based on UNIX system implementations.
Process Management Commands The following are particularly useful commands, for managing processes. kill(1) Send a signal to a process, or terminate a process. ps(1) Display the status of current processes. nice(1) Run a command at low priority. top(1) Display and update information about the top CPU processes. nohup(1) Run a command immune to hangups and quits. sleep(1) Suspend execution for an interval.
Figure 1.
Notably is the kill command. It allows us to send signals** to other processes. It is just an interface to the kill() system function. This command is used to terminate a runaway background process.
** Signals are a UNIX provided capability used to notify a process that some exceptional condition has occurred. A signal is a software interrupt, backed by a routine, that can be generated by a terminal, hardware exception ( i.e. divide by 0 ) and software conditions ( i.e. when a process writes to a pipe after the reader of the pipe has terminated ).
Examples:
To decrease the priority: # nice -6 mybigjob To increase the priority: # nice --6 mybigjob The nice levels for SVR4 systems are from 0 to 39. The default is 20. To decrease the priority: has been set to 20-6, or 14. To increase the priority: In this example the level has been set to 20+6, or 26. # nice -6 mybigjob In this example the level
# nice +6 mybigjob
6799 co IW 6823 co IW 6829 co IW 6830 co S 6836 co I 6837 co I 6841 p0 I 6840 p1 I 6842 p2 S 6847 p2 O
0:01 -csh[rich] (csh) 0:00 /bin/sh /usr/bin/X11/startx 0:00 xinit /usr/lib/X11/xinit/xinitrc 0:12 X :0 0:01 twm 0:01 xclock -geometry 50x50-1+1 0:01 -sh[rich on xterm] (csh) 0:01 -sh[rich on xterm] (csh) 0:01 -sh[rich on login] (csh) 0:00 ps -elf <-heres our pf process! Figure 2.
Here are the meanings of the column titles shown above: COMMAND name of the command that issued the process PID process identification number TT controlling terminal of the process STAT state of the job TIME amount of CPU time the process has acquired so far
Xps is a tool, developed at the Technical University of Chemnitz-Zwickay, Germany, for watching and observing Unix processes ( Figure 3 ). It continuously extractsall processes of the system and displays them in a X Window. The parent-child relationsof the processes will be symbolized by lines connecting a parent with its children. Xps shows you a lot of properties of each process and allows you to send signals to a specified process or a specified process group. Additionally, there is a process state recorder implemented. Using this recorder helps in finding and investigating short term events occurring to UNIX processes.
Figure 3.
Bibliography
Ellzey, Roy S., Computer Systems Software, Science Reasearch Associates, Inc., 1987.
Xps - Xlib UNIX Process Analysis Tool, Technical University of Chemnitz-Zwickay, Germany.
http://www.infotech.tu-chemnitz.de/~dako/projects/xps/xps.html
UNIX uses several mechanisms for process synchronization, including pipes and semaphores, to coordinate the actions and resources between processes. Pipes allow byte-stream communication between processes, typically a parent and child, facilitating data transfer without interference . Semaphores are used as synchronization tools to control access to resources (critical sections) among multiple processes, preventing race conditions . By using semaphores, processes can wait for others to release resources, thus helping mitigate the risk of deadlocks where multiple processes wait indefinitely for each other to release resources .
In UNIX, the 'kill' command is used to send signals to processes, typically to terminate them. This command is linked to the kill() system function and can be used to stop processes that are unresponsive or consuming too many resources . The 'ps' command provides a snapshot of the current processes running in the system, showing details such as process ID, terminal associated, process state, CPU time consumed, and the command line used to initiate each process . Together, these commands aid in monitoring and managing processes effectively on UNIX systems .
In UNIX, the heap and stack segments are integral parts of process memory allocation. The heap is used for dynamic memory allocation, accommodating structures and objects that a process can increase or decrease as needed during execution (e.g., malloc, realloc). The stack segment contains function call information, such as temporary variables and return addresses, supporting recursive and nested functions . Memory management techniques, such as copy-on-write and segmentation, further optimize these areas by minimizing redundant data duplication and ensuring efficient memory use across active processes .
In UNIX, signals are software interrupts used to notify processes that specific events have occurred, such as an exceptional condition or a request to terminate the process. They act as inter-process communication mechanisms and are a core part of process management, enabling the handling of events like division by zero or termination requests from a terminal . Signals are typically managed by signal handlers, which define how a process should respond to specific signals, such as gracefully shutting down or reallocating resources . The 'kill' command is commonly used to send signals, altering process execution when necessary .
The Process Control Block (PCB) is essential in UNIX process management as it contains all the information needed by the kernel to manage the process scheduling and execution. This includes process state, process ID, program counter, and memory usage details, among other attributes . The PCB keeps track of CPU registers and process priority, guiding the scheduler in allocating CPU time according to priority and resource needs. It embodies the process's state at any time, enabling the system to pause, resume, and manage processes effectively, ensuring the multi-tasking environment's stability and efficiency .
The fork() system call is used to create a new process in UNIX systems. It works by duplicating the calling process, creating a separate child process that inherits the parent's data space, heap, and stack at the time of the fork operation. The child process receives a unique process ID, and both parent and child processes can continue execution from the point where fork() was called . The parent and child do not share memory segments unless the copy-on-write mechanism is present, which optimizes memory usage by sharing until changes are made .
UNIX handles process priorities using a system of nice values to determine process scheduling. Each process has a nice value that influences its scheduling priority; a lower nice value corresponds to a higher priority, and vice versa . The 'nice' command allows users to modify this value to increase or decrease the process's priority based on its impact on system resources. Users can choose to run processes with a lower priority to prevent them from interfering with other operations, or superusers can elevate priority for critical tasks . This system ensures that CPU time is allocated efficiently based on process importance and resource requirements .
Following a fork() operation, the exec() system call serves the purpose of replacing the child process's current memory image with a new program. When exec() is invoked, the current process (typically the child) loads a new program into memory, overwriting the existing data, heap, and stack segments, and starts executing from the new program's main function . This system call allows the child process to run a completely different application, providing flexibility in process execution and management .
Process IDs (PIDs) in UNIX are crucial identifiers that ensure each process can be uniquely managed and controlled. They facilitate operations such as sending signals, managing process priorities, and monitoring with commands like 'ps' and 'kill' . PIDs prevent conflicts and ensure that control signals are accurately targeted, thus maintaining system stability and effectiveness in multitasking environments. The uniqueness and non-negative integer nature of PIDs are vital for the orderly execution and management of concurrent processes in a multi-user Unix environment .
In the UNIX operating system, a program is a static, passive module stored on disk, essentially a set of instructions waiting to be executed . A process, on the other hand, is a dynamic, active entity that is created when a program is executed. It represents the instance of the program in execution and includes the program code (text segment) as well as the current activity represented by the program counter, process state, and the contents of the processor's registers . Processes also have unique process IDs and consist of multiple segments, such as the stack, heap, data, and text segments .