370 likes | 647 Views
UNIX Process. Processes Commands that deal with processes UNIX System Call … using system(“…shell command…”) ; fork’n exec System Calls to manipulate your processes UNIX System Calls … accessing the kernel Process ID, Parent Process ID, Process Group ID PID,PPID,GID. The Linux Kernel .
E N D
UNIX Process • Processes • Commands that deal with processes • UNIX System Call … using system(“…shell command…”); • fork’n exec System Calls to manipulate your processes • UNIX System Calls … accessing the kernel • Process ID, Parent Process ID, Process Group ID PID,PPID,GID
The Linux Kernel • Originally 10,000 lines of C, now a bit bigger, the kernel controls access to the hardware. It allows the creation of user programs and allocates resources and keeps track of who is using what. • The first program run is called init it starts other programs, e.g that monitor consoles. A console is a screen and has a keyboard… these may be through serial ports or ethernet
The Kernel boot • The processes monitoring “serial ports” will then run a loginprogram once input is detected. • Each user who gives a known username and password is then given a shell usually bash. • (look at the file /etc/passwd… the last lines) • Once in your shell, you can launch programs
Process Management To keep track of all the processes, tables are used. Also the CPU should provide some kind of hardware protection. E.g supervisor mode and user mode. Or use “protected memory”– the aim is that a user program can not access memory or devices “owned” by the kernel, or supervisor program, sometimes called an executive in other OSes
Kernel services A Unix box can appear to be running a lot of programs at once. If requests come over the network then various net orientated programs can run in the background, the logged in user is not even aware of this. E.g. a webserver (httpd) or ftp server (ftpd) or a remote login facility (sshd or telnetd). Note the ‘d’ stands for daemon… a background program
Processes and programs A process is a program in a state of execution. Programs exist on disk or in ram but only when a program is executed is a process is created. Usually there will be more than one process per program, any input or output will cause other processes to be launched. (Unix is a multiprogramming system)
Process management • The Process table is managed by the Kernel. • It holds one “line” per process; c.f ps –l shows some of these … status,uid,pid,ppid whether in ram or swopped out and pointers to a “per process data area”. …this holds saved registers (if swopped out) and users ids for file access. These tables are not accessible by a user - they are held in kernel memory space. The user data region is where the user’s program is stored (data and instructions or just data).
User data region • Each user program requires data space… a user stack and data area (heap). And a code space. A process image is comprised of the PPDA, the user stack, user data and code. It gets swopped to disk. • Code can be read only… allows more than one user to run it at the same time. Each user has his own user data but code may be shared read only called “pure text” in Unix • Read only code need not be swopped(written) to disk, since it hasn’t changed. Unix has a third table to keep track of read-only shared code .
User mode and Kernel Mode • A process will execute instructions in the user program until some outside facility is required or an outside event, such as a clock interrupt , occurs. The CPU switches from user mode to kernel mode. • When the execution of kernel instructions is complete another process will resume. The kernel has a process schedule to look after this. A process may be suspended or blocked (waiting on an external event such as disk I/O to complete)
Processes • Process. • Running program / application. • System – e.g. csh. • User initiated – e.g. pico. • Each uniquely numbered. • Process ID pid • Command “ps” lists (see man pages)
Example of $ps -el Note init is the first process started on powerup (PID is 1) Also try out the command $top for a dynamic display. NB $ps –el | more will “page” the display • $ps • PID TTY TIME CMD • 4086 tty1 00:00:00 bash • 4138 tty1 00:00:00 ps • $ps –el|more;echo shows 14 columns ! • F S UID PID PPID C PRI …TTY …CMD • 4 S 0 1 0 0 75 ? init
Commands for process control • Try the following… look up the man pages! • $find / -name * -print & • $jobs • $fg • $bg • $ps –el |grep find • $kill –9 nnn; echo use pid of find as nnn
System calls • stdlib.h includes the function system() • Permits system commands to be run from inside a ‘C’ program. • Prototype…..int system(char *string) • Where “string” • Unix command • shell script • user program
System call - example • Calling ls with a program • system(“ls”); or • char *command = “ls”;system(command); or • int main(void{ printf(“Files in Directory\n”); system(“ls –l”); return 0;}
system() - return • system() • returns an integer • exit status of command • exit(); value • final return value in main • That is why main returns a value.
Restrictions • Restrictions on using system() • Availability of memory • Calling program remains in memory • New copy of shell and calling program loaded into RAM. • If insufficient memory an error message is displayed.
Other system calls • execl(), wait() and fork() • execl – execute an leave • process executed and then terminate • The “ls” program could have been writtenint main(void){ printf(“Files in Directory\n”); execl(“/bin/ls, “ls”, “-l” ,0); return 0;} • See man pages for details.
UNIX System Calls * System Calls - A system call is a direct entry point through which an active process can obtain services from the kernel. - The system calls are specific routines in the operating system kernel that are directly accessible to application programs.
Features of UNIX System Calls * Features of System Calls - The system calls allow you to perform low-level I/O, manipulate files and directories, create and control multiple concurrent processes, manage interrupts, and control I/O to terminals. - Because UNIX is implemented in C, its system calls are specified in C syntax and directly called from C programs. - A system call may need one or more associated header files. These header files are clearly indicated with each call described.
UNIX Process * UNIX Process - An UNIX process is an instance of a program that is being executed by the operating system. (fork system call) - From a programming point of view, a UNIX process is the entity created by the fork system call.
States of the Process (1) * Running The process is executing. * Asleep A process in this state is waiting for an event to occur. Such an event could be an I/O completion by a peripheral device, the termination of another process, the availability of date or space in a buffer, the freeing of a system resource, and so on. When a runnng process has to wait for such an event it is blocked and goes to sleep. This creates an opportunity for a context switch, shifting the CPU to another process. Later, when the event that a sleeping process is waiting for occurs, it awakens and becomes ready to run.
States of the Process (2) * Ready A process in this state is then scheduled for CPU service. * Zombie After termination of execution, a process goes into the zombie state. The process no longer exits. The data structure left behind contains its exit status and any timing statistics collected. This is always the last state of a process.
Creating a Process 1. Process In Unix, when an executable program is read into system memory by the kernel and executed, it become a process. 2. fork System Call With the exception of some initial process generated by the kernel during bootstrapping (e.g. swapper, init and pagedaemon), all processes in a Unix programming environment are created by a fork system call. 3. Parent process and Child process The initiating process in termed theparent process. The newly generated process the child process.
fork System Call 1. Syntax of fork System Call Include File(s): <sys/types.h> <unistd.h> Summary: pid_t fork (void); Return Success : 0 in child, child PID in parent Failure: -1 Set errno: yes 2. Function of fork System Call When a fork system call is made, the operating system generates a copy of the parent process which becomes the child process.
Parent /Child Process Relationship Kernel swapper PID 0 init PID 1 pagedaemon PID 2 Kernel Process Parent Kernel Process Parent/child Parent/Child Child Child Child
Child Process Unique Information 1.The operating system will pass to the child process most of the parent’s system information (e.g. open file descriptors, environment information, etc.) 2. Unique Information to the Child Process # The child has its own process ID (PID). # The child will have a different parent process ID (PPID) than its parent. # System imposed process limits (e.g., amount of CPU time the process is allotted) are reset to zero. # All record locks on files are reset. # The action to be taken when receiving signals is different.
Generating Child Process(ian2.cc) Parent: {fork(); Child/Parent printf(“Hello\n”); {printf(“Hello\n”); fork(); fork(); Printf(“Bye\n”);} Printf(“Bye\n”);} Child Child Printf(“Bye\n”); Printf(“Bye\n”);
Process ID * Process ID Associated with each process is a unique positive integer identification number called a process ID (PID). * Initial Processes generated by the Kernel - Process 0 (swapper process in BSD-based UNIX, sched process in Solaris): responsible for process scheduling. - Process 1 (init process ): basic responsibility is the managing of terminal line and who is the parent process of all other UNIX processes. - Process 2 (pagedaemon process in BSD-based UNIX, pageout process in Solaris): responsible for paging. (BSD: Berkeley Software Distribution)
getpid system Call * getpid System Call Function: to obtain the process ID Include File(s): <sys/types.h> <unistd.h> Summary: pid_t getpid(void); Return: Success- the process ID Failure- -1 Sets errno- yes * Example of getpid printf (“My PID is %d \n”, getpid( ) );
Parent Process ID * Parent Process ID The parent process is the process that forked the child process. Associated with each parent process is a unique positive integer identification number called a parent process ID (PPID). * Save the Return Child Process ID There is no system call that allows a parent process to determine the process IDs of all its child processes. if such information is needed, the parent process should save the returned child process ID value from the fork system call as each child process is created.
getppid System Call * getppid System Call Function: to obtain parent process ID Include File(s): <sys/types.h> Summary: pid_t getppid( void ); Return: Success- the parent process ID Failure- -1 Sets errno- yes * Example of getppid System Call printf(“My Parent Process ID is %d \n”, getppid( ) );
Process Group ID * Process Group When a process generates child processes, the operating system will automatically create a process group. The initial parent process is known as the process leader. * Process Group ID Every process belong to a process group that is identified by an integer number called a process group ID. The process leader’s ID will be the same as its process group ID.
getpgid System Call * getpgid System Call Function: to obtain process group ID Include Files: <sys/types.h> <unistd.h> Summary: pid_t getpgid(pid_t pid); Return: Success - the process group ID Failure - -1 Sets errno: yes errno message: #1 : invalid access permissions for the calling process #3: no such process ID as pid
Example of getpgid (ian3.cc) (1) printf(“...PID PPID GID...,...); (PID 2728 PPID 2437 GID 2437) i=0; if(fork()==0) printf(...);2729 i=1; i=1; if(fork()==0) if(fork()==0) printf(...);2733 printf(...);2730 i=2; i=2; i=2; i=2; if(fork()==0) if(fork()==0) if(fork()==0) if(fork()==0); printf(...);2735 printf(...);2734 printf(...);2732 printf(...);2731
Example of getpgit (Ian3.cc) (2) 2437 2728 2729 2733 2735 2730 2732 2734 2731
To Use Floppies: cd /mnt mkdir flop mount –t MSDOS /dev/fd0 /mnt/flopp ls –l /home mv /home/student99/* /mnt/flopp umount /dev/fd0 NEVER REMOVE WITHOUT UNMOUNTING
Exercises 2 • Use the find command to list alphabetically all 3 letter commands on your system. (hint search the path, find files with their ‘x’ bit set, use the wildcard ???. Send the output of your command into a text file called 3.txt • Put your command into a script, check it works • Write a C program that calls it using the system call • Write a C program that invokes fork three times each child can call a script. Search three different directories; /bin, /usr/bin and /usr/X11R6/bin and writethe results to three textfiles “pid.txt”