Teaching Operating Systems With Programming and Freeware Lecture 1: Introduction, IPC and Lab1 A workshop by Dr. Junaid Ahmed Zubairi Visiting Associate.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Chapter 3 Process Description and Control
Introduction to Processes
Process Description and Control Module 1.0. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
Process Description and Control Chapter 3. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 Process Description and Control Chapter 3. 2 Process Management—Fundamental task of an OS The OS is responsible for: Allocation of resources to processes.
CSCE 351: Operating System Kernels
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CSSE Operating Systems
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.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process Description and Control
Process Description and Control. Process concepts n Definitions – replaces task, job – program in execution – entity that can be assigned to and executed.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Chapter 3 Process Description and Control Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Chapter 3 Process Description and Control
Chapter 3 Process Description and Control Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 41 Processes Chapter 4. 2 Processes  Multiprogramming operating systems are built around the concept of process (also called task).  A process.
Chapter 3 Advanced Operating System
Chapter 3 Process Description and Control
CS 390 Unix Programming Summer Unix Programming - CS 3902 Course Details Online Information Please check.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
30 October Agenda for Today Introduction and purpose of the course Introduction and purpose of the course Organization of a computer system Organization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 3 Process Description and Control Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic, N.Z.
8-Sep Operating Systems Yasir Kiani. 8-Sep Agenda for Today Review of previous lecture Process scheduling concepts Process creation and termination.
Computer Studies (AL) Operating System Process Management - Process.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
CS212: OPERATING SYSTEM Lecture 2: Process 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Politecnico di Milano © 2001 William Fornaciari Operating System Processes Lecturer: William Fornaciari Politecnico di Milano
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Operating Systems Process Creation
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Pipes Pipes are an inter-process communication mechanism that allow two or more processes to send information to each other.
1 Process Description and Control Chapter 3. 2 Requirements of an Operating System Interleave the execution of multiple processes to maximize processor.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Process Description and control G.Anuradha (Referred from william stallings and galvin 8 th edition)
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Lecture 3 Process.
Topic 3 (Textbook - Chapter 3) Processes
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 3: Process Concept
Chapter 3: Processes.
Applied Operating System Concepts
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Lecture 2: Processes Part 1
System Structure B. Ramamurthy.
Processes in Unix, Linux, and Windows
Process & its States Lecture 5.
Programming Assignment # 2 – Supplementary Discussion
Process Description and Control
Process Description and Control
Operating Systems Lecture 1.
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Presentation transcript:

