UNIX Process Processes Commands that deal with processes

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Chapter 3 Process Description and Control
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Using tcpdump. tcpdump is a powerful tool that allows us to sniff network packets and make some statistical analysis out of those dumps. tcpdump operates.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Process The Process A process is.
Lesson 10-Controlling User Processes. Overview Managing and processing processes. Managing jobs. Exiting/quitting when jobs have been stopped.
1 Introduction to UNIX Ke Liu
6/9/2015B.Ramamurthy1 Process Description and Control B.Ramamurthy.
CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
Process Description and Control
Page 1 Processes and Threads Chapter 2. Page 2 Processes The Process Model Multiprogramming of four programs Conceptual model of 4 independent, sequential.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
Introduction to Kernel
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Processes & Daemons Chapter IV / Part III. Commands Internal commands: alias, cd, echo, pwd, time External commands, code is in a file: grep, ls, more.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Process Description and Control A process is sometimes called a task, it is a program in execution.
University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
UNIX Processes. The UNIX Process A process is an instance of a program in execution. Created by another parent process as its child. One process can be.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Introduction to Processes CS Intoduction to Operating Systems.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
Chapter 3 Process Description and Control
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 6 System Calls OS System.
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.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
Hands On UNIX II Dorcas Muthoni. Processes A running instance of a program is called a "process" Identified by a numeric process id (pid)‏  unique while.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
Processes Dr. Yingwu Zhu. Process Concept Process – a program in execution – What is not a process? -- program on a disk - a process is an active object,
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
1 Lecture 6 Introduction to Process Management COP 3353 Introduction to UNIX.
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
...looking a bit closer under the hood
Introduction to Kernel
...looking a bit closer under the hood
Chapter 3: Processes.
Process Management Process Concept Why only the global variables?
Hands On UNIX AfNOG 2010 Kigali, Rwanda
Protection of System Resources
Chapter 2: System Structures
UNIX System Overview.
Processes A process is a running program.
Hands On UNIX AfNOG X Cairo, Egypt
Structure of Processes
System Structure and Process Model
...looking a bit closer under the hood
CSC 140: Introduction to IT
Process Description and Control
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Process Description and Control
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
Process Description and Control in Unix
Process Description and Control in Unix
Lecture 6 Introduction to Process Management
Presentation transcript:

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 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

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. Each uniquely numbered. 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 $ps PID TTY TIME CMD 4086 tty1 00:00:00 bash 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() That is why main returns a value. 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 written int 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 the parent 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

NEVER REMOVE WITHOUT UNMOUNTING 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”