process.pdf
Document Details
Uploaded by PersonalizedOctagon
Amrita Vidyalayam
Tags
Related
- OPERATING SYSTEMS SLM.pdf
- 6-Process Structures - Process creation, management in Unix-25-07-2024.pdf
- RCS 205 Operating Systems Lecture 5: Introduction to Processes PDF
- Introduction to Operating Systems PDF
- Operating Systems Chapter 3 Process Description and Control PDF
- COMP 007 Process Management PDF
Full Transcript
22AIE202 Operating Systems Process Management [email protected] 1 / 33 Process A program under execution can be reffered to as a process. The process is also known as job and is also reffered to as task. The program can contain i...
22AIE202 Operating Systems Process Management [email protected] 1 / 33 Process A program under execution can be reffered to as a process. The process is also known as job and is also reffered to as task. The program can contain instructions(code) and variables(data). The instructions and variables needs to be loaded into memory before being executed. 2 / 33 Contd.. The code that we write will be converted to hardware specific form [Machine language ]. Machine language is hugely dependant upon the processor on which the process run. Process code and data part will be loaded in memory. The top of stack will be pointed by stack pointer (SP). The program counter [PC ]/instruction pointer [ IP ]points to the next instruction to 3 / 33 run. A process in memory Text -> The code of the process. Data-> The data variables which are present prior to execution. Stack->Space to store,intermediate result,return addresses etc. Heap ->Used to allocate variable space during run 4 / 33 time. Process States Every process in the system can have a state which determines its execution pattern. One commonly used is the 5 state model. 1.New ->The process is just created. 2.Ready ->The process has all the resources for it to run but yet to get to the processor. 3.Running -> The process is executing. 4.Waiting ->The process is not running now and is waiting for some resource or event to occur. 5.Stop -> The process has finished execution. 5 / 33 5 State process model For each state a queue will be associated with. What could be the difference between a process in ready state and waiting state ? 6 / 33 7 state model Ready -suspend and blocked- suspend added. The process is contending but rather suspended. 7 / 33 Process Control Block (PCB) PCB stores details about individual process. PCB stores details about individual process. The fields may differ between different operating system. Every process will be given a unique ID /number. The program counter points to the next instruction to be executed. There could be associated priority information also. 8 / 33 PCB and states During the execution of a process it has to go through several of the states. Whenever state transition happens ,the details of the process is saved to PCB. When the process is again selected for execution the information is loaded from PCB. PCB is also stored in main memory. Most OS keep an index of PCB also. 9 / 33 Contd.. A sample scenario including two process P0 and P1. 10 / 33 Process creation Every OS sets up a new process with the help of system calls. A set of resouces including memory will be allocated for the processes. The processes created maintains a parent - child relation. In UNIX based systems,init is the first process. 11 / 33 Contd.. When a new process is created the resource allocation can have two strategies. – The child process duplicates everything from the parent. OR – A new set of code and other elements will be loaded into the address space of the child. During execution, the parent can run concurrently with the children OR The parent waits for all or most of its children 12 / 33 to finish execution. Process Termination Termination can occur normally when the process finishes the code to be executed Terminated for a child can be of two more reasons – The child exceeded the usage of some resources – The parent is exiting and the OS does not allow the child to continue without the parent. 13 / 33 Orphan and Zombie process When a parent process exits ,the child process become a Orphan process. In UNIX systems orphaned systems are adopted by init process. Zombies are those child processes which have exited. Its exit code is stored in it process table,which has to be read by the parent process. Between the time the chid exit and the parent reading the status ,the child process is known as zombie process. 14 / 33 The fork() system call fork() system call is used to create a new process in linux. The child process created will duplicate the memory contents from the parent. Both the parent and child will continue execution from the very next instruction after fork(). Since the resources are shared data variables will be accessible. fork() will return three possible values If return value is negative,process creation failed. Child process will have the return value of zero. Parent process will the a positive return value. 15 / 33 Using parent and child process The return value from fork() can be used to distinguish between parent and child process. If the parent wants to wait till the child process terminates , the parent can invoke the wait() system call. wait() system call lets the parent to get a exit signal from the child process. 16 / 33 Can you predict the output This code will run only on a Linux platform. 17 / 33 Controlling parent and child Here the return value is checked. 18 / 33 Can we load a new program In the child / parent process , a new code can be loaded by using exec() class of functions. We can give the path of a executable program. 19 / 33 ls is a program(command) available in linux to display the contents of a folder. First input is the path,2nd is the name of the program,3rd is any optional data input 20 / 33 Interprocess Communication(IPC) Every process has its own memory space. This space is not accessible to other processes. But there could be cases in which one process needs to send data to another process. – Break into subtask and share results. – Sharing common information. Most OS define mechanisms so that processes can communicate. 21 / 33 Shared Memory. A region in memory will be accessible for the processes which wants to share the data. This space is allocated in the memory region of the process which is initiating the procedure. In normal case such an access would have been blocked by the OS. The processes involved should ensure that only the data read and write does not affect the integrity of the other parts of memory. 22 / 33 23 / 33 Message passing The sharing if information is faciliated by OS. A kernel space will be given to processes which needs to communicate. This could be analogous to a mail box. Two basic functions required are send(mesg,opt),receive(mesg,opt) There could be optional parameters also. 24 / 33 The space is allotted in the OS kernel space. 25 / 33 Implementing Shared Memory in Linux using C Two seperate programs are required. One for the process which wants to write the message. Another for the process which needs to read the message. 26 / 33 A key with a value uniquely identifies the shared memory. int key=123; Use the shmget function with three aruguments to create the shared memory. – Arg 1- >key – Arg 2->Size of buffer – Agr 3 -> Code for creation Shmget() returns a positive value if success else a negative value. int shmid = shmget(key,1024,0666|IPC_CREAT); – IPC_CREAT holds a predefined value. – 0666 -> UNIX style of defining read,write,execute 27 / 33 Attach to the shared memory using shmat() Arg 1-> The id returned by shmget() Arg 2-> The address. It is a safe practice to put a zero hero so that OS finds the free space. Arg 3 -> Flag values. Four flags are defined.Its also safe to put the value as 0 to indicate all are reset. char *str = (char*) shmat(shmid,0,0); (char *) -> Explicitly indicating that the memory is of type alpha numric [ String ] Char * str->Holds the address of the shared 28 / 33 memory. After reading the data Detach our variables from shared memory using shmdt(str); Delete the shared memory using shmctl() – The function is for controlling the shared memory. Arg 1 -> The id of the shared memory. Arg2 ->The predefined value to indicate control operation.IPC_RMID to delete. Arg 3->This points to the buffer to be manipulated.For deletion it is set as NULL. shmctl(shmid,IPC_RMID,NULL); 29 / 33 Threads A unit of execution / flow of control within a process is reffered to as a thread. Modern OS support mutiple threads. Running multiple threads require hardware support also. Multi-threaded applications needs to be written is such a manner to avoid conflicts. Multiple threads improve program responsiveness,resource sharing and make the program scalable also. 30 / 33 Multiple threads share common resources. Economical than running multiple processes. 31 / 33 Although threads are a part of OS feature,the CPU still has to support it. Thread models Thread models are defined based on how user level threads interact with kernel level threads. 1.One to One model. This model may still cause delay at the kernel 32 / 33 level. 2. One to model. Since creation of individual kernel threads is resource intensive ,many OS does not support this model. 3.Many to Many This model is preferable when there is only specific number of kernel threads 33 / 33