Teaching Operating Systems With Programming and Freeware Lecture 1: Introduction, IPC and Lab1 A...

55
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
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    212
  • download

    0

Transcript of Teaching Operating Systems With Programming and Freeware Lecture 1: Introduction, IPC and Lab1 A...

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

Teaching Operating Systems With Programming and FreewareLecture 1: Introduction, IPC and Lab1

A workshop by

Dr. Junaid Ahmed ZubairiVisiting Associate Professor

CIT, Agriculture University, Rawalpindi

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Workshop References

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Course Outline

A typical undergraduate operating systems course would include:Process and thread modelsConcurrency and deadlocksUni and multiprocessor schedulingRealtime systemsMemory managementDisk scheduling

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

The Need for Programming

Operating systems are software programsVarious algorithms and mechanisms are implemented in operating systems to manage the computerThe students will get a better understanding of the main concepts if they are given programming assignmentsSome institutions require the students to develop a full working operating system

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Requirements of anOperating 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

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

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?

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Two-State Process Model

Process may be in one of two statesRunningNot-running

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Not-Running Process in a Queue

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

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

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Processes

Not-running ready to execute

Blockedwaiting for I/O

Dispatcher cannot just select the process that has been in the queue the longest because it may be blocked

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

A Five-State Model

Running

Ready

Blocked

New

Exit

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Using Two Queues

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

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/OSwap these processes to disk to free up more memoryBlocked state becomes suspend state when swapped to diskTwo new states Blocked, suspend Ready, suspend

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

One Suspend State

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Two Suspend States

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Reasons for Process Suspension

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Process Creation

Assign a unique process identifierAllocate space for the processInitialize process control blockSet up appropriate linkagesEx: add new process to linked list used for

scheduling queue

Create of expand other data structuresEx: maintain an accounting file

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

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

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

UNIX Process States

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

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.

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

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

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

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”

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

UNIX Utility Programs

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Processes in UNIX

Process creation in UNIX.

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

POSIX Shell

A highly simplified shell

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

The Editor

“pico” is the best text mode editor available under LinuxPico allows you to start entering text after you reach the text insertion point using arrowsPico has some commands that can be given with control-key combinations, including the command to save and exit

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Concurrent Process Creation Example

#include <stdlib.h>#include <stdio.h>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);}

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

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”

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Parent-Child Identification#include <stdlib.h>#include <stdio.h>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);}

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

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.

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Pipes

Two processes connected by a pipe

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

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

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

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 ccThus pipes connect output of one command to the input of the next command

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Unnamed Pipes

The pipe that we used in shell command is the unnamed pipeWe can also create unnamed pipes in our C programs with:#include <unistd.h>int pipe(int fd[2]);Returns 2 file descriptors in the fd array.fd[0] is for readfd[1] is for writeReturns 0 on successful creation of pipe, 1 otherwise.

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

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)

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Example of Unnamed Pipe

#include <stdio.h>#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 */{

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

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 */} }

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

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)

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

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

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

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

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Named Pipe Example: Writer

#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>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);}

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

Operating Systems Workshop CIT, Arid Agriculture University Aug 2002

Named Pipe Reader

Reader#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main (){ int fd1;char buf [100]; fd1 = open ( “mypipe”, O_RDONLY ); read ( fd1, buf, 100 ); printf ( “%s\n”, buf ); close (fd1);}