Chapter 3: Processes CSS503 Systems Programming

Slides:



Advertisements
Similar presentations
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Understand Process concept Process scheduling Creating.
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
CMPT 300: Operating Systems I Ch 3: Processes Dr. Mohamed Hefeeda
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
1/26/2007CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
CE Operating Systems Lecture 5 Processes. Overview of lecture In this lecture we will be looking at What is a process? Structure of a process Process.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Processes: program + execution state
Chapter 3 Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Jan 19, 2005 Chapter 3: Processes Process Concept.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Processes. Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
Computer Studies (AL) Operating System Process Management - Process.
Processes – Part I Processes – Part I. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Review on OSs Upon brief introduction of OSs,
CS212: OPERATING SYSTEM Lecture 2: Process 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
CSS430 Process Management
Lecture 4: Processes & Threads. Lecture 4 / Page 2AE4B33OSS Silberschatz, Galvin and Gagne ©2005 Contents The concept of Process Process states and life-cycle.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
CSS430 Process Management Textbook Chapter 3
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Operating System Components) These components reflect the services made available by the O.S. Process Management Memory Management I/O Device Management.
Lecture 3 Process.
Chapter 3: Processes.
Chapter 3: Process Concept
Topic 3 (Textbook - Chapter 3) Processes
Operating System Concepts
Process Management Presented By Aditya Gupta Assistant Professor
Processes Overview: Process Concept Process Scheduling
Chapter 3: Process Concept
Chapter 3: Processes.
System Structure and Process Model
Applied Operating System Concepts
Chapter 3: Processes.
System Structure and Process Model
Chapter 3 Process Management.
CGS 3763 Operating Systems Concepts Spring 2013
Chapter 4: Processes Process Concept Process Scheduling
Lecture 2: Processes Part 1
System Structure B. Ramamurthy.
Recap OS manages and arbitrates resources
CGS 3763 Operating Systems Concepts Spring 2013
Processes in Unix, Linux, and Windows
System Structure and Process Model
Operating Systems Lecture 6.
CGS 3763 Operating Systems Concepts Spring 2013
Process & its States Lecture 5.
Chapter 3: Processes.
Operating System Concepts
Process Creation Process Termination
Chapter 3: Processes.
Processes in Unix and Windows
Process Description and Control in Unix
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
EECE.4810/EECE.5730 Operating Systems
Process Description and Control in Unix
Chapter 3: Process Concept
Chapter 1: Introduction CSS503 Systems Programming
Processes August 10, 2019 OS:Processes.
Presentation transcript:

Chapter 3: Processes CSS503 Systems Programming Prof. Munehiro Fukuda Computing & Software Systems University of Washington Bothell CSS503 Chapter 3: Processes

Process Concept Process – a program in execution; process execution must progress in sequential fashion. Textbook uses the terms job and process almost interchangeably. A process includes: Program counter Stack (local variables) Data section (global data) Text (code) Heap (dynamic data) Files (cin, cout, cerr, other file descriptors) text global data heap stack CSS503 Chapter 3: Processes

Process Control Block Process ID CPU status Memory limits UID GID EUID EGID Directory Entry Process ID CPU status Memory limits List of open files TTY code Signal Dispatch Table heap Memory Map stdin priority 1 stdout Intr. mask 2 stderr registers stack 3 CPU Status File Descriptors trek.txt CSS503 Chapter 3: Processes

Process Status user running kernel running zombie asleep ready new user mode memory system call system call interface Interrupt return kernel mode interrupt kernel running kill/exit zombie Interrupt return PCB 5 exit( ) signal sleep schedule wait queue ready queue asleep ready wakeup PCB 3 PCB 4 PCB 1 PCB 2 memory new disk process program CSS503 Chapter 3: Processes

Process Scheduling Short-term scheduler: picks up a process from ready queue every 100ms Long-term scheduler: swaps I/O waiting processes in and out of memory CSS503 Chapter 3: Processes

