process contd
play

Process (contd.) Indranil Sen Gupta (odd section) and Mainack - PowerPoint PPT Presentation

Process (contd.) Indranil Sen Gupta (odd section) and Mainack Mondal (even section) CS39002 Spring 2019-20 So far on processes What is a process? Structure of a process Process states Process control block Context switch


  1. Process (contd.) Indranil Sen Gupta (odd section) and Mainack Mondal (even section) CS39002 Spring 2019-20

  2. So far on processes • What is a process? • Structure of a process • Process states • Process control block • Context switch

  3. Process Representation in Linux Represented by the C structure task_struct pid t pid; /* process identifier */ long state; /* state of the process */ unsigned int time slice /* scheduling information */ struct task struct *parent; /* this process’s parent */ struct list head children; /* this process’s children */ struct files struct *files; /* list of open files */ struct mm_struct *mm; /* address space of this pro */ Doubly linked list

  4. Process scheduling

  5. Process scheduling • The process scheduler selects an available process for execution on the CPU • Dispatcher: The kernel process that assigns CPU to a process

  6. Recap: Process state diagram Terminated Interrupt New exit Ready Running Scheduler Dispacher I/O or event wait I/O or event completion Waiting

  7. Recap: Process state diagram Terminated Interrupt New exit Ready Running Job queue Scheduler Dispacher I/O or event wait I/O or event completion Waiting

  8. Recap: Process state diagram Terminated Interrupt New exit Ready Running Job queue Scheduler Dispacher Ready queue I/O or event wait I/O or event completion Waiting

  9. Recap: Process state diagram Terminated Interrupt New exit Ready Running Job queue Scheduler Dispacher Ready queue I/O or event wait I/O or event completion Waiting Device queue

  10. Process scheduling • Several scheduling queues exist in OS • A PCB is linked to one of the queues at any given tome • The PCBs in a queue are connected as a linked list

  11. Structure of process queues

  12. Structure of process queues

  13. Structure of process queues

  14. Structure of process queues

  15. Structure of process queues

  16. Characteristics of process queues • Each I/O device has its own device queue • Each event also has its own queue • Process scheduling can be represented as a queueing diagram • Queueing diagram represents queues, resources, flows • We will discuss actual scheduling algorithm later

  17. Representation of process scheduling

  18. Representation of process scheduling

  19. Operations on processes

  20. Process creation During execution a process may create several new processes • • Each process has a unique process identifier (pid) • Other than the first process (init), all other processes are created by fork system call • Parent process create children processes, which, in turn create other processes, forming a tree of processes

  21. Process creation (contd.) • Address space • Child duplicate of parent • Child has a program loaded into it • UNIX example • fork() : creates a new process • exec(): replace new process’s memory with new code

  22. Process creation (contd.) • Address space • Child duplicate of parent • Child has a program loaded into it • UNIX example • fork() : creates a new process • exec(): replace new process’s memory with new code pid > 0 pid = 0

  23. Process creation example Error condition child process parent process

  24. Process termination • A child process executes last statement • exit() call for deleting the process • return status data from child to parent via wait() • Deallocate the resources

  25. Process termination • A child process executes last statement • exit() call for deleting the process • return status data from child to parent via wait() • Deallocate the resources Child process Parent process . pid_t pid; . Int status; exit(2) // Exit with status code . pid = wait (&status) // pid of terminated child

  26. Process termination: Corner cases • In some OS • All child must terminate when a process terminates • Cascading termination: All children, grandchildren etc. must be terminated • OS takes care of this cascade • Combinations of exit() and wait() • If no parent is waiting then zombie process • If parent terminated without invoking wait then orphan process

  27. Zombie and orphan process • Zombie process A process that has terminated, but who parent had not • not yet called wait() All processes move to this state when they terminate and • remain there until parent calls wait() Entry in process table removed only after calling wait() •

  28. Zombie and orphan process • Zombie process A process that has terminated, but who parent had not • not yet called wait() All processes move to this state when they terminate and • remain there until parent calls wait() Entry in process table removed only after calling wait() • • Orphan process parent terminated without invoking wait • Immediately “init” process assigned as parent • “init” periodically invokes wait() •

  29. Inter-process communication (IPC) • Processes executing concurrently in OS may be independent or cooperating • Cooperating process Affect or be affected by other processes, e.g., sharing data •

  30. Inter-process communication (IPC) • Processes executing concurrently in OS may be independent or cooperating • Cooperating process Affect or be affected by other processes, e.g., sharing data • Can share information • Speed-up in computation • Design can be modular •

  31. Inter-process communication (IPC) • Processes executing concurrently in OS may be independent or cooperating • Cooperating process Affect or be affected by other processes, e.g., sharing data • Can share information • Speed-up in computation • Design can be modular • • Cooperating processes need IPC shared memory • Message passing •

  32. Inter-process communication (IPC) • Ways to do IPC way 1: shared memory - shmget(), shmcat(), shmaddr(), • shmat(), shmdt(), shmctl() way 2: message passing (pipe) - pipe(), read(), write(), close() • way 3: message passing (named pipe) - mkfifo(), read(), • write(), close() way 4: Over network - RPC or Remote Procedure Call, • sockets

  33. Shared memory system

  34. Schematic for shared memory process A shared memory process B kernel

  35. Let’s check the function calls char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); myseg = shmat(shmid, NULL, 0); . . shmdt(myseg); . . shmctl(shmid, IPC_RMID, NULL);

  36. Let’s check the function calls char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); // create shared memory segment myseg = shmat(shmid, NULL, 0); . . shmdt(myseg); . . shmctl(shmid, IPC_RMID, NULL);

  37. Let’s check the function calls char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); // create shared memory segment myseg = shmat(shmid, NULL, 0); // attach the segment to the // address space of this process . . shmdt(myseg); . . shmctl(shmid, IPC_RMID, NULL);

  38. Let’s check the function calls char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); // create shared memory segment myseg = shmat(shmid, NULL, 0); // attach the segment to the // address space of this process . . shmdt(myseg); // detach the segment from the address space . . shmctl(shmid, IPC_RMID, NULL);

  39. Let’s check the function calls char *myseg; key_t key; int shmid; key = 235; // some unique id shmid = shmget(key, 250, IPC_CREAT | 0666); // create shared memory segment myseg = shmat(shmid, NULL, 0); // attach the segment to the // address space of this process . . shmdt(myseg); // detach the segment from the address space . . shmctl(shmid, IPC_RMID, NULL); // mark the segment to be destroyed

  40. Producer consumer problem • A producer process produces information that is consumed by the consumer process Compiler produces assembly code consumed by assembler • Program produces lines to print, print spool consumes • The information is read/write from a buffer •

  41. Producer consumer problem • A producer process produces information that is consumed by the consumer process Compiler produces assembly code consumed by assembler • Program produces lines to print, print spool consumes • The information is read/write from a buffer • • Two variants Bounded buffer • Unbounded buffer •

  42. Producer consumer problem • A producer process produces information that is consumed by the consumer process Compiler produces assembly code consumed by assembler • Program produces lines to print, print spool consumes • The information is read/write from a buffer • • Two variants Bounded buffer • Unbounded buffer • Bounded buffer : producer waits when buffer is full, • consumer waits when buffer is empty

  43. Producer consumer solution with bounded buffer Shared data: implemented as a circular array • #define BUFFER_SIZE 10 typedef struct { . . . // information to be shared in out } item; 0 item buffer[BUFFER_SIZE]; int in = 0; int out = 0; 6 6

Recommend


More recommend