Title: Teaching%20Operating%20Systems%20With%20Programming%20and%20Freeware%20Lecture%201:%20Introduction,%20IPC%20and%20Lab1
1Teaching Operating Systems With Programming and
FreewareLecture 1 Introduction, IPC and Lab1
- A workshop by
- Dr. Junaid Ahmed Zubairi
- Visiting Associate Professor
- CIT, Agriculture University, Rawalpindi
2Workshop 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
3Workshop References
- Operating Systems Internals and Design Principles
by William Stallings, 4th Edition Prentice Hall
2001 - Modern Operating Systems by Andrew Tanenbaum
- Linux Programmers Guide by S. Goldt, S. Meer,
S. Burkett and M. Welsh March 1995
4Course 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
5The 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
6Topics 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
7Requirements 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
8Process
- 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?
9(No Transcript)
10Two-State Process Model
- Process may be in one of two states
- Running
- Not-running
11Not-Running Process in a Queue
12Process Creation
- Submission of a batch job
- User logs on
- Created to provide a service such as printing
- Process creates another process
13Process Termination
- Batch job issues Halt instruction
- User logs off
- Quit an application
- Error and fault conditions
14Processes
- 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
15A Five-State Model
- Running
- Ready
- Blocked
- New
- Exit
16(No Transcript)
17Using Two Queues
18(No Transcript)
19Suspended 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
20One Suspend State
21Two Suspend States
22Reasons for Process Suspension
23Group Worksheet
- Please complete the group worksheet 1 and hand it
over in 15 minutes. A maximum of 3 members are
allowed per group
24(No Transcript)
25Process 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
26Execution 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
27UNIX SVR4 Process Management
- Most of the operating system executes within the
environment of a user process
28UNIX Process States
29(No Transcript)
30Programming 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.
31UNIX/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
32UNIX/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
33UNIX Utility Programs
34Processes in UNIX
- Process creation in UNIX.
35POSIX Shell
- A highly simplified shell
36The 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
37Practice 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
38Concurrent Process Creation Example
- include ltstdlib.hgt
- include ltstdio.hgt
- int sum
- main()
- int i
- sum0
- fork()
- for (i1 ilt5 i)
- printf(the value of i is d\n,i)
- fflush(stdout)
- sumi
-
- printf(the sum is d\n, sum)
- exit(0)
39Explanation
- 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
40Parent-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
41Parent-Child Identification
- include ltstdlib.hgt
- include ltstdio.hgt
- int sum
- main()
- int i
- sum0
- if (fork()) printf("This is parent\n")
- else printf("This is child\n")
- for (i1 ilt5 i)
- printf(the value of i is d\n,i)
- fflush(stdout)
- sumi
-
- printf(the sum is d\n, sum)
- exit(0)
-
42Programming 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.
43Interprocess 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
44Pipes
- Two processes connected by a pipe
45UNIX 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
46Pipes
- 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
47Unnamed 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 ltunistd.hgt
- int pipe(int fd2)
- Returns 2 file descriptors in the fd array.
- fd0 is for read
- fd1 is for write
- Returns 0 on successful creation of pipe, 1
otherwise.
48Reading 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)
49Example of Unnamed Pipe
- include ltstdio.hgt
- 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 fd2, bytesRead
- char message 100 / Parent processs message
buffer / - pipe ( fd ) /Create an unnamed pipe/
- if ( fork () 0 ) / Child is the Writer /
-
50Example Code Continued
- close (fdREAD) / Close unused end/
- write (fdWRITE, phrase, strlen ( phrase) 1)
- close (fdWRITE) / Close used end/
-
- else / Parent is the Reader /
-
- close (fdWRITE) / Close unused end/
- bytesRead read ( fdREAD, message, 100)
- printf ( Read d bytes s\n, bytesRead,
message) - close ( fdREAD) / Close used end /
-
-
51IPC 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)
52Named 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
53Named 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
54Named Pipe Example Writer
- include ltstdio.hgt
- include ltsys/types.hgt
- include ltsys/stat.hgt
- include ltfcntl.hgt
- 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) -
55Named Pipe Reader
- Reader
- include ltstdio.hgt
- include ltsys/types.hgt
- include ltsys/stat.hgt
- include ltfcntl.hgt
- int main ()
-
- int fd1
- char buf 100
- fd1 open ( mypipe, O_RDONLY ) read ( fd1,
buf, 100 ) printf ( s\n, buf ) close
(fd1) -