0% found this document useful (1 vote)
139 views14 pages

Operating Systems

This document discusses operating systems and their key concepts. It covers: 1. The main goals of an operating system including efficient use of computer resources and preventing interference between processes. 2. Key OS components like scheduling, which decides which program uses the CPU, and different scheduling algorithms. 3. How the CPU has two modes of operation - supervisor/kernel mode for OS control and user mode for programs. 4. Context switching which allows multitasking by stopping the execution of one program to start another, and involves saving a program's state. 5. Processes and how they are represented in the OS through a process control block containing their state and resources.

Uploaded by

espyter
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
139 views14 pages

Operating Systems

This document discusses operating systems and their key concepts. It covers: 1. The main goals of an operating system including efficient use of computer resources and preventing interference between processes. 2. Key OS components like scheduling, which decides which program uses the CPU, and different scheduling algorithms. 3. How the CPU has two modes of operation - supervisor/kernel mode for OS control and user mode for programs. 4. Context switching which allows multitasking by stopping the execution of one program to start another, and involves saving a program's state. 5. Processes and how they are represented in the OS through a process control block containing their state and resources.

Uploaded by

espyter
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Operating Systems

Wednesday, January 14, 2009


9:02 AM
XMING (xming putty client) and putty
What is an OS
Interface to hardware
Goals
Efficient use of computer resources
Uses policies that ensure efficiency
Non interference
Only one process/program can access a certain disk location or something at a time
Why
Reduce set up time
More efficient
Use of resources
Hide low level
 
Scheduling decides which program gets to use CPU
many types of scheduling algorithms
Policy influences efficient CPU use and user service
Preemption: taking CPU away from a program mid-run
CPU allocation strategies
resource partitioning: divides resources into partitions allocates to user programs
Pool based: OS allocates resources from a pool
Consults table and allocates the resources if its free
Virtual resources
Provides effect of more resources
Virtual memory
Virtual machine
OS is software system
Requires cpu
Cpu services both user and OS programs
Context switching (switching of the CPU)
Stop execution o f a program and start execution of another program
Key concepts
Interrupts
Interrupt handler (servicing)
System calls
CPU has two modes of operation
Supervisor mode (kernel mode) 0
CPU can execute all instructions
Kernel operates with CPU in this mode so it can control computer operations
User mode 1
Cannot execute instructions that could interfere with other programs or with the OS
if uses indiscriminately (privileged instructions)
Hardware controlled
 
Cache
Pre-fetch from memory, speeds up execution, read page 42-43
Dirty instruction/cache paging out: sending cache back to memory
Ones that weren't changed; clean; are best because you don’t have to copy it back
to memory
 
Process system calls
Kernel: creates process
Context switching; switching which processes have control of cpu
Process control Block: contains process id… registers of process
Parent child processes: processes can initiate other processes
Process states: ready running terminated preempted waiting
Unix process structure: text data stack U area
Fork(): system call creates process returns 0 in child process, returns child process id pid to
parent
 
Computing environments and nature of components
Computer environment
Computers used to be non interactive, just running backend stuff
Now interactive
program
Set of functions or modules
Job
Sequence of programs that together achieve a common goal
Process
Execution of a program
DMA
Read or write through memory without CPU
Priorities
I/O bound typically has higher priority than CPU bound programs because
More than 1 can access memories at a time, only 1 can use CPU
Timesharing
Another type of OS, because user would be waiting a lot with the last 1
Hard and soft real time systems
Hard real time meets response requirements always
Soft would be something like watching a movie where its ok if a few frames get
dropped
Distributed OS
Multiple computers put together to work as one, a cluster, network, grid computing
 
Chapter 4
Policy
is a general rule of what should be done
Mechanism
How you should do it
Problems with designing an OS
Portability
Making for different hardware
Extensibility
Adding new support for new hardware/devices
Types
Monolithic
Sole OS layer
Layered
Break problems down into layers of distraction
Kernel
Mechanisms inside, policies outside
Hard to extend
Designed as a set of modules, which run as needed
User level device drivers
Run as a user process but act as a driver
Chapter 5 processes and threads
Process
An execution of a program using resources allocated to it
6 components
ID, code data stack resources CPU state
A program is a set of functions and procedures
Shell is the process that is always in the background waiting for input
Benefits of child process
Speed up primary process, do things in parallel
Priority for critical
Unix/google chrome vs IE/windows
Unix and google create a new process for new things, if it fails, kernel/browser still
of
IE/windows if one thing fails, a crash occurs
Parallelism
Two tasks performed at same time, simultaneously
Multiple CPUs
Concurrency
Illusion of parallelism
Interleaving operation of multiple processes
Both concurrency and parallelism produce better throughput
Process states:
ready
running
blocked
terminated
preempted
Used its allocated time in CPU, has to go back to ready to wait for more
resources
Waiting
Context switching
Saves cpu state in pcb and saves information concerning context
Changes process state from running to ready
Scheduling function
Uses process state information from PCBs to select a ready process for execution
and passes its id to dispatching function
Dispatching function
Sets up context
Context save function:
– Saves CPU state in PCB, and saves information concerning context
– Changes process state from running to ready
• Scheduling function:
– Uses process state information from PCBs to select a ready process
for execution and passes its id to dispatching function
• Dispatching function:
– Sets up context of process, changes its state to running, and loads
saved CPU state from PCB into CPU
– Flushes address translation buffers used by MMU
 
