CS604 - Operating Systems - Lecture Handout 05

User Rating:  / 0
PoorBest 

Related Content: CS604 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Operating Systems

Summary

  • Browsing UNIX/Linux directory structure
  • Useful UNIX/Linux commands
  • Process concept
  • Process scheduling concepts
  • Process creation and termination

Browsing UNIX/Linux directory structure

We discussed in detail the UNIX/Linux directory structure in lecture 4. We will continue that discussion and learn how to browse the UNIX/Linux directory structure. In Figure 5.1, we have repeated for our reference the home directory structure for students. In the rest of this section, we discuss commands for creating directories, removing directories, and browsing the UNIX/Linux directory structure.

Browsing UNIX-Linux directory structure

Displaying Directory Contents

You can display the contents (names of files and directories) of a directory with the ls command. Without an argument, it assumes your current working directory. So, if you run the ls command right after you login, it displays names of files and directories in your home directory. It does not list those files whose names start with a dot (.). Files that start with a dot are known as hidden files (also called dot files). You should not modify these files unless you are quite familiar with the purpose of these files and why you want to modify them. You can display all the files in a directory by using ls –a command. Your can display the long listing for the contents of a directory by using the ls –l command. The following session shows sample runs of these commands.

$ ls
books courses LinuxKernel chatClient.c chatServer.c
$ ls -a
. .bash_history courses .login .profile
.. .bash_profile .cshrc books
chatClient.c chatServer.c LinuxKernel
$ ls –l
drwxr-xr-x 3 msarwar faculty 512 Oct 28 10:28 books
-rw-r--r-- 1 msarwar faculty 9076 Nov 4 10:14 chatClient.c
-rw-r--r-- 1 msarwar faculty 8440 Nov 4 10:16 chatServer.c
drwxr-xr-x 2 msarwar faculty 512 Feb 27 17:21 courses
drwxr-xr-x 2 msarwar faculty 512 Oct 21 14:55 LinuxKernel
$

The output of the ls –l command gives you the following information about a file:

  • 1st character: type of a file
  • Rest of letters in the 1st field: access privileges on the file
  • 2nd field: number of hard links to the file
  • 3rd field: owner of the file
  • 4th field: Group of the owner
  • 5th field: File size in bytes
  • 6th and 7th fields: Date last updated
  • 8th field: Time last updated
  • 9th field: File name

We will talk about file types and hard links later in the course.

Creating Directories

You can use the mkdir command to create a directory. In the following session, the first command creates the courses directory in your current directory. If we assume that your current directory is your home directory, this command creates the courses directory under your home directory. The second command creates the cs604 directory under the ~/courses directory (i.e., the under the courses directory under your home directory). The third command creates the programs directory under your ~/courses/cs604 directory.

$ mkdir courses
$ mkdir ~/courses/cs604
$ mkdir ~/courses/cs604/programs
$

You could have created all of the above directories with the mkdir –p ~/courses/cs604/programs command.

Removing (Deleting) Directories

You can remove (delete) an empty directory with the mkdir command. The command in the following session is used to remove the ~/courses/cs604/programs directory if it is empty.

$ rmdir courses
$

Changing Directory

You can jump from one directory to another (i.e., change your working directory) with the cd command. You can use the cd ~/courses/cs604/programs command to make ~/courses/cs604/programs directory your working directory. The cd or cd $HOME command can be used to make your home directory your working directory.

Display Absolute Pathname of Your Working Directory

You can display the absolute pathname of your working directory with the pwd command, as shown below.

$ pwd
/home/students/nadeem/courses/cs604/programs
$

Copying, Moving, and Removing Files

We now discuss the commands to copy, move (or rename), and remove files.

Copying Files

You can use the cp command for copying files. You can use the cp file1 file2 command to copy file1 to file2. The following command can be used to copy file1 in your home directory to the ~/memos directory as file2.

$ cp ~/file1 ~/memos/file2
$

Moving Files

