Mariam Elbarbary

Without some goal and some effort to reach it, no one can live.

Operating System Concepts C/C++ — December 13, 2012

Operating System Concepts C/C++

This is my first blog post , I hope it is useful .

In this post we will talk about some important concepts in Operating Systems , Mainly we will talk about processes and scheduling in the operating systems.

All the functions and examples given will be written in C/C++.

What is an Operating System?

  • Os controls all computer resources and provides the base upon which the application programs can be written
  • Os is the software layer that is on top of the hardware to manage all parts of the system, and present the user with interface or virtual machine  that is easier to understand and program.

History of OS

OS historically have been closely tied to the architecture of the computers on which, they run. So looking at generation of the computers enables us to see what their OS were like.

  • The first generation: Vacuum tubes and plugboards (1945-55)
  • The second generation: Transistors and batch systems (1955-65)
  • The third generation: ICs and multiprogramming: (1956-1980)
  • The forth generation: Personal Computers (1980- present)
    1. Workstations and network operating systems and distributed systems
    2. MSDOS, UNIX (supports IEEE POSIX standard system calls interface), WINDOWS
    3. MINIX is Unix like operating system written in C for educational purposes (1987)
    4. Linux is the extended version of MINIX.

We will start by the definition of a Process

A process  is basically a single running program. It may be a “system” program (e.g login, update, csh) or program initiated by the user (textedit, dbxtool or a user written one).

Process table: A link list for each existing process that contains all  information about the process, other than the contents of its own address.
When the process is suspended all of its information is saved in process table.
Command interpreter (Shell) reads the user commands from terminal to create a process.

A process can create child processes and communicate with them (interprocess communication)

Process Control: <stdlib.h>,<unistd.h>

When UNIX runs a process it gives each process a unique number – a process ID, pid.

The UNIX command ps will list all current processes running on your machine and will list the pid.

The C function int getpid() will return the pid of process that called this function.

System Calls for process control

you can man each of these function in linux terminal to know more details about them.
fork()
wait()
execl(), execlp(), execv(), execvp()
exit()
signal(sig, handler)
kill(sig, pid)
System Calls for IPC
pipe(fildes)
dup(fd)

Process Execution States

For convenience, we describe a process as being in one of several basic states.
Most basic:
Running
Ready
Blocked (or sleeping)

Context Switching

A context switch involves two processes:
One leaves the Running state
Another enters the Running state
The status (context) of one process is saved; the status of the second  process restored.

Concurrent Processes occurs in several ways

Multiprogramming: Creates logical parallelism by running several processes/threads at a time.  The OS keeps several jobs in memory simultaneously. It selects a job from the ready state and starts executing it. When that job needs to wait for some event the CPU is switched to another job. Primary objective: eliminate CPU idle time

Time sharing: An extension of multiprogramming. After a certain amount of time the CPU is switched to another job regardless of whether the process/thread needs to wait for some operation. Switching between jobs occurs so frequently that the users can interact with each program while it is running.

Multiple processors on a single computer run multiple processes at the same time.  Creates physical parallelism.

Process Scheduling

  • Process scheduling decides which process to dispatch (to the Run state) next.
  • In a multiprogrammed system several processes compete for a single processor
  • Preemptive scheduling: a process can be removed from the Run state before it completes or blocks (timer expires or higher priority process enters Ready state).

Scheduling Algorithms

  • FCFS (first-come, first-served): non-preemptive: processes run until they complete or block themselves for event wait
  • RR (round robin): preemptive FCFS, based on time slice

Time slice  = length of time a process can run before being preempted
Return to Ready state when preempted

Scheduling Goals

Optimize turnaround time and/or response time
Optimize throughput
Avoid starvation (be “fair” )
Respect priorities
Static
Dynamic