Presentation is loading. Please wait.

Presentation is loading. Please wait.

EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1 UNIX Process 1. Processes 2. Commands that deal with processes 3. UNIX System Call … using system(“…shell.

Similar presentations


Presentation on theme: "EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1 UNIX Process 1. Processes 2. Commands that deal with processes 3. UNIX System Call … using system(“…shell."— Presentation transcript:

1 EEE522: Ian McCrum: 1 UNIX Process 1. Processes 2. Commands that deal with processes 3. UNIX System Call … using system(“…shell command…”); 4. fork’n exec System Calls to manipulate your processes 5. UNIX System Calls … accessing the kernel 6. Process ID, Parent Process ID, Process Group ID PID,PPID,GID

2 EEE522: Ian McCrum: 2 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

3 EEE522: Ian McCrum: 3 The Kernel boot The processes monitoring “serial ports” will then run a login program 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

4 EEE522: Ian McCrum: 4 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

5 EEE522: Ian McCrum: 5 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

6 EEE522: Ian McCrum: 6 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)

7 EEE522: Ian McCrum: 7 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).

8 EEE522: Ian McCrum: 8 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.

9 EEE522: Ian McCrum: 9 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)

10 EEE522: Ian McCrum: 10 Processes u Process. Running program / application. –System – e.g. csh. –User initiated – e.g. pico. u Each uniquely numbered. Process IDpid Command “ps” lists(see man pages)

11 EEE522: Ian McCrum: 11 Example of $ps -el u $ps u PID TTY TIME CMD u 4086 tty1 00:00:00 bash u 4138 tty1 00:00:00 ps u $ps –el|more;echo shows 14 columns ! u F S UID PID PPID C PRI …TTY …CMD u 4 S ? init 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

12 EEE522: Ian McCrum: 12 Commands for process control u Try the following… look up the man pages! u $find / -name * -print & u $jobs u $fg u $bg u $ps –el |grep find u $kill –9 nnn; echo use pid of find as nnn

13 EEE522: Ian McCrum: 13 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

14 EEE522: Ian McCrum: 14 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; }

15 EEE522: Ian McCrum: 15 system() - return u system() returns an integer exit status of command exit();value final return value in main u That is why main returns a value.

16 EEE522: Ian McCrum: 16 Restrictions u Restrictions on using system() u 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.

17 EEE522: Ian McCrum: 17 Other system calls u execl(), wait() and fork() u execl – execute an leave process executed and then terminate The “ls” program could have been written int main(void) { printf(“Files in Directory\n”); execl(“/bin/ls, “ls”, “-l”,0); return 0; } u See man pages for details.

18 EEE522: Ian McCrum: 18 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.

19 EEE522: Ian McCrum: 19 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.

20 EEE522: Ian McCrum: 20 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.

21 EEE522: Ian McCrum: 21 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.

22 EEE522: Ian McCrum: 22 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.

23 EEE522: Ian McCrum: 23 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 the parent process. The newly generated process the child process.

24 EEE522: Ian McCrum: 24 fork System Call 1. Syntax of fork System Call Include File(s): 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.

25 EEE522: Ian McCrum: 25 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

26 EEE522: Ian McCrum: 26 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.

27 EEE522: Ian McCrum: 27 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”);

28 EEE522: Ian McCrum: 28 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)

29 EEE522: Ian McCrum: 29 getpid system Call * getpid System Call Function: to obtain the process ID Include File(s): 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( ) );

30 EEE522: Ian McCrum: 30 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.

31 EEE522: Ian McCrum: 31 getppid System Call * getppid System Call Function: to obtain parent process ID Include File(s): 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( ) );

32 EEE522: Ian McCrum: 32 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.

33 EEE522: Ian McCrum: 33 getpgid System Call * getpgid System Call Function: to obtain process group ID Include Files: 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

34 EEE522: Ian McCrum: 34 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; if(fork()==0) printf(...);2733 printf(...);2730 i=2; i=2; if(fork()==0) if(fork()==0) if(fork()==0) if(fork()==0); printf(...);2735 printf(...);2734 printf(...);2732 printf(...);2731

35 EEE522: Ian McCrum: 35 Example of getpgit (Ian3.cc) (2)

36 EEE522: Ian McCrum: 36 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

37 EEE522: Ian McCrum: 37 Exercises 2 1. 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 2. Put your command into a script, check it works 3. Write a C program that calls it using the system call 4. 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”


Download ppt "EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1 UNIX Process 1. Processes 2. Commands that deal with processes 3. UNIX System Call … using system(“…shell."

Similar presentations


Ads by Google