You can use the mv command for moving files. You can use the mv file1 file2 command to move file1 to file2. The following command can be used to move file1 in your home directory to the ~/memos directory as file2.

$ mv ~/file1 ~/memos/file2
$

Removing Files

You can use the rm command to remove files. You can use the rm file1 command to remove file1. You can use the first command the following comman to remove the test.c file in the ~/courses/cs604/programs directory and the second command to remove all the files with .o extension (i.e., all object files) in your working directory.

$ rm ~/courses/cs604/programs/test.c
$ rm *.o
$

Compiling and Running C Programs

You can compile your program with the gcc command. The output of the compiler command, i.e., the executable program is stored in the a.out file by default. To compile a source file titled program.c, type:

$ gcc program.c
$

You can run the executable program generated by this command by typing./a.out and hitting the <Enter> key, as shown in the following session.

$ ./a.out
[ ... program output ... ]
$

You can store the executable program in a specific file by using the –o option. For example, in the following session, the executable program is stored in the assignment file.

$ gcc program.c –o assignment
$

The gcc compiler does not link many libraries automatically. You can link a library explicitly by using the –l option. In the following session, we are asking the compiler to link the math library with our object file as it creates the executable file.

$ gcc program.c –o assignment -lm
$ assignment
[ ... program output ... ]
$

Process Concept

A process can be thought of as a program in execution. A process will need certain resources – such as CPU time, memory, files, and I/O devices – to accompany its task.
These resources are allocated to the process either when it is created or while it is executing.
A process is the unit of work in most systems. Such a system consists of a collection of processes: operating system processes execute system code and user processes execute user code. All these processes may execute concurrently.

Although traditionally a process contained only a single thread of control as it ran, most modern operating systems now support processes that have multiple threads.
A batch system executes jobs (background processes), whereas a time-shared system has user programs, or tasks. Even on a single user system, a user may be able to run several programs at one time: a word processor, web browser etc.
A process is more than program code, which is sometimes known as the text section.
It also includes the current activity, as represented by the value of the program counter and the contents of the processor’s register. In addition, a process generally includes the process stack, which contains temporary data (such as method parameters, the process stack, which contains temporary data), and a data section, which contains global variables.
A program by itself is not a process: a program is a passive entity, such as contents of a file stored on disk, whereas a process is an active entity, with a program counter specifying the next instruction to execute and a set of associated resources. Although two processes may be associated with the same program, they are considered two separate sequences of execution. E.g. several users may be running different instances of the mail program, of which the text sections are equivalent but the data sections vary.
Processes may be of two types:

  • IO bound processes: spend more time doing IO than computations, have many short CPU bursts. Word processors and text editors are good examples of such processes.
  • CPU bound processes: spend more time doing computations, few very long CPU bursts.

Process States