Event handling
1. Process creation event
2. Process termination event
PCB may be preserved
parent may want to check status of child (zombie)
3. Timer event
4. Resource request event
5. Resource release event
6. I/O initiation request event
7. I/O completion event
8. Message send event
9. Message receive event
10. Signal send event
11. Signal receive event
12. A program interrupt
13. A hardware malfunction event
When an event happens, kernel/OS must find process affected
Various ways to make this faster
Event Control Blocks
Sharing

Signals
Like pressing Ctrl C, sends a signal to a process
Handled by process defined signal handler or OS default
Need to setup a handler in the process code to take it
– Situations and signal names/numbers defined in OS
• CPU conditions like overflows
• Conditions related to child processes
• Resource utilization
• Emergency communications from a user to a process
• Can be synchronous or asynchronous
• Handled by process-defined signal handler or OS provided default
handler
Thread
Lightweight process
Doesn’t quite have all of the formalities of a process, cpu doesn’t think of it as a process
Advantage: easier/quicker than new process, easier communication, switching very
fast w/o kernel
Not known to OS (unless it's a kernel thread)
Has Thread Control Block within process, not related to OS
Uses resources of process, NONE OF ITS OWN RESOURCES
If a thread gets blocked, the whole process and other threads are also blocked
Has its own TCB
Scheduling information
CPU state
Pointer to parents PCB
pointer to RCB (linked list)
Still has ready when parent has all resources available
Only running when parent says so
Servers/web servers create threads for handling requests
Kernel-Level Threads
Threads are managed by the kernel
User-Level Threads
Threads are managed by thread library
Hybrid Threads
Combination of kernel-level and user-level threads
With messages, library has special way of dealing with getmessage() because normally
kernel would block it
If thread gets blocked waiting for message, whole process gets blocked, so other
thread can't send message which would normally create a dead lock. But library
handling of thread messages doesn’t block the whole process
Parent processes
Pcb of child process stays so parent can check for it, zombie process
If parent die, child continues as child of init, orphan
 
 
 
 
 
Hardware
Kernel
Non kernel
User Interface
 
Test review
Thursday, February 19, 2009
2:14 PM
 
Context switching
Stopping execution of a program for one reason or another and starts execution of a
different program
Saves PSW; info about program that is currently running on CPU, and will reload it when
it's that program's turn again
Kernel
Collection of routines that form core of OS
Implements control functions
Allows set of services to user programs
Exists in memory during operation of OS
interrupt
diverts the CPU to execution of the kernel code
A software interrupt
used by programs to communicate their requests to the kernel
CPU modes, 2
0 Supervisor mode
CPU can execute all instructions
Kernel operates in this mode so it can control computer operations
1 User mode
CPU is put in this mode to execute user programs
Cannot execute all instructions; won't run instructions that could interfere with
other programs or OS (privileged instructions)
General purpose registers
Addresses, data… available to programmer
Control registers
PC, IR, grouped in Program Status Word, PSW
PSW
Similar to Gen. Purpose Registers, saved on context switch to be reloaded
Contains
PC field
Mode field (1 user, 0 privileged/OS/Kernel/super)
Memory protection info
Start address and size of program
Interrupt Mask field
Interrupt mask
What interrupts are allowed to happen/interrupt right now
Memory management unit
Virtual memory illusion of more memory, larger than real comp. memory
implemented using noncontiguous memory allocation and MMU
OS allocates memory areas to a program
programs use logical addresses
CPU passes addresses to MMU
MMU translates logical addresses to physical addresses
dynamic address translation (DAT)
Memory hierarchy
Arrangement of memory types with different sizes and access speeds
Fastest is cache, CPU only accesses this kind
If no address in cache, go on to next slowest memory type
Best if cache doesn’t change when in use, so that it stays clean, doesn’t have to be written
back to memory
Dirty cache; changed, needs to take time to be written back to memory
Memory protection
Implemented by seeing if addresses are within program's address space
Types of IO
Programmed
Input device and memory; takes place using I/O, CPU is in use until operation is
done
Interrupt
CPU able to execute other instructions after I/O but interrupt is called when a byte
needs to be transferred and the CPU handles it right away
Direct Memory Access
I/O and memory takes place through BUS, no CPU involvement , interrupt when
complete
Event
Requires OS's attention, interrupt associated with it
Immediately preempts current program, saves PSW and context switches to Interrupt
Servicing Routine ISR
System calls
When user programs need to interact with OS
Such as I/O
Job
Set of programs
Process
Program running
real-time
computation has specific time constraints
OS ensures computations complete within constraints
Distributed computing environment: NETWORK
enables a computation to use resources located in several computer systems through a
network
Embedded computing environment:
Computer system is a part of a specific hardware system
OS has to meet the time constraints
Multiprogramming
OS keeps programs ready to be executed so no idle time
Mix of IO and CPU programs so they can run simultaneously with DMA
Degree of multiprogramming, # of programs stored, increases throughput
Time sharing (of CPU)
Round robin and time slice,
If allotted time for process is up, preempt it
Real time
Deadline for sub-process requests
Hard real time
Dedicated to real time requests
Soft real time
Tries its hardest to make deadline
Distributed
Across a network, much harder to work with
Benefits; if one fails there are others, can use others for computations
Communication between users/PCs
Batch processing
Doing shit with idle time altogether in jobs, running in background
computing environment
consists of a computer system, its interfaces with other systems, and the services
provided by its OS to users and programs
Modern OS
Combines techniques of previous OS's to make 1 sweet one
Boot procedure
Analyzes what's up? Like memory size and stuff
Loads part of OS into memory, sets up data structures, and lets it take over
Scheduler
Puts process/saved context into queue, order depends on OS structure
OS design
Policy
What something will do
Mechanism
How something will do it
Monolithic
One OS deals with all hardware and users stuff
Layered
Separate components deal with hardware, easier to upgrade
Too complex
Slowed shit down
Kernel
Mechanisms in kernel, policies outside
Poor extensibility (ability to add stuff later)
Base kernel loaded on boot, others load as needed, kernel is set of modules
Micro kernel
Bare necessities of kernel, more extensible
 
 
 
 
 
 
Test 2
Monday, February 23, 2009
9:04 AM
 
Chapter 6
Processes and sharing information
Multiprocessor; several instructions at once
2 or more CPUs, usually just 1 shared memory
Shared variable
Can be an issue, could execute more than once by accident on multiple processors
or 1 processor concurrent processing
Concurrency
Execution is sequential
Mutual exclusion problem
Processes using shared resource concurrently
Control execution of shared code
Critical section
Protected, execution of critical section must be mutually exclusive
Assumptions
Instructions like load store test are indivisible/atomic
Can't be terminated w/ interruption
Speed of processor unknown,
Even identical processors can have different turnaround times
Critical section will take a finite amount of time
Process executing non-critical section cannot take over from a process in
critical section
Critical processes cannot die
To solve problem
Dekker's Algorithm
Guarantees mutual exclusion
Two functions with while loops, while (turn ==1) and while (turn==2)
Only 1 can be going at a time, critical code within loop
While (key2)
;
Busy wait: infinite loop while other process is in critical section
Deficiency: busy wait infinite loop is a waste
Need hardware support
Low level primitives
Dikjstra
Producer consumer problem
Producing items in buffer and consumer taking them away at the same time
Buffer -- or ++ is the critical section
Busy wait
Test and Set, TST, returns false of passed variable, atomic
Swap, assumed atomic , in both processes
Semaphores
Type of low level primitive (machine structure)
Avoids busy wait
Atomic
Two operations
P&V
Originally non negative variables 1 or 0
Semaphores
Only valid operations on a semaphore variable
Operations on same semaphore are mutually exclusive
Sequential effect in no particular order (not FIFO)
Any waiting process may be shared
P(n)
Wait for n to be released
Bounded buffer producer-consumer problem
New semaphore:
Empty: number of empty buffers
Now have 3 semaphores
Mutex: mutual exclusion
Full: number of full buffers
Empty: number of empty buffers
Initially
Mutex = 1
Full = 0
Empty = n
Understand the code
Readers & writers problem Curtois 1971
Common database problem
Two classes of processes accessing a shared resource
Readers
May share acess with processes of the same class
Writers
Require exclusive access to resource
Example
Database
Concurrent reads
Exclusive writes
How to fix
First readers and writers
No reader should wait for other readers to finish because a
writer is waiting
Writers may starve
Dining philosopher's
5 philosophers
Think and eat
Life cycle
While true
Think; eat;
Dining room
Circular table
Five plates
5 forks
Infinite spaghetti in middle
Requires 2 forks to eat
 
 
 
Lab1
Sunday, February 08, 2009
6:43 PM
 
Questions
1. Maximum # of processes:
Using command "ulimit -a"
Maximum user processes is 2047
2. What data structure is used to implement PCB
The set of processes on the Linux system is represented as a collection
of struct task_struct structures which are linked in two ways:
1. as a hashtable, hashed by pid, and
2. as a circular, doubly-linked list using p->next_task and p-
>prev_task pointers.
 
Pasted from <http://www.moses.uklinux.net/patches/lki-2.html>
3. What does sleep system call do?
Pauses for a certain amount of time, determined by the arguments sent to it
4. What happens if you insert the code "sleep(2)" before the read instruction in the child
process? Explain
I am using a 100,000 line file. Without the sleep(2) command, the parent process
started outputting first, but at line 21,432, the child process took over outputting
lines until line 28,099. they continued to alternate for the rest of the file
With the sleep(2) command, the parent process did all of the outputting, and the
child process only displayed its process ID at the end. This is because sleeping the
child process, even for only 2 seconds, allowed the parent process to complete the
file and clear the stream before the child process could do anything
5. What is the effect if the same sleep instruction is inserted before the read instruction,
but in the parent process. Explain
The exact same happened as in the previous example, except with the child process
doing all the work before the parent process could do any
6. What is output by the following program
answer
"hello world
hello hello world"
The parent process finishes before the child process gets to start and print its text.
When the parent forks a new child, it still continues its code, which in this case, is
only print out one more line. The creation of the child process lasts long enough that
the parent process finishes before the child starts. The child process then prints the
first "hello " then goes into the if statement and prints "hello world\n" and then
exits
 
# Assignment: Lab1        
 
CC = g++
CFLAGS = -g -pg -ansi
lab1.a: lab1.cc
$(CC) -o lab1.a lab1.cc
 
 
#include <stdio.h>
#include <stdlib.h>
 
int pid;
main()
{
printf("hello ");
pid=fork();
if ( pid == 0 )
{
printf(" hello world\n");
exit(0);
}
printf("world\n");
}
 
getppid() returns parent ID
getpid() returns current process ID
 
1 line 1
2 line 2
3 line 3
4 line 4
 
 
#include<sys/types.h>
#include<sys/uio.h>
#include<unistd.h>
 
 
Int fd = open("lab.txt", O_RDONLY);
Read();
Write();
 
 
 
Lab2 questions
Monday, February 23, 2009
1:32 AM
 
1- Explain one of the fundamental differences between signal() and sigaction()
system calls
Signal just sets up a signal handler
Sigaction is a little fancier, it can tell you the previous handler, info about the signal, signal
masks
http://kerneltrap.org/node/14372
2- What is the purpose of the SIGKILL signal if it cannot be intercepted?
It is still a signal that all processes handle. You can't intercept it for security reasons, but
the signal still needs to be defined so it can be sent to processes and terminate them.
3- This following illustrates how to wait for all child processes to terminate, and how to
handle the returned value of a process. The wait() system call waits for a process to
terminate. It blocks the calling process until a child processes terminates. The wait()
returns a pid_t type (usually a long int) with the terminated child process id and
receives as an argument the terminating status set by the child process.
The terminating status variable values is a 32 bit integer and has two different formats,
one if the process terminated normally, and a second when the process terminated due
to an uncaught signal.
Describe the format of both possible values of the terminating status word. You may use
internet resources to answer this question.
If a program terminates normally exit(), its format is
If you bitwise & the status with 7f, which is eleven 1s, the int will not == 0
The least significant 8 bits determine the status of the exit
Abnormal exit if the status & x7f is equal to 0
Rest of status can tell how/why process was terminated, if by core dump or a signal
instructed it to
 
2 formats, all 32 bits
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<fstream>
#include<string>
#include<iostream>
#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
 
using namespace std;
 
int pid;
int actualpid;
void alarmcatch(int);
main()
{
signal(SIGALRM, alarmcatch);
signal(SIGINT, SIG_IGN);
string username;
//kill(getpid(),SIGALRM);//test alarm catcher
alarm(30); //change to 3 later
cout<<"Input your username: ";
cin>>username;
//if (username != ")
alarm(0); //void the alarm, a username was entered
if (username == "exit" || username =="q")
exit(0);
cout<<"\nYour username is "<<username<<"\nCongrats! You can type\n";
return 0;
}
void alarmcatch(int param)
{
cout<<"\nTime's Up! should've typed faster\n";
exit(0);
}
 
 
Lab3 questions
Saturday, February 28, 2009
9:04 PM
 
1 in my shell, I have the following code "
if (execvp(command,argsarray))
{
cout<<"There was an error with your last command";
exit(0);
}        
" because if execvp returns at all, it means that the command failed. Anything else is
automatically a success because it means execvp didn't return
2 " If any of the exec() functions returns, an error will have occurred.
The return value is -1, and the global variable errno will be set to
indicate the error." copied from the exec manual page

You might also like