Process Creation Parent process creates children processes. Resource sharing Resource inherited by children: file descriptors, shared memory and system queues Resource not inherited by children: address space Execution Parent and children execute concurrently. Parent waits by wait system call until children terminate. UNIX examples fork system call creates new process. execlp system call used after a fork to replace the process’ memory space with a new program. CSS503 Chapter 3: Processes

C Program Forking Separate Process parent #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { int pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls","ls",NULL); else { /* parent process */ /* parent will wait for the child to complete */ wait(NULL); printf("Child Complete"); exit(0); a.out child duplicated a.out ls synchronized CSS503 Chapter 3: Processes

Process Termination Process termination occurs when It executes the last statement It executes exit system call explicitly Upon process termination Termination code is passed from child (via exit) to parent (via wait). Process’ resources are deallocated by OS. Parent may terminate execution of children processes (via kill) when Child has exceeded allocated resources. Task assigned to child is no longer required. Parent is exiting (cascading termination). Some operating system does not allow child to continue if its parent terminates. CSS503 Chapter 3: Processes

Cooperating Processes Process independency: Processes belonging to a different user does not affect each other unless they give each other some access permissions Process Cooperation: Processes spawned from the same user process share some resources and communicate with each other through them (e.g., shared memory, message queues, pipes, and files) Advantages of process cooperation Information sharing: (sharing files) Computation speed-up: (parallel programming) Modularity: (like who | wc –l, one process lists current users and another counts the number of users.) Convenience: (net-surfing while working on programming with emacs and g++) CSS503 Chapter 3: Processes

Communication Models Message passing Shared memory CSS503 Process A Process A (1) system call int *data = (int *)shmat( shmget( …) )); data = 12345; write( sd, buf, size ); msgsnd( msgid, buf, size, 0 ); (1) assignment statement Process B Process B int *data = (int *)shmat( shmget( …) )); int myVariable = data; read( sd, buf, size ); msgrcv( msgid, buf, size, 0, 0 ); (2) system call (2) assignment statement Shared pages data Kernel Kernel I/O Buffer CSS503 Chapter 3: Processes

Discussion 1 Discuss about the difference between Unix process fork( ) and Windows CreateProcess( ). Describe the actions taken by a kernel to context-switch between processes. Summarize the pros and cons of shared memory versus message passing. CSS503 Chapter 3: Processes