Teaching Operating Systems With Programming and Freeware Lecture 1: Introduction, IPC and Lab1 A workshop by Dr. Junaid Ahmed Zubairi Visiting Associate Professor CIT, Agriculture University, Rawalpindi

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Workshop Overview Operating Systems Course Outline Topics Suited for Programming Assignments Process Model and IPC(Lab1) Concurrency Issues (Lab2) Processor Scheduling (Lab3) Disk Scheduling and RAID Programming Project

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Workshop References Operating Systems Internals and Design Principles by William Stallings, 4 th Edition Prentice Hall 2001 Modern Operating Systems by Andrew Tanenbaum Linux Programmer’s Guide by S. Goldt, S. Meer, S. Burkett and M. Welsh March 1995

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Course Outline A typical undergraduate operating systems course would include: Process and thread models Concurrency and deadlocks Uni and multiprocessor scheduling Realtime systems Memory management Disk scheduling

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 The Need for Programming Operating systems are software programs Various algorithms and mechanisms are implemented in operating systems to manage the computer The students will get a better understanding of the main concepts if they are given programming assignments Some institutions require the students to develop a full working operating system

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Topics Suited for Programming Assignments Following topics are considered suitable for programming assignments Process and thread models Concurrency issues, semaphores Deadlocks and resolution Processor scheduling Disk scheduling

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing reasonable response time Allocate resources to processes Support interprocess communication and user creation of processes

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Process Also called a task Execution of an individual program Can be traced The diagram shows a currently active process. What will be the next process to execute?

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Two-State Process Model Process may be in one of two states Running Not-running

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Not-Running Process in a Queue

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Process Creation Submission of a batch job User logs on Created to provide a service such as printing Process creates another process

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Process Termination Batch job issues Halt instruction User logs off Quit an application Error and fault conditions

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Processes Not-running ready to execute Blocked waiting for I/O Dispatcher cannot just select the process that has been in the queue the longest because it may be blocked

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 A Five-State Model Running Ready Blocked New Exit

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Using Two Queues

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Suspended Processes Processor is faster than I/O so all processes could be waiting for I/O Swap these processes to disk to free up more memory Blocked state becomes suspend state when swapped to disk Two new states Blocked, suspend Ready, suspend

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 One Suspend State

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Two Suspend States

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Reasons for Process Suspension

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Group Worksheet Please complete the group worksheet 1 and hand it over in 15 minutes. A maximum of 3 members are allowed per group

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Process Creation Assign a unique process identifier Allocate space for the process Initialize process control block Set up appropriate linkages Ex: add new process to linked list used for scheduling queue Create of expand other data structures Ex: maintain an accounting file

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Execution of the Operating System Non-process Kernel execute kernel outside of any process operating system code is executed as a separate entity that operates in privileged mode Execution Within User Processes operating system software within context of a user process process executes in privileged mode when executing operating system code

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 UNIX SVR4 Process Management Most of the operating system executes within the environment of a user process

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 UNIX Process States

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Programming Assignment 1 Lab 1 In order to understand the processes in a better way, it is recommended that the participants become familiar with UNIX/Linux user interface and then write a program that uses fork command to start several processes. We will use a red hat linux server. Please login to workshop group accounts and change your passwords.

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 UNIX/Linux User Interface UNIX/Linux user interface is simple and easy to use Mostly the user logs in to the default “bash” shell Use “ls –al” to list all directories and files Use “ls –F” to see files and directories separately

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 UNIX/Linux User Interface Use “cat filename” to see contents of a file Use “more filename” to see a file longer than a page Use “w”, “who”, and “finger” to see who else is logged on Try “chfn”

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 UNIX Utility Programs

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Processes in UNIX Process creation in UNIX.

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 POSIX Shell A highly simplified shell

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 The Editor “pico” is the best text mode editor available under Linux Pico allows you to start entering text after you reach the text insertion point using arrows Pico has some commands that can be given with control-key combinations, including the command to save and exit

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Practice Problem 1 Lab1 Using pico, type the source code given into your Linux accounts and save and exit Using “gcc”, compile and run the program (Example: gcc myforks.c –o myforks) Comment on the output of the program

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Concurrent Process Creation Example #include int sum; main() { int i; sum=0; fork(); for (i=1; i<=5; i++) { printf(“the value of i is %d\n”,i); fflush(stdout); sum+=i; } printf(“the sum is %d\n”, sum); exit(0); }

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Explanation When the program reaches the line with statement fork(), the system duplicates the process and allows both the original and duplicate processes to execute forward The original process is called “parent” and the duplicate process is called “child”

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Parent-Child Identification It is easy to identify the parent and the child. The value returned by fork() is examined. If it is zero, it is the child else it is the parent Consider the same program with the identification of parent and child

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Parent-Child Identification #include int sum; main() { int i; sum=0; if (fork()) printf("This is parent\n"); else printf("This is child\n"); for (i=1; i<=5; i++) { printf(“the value of i is %d\n”,i); fflush(stdout); sum+=i; } printf(“the sum is %d\n”, sum); exit(0); }

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Programming Assignment 2 Lab 1 Rewrite the program so that the parent and the child do different activities and display different messages on the screen. For example, parent could run a loop to display all odd integers from 1 to 100 and the child could display all even integers from 1 to 100.

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Interprocess Communication Using Pipes Processes communicate among themselves using messages, sockets and pipes In this workshop, we will learn how to use UNIX pipes for interprocess communications

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Pipes Two processes connected by a pipe

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 UNIX Pipes The most common example is the use of pipes in shell commands. For example: “ls –al | grep cc | wc –l” In this line, three commands are connected together using pipes. Let us analyze this line and all commands one by one

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Pipes “ls –al” command lists all the contents of the current directory “grep cc” searches for and outputs the lines in the result of the previous command that contain the search pattern cc “wc –l” counts the number of lines that have the search pattern cc Thus pipes connect output of one command to the input of the next command

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Unnamed Pipes The pipe that we used in shell command is the unnamed pipe We can also create unnamed pipes in our C programs with: #include int pipe(int fd[2]); Returns 2 file descriptors in the fd array. fd[0] is for read fd[1] is for write Returns 0 on successful creation of pipe, 1 otherwise.

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Reading and Writing When reading from a pipe, the unused end of the pipe (write end) is closed. if write end of the is still open and there is no data, read() will sleep until input become available. Please compile and execute the source code given using gcc (execute with./filename)

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Example of Unnamed Pipe #include #define READ 0 /* The index of the “read” end of the pipe */ #define WRITE 1 /* The index of the “write” end of the pipe */ char * phrase = “Stuff this in your pipe and smoke it”; main () { int fd[2], bytesRead; char message [100]; /* Parent process’s message buffer */ pipe ( fd ); /*Create an unnamed pipe*/ if ( fork () == 0 ) /* Child is the Writer */ {

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Example Code Continued close (fd[READ]); /* Close unused end*/ write (fd[WRITE], phrase, strlen ( phrase) +1); close (fd[WRITE]); /* Close used end*/ } else /* Parent is the Reader */ { close (fd[WRITE]); /* Close unused end*/ bytesRead = read ( fd[READ], message, 100); printf ( “Read %d bytes: %s\n”, bytesRead, message); close ( fd[READ]); /* Close used end */ }

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 IPC Using Named Pipes Named pipes are created as special files. They are also called FIFO (First-in First-out) Named pipes can be created from the shell with (for example)“mknod myfifo p” This command results in the creation of a named pipe called myfifo with a file type p (try it out)

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Named Pipes Named pipes can be created within programs by using a command: mknod ( “mypipe”, SIFIFO, 0 ); Once a named pipe is created, processes can open(), read() and write() them just like any other file. There is a difference however from normal files

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Named Pipes opens for reading will block until a process opens it for writing. opens for writing will block until a process opens it for reading. Thus the IPC can be achieved in a smooth way using named pipes Compile and run next two programs to get a feel for how the named pipes operate. It is assumed that a named pipe “mypipe” already exists

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Named Pipe Example: Writer #include char * phrase = “Stuff this in your pipe and smoke it”; int main () { int fd1; fd1 = open ( “mypipe”, O_WRONLY ); write (fd1, phrase, strlen ( phrase)+1 ); close (fd1); }

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002 Named Pipe Reader Reader #include int main () { int fd1; char buf [100]; fd1 = open ( “mypipe”, O_RDONLY ); read ( fd1, buf, 100 ); printf ( “%s\n”, buf ); close (fd1); }