Bca 3
Bca 3
Bca 3
1
Dear Students,
We, at CIMAGE, are dedicated to provide the best of education and to bring
an overall positive growth in our students. No wonder, that’s the reason, our
students have been Toppers in the University Examinations; and after
successful completion of the course, they have been provided with the best
placements or have been selected in top ranking institutes for further studies.
I am happy that CIMAGE have been pioneer in setting milestone in the
segment of higher education, for others to be followed.
The publication of ‘CIMAGE Courseware’ is also one of the several innovative initiatives taken by our
college. This textbook is unique in several ways. It has been written in simple language, so that
students could understand even the complex and tough topics very easily. Every chapter has been
divided into three sections, ‘The Objective’, ‘The Chapter itself’ & ‘The Summary’ part. The
‘Objective’ section will help the students to understand about the ‘Objective and Purpose’ of the
chapter. It will also help them in understanding, how to use the knowledge, studied in the chapter.
The second section is ‘The Chapter’ itself, which has the relevant study material, prescribed by the
university. In this section some practical examples have also been cited to clarify concept. The third
section is the ‘Summary of the chapter’. It has been written using Hindi & English both the
languages. Hence it will help the student from Hindi medium background also to understand the
concept Properly, Deeply and Completely. At the end of the chapters, relevant Projects,
Assignments and Questions have also been given, to make the students well prepared in the studied
chapter.
I am confident that this book will serve the purpose of providing a Clear and Concise guide-line to
the students. However, there is ‘No limit’ of acquiring the knowledge, hence I advice the students to
read other text & reference books as well, as it will help them in gaining more knowledge in the
subject. At CIMAGE Library, a number of Reference Books, Video Lectures, Journals, Research
Papers are there to serve this purpose.
I suggest the students, not to ‘Mug-up’, but to ‘Understand’ the subject properly. You should attend
the classes seriously and if something is not well understood by you; Do not hesitate in asking
questions from your teachers. I also suggest students to discuss the topic and subjects in a group,
as group study has its own advantages. Group Study makes the study easy and interesting, at the
same time, it also makes you aware of the different prospective and dimensions.
You Know, at CIMAGE, we have one more innovative system for you, i.e. ‘Teacher – Student Doubt
Clearing Session’. If you have any doubts in any chapter, you may meet the respective teachers
individually and get your doubts cleared. Every teacher has been assigned a special time table for it.
You are advised to get benefited with it.
At last, but not the least, I thank all the faculty members of CIMAGE for their valuable contribution in
preparing the courseware.
If you have any suggestions or concerns, you may mail me at info.cimage@gmail.com. Go ahead;
Study Well, Give your Best & Keep adding glory to the rich legacy of CIMAGE.
Thank You.
Neeraj Agrawal
Director,
CIMAGE Group of Institutions.
1
TABLE OF CONTENTS
1
PART-I
OPERATING SYSTEM
2
Chapter-1
Objectives
Unit Operating System Operating System
Operating System components generations
The operating system must provide certain services to programs and to the
users of those programs in order to make the programming task easier,
these services will differ from one OS to another.
An abstraction is software that hides lower level details and provides a set of
higher-level functions. An operating system transforms the physical world of
devices, instructions, memory, and time into virtual world that is the result
of abstractions built by the operating system. There are several reasons for
abstraction.
1
First, the code needed to control peripheral devices is not standardized.
Operating systems provide subroutines called device drivers that perform
operations on behalf of programs for example, input/output operations.
Third, the operating system transforms the computer hardware into multiple
virtual computers, each belonging to a different program. Each program that
is running is called a process. Each process views the hardware through the
lens of abstraction.
Resources Management
The user interacts with the operating systems through the user interface and
usually interested in the look and feel of the operating system. The most
important components of the user interface are the command interpreter,
the file system, on-line help, and application integration. The recent trend
has been toward increasingly integrated graphical user interfaces that
encompass the activities of multiple processes on networks of computers.
2
Evolution of Operating System
Early Systems
The lack of any operating system meant that only one person could use a
computer at a time. Even in the research lab, there were many researchers
competing for limited computing time. The first solution was a reservation
system, with researchers signing up for specific time slots.
The high cost of early computers meant that it was essential that the rare
computers be used as efficiently as possible. The reservation system was not
particularly efficient. If a researcher finished work early, the computer sat
idle until the next time slot. If the researcher's time ran out, the researcher
might have to pack up his or her work in an incomplete state at an awkward
moment to make room for the next researcher. Even when things were
going well, a lot of the time the computer actually sat idle while the
researcher studied the results (or studied memory of a crashed program to
figure out what went wrong).
The solution to this problem was to have programmers prepare their work
off-line on some input medium (often on punched cards, paper tape, or
magnetic tape) and then hand the work to a computer operator. The
computer operator would load up jobs in the order received (with priority
overrides based on politics and other factors). Each job still ran one at a
time with complete control of the computer, but as soon as a job finished,
the operator would transfer the results to some output medium (punched
3
tape, paper tape, magnetic tape, or printed paper) and deliver the results to
the appropriate programmer. If the program ran to completion, the result
would be some end data. If the program crashed, memory would be
transferred to some output medium for the programmer to study (because
some of the early business computing systems used magnetic core memory,
these became known as "core dumps")
Soon after the first successes with digital computer experiments, computers
moved out of the lab and into practical use. The first practical application of
these experimental digital computers was the generation of artillery tables
for the British and American armies. Much of the early research in computers
was paid for by the British and American militaries. Business and scientific
applications followed.
Every programmer was writing his or her own routines for I/O, such as
reading input from a magnetic tape or writing output to a line printer. It
made sense to write a common device driver for each input or output device
and then have every programmer share the same device drivers rather than
each programmer writing his or her own. Some programmers resisted the
use of common device drivers in the belief that they could write "more
efficient" or faster or ""better" device drivers of their own.
Additionally each programmer was writing his or her own routines for fairly
common and repeated functionality, such as mathematics or string
functions. Again, it made sense to share the work instead of everyone
repeatedly "reinventing the wheel". These shared functions would be
organized into libraries and could be inserted into programs as needed. In
the spirit of cooperation among early researchers, these library functions
were published and distributed for free, an early example of the power of the
open source approach to software development.
When punched cards were used for user jobs, processing of a job involved
physical actions by the system operator, e.g., loading a deck of cards into
the card reader, pressing switches on the computer's console to initiate a
job, etc. These actions wasted a lot of central processing unit (CPU) time.
4
Operating System
User Program Area
Figure 1.1: Simple Batch System
To speed up processing, jobs with similar needs were batched together and
were run as a group. Batch processing (BP) was implemented by locating a
component of the BP system, called the batch monitor or supervisor,
permanently in one part of computer's memory. The remaining memory was
used to process a user job - the current job in the batch as shown in the
figure 1.1 above.
The delay between job submission and completion was considerable in batch
processed system as a number of programs were put in a batch and the
entire batch had to be processed before the results were printed. Further
card reading and printing were slow as they used slower mechanical units
compared to CPU which was electronic. The speed mismatch was of the
order of 1000. To alleviate this problem programs were spooled. Spool is an
acronym for simultaneous peripheral operation on-line. In essence the idea
was to use a cheaper processor known as peripheral processing unit (PPU) to
read programs and data from cards store them on a disk. The faster CPU
read programs/data from the disk processed them and wrote the results
back on the disk. The cheaper processor then read the results from the disk
and printed them.
Even though disks are faster than card reader/ printer they are still two
orders of magnitude slower than CPU. It is thus useful to have several
programs ready to run waiting in the main memory of CPU. When one
program needs input/output (I/O) from disk it is suspended and another
program whose data is already in main memory (as shown in the figure 1.2
below) is taken up for execution. This is called multiprogramming.
Operating System
Program 1
Program 2
Program 3
Program 4
Figure 1.2: Multi Programmed Batch Systems
5
Multiprogramming (MP) increases CPU utilization by organizing jobs such
that the CPU always has a job to execute. Multiprogramming is the first
instance where the operating system must make decisions for the user.
The MP arrangement ensures concurrent operation of the CPU and the I/O
subsystem. It ensures that the CPU is allocated to a program only when it is
not performing an I/O operation.
Distributed Systems
The processors in a distributed system may vary in size and function, and
referred by a number of different names, such as sites, nodes, computers
and so on depending on the context. The major reasons for building
distributed systems are:
6
system may allow a user to distribute computation among the various sites
to run them concurrently.
Reliability: If one site fails in a distributed system, the remaining sites can
potentially continue operations.
7
provides the possibility of overlapping the CPU and I/O activities of the
application with one another. This helps in reducing its elapsed time. The
ability to specify priorities for the tasks provides additional controls to a
designer while structuring an application to meet its response-time
requirements.
Even though, not all systems have the same structure many modern
operating systems share the same goal of supporting the following types of
system components.
Process Management
The operating system manages many kinds of activities ranging from user
programs to system programs like printer spooler, name servers, file server
etc. Each of these activities is encapsulated in a process. A process includes
the complete execution context (code, data, PC, registers, OS resources in
use etc.).
8
Main-Memory Management
1. Keep track of which part of memory are currently being used and by
whom.
2. Decide which processes are loaded into memory when memory space
becomes available.
File Management
A file system normally organized into directories to ease their use. These
directories may contain files and other directions.
9
I/O System Management
I/O subsystem hides the peculiarities of specific hardware devices from the
user. Only the device driver knows the peculiarities of the specific device to
whom it is assigned.
Secondary-Storage Management
Networking
Protection System
If a computer system has multiple users and allows the concurrent execution
of multiple processes, then various processes must be protected from one
another's activities. Protection refers to mechanism for controlling the access
10
of programs, processes, or users to the resources defined by a computer
system.
If we want to change the way the command interpreter looks, i.e., I want to
change the interface of command interpreter, I am able to do that if the
command interpreter is separate from the kernel. I cannot change the code
of the kernel so I cannot modify the interface.
Following are the five services provided by operating systems for the
convenience of the users.
Program Execution
11
I/O Operations
Each program requires an input and produces output. This involves the use
of I/O. The operating systems hides from the user the details of underlying
hardware for the I/O. All the users see that the I/O has been performed
without any details. So the operating system, by providing I/O, makes it
convenient for the users to run programs.
For efficiently and protection users cannot control I/O so this service cannot
be provided by user-level programs.
The output of a program may need to be written into new files or input taken
from some files. The operating system provides this service. The user does
not have to worry about secondary storage management. User gives a
command for reading or writing to a file and sees his/her task accomplished.
Thus operating system makes it easier for user programs to accomplish their
task.
This service involves secondary storage management. The speed of I/O that
depends on secondary storage management is critical to the speed of many
programs and hence I think it is best relegated to the operating systems to
manage it than giving individual users the control of it. It is not difficult for
the user-level programs to provide these services but for above mentioned
reasons it is best if this service is left with operating system.
Communications
There are instances where processes need to communicate with each other
to exchange information. It may be between processes running on the same
computer or running on the different computers. By providing this service
the operating system relieves the user from the worry of passing messages
between processes. In case where the messages need to be passed to
processes on the other computers through a network, it can be done by the
user programs. The user program may be customized to the specifications of
the hardware through which the message transits and provides the service
interface to the operating system.
12
Error Detection
An error in one part of the system may cause malfunctioning of the complete
system. To avoid such a situation the operating system constantly monitors
the system for detecting the errors. This relieves the user from the worry of
errors propagating to various part of the system and causing malfunctioning.
OS for Supercomputers:
13
Mainframe Operating Systems:
Desktop operating systems are used for personal computers, for example
DOS, Windows 9x, Windows XP, Macintosh OS, Linux, etc.
14
Embedded Operating Systems:
Handheld operating systems are much smaller and less capable than
desktop operating systems, so that they can fit into the limited memory of
handheld devices. The operating systems include Palm OS, Windows CE,
EPOC, and many Linux versions such as Qt Palmtop, and Pocket Linux, etc.
QUESTIONS
1. What is an operating system?
2. What are the objectives of an operating system?
3. Describe in brief, the function of an operating system.
4. Explain the evolution of operating system in brief.
5. Write a note on Batch OS. Discuss how it is differ from Multi Programmed Batch
Systems.
6. What is difference between multi-programming and timesharing operating systems?
7. What are the typical features of an operating system provides?
8. Explain the functions of operating system as file manager.
9. What are different services provided by an operating system?
10. Write Note on :
a. Mainframe Operating Systems
b. Embedded Operating Systems
c. Servers Operating Systems
d. Desktop Operating Systems
11. Discuss the various components of OS?
12. Explain the Memory Management and File Management in brief.
13. Write Note on -
a. Secondary-Storage Management
b. Command Interpreter System
14. Explain the five services provided by the operating system.
15
Summary
Operating System system program applications program
users
16
Chapter-2
PROCESS MANAGEMENT
Objectives
Unit Proces PCB components, Process State model
What is a Process?
· A program in Execution.
· An asynchronous activity.
And many more, but the definition "Program in Execution" seem to be most
frequently used. And this is a concept we will use in the present study of
operating systems.
Process is not the same as program. A process is more than a program code.
A process is an 'active' entity as oppose to program which considered being
a 'passive' entity. As we all know that a program is an algorithm expressed
in some programming language. Being a passive, a program is only a part of
process. Process, on the other hand, includes:
17
· Current value of Program Counter (PC)
Process Creation
· System initialization.
18
3. Operating systems creates process to provide service, e.g., to manage
printer.
2. Allocate space for the process: this operating involves finding how much
space is needed by the process and allocating space to the parts of the
process such as user program, user data, stack and process attributes.
The requirement of the space can be taken by default based on the type
of the process, or from the parent process if the process is spawned by
another process.
Process Termination
Normal Exit-Most processes terminates because they have done their job.
This call is exit in UNIX.
19
Error Exit-When process discovers a fatal error. For example, a user tries to
compile a program that does not exist.
Process States
A process goes through a series of discrete process states during its lifetime.
Depending on the implementation, the operating systems may differ in the
number of states a process goes though. The state of a process is defined in
part by the current activity of that process. Each process may be in one of
the following states:
• Waiting. The process is waiting for some event to occur (such as an I/O
20
New State -The process being created.
Terminated State -The process has finished execution.
Blocked (waiting) State - When a process blocks, it does so because
logically it cannot continue, typically because it is waiting for input that
is not yet available. Formally, a process is said to be blocked if it is
waiting for some event to happen (such as an I/O completion) before
it can proceed. In this state a process is unable to run until some
external event happens.
Running State - A process is said to be running if it currently has the
CPU, which is, actually using the CPU at that particular instant.
Ready State - A process is said to be ready if it use a CPU if one
were available. It is run-able but temporarily stopped to let another
process run.
Swapping
Many of the operating systems follow the above shown process model.
However the operating systems which does not employ virtual memory, the
processor will be idle most of the times considering the difference between
speed of I/O and processor. There will be many processes waiting for I/O in
the memory, and exhausting the memory. If there is no ready process to
run; new processes cannot be created as there is no memory available to
accommodate new process. Thus the processor has to wait till any of the
waiting processes become ready after completion of an I/O operation.
This problem can be solved by adding to more states in the above process
model by using swapping technique. Swapping involves moving part or all of
a process from main memory to disk. When none of the processes in main
memory is in the ready state, the operating system swaps one of the
blocked processes out onto disk in to a suspend queue. This is a queue of
existing processes that have been temporarily shifted out of main memory,
or suspended. The operating system then either creates new process or
brings a swapped process from the disk which has become ready.
21
Seven State Process Model
The following figure shows the seven state process model in which uses
above described swapping technique.
Apart from the transitions we have seen in five states model, following are
the new transitions which occur in the above seven state model.
Blocked to Blocked / Suspend: If there are now ready processes in the main
memory, at least one blocked process is swapped out to make room for
another process that is not blocked.
22
processes in main memory but having lower priority than one of the
processes in Ready / Suspend state. So the high priority process is brought
in the main memory.
Process Structure
Process identification
Processor state information
Process control information
User Stack
Private user address space
(program, data)
Shared address space
Figure 3.3: Process Image
23
program, data, system stack, and process control block is called as Process
Image as shown in the figure 3.3 above.
• Process state-The state may be new, ready, running, waiting, halted, and
so on.
24
• Memory-management information- This information may include such
information as the value of the base and limit registers, the page tables, or
the segment tables, depending on the memory system used by the
operating system.
• I/O status information- This information includes the list of I/O devices
allocated to the process, a list of open files, and so on. In brief, the PCB
simply serves as the repository for any information that may vary from
process to process.
Context Switching
25
Switching the CPU to another process requires performing a state save of
the current process and a state restore of a different process. This task is
known as a context switch. When a context switch occurs, the kernel saves
the context of the old process in its PCB and loads the saved context of the
new process scheduled to run. Context-switch time is pure overhead,
because the system does no useful work while switching. Its speed varies
from machine to machine, depending on the memory speed, the number of
registers that must be copied, and the existence of special instructions (such
as a single instruction to load or store all registers). Typical speeds are a few
milliseconds. Context-switch times are highly dependent on hardware
support. For instance, some processors (such as the Sun Ultras ARC) provide
multiple sets of registers. A context switch here simply requires changing the
pointer to the current register set. Of course, if there are more active
processes than there are register sets, the system resorts to copying
register data to and from memory, as before. Also, the more complex the
operating system, the more work must be done during a context switch
space is preserved, and what amount of work is needed to preserve it,
depend on the memory-management method of the operating system.
26
A context switch is sometimes described as the kernel suspending execution
of one process on the CPU and resuming execution of some other process
that had previously been suspended.
A context switch occurs due to interrupts, trap (error due to the current
instruction) or a system call as described below:
Clock interrupt: when a process has executed its current time quantum
which was allocated to it, the process must be switched from running state
to ready state, and another process must be dispatched for execution.
I/O interrupt: whenever any I/O related event occurs, the OS is interrupted,
the OS has to determine the reason of it and take necessary action for that
event. Thus the current process is switched to ready state and the interrupt
routine is loaded to do the action for the interrupt event (e.g. after an I/O
interrupt the OS moves all the processes which were blocked on the event,
from blocked state to ready state, and blocked/suspended to
ready/suspended state). After completion of the interrupt related actions, it is
expected that the process which was switched, should be brought for
execution, but that does not happen. At this point the scheduler again decides
which process is to be scheduled for execution from all the ready processes
afresh. This is important as it will schedule any high priority process present
in the ready queue added during the interrupt handling period.
Memory fault: when virtual memory technique is used for memory
management, many a times it happens that a process refers to a memory
address which is not present in the main memory, and needs to be brought
in. As the memory block transfer takes time, another process should be
given chance for execution and the current process should be blocked. Thus
the OS blocks the current process, issues an I/O request to get the memory
block in the memory and switches the current process to blocked state, and
loads another process for execution.
Trap: if the instruction being executed has any error or exception,
depending on the criticalness of the error / exception and design of
operating system, it may either move the process to exit state, or may
execute the current process after a possible recovery.
System call: many a times a process has to invoke a system call for a
privileged job, for this the current process is blocked and the respective
operating system's system call code is executed. Thus the context of the
current process is switched to the system call code.
27
CPU Scheduler
Whenever the CPU becomes idle, it is the job of the CPU Scheduler (a.k.a.
the short-term scheduler) to select another process from the ready queue to
run next. The storage structure for the ready queue and the algorithm used
to select the next process are not necessarily a FIFO queue. There are
several alternatives to choose from, as well as numerous adjustable
parameters for each algorithm, which is the basic subject of this entire unit.
Preemptive Scheduling
1. When a process switches from the running state to the waiting state, such
as for an I/O request or invocation of the wait( ) system call.
2. When a process switches from the running state to the ready state, for
example in response to an interrupt.
3. When a process switches from the waiting state to the ready state, say at
completion of I/O or a return from wait ( ).
28
allowing the preemption Unfortunately this solution is problematic for real-
time systems, as real-time response can no longer be guaranteed. Some
critical sections of code protect themselves from concurrency problems by
disabling interrupts before entering the critical section and re-enabling
interrupts on exiting the section. Needless to say, this should only be done
in rare situations, and only on very short pieces of code that will finish
quickly, (usually just a few machine instructions).
Dispatcher
The dispatcher is the module that gives control of the CPU to the process
selected by the scheduler. This function involves:· Switching
context. · Switching to user mode. · Jumping to the proper location in the
newly loaded program.
Scheduling Criteria
There are several different criteria to consider when trying to select the
"best" scheduling algorithm for a particular situation and environment,
including:·
CPU utilization – Ideally the CPU would be busy 100% of the time, so as to
waste 0 CPU cycles. On a real system CPU usage should range from 40%
(lightly loaded) to 90% (heavily loaded.)·
Waiting time – How much time processes spend in the ready queue waiting
their turn to get on the CPU.
Scheduling Algorithms
29
First-Come First-Serve Scheduling, FCFS
FCFS is very simple - Just a FIFO queue, like customers waiting in line at the
bank or the post office or at a copying machine. Unfortunately, however,
FCFS can yield some very long average wait times, particularly if the first
process to get there takes a long time. For example, consider the following
three processes:
FCFS can also block the system in a busy dynamic system in another way,
known as the convoy effect. When one CPU intensive process blocks the
CPU, a number of I/O intensive processes can get backed up behind it,
leaving the I/O devices idle. When the CPU hog finally relinquishes the CPU,
then the I/O processes pass through the CPU quickly, leaving the CPU idle
while everyone queues up for I/O, and then the cycle repeats itself when the
CPU intensive process gets back to the ready queue.
The idea behind the SJF algorithm is to pick the quickest fastest little job
that needs to be done, get it out of the way first, and then pick the next
smallest fastest job to do next. (Technically this algorithm picks a process
based on the next shortest CPU burst, not the overall process time.). For
example, the Gantt chart below is based upon the following CPU burst times,
(and the assumption that all jobs arrive at the same time.)
30
Process Burst Time
P1 6
P2 8
P3 7
P4 3
In the case above the average wait time is (0 + 3 + 9 + 16) / 4 = 7.0 ms,
(as opposed to 10.25 ms for FCFS for the same processes.)
P1 0 8
P2 1 4
P3 2 9
p4 3 5
Priority Scheduling
31
An SJF algorithm is simply a priority algorithm where the priority (p) is the
inverse of the (predicted) next CPU burst. The larger the CPU burst, the
lower the priority, and vice versa.
given in milliseconds:
10 3 6 16
1 1 0 1
2 4 16 18
1 5 18 19
5 2 1 6
Average - - 8.2 12
32
requirements, the number of open files, and the ratio of average I/O burst to
average CPU burst have been used in computing priorities.
External priorities are set by criteria outside the OS, such as the importance
of the process, the type and amount of funds being paid for computer use,
the department sponsoring the work, and other, often political, factors.
A non preemptive priority scheduling algorithm will simply put the new
process at the head of the ready queue.
A priority scheduling algorithm can leave some low priority processes waiting
indefinitely.
33
Figure 5.9: A scheduling algorithm with four priority classes.
For example, if priorities range from 127 (low) to 0 (high), we could increase
the priority of a waiting process by 1 every 15 minutes.
Eventually, even a process with an initial priority of 127 would have the
highest priority in the system and would be executed.
Round-Robin Scheduling
34
A small unit of time, called a time quantum or time slice, is defined. A time
quantum is generally from 10 to 100 milliseconds. The ready queue is
treated as a circular queue.
To implement RR scheduling,
The CPU scheduler picks the first process from the ready queue, sets a timer
to interrupt after 1 time quantum, and dispatches the process.
The process may have a CPU burst of less than 1 time quantum.
In this case, the process itself will release the CPU voluntarily.
The scheduler will then proceed to the next process in the ready queue.
Otherwise, if the CPU burst of the currently running process is longer than 1
time quantum,
The timer will go off and will cause an interrupt to the OS.
A context switch will be executed, and the process will be put at the tail of
the ready queue.
The CPU scheduler will then select the next process in the ready queue.
The average waiting time under the RR policy is often long. Consider the
following set of processes that arrive at time 0, with the length of the CPU
burst given in milliseconds: (a time quantum of 4 milliseconds)
35
Burst Waiting Turnaround
Process Time Time Time
24 6 30
3 4 7
3 7 10
Note that under this algorithm jobs cannot switch from queue to queue -
Once they are assigned a queue that is their queue until they finish.
36
Multilevel Feedback-Queue Scheduling
37
Deadlock
A process requests resources; and if the resources are not available at that
time, the process enters a waiting state. Sometimes, a waiting process is
never again able to change state, because the resources it has requested are
held by other waiting processes.
38
Necessary Conditions
That is, only one process at a time can use the resource.
Hold and wait. A process must be holding at least one resource and waiting
to acquire additional resources that are currently being held by other
processes.
39
is waiting for a resource held by ,
We emphasize that all four conditions must hold for a deadlock to occur.
Resource-Allocation Graph
and s.
resource type and is currently waiting for that resource (request edge).
type as a rectangle.
40
Figure: Resource allocation graphs. (a) Holding a resource. (b)
Requesting a resource. (c) Deadlock.
An arc from a resource node (square) to a process node (circle) means that
the resource has previously been requested by, granted to, and is currently
held by that process.
Since resource type may have more than one instance, each such
41
Given the definition of a resource-allocation graph, it can be shown that, if
the graph contains no cycles, then no process in the system is deadlocked. If
the graph does contain a cycle, then a deadlock may exist.
A cycle in the graph is a necessary but not a sufficient condition for the
existence of deadlock with resource types of several instances. A knot must
exist; a cycle with no non-cycle outgoing path from any involved node
42
Figure: Resource Allocation Graphs. Lower; either or could
43
To ensure that deadlocks never occur, the system can use either a deadlock-
prevention or a deadlock-avoidance scheme.
Deadlock prevention provides a set of methods for ensuring that at least one
of the necessary conditions (Section 7.2.1) cannot hold.
We can allow the system to enter a deadlock state, detect it, and recover.
We can ignore (The Ostrich Algorithm; maybe if you ignore it, it will ignore
you) the problem altogether and pretend that deadlocks never occur in the
system.
If a system neither ensures that a deadlock will never occur nor provides a
mechanism for deadlock detection and recovery, then we may arrive at a
situation where the system is in a deadlocked state yet has no way of
recognizing what has happened.
Eventually, the system will stop functioning and will need to be restarted
manually.
44
In many systems, deadlocks occur infrequently (say, once per year); thus,
this method is cheaper than the prevention, avoidance, or detection and
recovery methods.
Most OSs potentially suffers from deadlocks that are not even detected.
Process table slots are finite resources. If a fork fails because the table is
full, a reasonable approach for the program doing the fork is to wait a
random time and try again.
The maximum number of open files is similarly restricted by the size of the i-
node table, so a similar problem occurs when it fills up. Swap space on the
disk is another limited resource. In fact, almost every table in the OS
represents a finite resource.
The third solution is the one used by most OSs, including UNIX and
Windows; it is then up to the application developer to write programs that
handle deadlocks.
Deadlock Prevention
If we can ensure that at least one of the four following conditions is never
satisfied, then deadlocks will be structurally impossible.
45
Figure: Summary of approaches, to deadlock prevention.
Mutual Exclusion
The mutual-exclusion condition must hold for non sharable resources (i.e., a
printer).
Attacking the Hold and Wait Condition; can a process hold a resource and
ask for another? Can we require processes to request all resources at once?
Most processes do not statically know about the resources they need.
One protocol that can be used requires each process to request and be
allocated all its resources before it begins execution.
46
can request any additional resources, however, it must release all the
resources that it is currently allocated.
First, resource utilization may be low, since resources may be allocated but
unused for a long period.
No Preemption
To ensure that this condition does not hold, we can use the following
protocol.
The preempted resources are added to the list of resources for which the
process is waiting. The process will be restarted only when it can regain its
old resources, as well as the new ones that it is requesting.
47
Circular Wait
Attacking the Circular Wait Condition; Can circular waits exist? Order
resources by an index: ; requires that resources are always
requested in order.
One way to ensure that this condition never holds is to impose a total
ordering of all resource types and to require that each process requests
resources in an increasing order of enumeration.
If these two protocols are used, then the circular-wait condition cannot hold.
One possibility is to inform the operator that a deadlock has occurred and to
let the operator deal with the deadlock manually.
Resource Preemption
48
nature of the resource. Recovering this way is frequently difficult or
impossible.
Check pointing; means that its state is written to a file so that it can be
restarted later.
The checkpoint contains not only the memory image, but also the resource
state, that is, which resources are currently assigned to the process.
Since, in general, it is difficult to determine what a safe state is, the simplest
solution is a total rollback: Abort the process and then restart it.
Although it is more effective to roll back the process only as far as necessary
to break the deadlock, this method requires the system to keep more
information about the state of all running processes.
Starvation. How do we ensure that starvation will not occur? That is, how
can we guarantee that resources will not always be preempted from the
same process?
Solutions to deadlock
49
Dynamic avoidance by careful resource allocation. Check to see if a
resource can be granted, and if granting it will cause deadlock, don't
grant it.
Prevention - change the rules
Ignore deadlock
The text refers to this as the Ostrich Algorithm. Just hope that deadlock
doesn't happen. In general, this is a reasonable strategy. Deadlock is
unlikely to occur very often; a system can run for years without deadlock
occurring. If the operating system has a deadlock prevention or detection
system in place, this will have a negative impact on performance (slow the
system down) because whenever a process or thread requests a resource,
the system will have to check whether granting this request could cause a
potential deadlock situation.
Add the current node to L and check to see if it appears twice. If it does,
there is a cycle in the graph.
From the given node, check to see if there are any unmarked outgoing
edges. If yes, go to the next step, if no, skip the next step
50
Pick an unmarked edge, mark it, then follow it to the new current node and
go to step 3.
We have reached a dead end. Go back to the previous node and make that
the current node. If the current node is the starting Node and there are no
unmarked edges, there are no cycles in the graph. Otherwise, go to step 3.
Let's work through an example with five processes and five resources. Here
is the resource request/allocation graph.
The algorithm needs to search each node; let's start at node P1. We add P1
to L and follow the only edge to R1, marking that edge. R1 is now the
current node so we add that to L, checking to confirm that it is not already in
L. We then follow the unmarked edge to P2, marking the edge, and making
P2 the current node. We add P2 to L, checking to make sure that it is not
already in L, and follow the edge to R2. This makes R2 the current node, so
we add it to L, checking to make sure that it is not already there. We are
now at a dead end so we back up, making P2 the current node again. There
are no more unmarked edges from P2 so we back up yet again, making R1
the current node. There are no more unmarked edges from R1 so we back
up yet again, making P1 the current node. Since there are no more
unmarked edges from P1 and since this was our starting point, we are
through with this node (and all of the nodes visited so far).
We move to the next unvisited node P3, and initialize L to empty. We first
follow the unmarked edge to R1, putting R1 on L. Continuing, we make P2
the current node and then R2. Since we are at a dead end, we repeatedly
back up until P3 becomes the current node again.
L now contains P3, R1, P2, and R2. P3 is the current node, and it has
another unmarked edge to R3. We make R3 the current node, add it to L,
follow its edge to P4. We repeat this process, visiting R4, then P5, then R5,
then P3. When we visit P3 again we note that it is already on L, so we have
detected a cycle, meaning that there is a deadlock situation.
Once deadlock has been detected, it is not clear what the system should do
to correct the situation. There are three strategies.
51
the resource is a printer and a print job is half completed. It is often difficult
to restart such a job without completely starting over.
Kill one or more processes – this is the simplest and crudest, but it works.
Deadlock avoidance
The above solution allowed deadlock to happen, then detected that deadlock
had occurred and tried to fix the problem after the fact. Another solution is
to avoid deadlock by only granting resources if granting them cannot result
in a deadlock situation later. However, this works only if the system knows
what requests for resources a process will be making in the future, and this
is an unrealistic assumption. The text describes the bankers algorithm but
then points out that it is essentially impossible to implement because of this
assumption.
Deadlock Prevention
52
and without this assumption, would pick up chopstick 5, thus causing
deadlock. However, if the lower number rule is in effect, he/she has to pick
up chopstick 1 first, and it is already in use, so he/she is blocked.
Philosopher 5 picks up chopstick 5, eats, and puts both down, allows
philosopher 4 to eat. Eventually everyone gets to eat.
Livelock
The Unix file locking system lockf has a deadlock detection mechanism built
into it. Whenever a process attempts to lock a file or a record of a file, the
operating system checks to see if that process has locked other files or
53
records, and if it has, it uses a graph algorithm similar to the one discussed
above to see if granting that request will cause deadlock, and if it does, the
request for the lock will fail, and the lockf system call will return and errno
will be set to EDEADLK.
Killing Zombies
Recall that if a child dies before its parent calls wait, the child becomes a
zombie. In some applications, a web server for example, the parent forks off
lots of children but doesn't care whether the child is dead or alive. For
example, a web server might fork a new process to handle each connection,
and each child dies when the client breaks the connection. Such an
application is at risk of producing many zombies, and zombies can clog up
the process table.
Pipes
What is concurrency
"Concurrency occurs when two or more execution flows are able to run
simultaneously." – Edsger Dijkstra.
54
Race Conditions
A race condition occurs when multiple processes or threads read and write
data items so that the final result depends on the order of execution of
instructions in the multiple processes.
Suppose that two processes, P1 and P2, share the global variable A. At some
point in its execution, P1 updates variable A to the value 1, and at some
point in its execution, P2 updates variable A to the value 2. Thus, the two
processes are in a race to write variable A. In this example the "loser" of the
race (the process that updates last) determines the final value of A.
Critical Section
Mutual Exclusion
That is, while one process executes the shared variable, all other processes
desiring to do so at the same time moment should be kept waiting; when
that process has finished using the shared variable, one of the processes
waiting to do so should be allowed to proceed. In this fashion, each process
using the shared data (variables) excludes all others from doing so
simultaneously. This is called Mutual Exclusion.
55
1. Mutual exclusion must be enforced: Only one process at a time is
allowed into its critical section, among all processes that have critical
sections for the same resource or shared object.
2. A process that halts in its non critical section must do so without
interfering with other processes.
3. It must not be possible for a process requiring access to a critical
section to be delayed indefinitely.
4. When no process is in a critical section, any process that requests
entry to its critical section must be permitted to enter without delay.
5. No assumptions are made about relative process speed or number of
processors.
6. A process remains inside its critical section for a finite time only.
On entry to the critical section the process can disable all interrupts, and on
exit from it can enable them again as shown bellow.
while (true)
/* remainder */;
56
Mutual exclusion by using Lock variable:
process (i)
while(lock != 0)
/* no operation */;
lock = 1;
lock = 0;
/* remainder */;
The flaw in this proposal can be best explained by example. Suppose process
A sees that the lock is 0. Before it can set the lock to 1 another process B is
scheduled, runs, and sets the lock to 1. When the process A runs again, it
will also set the lock to 1, and two processes will be in their critical section
simultaneously. Thus this method does not guarantee mutual exclusion.
In this method, the integer variable 'turn' keeps track of whose turn is to
enter the critical section. Initially, process 0 inspect turn, finds it to be 0,
and enters in its critical section. Process 1 also finds it to be 0 and sits in a
loop continually testing 'turn' to see when it becomes 1. Process 0, after
coming out of critical section, sets turn to 1, to allow process 1 to enter in its
critical section, as shown bellow.
57
/* Process 0 */
while (true)
while(turn != 0)
/* no operation */;
turn = 1;
/* remainder */;
/* Process 1 */
while (true)
while(turn != 1)
/* no operation */;
turn = 0;
/* remainder */;
Taking turns is not a good idea when one of the processes is much slower
than the other. Suppose process 0 finishes its critical section quickly, and
again wants to enter in its critical section, but it cannot do so, as the turn
value is set to 1. It has to wait for process 1 to finish its critical section part.
Here both processes are in their non-critical section. This situation violates
above mutual exclusion requirement condition no. 4.
58
Mutual exclusion by Peterson's Method:
The algorithm uses two variables, flag, a boolean array and turn, an integer.
A true flag value indicates that the process wants to enter the critical section.
The variable turn holds the id of the process whose turn it is. Entrance to the
critical section is granted for process P0 if P1 does not want to enter its critical
section or if P1 has given priority to P0 by setting turn to 0.
flag[0]=false;
flag[1]=false;
turn = 0;
/* Process 0 */
while (true)
flag[0] = true;
turn = 1;
/* no operation */;
flag[0] = false;
/* remainder */;
/* Process 1 */
while (true)
flag[1] = true;
turn = 0;
59
/* no operation */;
flag[1] = false;
/* remainder */;
Test and Set Instruction: The test and set instruction can be defined as
follows:
if (i = = 0)
i=1;
return true;
} else.
return false;
60
Figure : Test and Set Instructions
where the variable i is used like a traffic light. If it is 0, meaning green, then
the instruction sets it 1, i.e. red, and return true. Thus the current process is
permitted to pass but the others are told to stop. On the other hand, if the
light is already red, then the running process will receive false and realize
not supposed to proceed.
int temp;
temp = memory;
memory = register;
register = temp;
Semaphores
All the above methods of mutual exclusion have a basic problem of busy
waiting. If a process is unable to enter in to its critical section; it tightly
executes the loop of testing the shared global variable, wasting CPU time, as
well as resources. Semaphores avoid this wastage of time and resources by
blocking the process if it cannot enter into its critical section. This process
will be wake up by the currently running process after coming out of critical
section.
61
What are Semaphores
Semaphore Implementation:
The wait operation decrements the semaphore value. If the value becomes
negative, the process executing the wait is blocked
The signal operation increments the semaphore value. If the value is not
positive, then the process blocked by wait operation is unblocked. There is
no other way to manipulate semaphores
62
wait (S)
S--;
signal (S)
S++;
Shared data:
Process: Pi:
do
signal(mutex);
/* remainder section */
} while (1);
63
Following code gives the detailed implementation of wait and signal
procedures for above example. The structure definition has semaphore value
and process link. The wait operation decrements the semaphore value, and if
it is less than 0 then adds it to waiting queue and blocks the process.
Declaration:
typedef struct
int value;
} semaphore;
wait(S):
S.value--;
if (S.value < 0)
block;
signal(S):
S.value++;
if (S.value <= 0)
wakeup(P);
64
}
QUESTIONS
1. What do you mean by a deadlock? Explain
2. Discuss various conditions to be true for deadlock to occur.
3. Discuss various applications to overcome the problem of deadlock.
4. What do you mean by a Zombie? Discuss in brief.
5. What is concurrency?
6. Discuss the problems caused by concurrent executions of processes.
7. What is race condition?
8. Describe critical section.
9. What is mutual exclusion? What are its requirements?
10. Discuss the process state with its five state process model.
11. Explain the seven state process models.
12. What is Process Control? Discuss the process control block.
13. What is dispatcher?
14. Write note on Context Switching.
a. CPU-I/O Burst Cycle
b. System call
15. Explain the several common scheduling strategies in brief.
16. Explain the Priority scheduling with a suitable example.
17. Write note on:
a. FCFS Scheduling b. RR Scheduling c. SJF Scheduling
65
Summary
Process Management, operating system function process creation, termination
process Context switching, dispatching, swapping
PCB process details contain Process state
(1) new,
(2) ready,
(3) running,
(4) waiting,
(5) terminate
66
Chapter-3
Objectives
Unit DOS DOS
MS-DOS
Disk Operating System is an operating system, which makes computer
useable, it is a program that has to be loaded to initialize computer.
The single user operating system used in microcomputers is called
DOS because it controls the storage of information and detail on disks.
DOS COMMANDS
Internal Commands
External Commands
The internal DOS commands are those commands, which are loaded
automatically during boot up process and are present in the computer
memory. Internal commands (DOS disk is not needed use any time
except when you are in software package).
67
BREAK
CD CHDIR
CHCP
CLS
COPY
CTTY
DATE
DEL ERASE
DIR
MD
MKDIR
PATH
PROMPT
RD
RMDIR
SET
TIME
TYPE
VER
VOL
VERIFY
LOCK
UNLOCK
LOADHIGH
EXIT
TRUENAME
68
DOS EXTERNAL COMMANDS
These commands are not present in the computer’s memory but these
exists as separate files on the disk. Whenever you type an external
command it is first loaded in the RAM and then execute.
Attrib
The attrib command is used to change the attributes of a single file or
a directory.
Break
The break command sets or clears extended CTRL+C checking.
Call
The call command is used to run a script or batch program from within
another script or batch program.
Cd
The cd command is the shorthand version of the chdir command.
Chcp
The chcp command displays or configures the active code page
number.
69
Chdir
The chdir command is used to display the drive letter and folder that
you are currently in. Chdir can also be used to change the drive and/or
directory that you want to work in.
Chkdsk
The chkdsk command, often referred to as check disk, is used to
identify and correct certain hard drive errors.
Choice
The choice command is used within a script or batch program to
provide a list of choices and return the value of that choice to the
program.
Cls
The cls command clears the screen of all previously entered
commands and other text.
Command
The command command starts a new instance of the command.com
command interpreter.
Copy
The copy command does exactly that - it copies one or more files from
one location to another.
Ctty
The ctty command is used to change the default input and output
devices for the system.
Date
The date command is used to show or change the current date.
Dblspace
The dblspace command is used to create or configure DoubleSpace
drives.
70
Debug
The debug command starts Debug, a command line application used to
test and edit programs.
Defrag
The defrag command is used to defragment a drive you specify. The
defrag command is the command line version of Microsoft's Disk
Defragmenter.
Del
The del command is used to delete one or more files.
Deltree
The deltree command is used to delete a directory and all the files and
subdirectories within it.
Dir
The dir command is used to display a list of files and folders contained
inside the folder that you are currently working in.
The dir command also displays other important information like the
hard drive's serial number, the total number of files listed, their
combined size, the total amount of free space left on the drive, and
more.
Diskcomp
The diskcomp command is used to compare the contents of two floppy
disks.
Diskcopy
The diskcopy command is used to copy the entire contents of one
floppy disk to another.
Doskey
The doskey command is used to edit command lines, create macros,
and recall previously entered commands.
71
Dosshell
The dosshell command starts DOS Shell, a graphical file management
tool for MS-DOS.
The dosshell command was only available up to MS-DOS 6.0 but most
MS-DOS 6.22 installations were upgrades from previous versions so
the dosshell command is usually still available.
Drvspace
The drvspace command is used to create or configure DriveSpace
drives.
Echo
The echo command is used to show messages, most commonly from
within script or batch files. The echo command can also be used to
turn the echoing feature on or off.
Edit
The edit command starts MS-DOS Editor, a tool used to create and
modify text files.
Edlin
The edlin command starts the Edlin tool which is used to create and
modify text files from the command line.
Erase
The erase command is used to delete one or more files.
72
Exit
The exit command is used to end the command.com session that
you're currently working in.
Expand
The expand command is used to extract a single file or a group of files
from a compressed file.
Fasthelp
The fasthelp command provides more detailed information on any of
the other MS-DOS commands.
Fastopen
The fastopen command is used to add a program's hard drive location
to a special list stored in memory, potentially improving the program's
launch time by removing the need for MS-DOS to locate the
application on the drive.
Fc
The fc command is used to compare two individual or sets of files and
then show the differences between them.
Fdisk
The fdisk command is used to create, manage, and delete hard drive
partitions.
Find
The find command is used to search for a specified text string in one
or more files.
For
The for command is used to run a specified command for each file in a
set of files. The for command is most often used within a batch or
script file.
Format
The format command is used to format a drive in the file system that
you specify.
Goto
The goto command is used in a batch or script file to direct the
command process to a labeled line in the script.
73
Graphics
The graphics command is used to load a program that can print
graphics.
Help
The help command provides more detailed information on other DOS
commands.
If
The if command is used to perform conditional functions in a batch file.
Interlnk
The interlnk command is used to connect two computers via a serial or
parallel connection to share files and printers.
Intersvr
The intersvr command is used to start the Interlnk server and to copy
Interlnk files from one computer to another.
Keyb
The keyb command is used to configure a keyboard for a specific
language.
Label
The label command is used to manage the volume label of a disk.
Lh
The lh command is the shorthand version of the loadhigh command.
Loadfix
The loadfix command is used to load the specified program in the first
64K of memory and then runs the program.
Loadhigh
The loadhigh command is used to load a program into high memory
and is usually used from within the autoexec.bat file.
Md
The md command is the shorthand version of the mkdir command.
74
Mem
The mem command shows information about used and free
conventional, upper, reserved, and extended (XMS) memory.
Memmaker
The memmaker command is used to start MemMaker, a memory
optimization tool.
Mkdir
The mkdir command is used to create a new folder.
Mode
The mode command is used to configure system devices, most often
COM and LPT ports.
More
The more command is used to display the information contained in a
text file. The more command can also be used to paginate the results
of any other MS-DOS command.
Move
The move command is used to move one or files from one folder to
another. The move command is also used to rename directories.
Msav
The msav command starts Microsoft Antivirus.
Msbackup
The msbackup command starts Microsoft Backup, a tool used to back
up and restore one or more files.
Mscdex
The mscdex command is used to provide CD-ROM access to MS-DOS.
Msd
The msd command starts Microsoft Diagnostics, a tool used to display
information about your computer.
Nlsfunc
The nlsfunc command is used to load information specific to a
particular country or region.
75
Path
The path command is used to display or set a specific path available to
executable files.
Pause
The pause command is used within a batch or script file to pause the
processing of the file.
Power
The power command is used to reduce the power consumed by a
computer by monitoring software and hardware devices.
Print
The print command is used to print a specified text file to a specified
printing device.
Prompt
The prompt command is used to customize the appearance of the MS-
DOS prompt.
Qbasic
The qbasic command starts QBasic, the MS-DOS based programming
environment for the BASIC programming language.
Rd
The rd command is the shorthand version of the rmdir command.
Rem
The rem command is used to record comments or remarks in a batch
or script file.
Ren
The ren command is the shorthand version of the rename command.
Rename
The rename command is used to change the name of the individual file
that you specify.
76
Replace
The replace command is used to replace one or more files with one or
more other files.
Restore
The restore command is used to restore files that were backed up
using the backup command.
The backup command was only available up to MS-DOS 5.00 but the
restore command was included by default with later versions of MS-
DOS to provide a way to restore files that were backed up in previous
versions of MS-DOS.
Rmdir
The rmdir command is used to delete a directory.
Scandisk
The scandisk command is used to start Microsoft ScanDisk, a disk
repair program.
Set
The set command is used to display, enable, or disable environment
variables in MS-DOS.
Setver
The setver command is used to set the MS-DOS version number that
MS-DOS reports to a program.
Share
The share command is used to install file locking and file sharing
functions in MS-DOS.
Shift
The shift command is used to change the position of replaceable
parameters in a batch or script file.
Smartdrv
The smartdrv command installs and configures SMARTDrive, a disk
caching utility for MS-DOS.
77
Sort
The sort command is used to read data from a specified input, sort
that data, and return the results of that sort to the MS-DOS prompt, a
file, or another output device.
Sys
The sys command is used to copy the MS-DOS system files and
command interpreter to a disk. The sys command is used most often
to create a simple bootable disk or hard drive.
Time
The time command is used to show or change the current time.
Tree
The tree command is used to graphically display the folder structure of
a specified drive or path.
Type
The type command is used to display the information contained in a
text file.
Undelete
The undelete command is used to undo a deletion performed with the
MS-DOS delete command.
Unformat
The unformat command is used to undo the formatting on a drive
performed by the MS-DOS format command.
Ver
The ver command is used to display the current MS-DOS version
number.
Verify
The verify command is used to enable or disable the ability of MS-DOS
to verify that files are written correctly to a disk.
Vol
The vol command shows the volume label and serial number of a
specified disk, assuming this information exists.
78
Vsafe
The vsafe command is used to start Vsafe, a basic virus protection
system for MS-DOS.
Xcopy
The xcopy command can copy one or more files or directory trees from
one location to another.
Summary
Disk Operating System (DOS) operating system command mode
Internal command
external command
79
PART-II
BUSINESS APPLICATION -
FOXPRO
80
Chapter-1
Objectives
Unit database concept
Data manage
DBMS RDBMS
Foxpro unit
80
DATABASE CONCEPTS
The data must be organized for its effective and efficient use. Organize
data serves as a base form which desired information can be retrieved
and meaningful information can be drawn. These databases lie at the
heart of the information system.
1. Manual database are not effective and efficient for huge amount
of data.
2. In a manual database system, each application has its own set of
private files. This often leads to considerable redundancy in
stored data with wastage of storage space.
3. Searching is difficult in a manual database system.
4. Modification in a manual database system is cumbersome.
81
Advantages of Computerized Database System
1. Redundancy is reduced.
2. Integrity can be maintained.
3. Data can be shared.
4. Modification in database structure is possible and relatively
easier.
5. Security checks can be built.
6. Conflicting requirements can be balanced.
7. Searching is easy.
This DBMS acts as a layer between the physical storage of data and the
users of database. All requests from users for access to database are
handled by the DBMS.
Application Programs
DBMS
Operating System
DATABASE
82
Examples of DBMS:
Relational Database
Currently most popular one in DBMS. Data and relationship among data
is represented by tables.
STUDENT Table
83
INST_FEES Table
ADVANTAGES OF RDBMS
1. Multi user environment
2. Security
3. Concurrent operation
4. Query optimization
5. Backup and recovery
6. Transparent indexing
Examples of RDBMS
1. Oracle 2. Ingress
3. Sybase 4. MS-ACCESS
WHAT IS FOXPRO?
FoxPro helps you to design database files as per the requirements and as
per the specified format. It also enables to enter and manage data in
database files. Through simple built-in commands, you can easily edit,
view, and change your data in database.
84
Foxpro acts as an interface between the user and the data stored by the
user. Foxpro is one of the most widely used databases on PCs, and one of
the first to be ported on windows.
It can run on a single user, stand alone machine and also on multiple
user local area network [LAN].
FEATURES OF FOXPRO
Foxpro offers than the capability to create and maintain files; it provides
all the tools needed to create, maintain and extract data. Following are
the main features of FoxPro:
Creating databases
Adding new data to the database
Editing existing data stored in the database
Removing data from the database
Searching and retrieving information from the database
using a powerful tool called RQBE (Relational Query By
Example)
Creating labels using the label designer
Designing and printing custom reports
Simple to moderately complex screens can be created with
screen builder
Managing various types of files, such as tables, screen,
reports, labels, queries, and programs
Foxpro also includes a set of wizards – a graphical interface
that lets you create tables, reports, screens, labels, queries,
etc, very easily
STARTING FOXPRO
After loading windows in the program manager window, double click
foxpro icon for windows. An introductory screen and copyright message
appears. Within a moment, the Foxpro menus and command window
appear.
FOXPRO INTERFACES
Foxpro provides more than one method of interaction with the user: a
Catalog Manager, a Menu system, and a Command window.
85
The Catalog Manager
The Catalog manager feature makes it possible to work in foxpro without
the knowledge of menu options or commands. The catalog allows you to
group various kinds of files, such as database, screens, labels, reports,
queries, and programs. Catalog manager is activated by:
Highlight menu across the top of the screen is called menu bar. Each of
the menu option written in menu bar is called menu pad. Each pad acts
as button.
When you select one of the menu pads, a menu popup will appear,
containing a group of choice called menu options.
When you perform operations with Foxpro, you will find that some of the
menu pads that are no longer relevant to what you are doing will appear
in a lighter color or disappear altogether from the menu bar.
COMMAND WINDOW
Another method in which you can use Foxpro is to enter commands
directly in the Command window. Foxpro saves the commands entered in
the command window in a history list. This makes it easy to recall, edit,
and re-execute a command, position the cursor on the appropriate
command line and press ENTER.
QUITTING FOXPRO
Quitting Foxpro means to exit from Foxpro. Foxpro offers several ways of
ending a Foxpro session.
86
2. Type QUIT in command window and press ENTER.
3. Select Exit from the file menu.
4. Press Alt + F4.
KEY-FEATURES
A database is a collection of related data pertaining to a
certain topic.
A database Management System [DBMS] is the set of data and
the programs needed to manipulate it.
A table is an arrangement of data in a row and column format.
A record is a set of information stored about a particular
entity.
A field is a column in a table that contains particular
information about a record.
Designing a database file involves three major steps: data
definition, data refinement, and establishing relationships
between the fields.
Foxpro is a RDBMS that can store and manipulate large
volumes of data.
Catalog manager, Screen builder, Report generator,
Application creator are some of the main features of Foxpro.
A user can interact with Foxpro using a Catalog manager, a
Menu system, and a Command window.
Summary
87
Foxpro traditional software DBMS RDBMS manage
programming support
Market software
88
Chapter-2
Objectives
Unit Database Database structure
Database unit
89
To create a new database file, choose any of the following methods:
Click OK.
CREATE <filename>
Field Name
90
Data type
Different data types are supported by different DBMS.
Generally , the following data types are supported by all the
DBMS:
1. Character
2. Numeric
3. Float
4. Date
5. Logical
6. Memo
7. General
91
Click OK button
Or
Press Ctrl+W.
After saving the file, a dialog box appears asking for adding new records.
Choose File>>Close
ADDING DATA
Once the table structure is ready, data can be added in the table. Record
are always added at the end of the table.
USING APPEND
APPEND command is used to add data to the end of the database file
that is open in the currently selected work area.
92
Press Ctrl+End or Ctrl+W to save your data entry session.
APPEND
APPEND BLANK
Adds data from another file to the end of the database file that is
currently selected.
VIEWING DATABASE
After adding records to the table, the next logical step is to view the data.
The LIST and DISPLAY, are the two very useful commands for viewing
the data using, using a command window.
USING LIST
93
Syntax: LIST [OFF][scope][field list][FOR <condition>][TO
PRINTER/TO
FILE
<filename>]
Options
Field list : By default, all fields from the table are displayed. Includes only
records.
LIST ROLL_NO,ST_NAME
LIST REST
94
USING DISPLAY
Displays a record from the active database. To display more records the
scope option is used.
e.g.: DISPLAY
NOTE : 1. The main difference between LIST and DISPLAY is that, LIST
command
2. The similarity between the two is when DISPLAY is used with ALL
option.
e.g. : GOTO 4
GO BOTTOM
95
KEY-FEATURES
To store information in a database file, you first need to create it.
To create a database file, select File>>New from the FoxPro menu
or enter Create command.
Define name, type, and width for each field you want to include in
your database.
There can be seven types of fields – character, numeric, float, date,
logical, memo, general in FoxPro.
Use APPEND command to add records in database file.
After entering records, you can use the LIST and DISPLAY
commands to display or print all or selected information.
The GOTO, GO TOP, GO BOTTOM commands can be used to move
the record pointer.
Summary
database
96
Chapter-3
EDITING A DATABASE
Objectives
Unit Database contents
Database records delete delete records
EDIT, CHANGE, and BROWSDE are useful commands for the above
mentioned purposes.
97
Using EDIT / CHANGE
Fields option will display only the fields listed in field list
The NOCLEAR option causes the EDIT display to remain on the screen
after the changes are complete.
e.g. EDIT 4
USING BROWSE
A new record at the end of the database can also be added using
Ctrl+N or select Append Record from Browse menu.
It is also possible to mark records for selection through the browse mode.
98
Syntax : BROWSE [FIELDS <field list> [FOR <expL1>]
FIELDS : The listed fields are displayed in the order specified in <field
list>, <field list>
LOCK : Specifies the number of fields displayed in the left partition of the
Browse window.
e.g. BROWSE
BROWSE NODELETE
The Browse menu is available only in the browse mode. The option
present in this menu are discussed below :
99
Size Field This option resizes a field.
Move Fields This option changes the position of the field.
Resize Partitions This option changes the size of an existing
partition.
GoTo This option is used to go to a specific record.
Seek This option is used to find an indexed record.
Toggle Delete This marks a record for deletion.
Append Record Choosing this option adds a blank record at the
end of the table for adding new data.
DELETING RECORDS
Sometimes, you may not require a particular record. There are three
useful commands to delete unwanted records from the database file
permanently or temporarily, the are Delete, Pack and Zap.
Using DELETE
Options
Scope : The scope clause are ALL, NEXT <n>, REST, Record <n>
<expL1>
e.g. : GOTO 5
DELETE NEXT 5
GOTO 10
DELETE REST
100
1. Browse, Edit or Change can also be used for the deletion purpose.
2. A function DELETED ([<expC> | <expN>]) is used to return logical
value that indicates whether the current record is marked for
deletion.
e.g. List for Deleted ()
Using PACK
Permanently removes all records marked for deletion in the current table.
When PACK is issued, FoxPro copies all records not marked for deletion
to a temporary database file and at last renames the temporary database
file with the original database file.
Syntax : PACK
Using Zap
Removes all records from an active database, file leaving just the table
structure.
Syntax : ZAP
RECALL can be used to recover records provided that a PACK or ZAP has
not been performed on the table.
101
e.g.: RECALL ALL
RECALL NEXT 5
Option
<field1>with <expr1>…. : The data in <field1> is replaced with value
of the expression <expr1> and so on. When the expression value is
longer than the width of a numeric field, REPLACE forces the value to
fit.
102
ADDITIVE : Applies to replacements in memo fields
only. If ADDITIVE is included,
replacement to memo fields are
appended to the end of the memo fields.
KEY-FEATURES
Records can be modified by using the CHANGE, EDIT, and
BROWSE commands.
Records can be removed from the table by using the DELETE
command.
Deleting records is a tow-step process in FoxPro. First the desired
record is marked for deletion. Then the PACK command is given
which physically, removes the record marked for deletion.
103
The RECALL command removes the marker placed against the
record.
The REPLACE command overwrites the contents of specified fields
in a table with new contents.
Add, delete or change the field names, change their types and
width using MODIFY STRUCTURE.
To display a database structure, use DISPLAY STRUCTURE command.
Summary
database concepts
Database structure
104
Chapter-4
Objectives
Unit Database arrange
Index
Index Sorting
Re-index compound index
Records in a table are arranged in the same order in which they are
entered. This arrangement will not be desirable for all applications,
specially when records are to be accessed according to the values in a
specified data field. In a table, data can be kept in a required order using
either of the two methods – Sorting or Indexing. In sorting, the data is
physically copied in the desired order to a new table, whereas in
indexing, a separate file is used to provide a reference as to how the
records in the original table should be arranged.
WHAT IS SORTING ?
Sorting means to arrange, group or classify information in some
meaningful of known order, such as alphabetical, numerical,
alphanumeric or chronological order.
Sorting helps in locating and searching the desired information quickly.
The best example of sorting is found in the dictionary. FoxPro provides
two such commands to sort database file. They are SORT and INDEX
105
The order of the new database depends on the fields and the options
specified. Sorting can be done on a single field or combination of two or
more fields.
The records can be arranged in ascending or descending order.
Syntax: SORT TO filename ON field1 [/A] [/C] [/D]
fieldlist]
Options
/A : For ascending order explicitly on a specific field
(default order)
new file
106
3. Sorting affects the order of the parent database, i.e., it
EMPCODE, BASIC
WHAT IS INDEXING ?
An index of a database file is just like and index of a book. It has a
Types of Indexes
There are two different types of index file :
107
The single index files store a single index based on a single index
expression or key. Many single-entry indexes can be created, but each
one is stored in a separate file and identified by its filename.
A compound index file can store many indexes in a single file. The
indexes are represented in the same manner as in the single entry index,
by specifying an index expression consisting of usually one or more
fields. In addition, each index is given a special name – TAG. The number
of indexes – and therefore tags – is limited only by available memory and
disk space.
Creating an Index
Using Single Index Files
Each index is kept in a separate file, with the extension .IDX.
For better management and to keep index files up to date, open each of
these index files explicitly while adding, editing or deleting records in the
database file.
The COMPACT clause forces the creation of an index file in the compact
format, which takes up less disk space.
108
e.g. : INDEX ON LASTNAME + FIRSTNAME TO M_IDX
USE EMPLOYEE
Or
APPEND
EDIT 7
FoxPro automatically places the newly entered record ant edited record.
109
Structural Compound Indexes
The keyword TAG creates a CDX index tag with the name that is
specified; if it already exists, it adds that tag to the CDX file.
If, ‘OF <cdx file>’, clause is used, the tag will be added to the CDX file
whose name is specified.
The optional ADDITIVE creates a new index file or tag without closing
indexes that are already open.
command is used.
110
To open the database file with tag name use the following command:
will not be required any longer. To delete an index tag from an open
If all the tags in a compound index file are deleted, the file is
automatically deleted.
Index Order
Only one index file (the master index file) or tag (the master tag ) controls
However, all open .IDX and .CDX index files are updated as changes are
made to the database.
111
The SET INDEX command opens the index file(s), and sets the index
order.
Using REINDEX
The REINDEX command rebuilds all open index files of the current
database.
If any changes have been made to the database while its index file was
closed, update the index files with REINDEX.
Syntax : REINDEX
REINDEX
All the disadvantage of SORT become the advantage of INDEX. These are
as follows:
112
In spite of the fact that the data is arranged in the required order
in sorting, FoxPro will still search for the required information
sequentially. In indexing, this problem can be overcome by using
SEEK of FIND commands.
KEY-FEATURES
Data can be kept in a required order by using Sorting and Indexing
The SORT command is used to sort records of a database file on
one or more fields. SORT creates a new database file to store the
sorted records.
Sorting is a slow process, and it cannot be used if data changes
frequently.
Indexing a database file using INDEX command creates an index
file that keeps track of information in the index field as well as the
physical record number in the database file.
A single index file stores a single index based on a single index
expression or key.
A compound index file can store many indexes in a single file.
Summary
database arrange (ascending / descending) order
113
Chapter-5
PERFORMING QUERIES
Objectives
Unit Queries
Queries Unit
Locate continue
Find Seek Locate Continue
Unit
SET Filter Queries
114
Using LOCATE
Sequentially searches the database for the first record that matches a
given logical expression.
If LOCATE finds a matching record, its record number is displayed.
Using CONTINUE
Continues the previous LOCATE. Used after LOCATE succeeds in finding
a record, to continue the LOCATE operation. CONTINUE moves the
record pointer to the next record for which the logical expression
specified in the previous LOCATE evaluates to true (.T.) CONTINUE can
115
be repeated until the end of the file is encountered, or until the end of
the scope, specified with LOCATE is reached.
Syntax : CONTINUE
SEEK searches the current indexed table for the first occurrence of a
record whose index key expression matches a general expression
SEEK is only used with indexed tables, and can search only on the index
key expression.
FOUND( ) function returns true (.T.) and EOF( ) function returns false
(.F.).
Specify the index key expression you want SEEK to search with <expr>
SEEK 55
DISPLAY
Use Employee
SEEK “Kapil”
Or
116
SEEK ‘Kapil’
Or
SEEK [KAPIL]
FIND moves the record pointer to the first record in the database whose
index key matches the character expression <expC>
Find Kapil
DISPLAY
Another problem with FIND and SEEK is the index file requirement. The
problem arises when a search is based on something different from what
you have indexed your file on.
FIND and SEEK is much faster than LOCATE because FIND and SEEK
search in the index file whereas LOCATE takes much time due to the
sequential search in a large database.
117
FIND and SEEK cannot locate logical values while LOCATE can be used
with logical values (.T.) or (.F.)
You can see whether a filter is in effect at any time by using the LIST
STATUS or DISPLAY STATUS command.
For creating view file, first open database and index files, load any
desired format file, and then set a filter condition with the SET FILTER
command.
Here filename is the name for the view file. FoxPro assigns the extension.
View to it.
118
SET VIEW TO filename
Here, filename is the name of the view file. FoxPro will open the same
database and index files. Any filter conditions if saved with view files will
be placed back into effect. To close the view file, the following command
is used :
SET VIEW TO
Summary
unit database queries
119
Chapter-6
FOXPRO’S RELATIONAL POWER
Objectives
Unit Database relation
SET relation command databases linking
Update command
Join command
RELATIONAL DATABASES
FoxPro is a relational manager, i.e., it offers the ability to use more than
one database file at a time, and to establish relationships between them.
120
WHY WE USE MORE THAN ONE DATABASE ?
PARTS.DBF ORDER.DBF
PART_NO N 4 CUST_NO N 4
DESCRIP C 20 CUST_NAME C 20
COST N 7 2 PART_NO N 4
QUANTITY N 4
Using a single file with all of the fields present in these two files.
Each time one customer ordered a part number, that had been
previously ordered by another customer, you would have to enter
the part description and part cost in the database even though the
same part description and part cost had been entered earlier.
To avoid the above duplication, two files are used, both of them
linked together, based upon the common key fields PART_NO.
121
Selecting a Work Area
FoxPro allows more than one table to be opened at a time. In FoxPro 2.5,
there are 225 work areas designated by their respective numbers. The
first ten can be referenced by the letters A to J and rest by using
numbers.
A database file can be opened in each of these work areas. A work area is
simply a place where FoxPro keeps track of a single table, and all related
information.
EMP.DBF
Empno Num 4
Ename Char 15
Salary Num 8 2
Deptno Num 2
DEPT.DBF
Deptno Num 2
Dname Char 20
Location Char 15
122
Data for table DEPT
10 Training Delhi
20 Accounting Patna
30 Research Mumbai
The alias is usually the name of the other database that the active
database is to be linked to.
The process of linking the above two databases using a common field,
Deptno is as follows:
1. Open the file from which you want to establish the relation in one
work area ( in our case it is EMP.DBF).
2. In another work area, open the file to link to the first file. (i.e.
DEPT.DBF)
3. Activate an index file based on the field that is common to both
files.
4. Use the SET RELATION command to establish the link.
SELECT A
USE EMP
SELECT B
SELECT A
123
Multiple Relations
Up to 225 database files can be linked in FoxPro. To explain the concept
of multiple relations let us consider the following example:
City
State
Here, ITEM.DBF and ORDER.DBF are linked on the Stockno field, and
ORDER.DBF and CUSTOMER.DBF on Custno field. To establish multiple
relations of this type, the following sequence of commands are used:
CLOSE DATABASES
SELECT A
USE ITEM
SELECT B
USE ORDER
SELECT C
USE CUSTOMER
SELECT B
124
UPDATING INFORMATION WITH UPDATE
UPDATE command is used to replace the contents of one or more fields
in a database with the contents of one or more fields from another
database. The selected work area must contain the target database that
you are updating. The source database must also be active but in an
unselected work area.
The key field must have the same field name in both the source
and target databases.
e.g.: SELECT A
SELECT B
FIELDS CUSTNO,NAME,A.STOCKNO,A.QUANTITY
125
KEY-FEATURES
Relations on multiple databases can be set using SET RELATION
command.
SELECT command is used to open and choose a work area for the
database.
With the SET RELATION command, you can link multiple database
files through a field that is common to all of them.
JOIN and UPDATE are the commands used on multiple files.
SQL [Structured Query Language], is a data retrieval and
manipulation language.
Summary
unit database
relation relation
126
Chapter-7
MEMO FIELD & FILE HANDLING
Objectives
Unit Memo field
Memo field handle unit
Foxpro file utilities unit
127
It helps in reducing the size of the main database file because the actual
data for the memo field is stored in the auxiliary database file.
With the help of FoxPro’s internal text editor, memo field can easily be
edited.
To enter data in the memo field, move the cursor to it and press Ctrl+Home
or Ctrl+Pgdn or double click on the marker with mouse. FoxPro opens a
window for entering text in the memo field for the current record.
The contents of the memo field can be viewed using the memo field name
with LIST or DISPLAY command.
The above given command changes the memo field width for display to
30 characters.
128
FILE UTILITIES IN FOXPRO
Displaying List of Files
Dir or Directory is the command to display the names of all tables in the
current directory, the number of records in each file, the date each file
was updated, and the size of each file in bytes.
It also displays the total number of files and the number of bytes free on
the disk.
Or
! <command>
COPY commands
Copy File
129
Copy Structure
If a FIELDS clause is included, the new file has only the fields that
are listed.
Copy To
RENAME
Use to assign a new name to any file.
130
Summary
unit long-textual information store memo field
131
Chapter-8
MEMORY VARIABLES AND FUNCTIONS
Objectives
Unit variable value store memory
function
Setting related command Mathematical command
unit
MEMORY VARIABLES
Memory variables are temporary quantities that are stored in the memory
[RAM]. They are needed in FoxPro programs to store values which can be later
used in calculations or to store results of calculation. Up to 256 different data
items can be stored. A unique name, called the memory variable name, needs
to be assigned to each data item.
132
1. It should be maximum of 10 characters in length spaces are not allowed.
2. The first character should be an alphabet.
3. Characters can be alphabets, numerals or the underscore character ( _ ).
Memory variables can be stored & retrieved using SAVE TO and RESTORE
FROM command.
SAVE TO command
This command saves memory variables to a disk files. They can later be
retrieved with the RESTORE FROM command. These command are generally
used in programming.
This command restores memory variables that you previously saved to a disk
file with the SAVE TO command. It is often used in programming.
CLEAR ALL
This command clears a specified item form the screen. Use CLEAR alone to
erase the screen or current output window. Use the ALL modifier to release all
memory variables and arrays, select the primary work area, and close any open
databases.
133
Viewing Data with ? and ??
This command is used to evaluate expressions and display the results. Use the
? to send a carriage-return and linefeed before the results of the expression.
Use the ?? to output results on the current line at the current position of the
screen or printer.
FoxPro FUNCTIONS
ABS() Function Syntax : ABS(<expN>)
This function returns the character expression with all leading and trailing
blanks removed.
This function returns the ASCII number of the first character in any character
expression.
Returns a logical value: indicates or sets the current state of CapsLock. The
logical expression returns the current mode and turns CapsLock on or off, if
the logical expression is not specified, the current CapsLock mode is returned,
but not changed.
?CTOD(C_DATE)
10/02/04
134
CMONTH() Function Syntax : CMONTH(<expD>)
This function returns the month of the date expression in character form.
Returns a numeric value: the nearest integer greater than or equal to a given
numeric expression
This function returns the current system date -- the date on your computer's
clock/calendar or the date you entered when you started your computer.
This function returns the day of the month of the specified date expression in
number form.
11/09/04
? DTOC(DATE())
10/02/04
Returns a numeric value. Finds the integer immediate less than or equal to a
specified value.
135
IIF() Function Syntax : IIF(<expL>,<exp1>,<exp2>)
This function, the "immediate if," returns the value of the first expression if the
logical expression is true or the value of the second expression if the logical
expression is false.
Returns true (.T.) if the leftmost character in the specified character expression
is a lower case alphabetic character. Any characters after the first character in
<expC> are ignored by ISLOWER().
This function returns the length of the specified character expression. The
expression may be a literal or a variable.
This function returns the specified character expression with all its letters
converted to lowercase. It does not effect nonalphabetic characters.
This function returns the specified character expression with all leading blanks
removed.
This function returns the month of the date expression in numeric form.
136
PROPER() Function Syntax : PROPER(<expC>)
This function returns the specified character expression with the initial letter of
each word converted to uppercase and the other letters converted to lowercase.
It does not affect non alphabetic characters.
This function tells you how many records are contained in the database
currently in use.
This function returns the record number of the current record (the record
where the pointer is located).
This function removes spaces following a character expression or field that you
specify.
137
UPPER() Function Syntax : UPPER(<expC>)
This function returns the specified character expression with all alphabetic
characters capitalized. It does not affect any nonalphabetic characters in the
character expression.
This function returns the specified character expression in the numeric data
type, so it can be used in calculations.
MATHEMATICAL COMMANDS
Commands that operates on numeric fields of a database file, such as SUM,
AVERAGE and COUNT.
SUM
AVERAGE
COUNT
138
SET COMMANDS
SET ALTERNATE TO Syntax : SET ALTERNATE TO <filename>
This command designates a text file to which you can send screen output by
using the command set alternate on.
This command saves unformatted screen output in a text file. Used with OFF,
it stops sending screen output to the text file.
This command prevents FoxPro from beeping during data entry when you
reach the end of a field or enter invalid data.
This command carries forward the data that is entered in each record while
you are appending data, so it appears as the default entry in the next record.
This command makes dates appear with all four digits of the year displayed.
SET CENTURY OFF makes dates appear with only the last two digits of the
year displayed.
This command determines whether you must press Enter to move to the next
field when entering data using @… GET …. READ statements.
This command lets you specify color pairs from color scheme 1 and color
scheme 2. It is included here for backward compatibility.
This command lets you specify and position the currency symbol; its default
values are $. Use the expC to specify a string of up to nine characters to
represent the currency symbol.
139
SET DATE TO [option] Default : AMERICAN
This command allows you to change the way dates are displayed. You can
determine the relative order of month, day, and year.
This command lets you change the current drive or directory, where FoxPro
looks for files by default. The path name used is a disk drive designation.
This command controls the trace window, which is useful for debugging
programs. When it is ON, the source code for your program is displayed in the
trace window as it runs, and the line that is currently being executed is
highlighted.
This command controls whether the Esc key can be used to interrupt the
program. If it is used with OFF, the Esc key has no effect and the program
continues to execute.
This command determines whether strings that are compared using the =
operator must be identical for there to be a match.
This command is used with a field list, only the fields that are listed will be
accessible to the user or to other commands. For example, if the user opens a
browse window, it will include only these fields.
This command is used with a logical expression, only the records of the
currently selected database for which the logical expression is evaluated as
true are accessible.
140
SET PROCEDURE TO [<file name>]
This command activates the procedure file that is specified, so procedures and
user-defined functions in it can be used. If the file name does not include an
extension, FoxPro assumes the extension .PRG.
This command lets you relate two files, and its most important use is in
creating relational databases.
This command lets you specify the status bar is displayed at the bottom of the
FoxPro desktop. Use OFF to suppress display of the status bar.
This command determines whether you will be warned before overwriting any files.
KEY-FEATURES
A memory variable is a memory location within the computer that is used
to store data.
Memory variables are referred to by their assigned names.
FoxPro allows you to create up to 256 memory variables.
You can store numeric, character, logical, or date type data in these
variables with the STORE command or by equating.
SAVE command saves all or specified variables in a disk file.
`CLEAR ALL erases all variables from the memory of the PC.
RELEASE command erases all or specified variables from memory.
? displays the specified expression in the next row.
?? displays the specified expression in the same row.
FoxPro supports several functions for manipulation of date and time.
141
Summary
unit value store memory variable
142