Shared Memory Producer-Consumer Problem int main( int argc, char *argv[] ) { // allocate 1024 ints to a shared region int shmid = shmget( 5678, sizeof( Queue ), SHM_R | SHM_W | IPC_CREAT ); Queue *queue = (Queue *)shmat( shmid, 0, 0 ); queue->init( ); // since we don't use "new", we need this method if ( fork( ) == 0 ) { // child (consumer) for ( int i = 0; ; i++ ) { int job = queue->get( ); cout << job << endl; if ( job != i ) { cout << "NAH!!!" << endl; exit( -1 ); } else { // parent (producer) for ( int i = 0; ; i++ ) queue->put( i ); #include <sys/shm.h> // shmget, shmat #include <unistd.h> // fork, getpid #include <stdlib.h> // exit #include <iostream> // cout, endl #define SIZE 10 using namespace std; class Queue { private: int jobs[SIZE]; int count, nextIn, nextOut; public: void init( ) { // behave like a constructor count = nextIn = nextOut = 0; } void put( int job ) { // producer places a new job while ( count == SIZE ); // wait while queue is full ++count; jobs[nextIn] = job; nextIn = ( nextIn + 1 ) % SIZE; int get( ) { // consumer picks up a next job while ( count == 0 ); // wait while queue is empty --count; int job = jobs[nextOut]; nextOut = ( nextOut + 1 ) % SIZE; return job; }; parent (producer) child (consumer) fork nextIn nextOut int[10]: count CSS503 Chapter 3: Processes

Message Passing Direct Communication Indirect Communication Messages are directed and received from mailboxes (also referred to as ports). Each mailbox has a unique id. Processes can communicate only if they share a mailbox. Processes must know only a mailbox id. They do not need to locate their partners Example: message queue Processes must name each other explicitly: send (P, message) – send a message to process P receive(Q, message) – receive a message from process Q How can a process locate its partner to communicate with? Processes are created and terminated dynamically and thus a partner process may have gone. Direct communication takes place between a parent and its child process in many cases. Example: pipe CSS503 Chapter 3: Processes

Message Passing Producer-Consumer Problem who | wc -l who pipe wc -l mfukuda tty1 Apr 1 14:14 stiber tty2 Apr 2 15:19 ksung tty3 Apr 2 15:30 Output: 3 Producer process: who produces a list of current users. Consumer process wc receives it for counting #users. Communication link: OS provides a pipe. CSS503 Chapter 3: Processes

Direct Communication Example: Pipe int main( void ) { int n, fd[2]; int pid; char line[MAXLINE]; if (pipe(fd) < 0 ) // 1: pipe created perror( “pipe error” ); else if ( (pid = fork( ) ) < 0 ) // 2: child forked perror( “fork error” ); else if ( pid > 0 ) { // parent close( fd[0] ); // 3: parent’s fd[0] closed write( fd[1], “hello world\n”, 12 ); } else { // child close( fd[1] ); // 4: child’s fd[1] closed n = read( fd[0], line, MAXLINE ); write( 1, line, n ); } exit( 0 ); child fd[0], fd[1] 2 parent fd[0], fd[1] pipe 1 3 4 CSS503 Chapter 3: Processes

Indirect Communication Example: Message Queues struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); strcpy( message_body.mtext, “hello world\n” ); msgsnd( msgid, &message_body, 512, 0 ); } struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); msgrcv( msgid, &message_body, 512, 0, 0 ); cout << message_body.mtext << endl; } Message queue (id = msgid) 1 2 Some other process can enqueue and dequeue a message CSS503 Chapter 3: Processes

Programming Assignment 1 Convex Hull (2) Graham’s Scan P0 P4 (next) P3 (last1) P2 (last2) P5 (3) Divide-and-Conquer-based Convex-Hull Program CSS503 Chapter 3: Processes

Programming Assignment 1 Dividing Phase Process 0 fork duplicates data just call divide_and_conquer( ) fork( ) and call divide_and_conquer( ) Process 0 Process 1 fork duplicates data fork duplicates data just call divide_and_conquer( ) just call divide_and_conquer( ) fork( ) and call divide_and_conquer( ) fork( ) and call divide_and_conquer( ) Process 0 Process 2 Process 1 Process 3 CSS503 Chapter 3: Processes

Programming Assignment 1 Conquering Phase Process 0 read( fd[0], hull, size ) just return from divide_and_conquer( ) pipe( ) write( fd[1], hull, size ) Process 0 Process 1 read( fd[0], hull, size ) read( fd[0], hull, size ) just return from divide_and_conquer( ) just return from divide_and_conquer( ) pipe( ) pipe( ) write( fd[1], hull, size ) write( fd[1], hull, size ) Process 0 Process 2 Process 1 Process 3 CSS503 Chapter 3: Processes

Discussion 2 In Unix, the first process is called init. All the others are descendants of “init”. The init process spawns a telnetd process that detects a new telnet connection. Upon a new connection, telnetd spawns a login process that then overloads a shell on it when a user successfully log in the system. Now, assume that the user types who | grep mfukuda | wc –l. Draw a process tree from init to those three commands. Add fork, exec, wait, and pipe system calls between any two processes affecting each other. Consider four different types of inter-process communication. Pipe: implemented with pipe, read, and write Socket: implemented with socket, read, and write Shared memory: implemented shmget, shmat, and memory read/write Shared message queue: implemented with msgget, msgsnd, and msgrcv Which types are based on direct communication? Which types of communication do not require parent/child process relationship? If we code a produce/consumer program, which types of communication require us to implement process synchronization? Which types of communication can be used to communicate with a process running on a remote computers? Which types of communication must use file descriptors? Which types of communication need a specific data structure when transferring data? CSS503 Chapter 3: Processes