As a process executes, it changes states. The state of a process is defined in part by the current activity of that process. Each process may be in either of the following states, as shown in Figure 5.2:

  • New: The process is being created.
  • Running: Instructions are being executed.
  • Waiting: The process is waiting for some event to occur (such as an I/O completion or reception of a signal.
  • Ready: The process is waiting to be assigned to a processor.
  • Terminated: The process has finished execution.

Process States

Process Control Block

Each process is represented in the operating system by a process control block (PCB) – also called a task control block, as shown in Figure 5.3. A PCB contains many pieces of information associated with a specific process, including these:

  • Process state: The state may be new, ready, running, waiting, halted and so on.
  • Program counter: The counter indicates the address of the next instruction to be executed for this process.
  • CPU registers: The registers vary in number and type, depending on the computer architecture. They include accumulators, index registers, stack pointers and general-purpose registers, plus any condition code information. Along with the program counter, this state information must be saved when an interrupt occurs, to allow the process to be continued correctly afterwards.
  • CPU Scheduling information: This information includes a process priority, pointers to scheduling queues, and any other scheduling parameters.
  • Memory-management information: This information may include such information such 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.
  • Accounting information: This information includes the amount of CPU and real time used, time limits, account numbers, job or process numbers, and so on.
  • I/O status information: The information includes the list of I/O devices allocated to the process, a list of open files, and so on.

Process Control Block

Process Scheduling

The objective of multiprogramming is to have some process running all the time so as to maximize CPU utilization. The objective of time-sharing is to switch the CPU among processors so frequently that users can interact with each program while it is running. A uniprocessor system can have only one running process at a given time. If more processes exist, the rest must wait until the CPU is free and can be rescheduled. Switching the CPU from one process to another requires saving of the context of the current process and loading the state of the new process, as shown in Figure 5.4. This is called context switching.

Process Scheduling

Scheduling Queues

As shown in Figure 5.5, a contemporary computer system maintains many scheduling queues. Here is a brief description of some of these queues:

  • Job Queue: As processes enter the system, they are put into a job queue. This queue consists of all processes in the system.
  • Ready Queue: The processes that are residing in main memory and are ready and waiting to execute are kept on a list called the ready queue. This queue is generally stored as a linked list. A ready-queue header contains pointers to the first and final PCBs in the list. Each PCB is extended to include a pointer field that points to the next PCB in the ready queue.
  • Device Queue: When a process is allocated the CPU, it executes for a while, and eventually quits, is interrupted or waits for a particular event, such as completion of an I/O request. In the case of an I/O request, the device may be busy with the I/O request of some other process, hence the list of processes waiting for a particular I/O device is called a device queue. Each device has its own device queue.

Scheduling Queues

In the queuing diagram shown in Figure 5.6 below, each rectangle box represents a queue, and two such queues are present, the ready queue and an I/O queue. A new process is initially put in the ready queue, until it is dispatched. Once the process is executing, one of the several events could occur:

  • The process could issue an I/O request, and then be placed in an I/O queue.
  • The process could create a new sub process and wait for its termination.
  • The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue.

Queuing diagram of a computer system

Schedulers

A process migrates between the various scheduling queues throughout its lifetime. The operating system must select, for scheduling purposes, processes from these queues in some fashion. The appropriate scheduler carries out this selection process. The Longterm scheduler (or job scheduler) selects which processes should be brought into the ready queue, from the job pool that is the list of all jobs in the system. The Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU.
The primary distinction between the two schedulers is the frequency of execution.
The short-term scheduler must select a new process for the CPU frequently. A process may execute for only a few milliseconds before waiting for an I/O request. Often the short-term scheduler executes at least once every 100 milliseconds. Because of the brief time between executions, the short-term scheduler must be fast. If it takes 10 milliseconds to decide to execute a process for 100 milliseconds, then 10/(100+10)=9 % of the CPU is being used for scheduling only. The long-term scheduler, on the other hand executes much less frequently. There may be minutes between the creations of new processes in the system. The long-term scheduler controls the degree of
multiprogramming – the number of processes in memory. If the degree of multiprogramming is stable, then the average rate of process creation must be equal to the average department rate of processes leaving the system. Because of the longer interval between execution s, the long-term scheduler can afford to take more time to select a
process for execution.
The long-term scheduler must select a good mix of I/O bound and CPU bound jobs.
The reason why the long-term scheduler must select a good mix of I/O bound and CPU bound jobs is that if the processes are I/O bound, the ready queue will be mostly empty and the short-term scheduler will have little work. On the other hand, if the processes are mostly CPU bound, then the devices will go unused and the system will be unbalanced.

Some operating systems such as time-sharing systems may introduce a medium-term scheduler, which removes processes from memory (and from active contention for the CPU) and thus reduces the degree of multiprogramming. At some later time the process can be reintroduced at some later stage, this scheme is called swapping. The process is swapped out, and is later swapped in by the medium term scheduler. Swapping may be
necessary to improve the job mix, or because a change is memory requirements has over committed available memory, requiring memory to be freed up. As shown in Figure 5.7, the work carried out by the swapper to move a process from the main memory to disk is known as swap out and moving it back into the main memory is called swap in. The area on the disk where swapped out processes are stored is called the swap space.

Computer system queues, servers, and swappi