SlideShare uma empresa Scribd logo
System Programming
Sayed Chhattan Shah
Associate Professor of Computer Science
Mobile Grid and Cloud Computing Lab
Department of Information Communication Engineering
Hankuk University of Foreign Studies Korea
www.mgclab.com
 Course Syllabus
o eClass link
System Programming
System Programming
 Software is any set of machine-readable instructions that directs a computer's
processor to perform specific operations
o Application Software
o System Software
System Programming
 Application software is a set of one or more programs designed to carry out
operations for a specific application
o Allows users to do things such as create text documents, play games, listen to music
o Application software cannot run on itself but is dependent on system software to
execute
 MS Word
 MS Excel
 Game
 Video Player
System Programming
 System software is computer software designed to operate and control
the computer hardware and to provide a platform for running application
software
 System software can be separated into two different categories:
o Utility software
o Operating systems
System Programming
 Utility software is designed to help analyze, configure, optimize or maintain a
computer
o Anti-virus
o Disk partitions
o Disk compression
System Programming
 An operating system is software that manages computer
hardware and software resources and provides common services for computer
programs
 Examples of popular modern operating systems
o Linux
o Unix
o Android
o iOS
o OS X
o Microsoft Windows
System Programming
 Types of operating systems
o Single- and multi-tasking
 A single-tasking system can only run one program at a time
 A multi-tasking operating system allows more than one program to be
running in concurrency
System Programming
 Types of operating systems
o Single- and multi-user
 Single-user operating systems have no facilities to distinguish users, but
may allow multiple programs to run at the same time.
 A multi-user operating system extends the basic concept of multi-tasking
with facilities that identify processes and resources, such as disk space,
belonging to multiple users, and the system permits multiple users to
interact with the system at the same time
System Programming
 Types of operating systems
o Embedded Operating System
 Embedded operating systems are designed to be used in embedded computer
systems.
o Real-time Operating System
 A real-time operating system is an operating system that guaranties to
process events or data within a certain short amount of time.
o Distributed Operating System
 A distributed operating system manages a group of distinct computers and
makes them appear to be a single computer.
Introduction to UNIX and Linux
 The UNIX operating system was originally developed at Bell Laboratories in 1969
 Initially intended for use inside the Bell System, AT&T licensed Unix to outside
parties leading to a variety of both academic and commercial Unix variants from
vendors
 Multi-user operating system
 Multi-tasking operating system
Introduction to Unix and Linux
 UNIX System Implementations
o Although based on a core set of Unix commands, different distributions have their own
unique commands and features and are designed to work with different types of hardware.
 UNIX System V Release 4 was a product of UNIX System Laboratories at AT&T
 BSD The Berkeley Software Distribution releases were produced and distributed by
Computer Systems Research Group at University of California at Berkeley
 AIX is a series of Unix-based operating environments developed by IBM for its servers.
 Linux is an operating system that provides a rich programming environment similar to that
of a UNIX System. It is freely available under the GNU Public License.
 Solaris is the version of the UNIX System developed by Sun Microsystems
 HP-UX is a series of Unix-based operating environments developed by HP for its servers
Introduction to Unix and Linux
 Operating systems that behave like Unix systems and provide similar utilities,
but do not conform to Unix specification or are not licensed by The Open Group,
are commonly known as Unix-like systems.
 Linux is a freely distributed implementation of a UNIX-like kernel.
 Because Linux takes the UNIX system as its inspiration, Linux and UNIX
programs are very similar.
o Almost all programs written for UNIX can be compiled and run on Linux.
 List of Linux distributions
Introduction to Unix and Linux
 Linux is the leading operating system on servers
 Top 500 supercomputers are running Linux
 Phones
 Cars
 Refrigerators
 Televisions
In-car entertainment system of the Tesla
Model S is based on Ubuntu
Introduction to Unix and Linux
 System Programming is the practice of writing system software
o System software lives at a low level, interfacing directly with the kernel and core
system libraries.
 System programmer must have an acute awareness of the hardware and the
operating system on which they work.
o What is the system-level interface?
o How do I write system-level applications in Linux?
o What exactly do the kernel and the C library provide?
o What interesting system calls are provided in Unix?
o How does it all work?
Introduction to Unix and Linux
 A vast range of programming languages are available for Linux systems
Introduction to Unix and Linux
 UNIX Architecture
o The interface to the kernel is a layer of software called the system calls
o Libraries of common functions are built on top of the system call interface
o Applications are free to use both
o The shell is a special application that provides an interface for running other applications
 The material in these slides is taken from different sources including:
o The Linux Programming Interface by Michael Kerrisk
o Advanced Programming in the Unix Environment by W. Richard Stevens
o Data and Computer Communications by William Stallings
o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/
Acknowledgements
Operating System Overview
Sayed Chhattan Shah
Associate Professor of Computer Science
Mobile Grid and Cloud Computing Lab
Department of Information Communication Engineering
Hankuk University of Foreign Studies Korea
www.mgclab.com
Operating System
 The term operating system is commonly used with two different
meanings
o To denote entire package consisting of the central software managing a
computer’s resources and all of the accompanying standard software
tools, such as command-line interpreters, graphical user interfaces, file
utilities, and editors.
o To refer to the central software that manages and allocates computer
resources.
 The term kernel is often used as a synonym for the second meaning
Kernel
Operating System
Lines of code
Ubuntu Linux
Kernel
Windows Fedora 9
50 million 22 million Over 50 million 204 million
Process Management
 A process is a program in execution
o Program is a passive entity, process is an active entity
 Process needs resources to accomplish its task
o CPU, memory, I/O, files
o Initialization data
 Process termination requires reclaim of any reusable resources
 Typically system has many processes, some user, some operating
system running concurrently on one or more CPUs
Process Management Activities
 Creating and deleting both user and system processes
 Suspending and resuming processes
 Providing mechanisms for process synchronization
 Providing mechanisms for process communication
 Providing mechanisms for deadlock handling
The operating system is responsible for the following activities in
connection with process management:
Memory Management
 To execute a program the instructions must be in memory
 The data that is needed by the program must be in memory
 Memory management activities
o Keeping track of which parts of memory are currently being used and by whom
o Deciding which processes and data to move into and out of memory
o Allocating and deallocating memory space as needed
Storage Management
 OS provides uniform, logical view of information storage
 Abstracts physical properties of storage devices to define a logical
storage unit
o Each medium is controlled by device
 Varying properties include access speed, capacity, data transfer
rate, access method such as sequential or random
Storage Management
 File system management
o Files usually organized into directories
o Access control on most systems to determine who can access what
o File system management activities
 Creating and deleting files and directories
 Primitives to manipulate files and directories
 Mapping files onto secondary storage
 Backup files onto stable non-volatile storage media
IO Subsystem
 One purpose of OS is to hide peculiarities of hardware devices
from the user
 IO subsystem responsible for
o Memory management of IO
o General device-driver interface
o Drivers for specific hardware devices
Protection and Security
 Protection any mechanism for controlling access of processes or
users to resources defined by the OS
 Security defense of the system against internal and external attacks
 Systems generally first distinguish among users, to determine who
can do what
o User identities include name and associated number
o User ID is associated with all files, processes to determine access control
o Group identifier allows set of users to be defined and controls managed,
then also associated with each process and file
Operating System Services
Operating System Services
 One set of services provides functions that are helpful to the user
o User interface
 Almost all operating systems have a user interface that varies between command-
line and graphics user interface
o Program execution
 The system must be able to load a program into memory and to run that program,
end execution, either normally or abnormally
o IO operations
 A running program may require I/O, which may involve a file or an I/O device
Operating System Services
o File-system manipulation
 The file system is of particular interest. Programs need to read and write
files and directories, create and delete them, search them, list file
Information, permission management.
o Communications
 Processes may exchange information, on the same computer or between
computers over a network
o Error detection
 OS needs to be constantly aware of possible errors
• May occur in the CPU and memory hardware, I/O devices, and user program
• For each type of error, OS should take the appropriate action to ensure correct
and consistent computing
Operating System Services
 Another set of OS functions exists for ensuring the efficient operation of
the system itself via resource sharing
o Resource allocation
 When multiple users or multiple jobs running concurrently, resources such as
CPU and main memory must be allocated to each of them
o Accounting
 To keep track of which users use how much and what kinds of computer
resources
o Protection and security
 The owners of information stored in a multiuser or networked computer system
may want to control use of that information, concurrent processes should not
interfere with each other
Kernel mode and User mode
 Modern processor architectures typically allow the CPU to operate in at
least two different modes: user mode and kernel mode
o When running in user mode the CPU can access only memory that is
marked as being in user space
 attempts to access memory in kernel space result in a hardware exception.
o When running in kernel mode the CPU can access both user and kernel
memory space
User and Operating System Interface
User and Operating System Interface
 There are several ways for users to interface with the operating system
o Command-line interface
o Graphical user interface or GUI
 CLI or command interpreter allows direct command entry
o Sometimes implemented in kernel, sometimes by systems program
o Primarily fetches a command from user and executes it
o Sometimes commands built-in, sometimes just names of programs
User and Operating System Interface
Bourne Shell Command Interpreter
 User friendly desktop metaphor interface
o Icons represent files, programs, actions, etc.
o Various mouse buttons over objects in the interface cause various actions
o Invented at Xerox PARC
 Many systems now include both CLI and GUI interfaces
o Microsoft Windows
o Apple Mac OS X
o Unix and Linux have CLI with optional GUI interfaces
GUI
Touchscreen Interfaces
 Touchscreen devices require new
interfaces
 Mouse not possible or not desired
 Actions and selection based on
gestures
 Virtual keyboard for text entry
 Voice commands
Evolution of Operating Systems
 A major OS will evolve over time for a number of reasons:
Hardware upgrades
Hardware upgrades
New types of hardware
New types of hardware
New services
New services
Fixes
Fixes
 How operating systems have evolved over the years?
Serial Processing
Serial Processing
Simple Batch Systems
Simple Batch Systems
Multiprogrammed Batch Systems
Multiprogrammed Batch Systems
Time Sharing Systems
Time Sharing Systems
Evolution of Operating Systems
Serial Processing
Earliest Computers
 No operating system
 programmers interacted
directly with the computer
hardware
 Computers ran from a console with
display lights, toggle switches,
some form of input device, and a
printer
Problems
 Scheduling:
o most installations used a hardcopy
sign-up sheet to reserve computer
time
 time allocations could
run short or long,
resulting in wasted
computer time
 Setup time
o a considerable amount of time was
spent just on setting up the program
to run
Simple Batch Systems
 Early computers were very expensive
 To improve utilization, the concept of a batch OS was developed
 The key idea is to use a software known as Monitor
 User no longer has direct access to processor
 Job is submitted to computer operator who batches them together and
places them on an input device
 Program branches back to the monitor when finished
 Monitor automatically begins loading the next program
Multiprogrammed Batch Systems
 Processor is often idle
 I/O devices are slow compared to processor
Uniprogramming
 The processor spends a certain amount of time executing, until it
reaches an I/O instruction; it must then wait until that I/O instruction
concludes before proceeding
Multiprogramming
 When one job needs to wait for IO the processor can switch to the other
job, which is likely not waiting for IO
 Multiprogramming also known as multitasking
 Memory is expanded to hold three, four, or more programs
and switch among all of them
Multiprogramming
Time-Sharing Systems
 Time sharing systems can be used to handle multiple interactive jobs
 Processor time is shared among multiple users
 Multiple users simultaneously access the system through terminals, with the OS
interleaving the execution of each user program in a short burst or quantum of
computation
System Calls
 System calls provide an interface to the services made available
by an operating system
Operating System Design
 User goals and System goals
o User goals
 Operating system should be convenient to use, easy to learn,
reliable, safe, and fast
o System goals
 Operating system should be easy to design, implement, and
maintain, as well as flexible, reliable, error-free, and efficient
Operating System Implementation
 Much variation
o Early operating systems in assembly language
o Then system programming languages like Algol
o Now C, C++
 Actually usually a mix of languages
o Lowest levels in assembly
o Main body in C
o Systems programs in C, C++, scripting languages like Perl, Python, shell
scripts
 More high-level language easier to port to other hardware but slower
Operating System Structure
 General-purpose OS is very large program
 Various ways to structure ones
o Simple structure: MS-DOS
o More complex: UNIX
o Layered: an abstraction
o Microkernel: Mach
Simple Structure : MS-DOS
 MS-DOS written to provide the
most functionality in the least
space
o Not divided into modules
o Although MS-DOS has some
structure, its interfaces and
levels of functionality are not
well separated
Non Simple Structure : UNIX
UNIX – limited by hardware functionality, the original UNIX
operating system had limited structuring.
The UNIX OS consists of two separable parts
o Systems programs
o The kernel
 Consists of everything below the system-call interface and above
the physical hardware
 Provides the file system, CPU scheduling, memory management,
and other operating-system functions; a large number of functions
for one level
Layered Approach
 The operating system is divided into a
number of layers
 The bottom layer is the hardware and
the highest is the user interface
 Each uses functions and services of
only lower level layers
Microkernel System Structure
 As UNIX expanded the kernel became large and difficult to manage
 Microkernel approach
o This method structures the operating system by removing all
nonessential components from the kernel and implementing them as
system and user-level programs.
 Mac OS X kernel
o Benefits
 Easier to extend a microkernel
 Easier to port the operating system to new architectures
o Disadvantages
 Performance overhead of user space to kernel space communication
Modules
 Many modern operating systems implement loadable kernel
modules
o The kernel has a set of core components and links in additional
services via modules, either at boot time or during run time
Hybrid Systems
 Most modern operating systems are actually not one pure model
o Hybrid combines multiple approaches to address performance,
security, usability needs
o Linux and Solaris kernels in kernel address space, so monolithic,
plus modular for dynamic loading of functionality
iOS
 Apple mobile OS for iPhone and iPad
o Structured on Mac OS X, added functionality
o Cocoa Touch Objective-C API for developing apps
o Media services layer for graphics, audio, video
o Core services provides cloud computing, databases
o Core operating system, based on Mac OS X kernel
Android
 Developed by Open Handset Alliance mostly Google
o Open Source
 Based on Linux kernel but modified
o Provides process, memory, device-driver management
o Adds power management
 Runtime environment includes core set of libraries and Dalvik
virtual machine
o Apps developed in Java plus Android API
 Java class files compiled to Java bytecode then translated to executable
than runs in Dalvik VM
 The material in these slides is taken from different sources including:
o The Linux Programming Interface by Michael Kerrisk
o Advanced Programming in the Unix Environment by W. Richard Stevens
o Data and Computer Communications by William Stallings
o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/
Acknowledgements
Shell Programming
Sayed Chhattan Shah
Associate Professor of Computer Science
Mobile Grid and Cloud Computing Lab
Department of Information Communication Engineering
Hankuk University of Foreign Studies Korea
www.mgclab.com
Shell Programming
 A shell is a program that acts as the interface between you and the Linux system
o It takes commands from keyboard and gives them to operating system to perform
o Some of the most powerful things that you can do are most easily done from the shell
Shell Programming
 The Bourne Shell
 The C Shell
 The Korn Shell
 The GNU Bourne-Again Shell or Bash
Shell Programming
 An OS starts a shell for each user when the user logs in or opens a terminal
window.
Basic Shell Commands
 Navigate Directory
Basic Shell Commands
 File or Directory Manipulation
Basic Shell Commands
 Viewing Files
Basic Shell Commands
 Locating Commands
Basic Shell Commands
 Install Program or Package
Basic Shell Commands
 Generic Useful Bash Syntax
Shell Programming
 A shell script is a sequence of commands
 To create a shell script
o Open a new empty file in editor such as vim and gedit
o Put UNIX commands in the new empty file
o Save file
o Make your new file executable using chmod command
with an option +x
o Execute new script by prefixing its name with ./
Shell Programming
 Hello World bash shell script
 #! characters tell the system that the argument that follows on the line is the program to be
used to execute this file.
Shell Programming
 Script used to backup user home directory
Shell Programming
 Variables
o All variables are considered and stored as strings, even when they are assigned numeric values
o The shell and some utilities will convert numeric strings to their values in order to operate on
them as required
o Linux is a case-sensitive
Shell Programming
 Variables
o You can assign user input to a variable by using the read command
Shell Programming
 Output of whoami and date command will be directly assigned to the user variable
 Variables can also be used directly on the command line
Shell Programming
The script uses a new shell scripting trick ${parameter} called parameter
expansion. Curly braces are required because variable $user is followed by
characters which are not part of its variable name.
Shell Programming
 Positional Parameters
o Positional parameters are assigned via command line arguments and are accessible
within a script as variables.
o During the script execution, any additional items supplied after the program name are
considered arguments and are available during the script execution.
Shell Programming
 Input, Output and Error Redirections
o When you execute a command, three possible outcomes might happen
 Command will produce an expected output
 Command will generate an error
 Command might not produce any output at all
Shell Programming
 Input, Output and Error Redirections
o notation is used to redirect stdout to a file
o notation is used to redirect stderr
o both
Shell Programming
 Input, Output and Error Redirections
o notation is used to redirect stdout to a file
o notation is used to redirect stderr
o both
Shell Programming
 Input, Output and Error Redirections
no tar stderr message will be displayed
Shell Programming
 Input, Output and Error Redirections
o To accept command input from a file use < notation
Shell Programming
 Pipes
o You can connect processes using the pipe operator |
Shell Programming
 Functions allow a programmer to organize and reuse code, hence increasing the
efficiency as well as readability of the entire script
 You can think of the function as a way to the group number of different
commands into a single command
Shell Programming
Shell Programming
 Numeric and String Comparisons
Shell Programming
 File Conditionals
Shell Programming
 Numeric and String Comparisons
Shell Programming
 Numeric and String Comparisons
Shell Programming
 Conditional Statements
Shell Programming
 Conditional Statements
Shell Programming
Shell Programming
 Conditional Statements
Shell Programming
 Conditional Statements
Shell Programming
 Loops
 let command evaluates a mathematical expression and stores its result into a variable
Shell Programming
Shell Programming
Shell Programming
 Bash Arithmetic
o Enclose any mathematical expression inside double parentheses
Shell Programming
 Bash Arithmetic
o Another alternative to arithmetic expansion is the expr command
o Using the expr command allows us to perform an arithmetic operation even without
enclosing our mathematical expression within brackets or quotes
o Do not forget to escape asterisk multiplication sign to avoid syntax error
Shell Programming
 Bash Arithmetic
o Use bc command for decimal numbers
o bc command will produce all results as an integer number
o Use scale=x to instruct the bc command to show real numbers
Shell Programming
 Bash Reference Manual
Structures
Structures
 A structure is a collection of related data items
 keyword struct in C is used to group related data of
different types
 Array is homogeneous since it can contain only data
of same type
 Individual components of a struct type are called
members or fields
 Definition of a structure:
struct <struct-type>{
<type> <identifier_list>;
<type> <identifier_list>;
...
} ;
struct Date {
int day;
int month;
int year;
} ;
Structures
struct StudentInfo{
int Id;
int age;
char Gender;
double CGA;
};
struct StudentGrade{
char Name[15];
char Course[9];
int Lab[5];
int Homework[3];
int Exam[2];
};
Structures
struct BankAccount{
char Name[15];
int AcountNo[10];
double balance;
Date Birthday;
};
struct StudentRecord{
char Name[15];
int Id;
char Gender;
char Dept[5];
};
Structures
 Declaration of a variable of struct type:
<struct-type> <identifier_list>;
 Example:
StudentRecord Student1, Student2;
Student1 and Student2 are variables of
StudentRecord type.
Student1 Student2
Name
Id Gender
Dept
Name
Id Gender
Dept
Structures
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
char address[100];
float percentage;
};
int main()
{
struct student record;
record.id = 1;
strcpy_s(record.name, 10, "LEE");
record.percentage = 86.5;
printf(" ID is: %d n", record.id);
printf(" Name is: %s n", record.name);
printf(" Percentage is: %.1f n", record.percentage);
}
#include <stdio.h>
#include <string.h>
#include <conio.h>
struct Students
{
int ID;
char name[20];
char address[200];
};
int main()
{
struct Students std[100];
int index = 0;
int next = 1;
while (1)
{
system("cls");
printf(" n");
printf("nEnter student ID: ");
scanf_s(" %d", &std[index].ID);
getchar();
printf("nEnter student name: ");
gets(std[index].name);
printf("nEnter student address: ");
gets(std[index].address);
index++;
printf("nRecord savedn");
printf("nWould you like to add another student record? n<< Enter 1 to continue and 0 to exit>> ");
scanf_s("%d", &next);
getchar();
if (next == 0) break;
}
system("cls");
printf("n n");
int i = 0;
for (i = 0; i < index; i++)
{
printf("nID %d ", std[i].ID);
printf("nName %s ", std[i].name);
printf("nAddress %s ", std[i].address);
}
}
 An ordinary array: One type of data
 An array of structs: Multiple types of data in each array
element.
0 1 2 … 98 99
0 1 2 … 98 99
Structures
 We often use arrays of structures.
 Example:
StudentRecord Class[100];
strcpy(Class[98].Name, “Shah");
Class[98].Id = 12345;
strcpy(Class[98].Dept, “ICE");
Class[98].gender = 'M';
Class[0] = Class[98];
. . .
0 1 2 … 98 99
Shah
12345 M
ICE
Structures
Pointers
• In memory, every stored data item occupies one or more contiguous
memory cells.
• The number of memory cells required to store a data item depends on
data item type.
• Whenever we define a variable, the system allocates memory locations
to hold the value of the variable.
14
Basic Concept
char c = ‘A’;
int x = 50;
15
Assume each memory cell is 1 byte
int x = 50;
•This statement instructs the compiler to allocate locations for the integer
variable x, and put the value 50 in those locations.
•During execution of the program, the system always associates the name
x with the address 1380.
•The value 50 can be accessed by using either name x or address 1380.
16
x  variable
50  value
1380  address
Basic Concept
Ampersand (&) operator returns address of the given variable
#include <stdio.h>
int main() {
int x;
char c;
printf("Address of x variable: %xn", &x);
printf("Address of c variable: %xn", &c);
return 0;
}
Pointers
• A pointer is a variable that stores the address of another variable
• p is a pointer to variable v
int *p;
pointer variable declaration
int v;
p = &v;
p stores the address of v
18
Pointers
• The general form of a pointer variable declaration
type *var_name;
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch; // pointer to a character
19
Pointers
• Pointer variables can appear in declarations along with other variables:
int i, j, a[10], b[20], *p, *q;
20
Pointers
Declaring Pointer Variables
• Few important operations, which we will do with the help of pointers
very frequently
 Define a pointer variable
 Assign the address of a variable to a pointer
 Access the value at the address available in the pointer variable
• This is done by using unary operator * that returns the value of the variable located at the
address specified by its operand.
21
22
#include <stdio.h>
int main() {
int v = 20; // actual variable declaration
int *ip; // pointer variable declaration
ip = &v; // store address of var in pointer variable
printf("Address of v variable: %xn", &v);
// address stored in pointer variable
printf("Address stored in ip variable: %xn", ip);
// access the value using the pointer
printf("Value of *ip variable: %dn", *ip);
return 0;
}
The Indirection Operator
• Applying the indirection operator to an uninitialized pointer variable
causes undefined behavior:
int *p;
printf("%d", *p); /*** WRONG ***/
• Assigning a value to *p is particularly dangerous:
int *p;
*p = 1; /*** WRONG ***/
23
Pointer Assignment
• Assume that the following declaration is in effect:
int i, j, *p, *q;
• Example of pointer assignment:
p = &i;
q = p;
q now points to the same place as p:
24
Pointer Assignment
• If p and q both point to i, we can change i by assigning a new value
to either *p or *q:
*p = 1;
*q = 2;
• Any number of pointer variables may point to the same object.
25
Pointer Assignment
p = &i;
q = &j;
i = 1;
*q = *p;
26
Pointer Expressions
• Like other variables, pointer variables can be used in expressions.
• If p1 and p2 are two pointers, the following statements are valid:
27
sum = *p1 + *p2;
prod = *p1 * *p2;
prod = (*p1) * (*p2);
*p1 = *p1 + 2;
x = *p1 / *p2 + 5;
#include <stdio.h>
main()
{
int a, b;
int c = 5;
int *p;
a = 4 * (c + 5);
p = &c;
b = 4 * (*p + 5);
printf("a = %dnb = %dn", a, b);
}
Program: Finding the Largest and Smallest Elements in an Array
• The program uses a function named max_min to find the largest and
smallest elements in an array.
• Prototype for max_min:
void max_min(int a[], int n, int *max, int *min);
• Example call of max_min:
max_min(b, N, &big, &small);
• When max_min finds the largest element in b, it stores the value in big
by assigning it to *max.
• max_min stores smallest element of b in small by assigning it to *min.
29
• The program will read 10 numbers into an array, pass it to the
max_min function, and print the results:
Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31
Largest: 102
Smallest: 7
30
Program: Finding the Largest and Smallest Elements in an Array
31
#include <stdio.h>
#define N 10
void max_min(int a[], int n, int* max, int* min);
int main(void)
{
int b[N], i, big, small;
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++)
scanf_s("%d", &b[i]);
max_min(b, N, &big, &small);
printf("Largest: %dn", big);
printf("Smallest: %dn", small);
return 0;
}
32
void max_min(int a[], int n, int* max, int* min)
{
int i;
*max = *min = a[0];
for (i = 1; i < n; i++) {
if (a[i] > *max)
*max = a[i];
else if (a[i] < *min)
*min = a[i];
}
}
Pointers as Return Values
Functions are allowed to return pointers:
#include <stdio.h>
int* max(int *a, int *b)
{
if (*a > *b)
return a;
else
return b;
}
int main(void)
{
int* p, i=10, j=20;
p = max(&i, &j);
printf("Largest: %dn", *p);
}
Pointers as Return Values
• A function could also return a pointer to an external variable or to a
static local variable.
• What f will return?
int* f(void)
{
int i=10;
return &i;
}
34
Pointers as Return Values
• Never return a pointer to an automatic local variable:
int* f(void)
{
int i=10;
return &i;
}
The variable i won’t exist after f returns.
35
Using Arguments for Results
• Pass address of variable where you want result stored
• This solves the mystery of why ‘&’ with argument to scanf
scanf("%d ", &data);
read a decimal integer and store in data
36
Pointer Arithmetic
• C allows you to perform some arithmetic operations on pointers.
• Consider
<datatype> *ptrn; //datatype can be int, double or long
37
char* mychar;
short* myshort;
long* mylong;
++mychar;
++myshort;
++mylong;
Assume:
char takes 1 byte
short takes 2 bytes
long takes 4 bytes
Pointers point to the memory locations
1000, 2000, and 3000, respectively
• We can use const to document that a function won’t change an
object whose address is passed to the function.
void f(const int *p)
{
*p = 0; /*** WRONG ***/
}
Attempting to modify *p is an error that the compiler will detect.
40
Using const to Protect Arguments
Pointers and Arrays
• When an array is declared,
• The compiler allocates a base address and sufficient amount of storage to
contain all the elements of the array in contiguous memory locations.
• The base address is the location of the first element (index 0) of the array.
41
Pointers and Arrays
• Consider the declaration:
int x[5] = {1, 2, 3, 4, 5};
Suppose that the base address of x is 2500, and each integer requires 4 bytes.
42
Element Value Address
x[0] 1 2500
x[1] 2 2504
x[2] 3 2508
x[3] 4 2512
x[4] 5 2516
Both x and &x[0]
have the value 2500
Pointers and Arrays
• Following statements are equivalent
p = x;
p = &x[0];
• We can access successive values of x by using p++ or p-- to move
from one element to another
• Relationship between p and x:
p = &x[0] = 2500
p+1 = &x[1] = 2504
p+2 = &x[2] = 2508
p+3 = &x[3] = 2512
p+4 = &x[4] = 2516
43
Element Value Address
x[0] 1 2500
x[1] 2 2504
x[2] 3 2508
x[3] 4 2512
x[4] 5 2516
Pointers and Arrays
Another example
short sArr[5] = {1,2,3,4,5};
float fArr[5] = {0.5f, 1.5f, 2.5f, 3.5f, 4.5f};
short *sp = sArr;
float *fp = fArr;
// both lines output sp: 1, 2, 3, 4, 5
printf("sp: %d, %d, %d, %d, %dn", *sp, *(sp + 1), *(sp + 2), *(sp + 3), *(sp + 4));
printf("sp: %d, %d, %d, %d, %dn", sp[0], sp[1], sp[2], sp[3], sp[4]);
45
#include <stdio.h>
float avg(int array[], int size)
{
int *p, sum = 0;
p = array;
for (int i = 0; i < size; i++)
sum = sum + *(p + i);
return ((float)sum / size);
}
void main()
{
int x[5] = {10,20,30,40,50};
printf("nAverage is %f", avg(x, 5));
}
Pointers and struct
• To access members of a structure using pointers, we use the  operator
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main()
{
struct person* personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf_s("%d", &personPtr->age);
printf("Enter weight: ");
scanf_s("%f", &personPtr->weight);
printf("Displaying:n");
printf("Age: %dn", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
Pointers, Functions, and struct
#include <stdio.h>
struct person
{
int age;
float weight;
};
void display(struct person *p) {
printf("Displaying:n");
printf("Age: %dn", p->age);
printf("weight: %f", p->weight);
}
int main()
{
struct person* personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf_s("%d", &personPtr->age);
printf("Enter weight: ");
scanf_s("%f", &personPtr->weight);
display(personPtr);
return 0;
}
48
C File Input and Output
C File Input and Output
• Types of Files
 Text files
 Text file is human readable because everything is stored in terms of text.
 Binary files
 In binary file everything is written in terms of 0 and 1, therefore binary file is
not human readable.
49
• File Operations
 Creating a new file
 Opening an existing file
 Closing a file
 Reading from and writing information to a file
50
C File Input and Output
• File Operations
 When working with files, you need to declare a pointer of type FILE
FILE *fptr;
 The FILE structure is the basic data type used when handling files with
the standard C library.
51
C File Input and Output
• File Operations
 In order to work with a file, we must open it first, using the fopen
function.
ptr = fopen("fileopen","mode");
Example
fopen("E:cprogramFile_A.txt","w");
52
C File Input and Output
• File Operations
 Once we are done working with the file, we need to close it
fclose(fptr);
// fptr is a file pointer associated with the file to be closed
 fclose returns 0 on success or -1 on failure
55
C File Input and Output
• Character input functions
 fgetc() returns one character from a file
 fgets() gets a string from the file ending at newline or end of file
 fscanf() works like the original scanf function
 Return values:
 Next character on success
 EOF on error or end-of-file
57
C File Input and Output
• Character output functions
 fputc() writes one character to a file
 fputs() writes a string to a file
 fprintf() enables printf output to be written to any file
 Return values:
 On success the character that was written
 EOF on error
58
C File Input and Output
Writing to a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE* fptr;
int error = fopen_s(&fptr, "C:UsersShahDocumentsFile_A.txt", "w");
if (error != 0)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf_s("%d", &num);
fprintf(fptr, "%d", num);
fclose(fptr);
return 0;
}
Reading from a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE* fptr;
int error = fopen_s(&fptr,"C:UsersShahDocumentsFile_A.txt", "r");
if (error!=0) {
printf("Error! opening file");
exit(1);
}
fscanf_s(fptr, "%d", &num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}
C program to write a sentence to a file
61
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000];
FILE* fptr;
int error = fopen_s(&fptr, "C:UsersShahDocumentsFile_A.txt", "w");
if (error != 0) {
printf("Error! opening file");
exit(1);
}
printf("Enter a sentence:n");
gets(sentence);
fprintf(fptr, "%s", sentence);
fclose(fptr);
return 0;
}
C program to read a line from a file
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE* fptr;
char str[60];
int error = fopen_s(&fptr, "C:UsersShahDocumentsFile_A.txt", "a+");
if (error != 0) {
printf("Error! opening file");
exit(1);
}
if (fgets(str, 60, fptr) != NULL) {
puts(str);
}
fclose(fptr);
return 0;
}
• Output functions
 fwrite function is used to write records to a file. A record may
be an array or a structure.
fwrite( ptr, int size, int n, FILE *fp )
ptr : reference of an array or a structure stored in memory.
size : total number of bytes to be written.
n : number of times a record will be written.
fp: file where the records will be written.
63
C File Input and Output
• Input functions
 fread() function is used to read bytes form a file.
fread( ptr, int size, int n, FILE *fp )
ptr : reference of an array or a structure where data will be stored after reading.
size : total number of bytes to be read from file.
n : number of times a record will be read.
fp: file where the records will be read.
64
C File Input and Output
Writing and reading structures
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person
{
int id;
char fname[20];
char lname[20];
};
Writing and reading structures
int main() {
FILE* outfile;
// open file for writing
int error = fopen_s(&outfile, "C:UsersShahDocumentsFile_A.txt", "w");
if (error != 0) {
printf("Error! opening file");
exit(1);
}
struct person input1 = { 1, "rohan", "sharma" };
struct person input2 = { 2, "mahendra", "dhoni" };
// write struct to file
fwrite(&input1, sizeof(struct person), 1, outfile);
fwrite(&input2, sizeof(struct person), 1, outfile);
if (fwrite != 0)
printf("contents to file written successfully !n");
else
printf("error writing file !n");
// close file
fclose(outfile);
}
Writing and reading structures
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person
{
int id;
char fname[20];
char lname[20];
};
Writing and reading structures
int main() {
FILE* infile;
struct person input;
// open file for writing
int error = fopen_s(&infile, "C:UsersShahDocumentsFile_A.txt", “r");
if (error != 0) {
printf("Error! opening file");
exit(1);
}
// read file contents till end of file
while (fread(&input, sizeof(struct person), 1, infile))
printf("id = %d name = %s %sn", input.id, input.fname, input.lname);
// close file
fclose(infile);
return 0;
}
Writing and reading structures
#include <stdio.h>
struct student
{
char name[50];
int height;
};
int main() {
struct student stud1[3], stud2[3];
FILE* fptr;
int error = fopen_s(&fptr, "C:UsersShahDocumentsFile_B.txt", "w");
if (error != 0) {
printf("Error! opening file");
exit(1);
}
for (int i = 0; i < 3; ++i)
{
printf("Enter name: ");
gets(stud1[i].name);
printf("Enter height: ");
scanf_s("%d", &stud1[i].height);
getchar();
}
fwrite(stud1, sizeof(stud1), 1, fptr);
fclose(fptr);
Writing and reading structures
error = fopen_s(&fptr, "C:UsersShahDocumentsFile_B.txt", "r");
fread(stud2, sizeof(stud2), 1, fptr);
for (int i = 0; i < 3; ++i)
{
printf("Name: %s Height: %dn", stud2[i].name, stud2[i].height);
}
fclose(fptr);
}
C library functions
• The C library function strtok breaks string into a series of tokens using the
delimiter.
char *strtok(char *string, const char *delimiter)
• Parameters
• string: The contents of this string are modified and broken into smaller strings or tokens.
• delimiter: This is the C string containing the delimiters.
• Return value
• This function returns a pointer to the first token found in the string.
• A null pointer is returned if there are no tokens left to retrieve.
#include <string.h>
#include <stdio.h>
int main(void)
{
char string[] = "A string of tokens and some more tokens";
char seps[] = " ";
char* token = NULL;
char* next_token = NULL;
printf("Tokens:n");
// Establish string and get the first token:
token = strtok_s(string, seps, &next_token);
// While there are tokens in "string"
while ((token != NULL) )
{
// Get next token:
printf(" %sn", token);
token = strtok_s(NULL, seps, &next_token);
}
}
 The material in these slides is taken from different sources including:
o The Linux Programming Interface by Michael Kerrisk
o Advanced Programming in the Unix Environment by W. Richard Stevens
o Data and Computer Communications by William Stallings
o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/
Acknowledgements
File Management
Sayed Chhattan Shah
Associate Professor of Computer Science
Mobile Grid and Cloud Computing Lab
Department of Information Communication Engineering
Hankuk University of Foreign Studies Korea
www.mgclab.com
File Management
 Users and Groups
o Every user has a unique login name and a corresponding user ID
o These are defined in the system password file which also includes
 Group ID the numeric group ID of the first of the groups of which the user is a member
 Home directory the initial directory into which the user is placed after logging in
 Login shell the name of the program to be executed to interpret user commands
File Management
 Users and Groups
o Users are organized into groups
o Each group is identified by a single line in the system group file which includes following
information:
 Group name
 Group ID
 User list a comma-separated list of login names of users who are members of this group
o Superuser with ID 0 and name root has special privileges within the system
File Management
 The kernel maintains a single hierarchical directory structure to organize all files in the system
o In Microsoft Windows each disk device has its own directory hierarchy
 At the base of this hierarchy is the root directory named /
File Management
 Everything is a file in Linux
o Programs can use disk files, printers, and other devices in exactly the same way
 Each file is marked with a type
o Ordinary data files
o Devices
o Pipes
o Sockets
o Directories
 Each file has an associated user ID and group ID that define the owner of the file
and the group to which it belongs.
File Management
 A directory is a special file whose contents take the form of a table of filenames
coupled with references to the corresponding files
 This filename-plus-reference association is called a link
o Files may have multiple links, and thus multiple names, in the same or in different
directories
 Every directory contains at least two entries
o . which is a link to the directory itself and .. which is a link to its parent directory
File Management
 A pathname describes the location of a file within the single directory hierarchy
o An absolute pathname begins with a / and specifies the location of a file with respect
to the root directory
o A relative pathname specifies the location of a file relative to a process’s current
working directory
o Each process has a current working directory
 A file is a sequence of m bytes
File Management
 File descriptor uniquely represents an opened file in operating system
o Each running program has a number of file descriptors associated with it
o File descriptors are small integers that you can use to access open files or devices
 When a program starts, it usually has three of these descriptors already opened.
 File descriptor 0 with the standard input of a process
 File descriptor 1 with the standard output
 File descriptor 2 with the standard error
 You can associate other file descriptors with files and devices by using the open
system call
File Management
 Registers are high speed storage areas in the CPU
o All data must be stored in a register before it can be processed.
File Management
 System call
o Request to the operating system to perform a task
 that the process does not have permission to perform
Linux kernel structure
File Management
 Few points related to system calls
o A system call changes the processor state from user mode to kernel mode
o The set of system calls is fixed
o Each system call is identified by a unique number
o Each system call may have a set of arguments that specify information to be
transferred from user space to kernel space and vice versa
Linux kernel structure
File Management
 From a programming point of view, invoking a system call looks much like
calling a C function but behind the scenes, several steps occur during the
execution of a system call
o The application program makes a system call by invoking a wrapper function in C
library
o The wrapper function must make all of the system call arguments available to the
system call routine
 These arguments are passed to the wrapper via the stack, but the kernel expects them in
specific registers
 The wrapper function copies the arguments to these registers
o The wrapper function copies system call number into a specific CPU register EAX
File Management
o The wrapper function executes a trap machine instruction (int 0x80), which causes
the processor to switch from user mode to kernel mode and execute code pointed to
by location 0x80 (128 decimal) of the system’s trap vector
o In response to the trap to location 0x80, the kernel invokes its system_call() routine to
handle the trap. This handler:
 Saves register values onto the kernel stack
 Checks the validity of the system call number
 Invokes the appropriate system call service routine
 Restores register values from kernel stack and places system call return value on the stack
 Returns to the wrapper function, simultaneously returning the processor to user mode
File Management
File Management
 The flags argument is a bit mask that specifies the access mode for the file, using one of
the constants shown below
 One and only one of the five constants must be specified.
o O_RDONLY Open for reading only.
o O_WRONLY Open for writing only.
o O_RDWR Open for reading and writing.
o O_EXEC Open for execute only.
o O_SEARCH Open for search only. It applies to directories.
File Management
 The following constants are optional:
o O_APPEND Append to the end of file on each write.
o O_CREAT Create the file if it doesn’t exist. This option requires a third argument to the open
function — the mode, which specifies the access permission bits of the new file.
o O_DIRECTORY Generate an error if path doesn’t refer to a directory.
o O_EXCL Generate an error if O_CREAT is also specified and the file already exists.
o O_TRUNC If the file exists and if it is successfully opened for either write-only or read–write,
truncate its length to 0.
File Management
 When open() is used to create a new file, the mode bit-mask argument specifies
the permissions to be placed on the file.
 If the open() call doesn’t specify O_CREAT, mode can be omitted.
File Management
File Management
File Management
 Errors from open()
o If an error occurs while trying to open the file open() returns –1 and errno identifies
the cause of the error
 The following are some possible errors
• EACCES
» The file permissions don’t allow the calling process to open the file in the mode
specified by flags.
• EISDIR
» The specified file is a directory, and the caller attempted to open it for writing.
• EMFILE
» The process resource limit on the number of open file descriptors has been
reached.
File Management
• ENFILE
» The system-wide limit on the number of open files has been reached.
• ENOENT
» The specified file doesn’t exist, and was not specified
• EROFS
» The specified file is on a read-only file system and the caller tried to open it for
writing.
• ETXTBSY
» The specified file is an executable file that is currently executing.
File Management
 count argument specifies the maximum number of bytes to read.
 buffer argument supplies the address of the memory buffer into which the input data is
to be placed.
 ssize_t data type is a signed integer type used to hold a byte count or a –1 error
indication.
File Management
File Management
File Management
 count is the number of bytes to write from buffer
 buffer is the address of the data to be written
 ssize_t data type is a signed integer type used to hold a byte count or a –1 error indication
File Management
 Program simply prints a message to the standard output
File Management
 This program copies the first 128 bytes of the standard input to the standard output. It copies all of
the input if there are fewer than 128 bytes.
File Management
 If you run the program, you should see the following:
o In the first execution, you create some input for the program using echo, which is
piped to your program.
o In the second execution, you redirect input from a file.
File Management
 This program copy one file to another, character by character.
File Management
 Modified program which copies the files in 1K blocks
File Management
 The close() system call closes an open file descriptor, freeing it for subsequent reuse by the process.
 When a process terminates, all of its open file descriptors are automatically closed.
File Management
 For each open file, kernel records a file offset which is the current position for
the next file operation
o The first byte of the file is at offset 0
o On open file offset points to the beginning of the file
o After reading or writing m bytes file offset moves m bytes forward
File Management
 The lseek() system call adjusts the file offset of the open file referred to by the
file descriptor, according to the values specified in offset and whence.
File Management
 offset argument specifies a value in bytes.
 whence argument indicates the base point from which offset is to be interpreted
o SEEK_SET
 The file offset is set offset bytes from the beginning of the file.
o SEEK_CUR
 The file offset is adjusted by offset bytes relative to the current file offset.
File Management
File Management
 Program that copies the contents of the existing file named in its first command line argument to the
new file named in its second command-line argument
File Management
Program that copies the contents of the existing file named in its first command
line argument to the new file named in its second command-line argument
File Management
 Following system call performs a range of control operations on an open file descriptor.
 The cmd argument can specify a wide range of operations
 Third argument can be of different types or it can be omitted
File Management
 Following system calls allow applications to seek and perform I/O atomically
File Management
File Management
 The set of buffers to be transferred is defined by the array iov
 The integer count specifies the number of elements in iov
File Management
 Each element of iov is a structure of the following form:
File Management
File Management
File Management
File Management
File Management
 The Standard IO Library
o It provides a versatile interface to low-level IO system calls and many sophisticated
functions for formatting output and scanning input
o It also takes care of the buffering requirements for devices
o Three file streams are automatically opened when a program is started
 stdin
 stdout
 stderr
File Management
If successful returns a pointer to a FILE object
If fails returns NULL
File Management
File Management
 The fgetc function returns the next byte as a character from a file stream
 The getchar function reads the next character from the standard input
 The fputc function writes a character to an output file stream and returns the value it has
written or EOF on failure.
 The putchar function writes a single character to the standard output
File Management
 stdio library maintains an internal buffer within the FILE structure and the low-level
system calls are made only when the buffer fills
 Write programs using system calls and stdio library functions and check performance
File Management
 The fgets function reads a string from an input file stream.
o fgets writes characters to the string pointed to by s until a newline is encountered, n-1
characters have been transferred, or the end of file is reached.
o When it successfully completes, fgets returns a pointer to the string s
 The gets function is similar to fgets, except that it reads from the standard input
File Management
 Formatted Input and Output
File Management
File Management
File Management
File Management
File Management
 chmod system call is used to change the permissions on a file or directory
 A superuser can change the owner of a file using the chown system call
File Management
 You can create and remove directories using the mkdir and rmdir system calls
 The rmdir system call removes directories, but only if they are empty
File Management
 A program can navigate directories using chdir system call in much the same way as a
user moves around the file system.
 A program can determine its current working directory by calling the getcwd function.
 The getcwd function writes the name of the current directory into the given buffer, buf.
o It returns NULL if the directory name would exceed the size of the buffer given as the parameter
size. It returns buf on success.
File Management
 Errors
o Many of the system calls and functions can fail for a number of reasons.
o When they fail, they indicate the reason for their failure by setting the value of the
external variable errno
o Program must inspect the errno variable immediately after the function giving
problems because it may be overwritten by the next function called
File Management
 Errors
o The values and meanings of the errors are listed in the header file errno.h
o They include
File Management
 The strerror function maps an error number into a string describing the type of error that
has occurred
 The perror function also maps the current error, as reported in errno, into a string and
prints it on the standard error stream.
File Management
 File Sharing
o The kernel uses three data structures to represent an open file
 Every process has an entry in the process table. Within each process table entry is a table of
open file descriptors. Associated with each file descriptor are
• The file descriptor flags
• A pointer to a file table entry
 The kernel maintains a file table for all open files. Each file table entry contains
• The file status flags for the file, such as read, write, and append
• The current file offset
• A pointer to the v-node table entry for the file
 Each open file has a v-node structure that contains information about the type of file and
pointers to functions that operate on the file.
• For most files, the v-node also contains the i-node for the file
File Management
 Tables for a single process that has two different files open:
o one file is open on standard input
o other is open on standard output
File Management
 The first process has the file open on descriptor 3 and that the second process has that
same file open on descriptor 4.
 Each process that opens the file gets its own file table entry, but only a single v-node table
entry is required for a given file.
File Management
 File Sharing
o The UNIX System supports the sharing of open files among different processes
o Each process has its own file table entry with its own current file offset.
o Works fine for multiple processes that are reading the same file.
o Unexpected results can arise when multiple processes write to the same file.
 The material in these slides is taken from different sources including:
o The Linux Programming Interface by Michael Kerrisk
o Advanced Programming in the Unix Environment by W. Richard Stevens
o Data and Computer Communications by William Stallings
o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/
Acknowledgements
Process Management
Sayed Chhattan Shah
Associate Professor of Computer Science
Mobile Grid and Cloud Computing Lab
Department of Information Communication Engineering
Hankuk University of Foreign Studies Korea
www.mgclab.com
 Process
o A program in execution
o A process is an instance of an executing program
o A process is an abstract entity to which system resources are allocated in order
to execute a program
Process Management
 A program is a file containing a range of information that describes how to
construct a process at run time
o Machine-language instructions
 Encode the algorithm of the program
o Program entry-point address
 Location of the instruction at which execution of the program should commence
o Data
 Values used to initialize variables and also literal constants used by the program
o Shared and dynamic library information
 One program may be used to construct many processes
Process Management
 As a process executes it changes state
 A process may be in one of the following states:
o New The process is being created.
o Running Instructions are being executed.
o Waiting The process is waiting for some event to occur such as an I/O completion.
o Ready The process is waiting to be assigned to a processor.
o Terminated The process has finished execution
Process Management
 Process Control Block
 A data structure used by operating system to store all
the information about a process.
 When a process is created, the operating system
creates a corresponding process control block.
Process Management
 PCB includes
o Unique identifier associated with this process
o Process state such as waiting and running state
o Program counter stores address of next instruction to be executed for this process
o CPU registers vary in number and type, depending on the computer architecture
o CPU-scheduling information includes a process priority and scheduling related info
o Memory-management information may include such items as the value of the base and
limit registers and the page tables
o IO status includes list of IO devices allocated to process, a list of open files, and so on
o Accounting information may include the amount of processor time and clock time used,
time limits, account numbers, and so on
Process Management
 Process scheduler selects an available process for program execution on
CPU
 As processes enter the system they are put into a job queue which consists
of all processes in the system
 The processes that are residing in main memory and are ready and waiting
to execute are kept on a list called the ready queue
 The processes that are waiting for an I/O device are kept on a list called the
device or blocked queue
 Processes migrate among the various queues
Process Management
Dispatch
Timeout
Event Wait
Event
Occurs
Ready Queue
Blocked Queue
Admit
Release
Processor
Dispatch
Release
Ready Queue
Admit
Processor
Timeout
(a) Single blocked queue
Figure 3.8 Queuing Model for Figure 3.6
Event 1 Wait
Event 2 Wait
Event n Wait
Event
Occurs
Dispatch
Release
Ready Queue
Admit
Processor
Timeout
Event 1 Queue
Event 1
Occurs
Event 2
Occurs
Event n
Occurs
Event 2 Queue
Event n Queue
(a) Single blocked queue
(b) Multiple blocked queues
 Context Switch
o When CPU switches to another process, the system must save the state of the
old process and load the saved state for the new process via a context switch
o Context of a process represented in the PCB
o Context switch time is overhead
 System does no useful work while switching
 More complex the OS and the PCB longer the context switch
Process Management
 The steps in a full process switch are:
save the context of
the processor
save the context of
the processor
update the process
control block of the
process currently in
the Running state
update the process
control block of the
process currently in
the Running state
move the process
control block of this
process to the
appropriate queue
move the process
control block of this
process to the
appropriate queue
select another
process for
execution
select another
process for
execution
update the process
control block of the
process selected
update the process
control block of the
process selected
update memory
management data
structures
update memory
management data
structures
restore the context of
the processor to that
which existed at the
time the selected
process was last
switched out
restore the context of
the processor to that
which existed at the
time the selected
process was last
switched out
If the currently running process is to be moved to another state
such as Ready, Blocked, etc. then the OS must make substantial
changes in its environment
Process Management
UNIX Process States
Process Management
The Linux kernel limits process IDs to being less than or equal to 32,767.
When a new process is created, it is assigned the next sequentially available process ID.
Process Management
 Memory layout of a process on Linux
o The memory allocated to each process is composed of a number of parts,
usually referred to as segments.
 Text segment contains the machine-language instructions of the program run by the
process.
 Initialized data segment contains global and static variables that are explicitly
initialized.
 Uninitialized data segment contains global and static variables that are not explicitly
initialized.
 Stack is a dynamically growing and shrinking segment containing stack frames.
 One stack frame is allocated for each currently called function.
» A frame stores the function’s local variables arguments, and return value.
 Heap is an area from which memory can be dynamically allocated at run time.
Process Management
Process Management
Process Management
The space labeled argv,
environ at the top of this
diagram holds the
program command-line
arguments
 Programs that require more memory than is physically available
Virtual Memory Management
 Code needs to be in memory to execute, but entire program rarely used
o Error code, unusual routines, large data structures
 Entire program code not needed at same time
Virtual Memory Management
 Consider ability to execute partially-loaded program
o Program no longer constrained by limits of physical memory
o Program and programs could be larger than physical memory
Virtual Memory Management
 Virtual memory is a technique that allows the execution of processes which
are not completely available in memory
Virtual Memory Management
 Virtual memory involves the separation of logical memory as perceived by
users from physical memory
o This separation allows an extremely large virtual memory to be provided for
programmers when only a smaller physical memory is available
Virtual Memory Management
 A virtual memory scheme splits the memory used by each program into
small, fixed-size units called pages
 RAM is divided into a series of page frames of the same size
 At any one time, only some of the pages of a program need to be resident in
physical memory page frames
 Copies of the unused pages of a program are maintained in the swap area
and loaded into physical memory only as required
o Swap area is a reserved area of disk space used to supplement RAM
Virtual Memory Management
 The kernel maintains a page table for each process which describes the
location of each page in the process’s virtual address space
o Each entry in the page table either indicates the location of a virtual page in
RAM or indicates that it currently resides on disk.
Virtual Memory Management
When a process references
a page that is not currently
resident in physical
memory, a page fault
occurs, at which point the
kernel suspends execution
of the process while the
page is loaded from disk
into memory.
Virtual Memory Management
 The Stack and Stack Frames
o The stack grows and shrinks linearly as functions are called and return.
o Each time a function is called, an additional frame is allocated on the stack, and
this frame is removed when the function returns.
o Since functions can call one another,
there may be multiple frames on the stack.
Virtual Memory Management
 The Stack and Stack Frames
o Each stack frame contains the following information:
 Function arguments and local variables
 Call linkage information
 Each function uses certain CPU registers, such as the program counter, which points to
the next machine-language instruction to be executed. Each time one function calls
another, a copy of these registers is saved in the called function’s stack frame so that when
the function returns, the appropriate register values can be restored for the calling
function.
Virtual Memory Management
 A signal is a notification to a process that an event has occurred
o Signals are sometimes described as software interrupts
 One process can send a signal to another process
Signals
 The usual source of many signals sent to a process is the kernel
o A hardware exception occurred, meaning that the hardware detected a fault
condition that was notified to the kernel, which in turn sent a corresponding
signal to the process concerned
 Dividing by 0 or referencing a part of memory that is inaccessible
o The user typed one of the terminal special characters that generate signals
 Control C or Control Z
o A software event occurred
 Input became available on a file descriptor
 A timer went off, the process’s CPU time limit was exceeded
 A child of this process terminated.
Signals
 Signal names are defined by including the header file signal.h
 They all begin with SIG
Signals
Signals
 A program can change action that occurs when the signal is delivered
o The default action should occur
 This is useful to undo an earlier change of the disposition of the signal to something
other than its default
o The signal is ignored
 This is useful for a signal whose default action would be to terminate the process
o A signal handler is executed
 A signal handler is a function, written by the programmer, that performs appropriate
tasks in response to the delivery of a signal
 Notifying the kernel that a handler function should be invoked is usually referred to
as installing or establishing a signal handler
Signals
Signals
Signals
Signals
A simple example of a
signal handler function and
a main program that
establishes it as the handler
for the SIGINT signal.
The terminal
driver generates this signal
when we type the terminal
interrupt character such as
Control C.
Signals
A program that establishes
a handler for SIGINT and
SIGQUIT
Signals
 One process can send a signal to another process using the kill system call
Signals
Signals
Signals
Signals
A simple signal handler
that catches either of the
two user-defined
signals and prints the
signal number
The pause function
suspends the calling
process until a signal is
received
Signals
 In many applications, creating multiple processes can be a useful way of
dividing up a task
o For example, a network server process may listen for incoming client requests
and create a new child process to handle each request
o The server process continues to listen for further client connections.
Process Creation
 The fork system call creates a new process, the child, which is an almost
exact duplicate of the calling process, the parent
Process Creation
 Return of PID and zero allows both the parent and child to determine which is which
Process Creation
 The key point to understanding fork is to realize that after it has completed
its work, two processes exist and in each process, execution continues from
the point where fork returns.
 The two processes are executing the same program text, but they have
separate copies of the stack, data, and heap segments.
o The child’s stack, data, and heap segments are initially exact duplicates of the
corresponding parts the parent’s memory.
o After the fork, each process can modify the variables in its stack, data, and
heap segments without affecting the other process.
Process Creation
Process Creation
This program creates a
child that modifies the
copies of global and
automatic variables
that it inherits
 A process may terminate in two general ways.
o Abnormal termination
o Normal termination using _exit system call
 The status argument given to _exit defines the termination status of the process,
which is available to the parent of this process when it calls wait
 A termination status 0 = process completed successfully and a nonzero status =
process terminated unsuccessfully
 EXIT_SUCCESS (0) and EXIT_FAILURE (1)
Process Termination
 Exit Handlers
o An exit handler is a programmer-supplied function that is registered at some
point during the life of the process and is then automatically called during
normal process termination via exit
o Exit handlers are not called if a program calls _exit directly or if the process is
terminated abnormally by a signal.
Process Management
Process Management
Registration of exit handlers
 Waiting on a Child Process
o In many applications where a parent creates child processes, it is useful for the
parent to be able to monitor the children to find out when and how they
terminate
o The wait system call waits for one of the children of the calling process to
terminate and returns the termination status of that child in the buffer pointed to
by status
Process Management
Process Management
The main program creates
two child processes to
execute the same printing
loop and display a message
before exit.
For the parent process, after
creating two child processes,
it enters the wait state by
executing the system call
wait. Once a child exits, the
parent starts execution and
the ID of the terminated
child process is returned in
PID so that it can be printed.
There are two child
processes and thus two wait
system calls, one for each
child process.
 Waiting on a Child Process
o Parent does not have to wait immediately after creating all child processes. It
may do some other tasks.
o Check eClass for an example
Process Management
 The wait system call has a number of limitations
o If a parent process has created multiple children, it is not possible to wait for
the completion of a specific child
 Parent can only wait for the next child that terminates.
o If no child has yet terminated, wait always blocks
o Using wait, we can find out only about children that have terminated
 It is not possible to be notified when a child is stopped by a signal or when a
stopped child is resumed by delivery of a signal.
Process Management
 The status value returned by wait and waitpid allows us to distinguish the
events for the child
Process Management
This program
creates a child
which immediately
exits.
This program
illustrates the use of
waitpid to wait for a
process to end.
 Orphans and Zombies
o The lifetimes of parent and child processes are usually not the same
o Who becomes the parent of an orphaned child?
 The orphaned child is adopted by init whose process ID is 1.
 init is ancestor of all processes
Process Management
 Orphans and Zombies
o What happens to a child that terminates before its parent has had a chance to
perform a wait?
 The kernel deals with this situation by turning the child into a zombie.
 Most of the resources held by the child are released back to the system to be reused
by other processes.
 The only part of the process that remains is an entry in the kernel’s process table
recording the child’s process ID, termination status, and resource usage statistics.
 When the parent does perform a wait, the kernel removes the zombie
Process Management
 Executing a Shell Command
o The system function allows the calling program to execute an arbitrary shell
command
o system returns 127 if a shell can’t be started to run the command and -1 if another
error occurs. Otherwise, system returns the exit code of the command.
Process Management
The program returns from the call to
system when the ps command has
finished.
ps command display all processes
 A thread is a lightweight process
 Running multiple threads simultaneously is called multithreading
Thread Management
 A single process can contain multiple threads
o All of these threads are independently executing the same program, and they all
share the same global memory, including the initialized data, uninitialized data,
and heap segments.
Thread Management
 Difference between the fork system call and the creation of new threads
o When a process executes a fork call, a new copy of the process is created with
its own variables and its own PID.
o This new process is scheduled independently, and executes almost
independently of the process that created it.
o When we create a new thread in a process, the new thread of execution gets its
own stack and hence local variables but shares global variables, file
descriptors, signal handlers, and its current directory state with the process that
created it.
Thread Management
 Following are some advantages of using threads
o Processes require more time for context switching as they are more heavy. Threads
require less time for context switching as they are lighter than processes.
o Because threads can share common data, they do not need to use interprocess
communication.
o Threads only need a stack and registers therefore, threads are cheap to create.
o Sometimes it is very useful to make a program appear to do two things at once.
 The classic example is to perform a real-time word count on a document while
still editing the text. One thread can manage the user’s input and perform
editing. The other, which can see the same document content, can continuously
update a word count variable.
Thread Management
 Threads also have drawbacks
o Writing multithreaded programs requires very careful design.
o Debugging a multithreaded program is much, much harder than debugging a
single-threaded one because the interactions between the threads are very hard
to control.
Thread Management
 Besides global memory, threads also share a number of other attributes
o Process ID and parent process ID
o Process group ID
o Open file descriptors
o Process credentials such as user and group IDs
o File system–related information: current working directory and root directory
Thread Management
 Among the attributes that are distinct for each thread are the following:
o Thread ID
o Alternate signal stack
o The errno variable
o Real-time scheduling policy and priority
o Local variables and function call linkage information
Thread Management
 Compiling Pthreads programs
o programs that use the Pthreads API must be compiled with the
cc –pthread option
Thread Management
Thread Management
 thread returns the thread id
 attr Set to NULL if default thread attributes are used.
 void * (*start ) pointer to the function to be threaded. Function has a single
argument: pointer to void.
 *arg pointer to argument of function. To pass multiple arguments, send a
pointer to a structure
Thread Management
 Thread Termination
Thread Management
 Thread ID
Thread Management
 Thread ID
Thread Management
 Joining with a Terminated Thread
Threads share
global and static
variables.
Thread Management
 Thread examples on eClass
o Example demonstrates how to pass a simple integer to each thread
o Returning a value from thread
o Returning a value from thread 2
 The material in these slides is taken from different sources including:
o The Linux Programming Interface by Michael Kerrisk
o Advanced Programming in the Unix Environment by W. Richard Stevens
o Data and Computer Communications by William Stallings
o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/
Acknowledgements
Interprocess Communication
Sayed Chhattan Shah
Associate Professor of Computer Science
Mobile Grid and Cloud Computing Lab
Department of Information Communication Engineering
Hankuk University of Foreign Studies Korea
www.mgclab.com
 Interprocess communication (IPC) enables processes to communicate with
each other to share information
Interprocess Communication
 A taxonomy of UNIX IPC facilities
Interprocess Communication
Pipes
 Oldest and perhaps simplest form of UNIX IPC
 Half duplex
o Data flows in only one direction
 Only usable between processes with a common ancestor
o Usually parent-child
Pipes
 Creating and using pipe
Interprocess Communication
Pipes
 Pipes are anonymous
o No name in file system
 How do two processes share a pipe?
Interprocess Communication
Interprocess Communication
Interprocess Communication
A process creates pipe
Writes to pipe
Reads from pipe
Create pipe
Create child
Parent writes to pipe
Child reads from pipe
Program to illustrate
pipe system call shared
by parent and child
Two pipes are used
First pipe to send input
string from parent
Second pipe to send a
string from child
Two pipes are used
First pipe to send input
string from parent
Second pipe to send a
string from child
Two pipes are used
First pipe to send input
string from parent
Second pipe to send a
string from child
FIFO
 FIFOs == pipe with name in file system
FIFO
 Any process can open and use FIFO
o FIFO to be used for communication between unrelated processes
 Once a FIFO has been opened, we use the same I/O system calls as are
used with pipes and other files
C program to
implement one side
of FIFO
This side writes
first, then reads
C program to
implement one side
of FIFO
This side reads first,
then writes
Message Queues
 Message queues allow processes to exchange data in the form of messages
o The reader receives whole messages, as written by the writer. It is not possible
to read part of a message, leaving the remainder in the queue, or to read
multiple messages at a time.
 In addition to containing data, each message has an integer type
o Messages can be retrieved from a queue in first-in, first-out order or retrieved
by type
 A message queue is a linked list of messages stored within the kernel and
identified by a message queue identifier.
Message Queues
Message Queues
Message Queues
Message Queues
Message Queues
Message Queues
C Program for
Message Queue
Writer Process
C Program for
Message Queue
Reader Process
Message Queues
 Client-Server Programming with Message Queues
Message Queues
 Client-Server Programming with Message Queues
o A client-server application that uses one message queue per client
o The application is a simple file server
o The client sends a request message to the server’s message queue asking for the
contents of a named file
o The server responds by returning the file contents as a series of messages to the
client’s private message queue
Message Queues
 Client-Server Programming with Message Queues
Program on eClass
 The material in these slides is taken from different sources including:
o The Linux Programming Interface by Michael Kerrisk
o Advanced Programming in the Unix Environment by W. Richard Stevens
o Data and Computer Communications by William Stallings
o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/
Acknowledgements
Network Interprocess Communication
Sayed Chhattan Shah
Associate Professor of Computer Science
Mobile Grid and Cloud Computing Lab
Department of Information Communication Engineering
Hankuk University of Foreign Studies Korea
www.mgclab.com
 TCP/IP Protocol Architecture
o Referred to as TCP/IP protocol suite
o Comprises a large collection of protocols used on the Internet
o It is commonly known as TCP/IP because the foundational protocols in the suite
are the Transmission Control Protocol (TCP) and the Internet Protocol (IP)
Network Interprocess Communication
Provides ccess to the
TCP/IP environment for
users and also provides
distributed information
services.
Application
Transfer of data between
end points. May provide
error control, flow control,
congestion control, reliable
delivery.
Transport
Shield higher layers from
details of physical network
configuration. Provides
routing. May provide QoS,
congestion control.
Internet
Logical interface to network
hardware. May be stream or
packet oriented. May
provide reliable delivery.
Network Access/
Data Link
Transmission of bit stream;
specifies medium, signal
encoding technique, data
rate, bandwidth, and
physical connector.
Physical
Twisted pair, optical fiber, satellite,
terrestrial microwave
Ethernet, WiFi, ATM, frame relay
IPv4, IPv6
TCP, UDP
SMTP, FTP, SSH, HTTP
ARP
ICMP,
OSPF,
RSVP
 Physical layer is responsible for carrying individual bits in a frame across the
link
 Concerned with issues like:
 Characteristics of transmission medium
 Nature of the signals
Network Interprocess Communication
 Network access layer covers the exchange of data between an end system
and the network that it is attached to
 Concerned with:
 Access to and routing data across a network for two end systems attached to the
same network
Network Interprocess Communication
 Internet layer implements procedures needed to allow data to travel across
multiple interconnected networks
 Implemented in end systems and routers
Network Interprocess Communication
• Transport layer may
provide reliable end-to-
end service or merely an
end-to-end delivery
service without
reliability mechanisms
Transmission Control Protocol
• Most commonly used
protocol to provide this
functionality
TCP
Network Interprocess Communication
 Application layer contains the logic needed to support the various user
applications
 A separate module is needed for each different type of application that is
peculiar to that application
Network Interprocess Communication
Router J
TCP
IP
Physical Physical
IP
NAP 1 NAP 2
Physical Physical
Network Access
Protocol #1
Host A
App X
App Y
TCP
IP
Network Access
Protocol #2
Host B
App Y
App X
Network 1 Network 2
Global internet
address
1 2 2 4 6
3
Subnetwork attachment
point address
Logical connection
(e.g., virtual circuit)
Logical connection
(TCP connection)
Port
Figure 2.4 TCP/IP Concepts
Operation of TCP and IP
Subnetwork Subnetwork
 Two levels of addressing are needed
o Each host must have a unique global internet address
o Each process with a host must have an address known as a port that is unique
within the host
Network Interprocess Communication
 TCP is the transport layer protocol
 TCP provides reliable, ordered and error checked delivery of data between
applications running on computers connected to a local area network or
Internet
 Flow control and congestion control mechanism
Network Interprocess Communication
 User Datagram Protocol
o Alternative to TCP
o Does not guarantee delivery, preservation of sequence, or protection against
duplication
o Enables a procedure to send messages to other procedures with a minimum of
protocol mechanism
o Adds port addressing capability to IP
o Includes a checksum to verify that no error occurs in the data
Network Interprocess Communication
 IP address
 The source and destination address fields in the IP header each contain a 32-bit
global internet address, generally consisting of a network identifier and a host
identifier
Three different notations in IPv4 addressing
Network Interprocess Communication
 Network Classes
 The address is coded to allow a variable allocation of bits to specify network and
host to accommodate both small and large networks
Network Interprocess Communication
 Routers forward packets based on only the network portion of the address
 The host portion does not matter to the routers because all hosts on the same
network will be sent in the same direction
 This makes the routing tables much smaller
Network Interprocess Communication
 A taxonomy of UNIX IPC facilities
Interprocess Communication
Sockets
 Sockets are a method of IPC that allow data to be exchanged between
applications, either on the same host or on different hosts connected by a
network
 Each application creates a socket
o A socket is one end of the communication channel between two applications
 Each socket exists in a domain
 Domain determines:
o Method of identifying socket
o Range of communication
 Processes on a single host
 Across a network
Sockets
 Modern operating systems support at least the following domains
o UNIX domain (AF_UNIX)
 Communication on single host
 Address == file system pathname
o IPv4 domain (AF_INET)
 Communication on IPv4 network
 Address = IPv4 address (32 bit) + port number
o IPv6 domain (AF_INET6)
 Communication on IPv6 network
 Address = IPv6 address (128 bit) + port number
Sockets
 Two main sockets types available in all domains
o Stream (SOCK_STREAM)
o Datagram (SOCK_DGRAM)
Sockets
 Stream sockets
o SOCK_STREAM
o Byte stream
o Connection-oriented
 Like a two-party phone call
o Reliable, ordered, unduplicated
o Uses TCP protocol
Sockets
 Datagram sockets
o SOCK_DGRAM
o Message-oriented
o Connection-less
 Like a postal system
o Unreliable, duplicated, out of order
o Uses UDP protocol
Sockets
 Creating a socket
Sockets
 Binding a socket to an address
This construct holds
the information for
socket address
Sockets
 IPv4 socket addresses
o This construct holds the information about the address family, port number,
Internet address, and the size of the struct sockaddr.
Sockets
 Network byte order
o IP addresses and port numbers are integer values
o Different hardware architectures store the bytes of a multibyte integer in
different orders
Sockets
 Network byte order
o Functions for converting integers in either direction between host and network
byte order
Sockets
 Network byte order
o Following functions allow conversion of both IPv4 and IPv6 addresses between
binary form and dotted-decimal or hex-string notation
Sockets
 Following functions are used to get and set options on sockets
 setsockopt (server_fd, SOL_SOCKET, SO_REUSEADDR |
SO_REUSEPORT, &opt, sizeof(opt))
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming
Introduction to System Programming

Mais conteúdo relacionado

Semelhante a Introduction to System Programming

e.pdf
e.pdfe.pdf
e.pdf
ay3539355
 
Operating system
Operating systemOperating system
Operating system
Ashit_24
 
system management -shell programming by Gaurav raikar
system management -shell programming by Gaurav raikarsystem management -shell programming by Gaurav raikar
system management -shell programming by Gaurav raikar
GauravRaikar3
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarsystem management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
GauravRaikar3
 
UNIT II-Programming in Linux
UNIT II-Programming in LinuxUNIT II-Programming in Linux
UNIT II-Programming in Linux
Dr.YNM
 
OPERATING SYSTEMS INTRODUCTION chapter 1.pptx
OPERATING SYSTEMS INTRODUCTION chapter 1.pptxOPERATING SYSTEMS INTRODUCTION chapter 1.pptx
OPERATING SYSTEMS INTRODUCTION chapter 1.pptx
natyesu
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
Mukesh Chinta
 
Chapter 5.0
Chapter 5.0Chapter 5.0
Chapter 5.0
Adebisi Tolulope
 
UNIX_Module 1.pdf
UNIX_Module 1.pdfUNIX_Module 1.pdf
UNIX_Module 1.pdf
krishnaraj714229
 
os.ppt
os.pptos.ppt
os.ppt
banu236831
 
Introduction to unix (1).pptx
Introduction to unix (1).pptxIntroduction to unix (1).pptx
Introduction to unix (1).pptx
virat834293
 
Operating system
Operating systemOperating system
Operating system
Nasrin Borsha
 
Linux and Java - Understanding and Troubleshooting
Linux and Java - Understanding and TroubleshootingLinux and Java - Understanding and Troubleshooting
Linux and Java - Understanding and Troubleshooting
Jérôme Kehrli
 
os_intro.ppt operating system introduction
os_intro.ppt operating system introductionos_intro.ppt operating system introduction
os_intro.ppt operating system introduction
SHAKIR325211
 
OS Intro
OS IntroOS Intro
OS Intro
ssuser2e34c3
 
An introduction to operating syste- Windows, Linux etc
An introduction to operating syste- Windows, Linux etcAn introduction to operating syste- Windows, Linux etc
An introduction to operating syste- Windows, Linux etc
shelly487611
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
Nishant Munjal
 
Lec 01_Linux System Administration (1).pptx
Lec 01_Linux System Administration (1).pptxLec 01_Linux System Administration (1).pptx
Lec 01_Linux System Administration (1).pptx
ShabanaShafi3
 
Operating system
Operating systemOperating system
Operating system
MusTufa Nullwala
 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating System
subhsikha
 

Semelhante a Introduction to System Programming (20)

e.pdf
e.pdfe.pdf
e.pdf
 
Operating system
Operating systemOperating system
Operating system
 
system management -shell programming by Gaurav raikar
system management -shell programming by Gaurav raikarsystem management -shell programming by Gaurav raikar
system management -shell programming by Gaurav raikar
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarsystem management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
 
UNIT II-Programming in Linux
UNIT II-Programming in LinuxUNIT II-Programming in Linux
UNIT II-Programming in Linux
 
OPERATING SYSTEMS INTRODUCTION chapter 1.pptx
OPERATING SYSTEMS INTRODUCTION chapter 1.pptxOPERATING SYSTEMS INTRODUCTION chapter 1.pptx
OPERATING SYSTEMS INTRODUCTION chapter 1.pptx
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
 
Chapter 5.0
Chapter 5.0Chapter 5.0
Chapter 5.0
 
UNIX_Module 1.pdf
UNIX_Module 1.pdfUNIX_Module 1.pdf
UNIX_Module 1.pdf
 
os.ppt
os.pptos.ppt
os.ppt
 
Introduction to unix (1).pptx
Introduction to unix (1).pptxIntroduction to unix (1).pptx
Introduction to unix (1).pptx
 
Operating system
Operating systemOperating system
Operating system
 
Linux and Java - Understanding and Troubleshooting
Linux and Java - Understanding and TroubleshootingLinux and Java - Understanding and Troubleshooting
Linux and Java - Understanding and Troubleshooting
 
os_intro.ppt operating system introduction
os_intro.ppt operating system introductionos_intro.ppt operating system introduction
os_intro.ppt operating system introduction
 
OS Intro
OS IntroOS Intro
OS Intro
 
An introduction to operating syste- Windows, Linux etc
An introduction to operating syste- Windows, Linux etcAn introduction to operating syste- Windows, Linux etc
An introduction to operating syste- Windows, Linux etc
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Lec 01_Linux System Administration (1).pptx
Lec 01_Linux System Administration (1).pptxLec 01_Linux System Administration (1).pptx
Lec 01_Linux System Administration (1).pptx
 
Operating system
Operating systemOperating system
Operating system
 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating System
 

Mais de Sayed Chhattan Shah

Introduction to Differential Equations
Introduction to Differential EquationsIntroduction to Differential Equations
Introduction to Differential Equations
Sayed Chhattan Shah
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
Sayed Chhattan Shah
 
Cloud and Edge Computing Systems
Cloud and Edge Computing SystemsCloud and Edge Computing Systems
Cloud and Edge Computing Systems
Sayed Chhattan Shah
 
Introduction to Internet of Things
Introduction to Internet of ThingsIntroduction to Internet of Things
Introduction to Internet of Things
Sayed Chhattan Shah
 
IoT Network Technologies
IoT Network TechnologiesIoT Network Technologies
IoT Network Technologies
Sayed Chhattan Shah
 
5G Network: Requirements, Design Principles, Architectures, and Enabling Tech...
5G Network: Requirements, Design Principles, Architectures, and Enabling Tech...5G Network: Requirements, Design Principles, Architectures, and Enabling Tech...
5G Network: Requirements, Design Principles, Architectures, and Enabling Tech...
Sayed Chhattan Shah
 
Data Center Networks
Data Center NetworksData Center Networks
Data Center Networks
Sayed Chhattan Shah
 
IEEE 802.11 Architecture and Services
IEEE 802.11 Architecture and ServicesIEEE 802.11 Architecture and Services
IEEE 802.11 Architecture and Services
Sayed Chhattan Shah
 
Routing in Mobile Ad hoc Networks
Routing in Mobile Ad hoc NetworksRouting in Mobile Ad hoc Networks
Routing in Mobile Ad hoc Networks
Sayed Chhattan Shah
 
Keynote Talk on Recent Advances in Mobile Grid and Cloud Computing
Keynote Talk on Recent Advances in Mobile Grid and Cloud ComputingKeynote Talk on Recent Advances in Mobile Grid and Cloud Computing
Keynote Talk on Recent Advances in Mobile Grid and Cloud Computing
Sayed Chhattan Shah
 
Keynote on Mobile Grid and Cloud Computing
Keynote on Mobile Grid and Cloud ComputingKeynote on Mobile Grid and Cloud Computing
Keynote on Mobile Grid and Cloud Computing
Sayed Chhattan Shah
 
Introduction to Mobile Ad hoc Networks
Introduction to Mobile Ad hoc NetworksIntroduction to Mobile Ad hoc Networks
Introduction to Mobile Ad hoc Networks
Sayed Chhattan Shah
 
Cloud Robotics
Cloud RoboticsCloud Robotics
Cloud Robotics
Sayed Chhattan Shah
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud Computing
Sayed Chhattan Shah
 
Tips on Applying for a Scholarship
Tips on Applying for a ScholarshipTips on Applying for a Scholarship
Tips on Applying for a Scholarship
Sayed Chhattan Shah
 
Cluster and Grid Computing
Cluster and Grid ComputingCluster and Grid Computing
Cluster and Grid Computing
Sayed Chhattan Shah
 
Introduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed ComputingIntroduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed Computing
Sayed Chhattan Shah
 

Mais de Sayed Chhattan Shah (17)

Introduction to Differential Equations
Introduction to Differential EquationsIntroduction to Differential Equations
Introduction to Differential Equations
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
Cloud and Edge Computing Systems
Cloud and Edge Computing SystemsCloud and Edge Computing Systems
Cloud and Edge Computing Systems
 
Introduction to Internet of Things
Introduction to Internet of ThingsIntroduction to Internet of Things
Introduction to Internet of Things
 
IoT Network Technologies
IoT Network TechnologiesIoT Network Technologies
IoT Network Technologies
 
5G Network: Requirements, Design Principles, Architectures, and Enabling Tech...
5G Network: Requirements, Design Principles, Architectures, and Enabling Tech...5G Network: Requirements, Design Principles, Architectures, and Enabling Tech...
5G Network: Requirements, Design Principles, Architectures, and Enabling Tech...
 
Data Center Networks
Data Center NetworksData Center Networks
Data Center Networks
 
IEEE 802.11 Architecture and Services
IEEE 802.11 Architecture and ServicesIEEE 802.11 Architecture and Services
IEEE 802.11 Architecture and Services
 
Routing in Mobile Ad hoc Networks
Routing in Mobile Ad hoc NetworksRouting in Mobile Ad hoc Networks
Routing in Mobile Ad hoc Networks
 
Keynote Talk on Recent Advances in Mobile Grid and Cloud Computing
Keynote Talk on Recent Advances in Mobile Grid and Cloud ComputingKeynote Talk on Recent Advances in Mobile Grid and Cloud Computing
Keynote Talk on Recent Advances in Mobile Grid and Cloud Computing
 
Keynote on Mobile Grid and Cloud Computing
Keynote on Mobile Grid and Cloud ComputingKeynote on Mobile Grid and Cloud Computing
Keynote on Mobile Grid and Cloud Computing
 
Introduction to Mobile Ad hoc Networks
Introduction to Mobile Ad hoc NetworksIntroduction to Mobile Ad hoc Networks
Introduction to Mobile Ad hoc Networks
 
Cloud Robotics
Cloud RoboticsCloud Robotics
Cloud Robotics
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud Computing
 
Tips on Applying for a Scholarship
Tips on Applying for a ScholarshipTips on Applying for a Scholarship
Tips on Applying for a Scholarship
 
Cluster and Grid Computing
Cluster and Grid ComputingCluster and Grid Computing
Cluster and Grid Computing
 
Introduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed ComputingIntroduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed Computing
 

Último

Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 

Último (20)

Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 

Introduction to System Programming

  • 1. System Programming Sayed Chhattan Shah Associate Professor of Computer Science Mobile Grid and Cloud Computing Lab Department of Information Communication Engineering Hankuk University of Foreign Studies Korea www.mgclab.com
  • 2.  Course Syllabus o eClass link System Programming
  • 3. System Programming  Software is any set of machine-readable instructions that directs a computer's processor to perform specific operations o Application Software o System Software
  • 4. System Programming  Application software is a set of one or more programs designed to carry out operations for a specific application o Allows users to do things such as create text documents, play games, listen to music o Application software cannot run on itself but is dependent on system software to execute  MS Word  MS Excel  Game  Video Player
  • 5. System Programming  System software is computer software designed to operate and control the computer hardware and to provide a platform for running application software  System software can be separated into two different categories: o Utility software o Operating systems
  • 6. System Programming  Utility software is designed to help analyze, configure, optimize or maintain a computer o Anti-virus o Disk partitions o Disk compression
  • 7. System Programming  An operating system is software that manages computer hardware and software resources and provides common services for computer programs  Examples of popular modern operating systems o Linux o Unix o Android o iOS o OS X o Microsoft Windows
  • 8. System Programming  Types of operating systems o Single- and multi-tasking  A single-tasking system can only run one program at a time  A multi-tasking operating system allows more than one program to be running in concurrency
  • 9. System Programming  Types of operating systems o Single- and multi-user  Single-user operating systems have no facilities to distinguish users, but may allow multiple programs to run at the same time.  A multi-user operating system extends the basic concept of multi-tasking with facilities that identify processes and resources, such as disk space, belonging to multiple users, and the system permits multiple users to interact with the system at the same time
  • 10. System Programming  Types of operating systems o Embedded Operating System  Embedded operating systems are designed to be used in embedded computer systems. o Real-time Operating System  A real-time operating system is an operating system that guaranties to process events or data within a certain short amount of time. o Distributed Operating System  A distributed operating system manages a group of distinct computers and makes them appear to be a single computer.
  • 11. Introduction to UNIX and Linux  The UNIX operating system was originally developed at Bell Laboratories in 1969  Initially intended for use inside the Bell System, AT&T licensed Unix to outside parties leading to a variety of both academic and commercial Unix variants from vendors  Multi-user operating system  Multi-tasking operating system
  • 12. Introduction to Unix and Linux  UNIX System Implementations o Although based on a core set of Unix commands, different distributions have their own unique commands and features and are designed to work with different types of hardware.  UNIX System V Release 4 was a product of UNIX System Laboratories at AT&T  BSD The Berkeley Software Distribution releases were produced and distributed by Computer Systems Research Group at University of California at Berkeley  AIX is a series of Unix-based operating environments developed by IBM for its servers.  Linux is an operating system that provides a rich programming environment similar to that of a UNIX System. It is freely available under the GNU Public License.  Solaris is the version of the UNIX System developed by Sun Microsystems  HP-UX is a series of Unix-based operating environments developed by HP for its servers
  • 13. Introduction to Unix and Linux  Operating systems that behave like Unix systems and provide similar utilities, but do not conform to Unix specification or are not licensed by The Open Group, are commonly known as Unix-like systems.  Linux is a freely distributed implementation of a UNIX-like kernel.  Because Linux takes the UNIX system as its inspiration, Linux and UNIX programs are very similar. o Almost all programs written for UNIX can be compiled and run on Linux.  List of Linux distributions
  • 14. Introduction to Unix and Linux  Linux is the leading operating system on servers  Top 500 supercomputers are running Linux  Phones  Cars  Refrigerators  Televisions In-car entertainment system of the Tesla Model S is based on Ubuntu
  • 15. Introduction to Unix and Linux  System Programming is the practice of writing system software o System software lives at a low level, interfacing directly with the kernel and core system libraries.  System programmer must have an acute awareness of the hardware and the operating system on which they work. o What is the system-level interface? o How do I write system-level applications in Linux? o What exactly do the kernel and the C library provide? o What interesting system calls are provided in Unix? o How does it all work?
  • 16. Introduction to Unix and Linux  A vast range of programming languages are available for Linux systems
  • 17. Introduction to Unix and Linux  UNIX Architecture o The interface to the kernel is a layer of software called the system calls o Libraries of common functions are built on top of the system call interface o Applications are free to use both o The shell is a special application that provides an interface for running other applications
  • 18.
  • 19.  The material in these slides is taken from different sources including: o The Linux Programming Interface by Michael Kerrisk o Advanced Programming in the Unix Environment by W. Richard Stevens o Data and Computer Communications by William Stallings o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/ Acknowledgements
  • 20. Operating System Overview Sayed Chhattan Shah Associate Professor of Computer Science Mobile Grid and Cloud Computing Lab Department of Information Communication Engineering Hankuk University of Foreign Studies Korea www.mgclab.com
  • 21. Operating System  The term operating system is commonly used with two different meanings o To denote entire package consisting of the central software managing a computer’s resources and all of the accompanying standard software tools, such as command-line interpreters, graphical user interfaces, file utilities, and editors. o To refer to the central software that manages and allocates computer resources.  The term kernel is often used as a synonym for the second meaning Kernel Operating System
  • 22. Lines of code Ubuntu Linux Kernel Windows Fedora 9 50 million 22 million Over 50 million 204 million
  • 23.
  • 24. Process Management  A process is a program in execution o Program is a passive entity, process is an active entity  Process needs resources to accomplish its task o CPU, memory, I/O, files o Initialization data  Process termination requires reclaim of any reusable resources  Typically system has many processes, some user, some operating system running concurrently on one or more CPUs
  • 25. Process Management Activities  Creating and deleting both user and system processes  Suspending and resuming processes  Providing mechanisms for process synchronization  Providing mechanisms for process communication  Providing mechanisms for deadlock handling The operating system is responsible for the following activities in connection with process management:
  • 26. Memory Management  To execute a program the instructions must be in memory  The data that is needed by the program must be in memory  Memory management activities o Keeping track of which parts of memory are currently being used and by whom o Deciding which processes and data to move into and out of memory o Allocating and deallocating memory space as needed
  • 27. Storage Management  OS provides uniform, logical view of information storage  Abstracts physical properties of storage devices to define a logical storage unit o Each medium is controlled by device  Varying properties include access speed, capacity, data transfer rate, access method such as sequential or random
  • 28. Storage Management  File system management o Files usually organized into directories o Access control on most systems to determine who can access what o File system management activities  Creating and deleting files and directories  Primitives to manipulate files and directories  Mapping files onto secondary storage  Backup files onto stable non-volatile storage media
  • 29. IO Subsystem  One purpose of OS is to hide peculiarities of hardware devices from the user  IO subsystem responsible for o Memory management of IO o General device-driver interface o Drivers for specific hardware devices
  • 30. Protection and Security  Protection any mechanism for controlling access of processes or users to resources defined by the OS  Security defense of the system against internal and external attacks  Systems generally first distinguish among users, to determine who can do what o User identities include name and associated number o User ID is associated with all files, processes to determine access control o Group identifier allows set of users to be defined and controls managed, then also associated with each process and file
  • 32. Operating System Services  One set of services provides functions that are helpful to the user o User interface  Almost all operating systems have a user interface that varies between command- line and graphics user interface o Program execution  The system must be able to load a program into memory and to run that program, end execution, either normally or abnormally o IO operations  A running program may require I/O, which may involve a file or an I/O device
  • 33. Operating System Services o File-system manipulation  The file system is of particular interest. Programs need to read and write files and directories, create and delete them, search them, list file Information, permission management. o Communications  Processes may exchange information, on the same computer or between computers over a network o Error detection  OS needs to be constantly aware of possible errors • May occur in the CPU and memory hardware, I/O devices, and user program • For each type of error, OS should take the appropriate action to ensure correct and consistent computing
  • 34. Operating System Services  Another set of OS functions exists for ensuring the efficient operation of the system itself via resource sharing o Resource allocation  When multiple users or multiple jobs running concurrently, resources such as CPU and main memory must be allocated to each of them o Accounting  To keep track of which users use how much and what kinds of computer resources o Protection and security  The owners of information stored in a multiuser or networked computer system may want to control use of that information, concurrent processes should not interfere with each other
  • 35.
  • 36. Kernel mode and User mode  Modern processor architectures typically allow the CPU to operate in at least two different modes: user mode and kernel mode o When running in user mode the CPU can access only memory that is marked as being in user space  attempts to access memory in kernel space result in a hardware exception. o When running in kernel mode the CPU can access both user and kernel memory space
  • 37. User and Operating System Interface
  • 38. User and Operating System Interface  There are several ways for users to interface with the operating system o Command-line interface o Graphical user interface or GUI
  • 39.  CLI or command interpreter allows direct command entry o Sometimes implemented in kernel, sometimes by systems program o Primarily fetches a command from user and executes it o Sometimes commands built-in, sometimes just names of programs User and Operating System Interface
  • 40. Bourne Shell Command Interpreter
  • 41.  User friendly desktop metaphor interface o Icons represent files, programs, actions, etc. o Various mouse buttons over objects in the interface cause various actions o Invented at Xerox PARC  Many systems now include both CLI and GUI interfaces o Microsoft Windows o Apple Mac OS X o Unix and Linux have CLI with optional GUI interfaces GUI
  • 42. Touchscreen Interfaces  Touchscreen devices require new interfaces  Mouse not possible or not desired  Actions and selection based on gestures  Virtual keyboard for text entry  Voice commands
  • 43. Evolution of Operating Systems  A major OS will evolve over time for a number of reasons: Hardware upgrades Hardware upgrades New types of hardware New types of hardware New services New services Fixes Fixes
  • 44.  How operating systems have evolved over the years? Serial Processing Serial Processing Simple Batch Systems Simple Batch Systems Multiprogrammed Batch Systems Multiprogrammed Batch Systems Time Sharing Systems Time Sharing Systems Evolution of Operating Systems
  • 45. Serial Processing Earliest Computers  No operating system  programmers interacted directly with the computer hardware  Computers ran from a console with display lights, toggle switches, some form of input device, and a printer Problems  Scheduling: o most installations used a hardcopy sign-up sheet to reserve computer time  time allocations could run short or long, resulting in wasted computer time  Setup time o a considerable amount of time was spent just on setting up the program to run
  • 46. Simple Batch Systems  Early computers were very expensive  To improve utilization, the concept of a batch OS was developed  The key idea is to use a software known as Monitor  User no longer has direct access to processor  Job is submitted to computer operator who batches them together and places them on an input device  Program branches back to the monitor when finished  Monitor automatically begins loading the next program
  • 47. Multiprogrammed Batch Systems  Processor is often idle  I/O devices are slow compared to processor
  • 48. Uniprogramming  The processor spends a certain amount of time executing, until it reaches an I/O instruction; it must then wait until that I/O instruction concludes before proceeding
  • 49. Multiprogramming  When one job needs to wait for IO the processor can switch to the other job, which is likely not waiting for IO
  • 50.  Multiprogramming also known as multitasking  Memory is expanded to hold three, four, or more programs and switch among all of them Multiprogramming
  • 51. Time-Sharing Systems  Time sharing systems can be used to handle multiple interactive jobs  Processor time is shared among multiple users  Multiple users simultaneously access the system through terminals, with the OS interleaving the execution of each user program in a short burst or quantum of computation
  • 52. System Calls  System calls provide an interface to the services made available by an operating system
  • 53.
  • 54.
  • 55. Operating System Design  User goals and System goals o User goals  Operating system should be convenient to use, easy to learn, reliable, safe, and fast o System goals  Operating system should be easy to design, implement, and maintain, as well as flexible, reliable, error-free, and efficient
  • 56. Operating System Implementation  Much variation o Early operating systems in assembly language o Then system programming languages like Algol o Now C, C++  Actually usually a mix of languages o Lowest levels in assembly o Main body in C o Systems programs in C, C++, scripting languages like Perl, Python, shell scripts  More high-level language easier to port to other hardware but slower
  • 57. Operating System Structure  General-purpose OS is very large program  Various ways to structure ones o Simple structure: MS-DOS o More complex: UNIX o Layered: an abstraction o Microkernel: Mach
  • 58. Simple Structure : MS-DOS  MS-DOS written to provide the most functionality in the least space o Not divided into modules o Although MS-DOS has some structure, its interfaces and levels of functionality are not well separated
  • 59. Non Simple Structure : UNIX UNIX – limited by hardware functionality, the original UNIX operating system had limited structuring. The UNIX OS consists of two separable parts o Systems programs o The kernel  Consists of everything below the system-call interface and above the physical hardware  Provides the file system, CPU scheduling, memory management, and other operating-system functions; a large number of functions for one level
  • 60.
  • 61. Layered Approach  The operating system is divided into a number of layers  The bottom layer is the hardware and the highest is the user interface  Each uses functions and services of only lower level layers
  • 62. Microkernel System Structure  As UNIX expanded the kernel became large and difficult to manage  Microkernel approach o This method structures the operating system by removing all nonessential components from the kernel and implementing them as system and user-level programs.  Mac OS X kernel o Benefits  Easier to extend a microkernel  Easier to port the operating system to new architectures o Disadvantages  Performance overhead of user space to kernel space communication
  • 63.
  • 64. Modules  Many modern operating systems implement loadable kernel modules o The kernel has a set of core components and links in additional services via modules, either at boot time or during run time
  • 65. Hybrid Systems  Most modern operating systems are actually not one pure model o Hybrid combines multiple approaches to address performance, security, usability needs o Linux and Solaris kernels in kernel address space, so monolithic, plus modular for dynamic loading of functionality
  • 66. iOS  Apple mobile OS for iPhone and iPad o Structured on Mac OS X, added functionality o Cocoa Touch Objective-C API for developing apps o Media services layer for graphics, audio, video o Core services provides cloud computing, databases o Core operating system, based on Mac OS X kernel
  • 67. Android  Developed by Open Handset Alliance mostly Google o Open Source  Based on Linux kernel but modified o Provides process, memory, device-driver management o Adds power management  Runtime environment includes core set of libraries and Dalvik virtual machine o Apps developed in Java plus Android API  Java class files compiled to Java bytecode then translated to executable than runs in Dalvik VM
  • 68.
  • 69.
  • 70.  The material in these slides is taken from different sources including: o The Linux Programming Interface by Michael Kerrisk o Advanced Programming in the Unix Environment by W. Richard Stevens o Data and Computer Communications by William Stallings o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/ Acknowledgements
  • 71. Shell Programming Sayed Chhattan Shah Associate Professor of Computer Science Mobile Grid and Cloud Computing Lab Department of Information Communication Engineering Hankuk University of Foreign Studies Korea www.mgclab.com
  • 72. Shell Programming  A shell is a program that acts as the interface between you and the Linux system o It takes commands from keyboard and gives them to operating system to perform o Some of the most powerful things that you can do are most easily done from the shell
  • 73. Shell Programming  The Bourne Shell  The C Shell  The Korn Shell  The GNU Bourne-Again Shell or Bash
  • 74. Shell Programming  An OS starts a shell for each user when the user logs in or opens a terminal window.
  • 75. Basic Shell Commands  Navigate Directory
  • 76. Basic Shell Commands  File or Directory Manipulation
  • 77. Basic Shell Commands  Viewing Files
  • 78. Basic Shell Commands  Locating Commands
  • 79. Basic Shell Commands  Install Program or Package
  • 80. Basic Shell Commands  Generic Useful Bash Syntax
  • 81. Shell Programming  A shell script is a sequence of commands  To create a shell script o Open a new empty file in editor such as vim and gedit o Put UNIX commands in the new empty file o Save file o Make your new file executable using chmod command with an option +x o Execute new script by prefixing its name with ./
  • 82. Shell Programming  Hello World bash shell script  #! characters tell the system that the argument that follows on the line is the program to be used to execute this file.
  • 83. Shell Programming  Script used to backup user home directory
  • 84. Shell Programming  Variables o All variables are considered and stored as strings, even when they are assigned numeric values o The shell and some utilities will convert numeric strings to their values in order to operate on them as required o Linux is a case-sensitive
  • 85. Shell Programming  Variables o You can assign user input to a variable by using the read command
  • 86.
  • 87. Shell Programming  Output of whoami and date command will be directly assigned to the user variable  Variables can also be used directly on the command line
  • 88. Shell Programming The script uses a new shell scripting trick ${parameter} called parameter expansion. Curly braces are required because variable $user is followed by characters which are not part of its variable name.
  • 89. Shell Programming  Positional Parameters o Positional parameters are assigned via command line arguments and are accessible within a script as variables. o During the script execution, any additional items supplied after the program name are considered arguments and are available during the script execution.
  • 90.
  • 91. Shell Programming  Input, Output and Error Redirections o When you execute a command, three possible outcomes might happen  Command will produce an expected output  Command will generate an error  Command might not produce any output at all
  • 92. Shell Programming  Input, Output and Error Redirections o notation is used to redirect stdout to a file o notation is used to redirect stderr o both
  • 93. Shell Programming  Input, Output and Error Redirections o notation is used to redirect stdout to a file o notation is used to redirect stderr o both
  • 94. Shell Programming  Input, Output and Error Redirections no tar stderr message will be displayed
  • 95. Shell Programming  Input, Output and Error Redirections o To accept command input from a file use < notation
  • 96. Shell Programming  Pipes o You can connect processes using the pipe operator |
  • 97. Shell Programming  Functions allow a programmer to organize and reuse code, hence increasing the efficiency as well as readability of the entire script  You can think of the function as a way to the group number of different commands into a single command
  • 99. Shell Programming  Numeric and String Comparisons
  • 100. Shell Programming  File Conditionals
  • 101. Shell Programming  Numeric and String Comparisons
  • 102. Shell Programming  Numeric and String Comparisons
  • 108. Shell Programming  Loops  let command evaluates a mathematical expression and stores its result into a variable
  • 111. Shell Programming  Bash Arithmetic o Enclose any mathematical expression inside double parentheses
  • 112. Shell Programming  Bash Arithmetic o Another alternative to arithmetic expansion is the expr command o Using the expr command allows us to perform an arithmetic operation even without enclosing our mathematical expression within brackets or quotes o Do not forget to escape asterisk multiplication sign to avoid syntax error
  • 113. Shell Programming  Bash Arithmetic o Use bc command for decimal numbers o bc command will produce all results as an integer number o Use scale=x to instruct the bc command to show real numbers
  • 114. Shell Programming  Bash Reference Manual
  • 115.
  • 117. Structures  A structure is a collection of related data items  keyword struct in C is used to group related data of different types  Array is homogeneous since it can contain only data of same type  Individual components of a struct type are called members or fields
  • 118.  Definition of a structure: struct <struct-type>{ <type> <identifier_list>; <type> <identifier_list>; ... } ; struct Date { int day; int month; int year; } ; Structures
  • 119. struct StudentInfo{ int Id; int age; char Gender; double CGA; }; struct StudentGrade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; }; Structures
  • 120. struct BankAccount{ char Name[15]; int AcountNo[10]; double balance; Date Birthday; }; struct StudentRecord{ char Name[15]; int Id; char Gender; char Dept[5]; }; Structures
  • 121.  Declaration of a variable of struct type: <struct-type> <identifier_list>;  Example: StudentRecord Student1, Student2; Student1 and Student2 are variables of StudentRecord type. Student1 Student2 Name Id Gender Dept Name Id Gender Dept Structures
  • 122. #include <stdio.h> #include <string.h> struct student { int id; char name[20]; char address[100]; float percentage; }; int main() { struct student record; record.id = 1; strcpy_s(record.name, 10, "LEE"); record.percentage = 86.5; printf(" ID is: %d n", record.id); printf(" Name is: %s n", record.name); printf(" Percentage is: %.1f n", record.percentage); }
  • 123. #include <stdio.h> #include <string.h> #include <conio.h> struct Students { int ID; char name[20]; char address[200]; };
  • 124. int main() { struct Students std[100]; int index = 0; int next = 1; while (1) { system("cls"); printf(" n"); printf("nEnter student ID: "); scanf_s(" %d", &std[index].ID); getchar(); printf("nEnter student name: "); gets(std[index].name); printf("nEnter student address: "); gets(std[index].address); index++; printf("nRecord savedn"); printf("nWould you like to add another student record? n<< Enter 1 to continue and 0 to exit>> "); scanf_s("%d", &next); getchar(); if (next == 0) break; }
  • 125. system("cls"); printf("n n"); int i = 0; for (i = 0; i < index; i++) { printf("nID %d ", std[i].ID); printf("nName %s ", std[i].name); printf("nAddress %s ", std[i].address); } }
  • 126.  An ordinary array: One type of data  An array of structs: Multiple types of data in each array element. 0 1 2 … 98 99 0 1 2 … 98 99 Structures
  • 127.  We often use arrays of structures.  Example: StudentRecord Class[100]; strcpy(Class[98].Name, “Shah"); Class[98].Id = 12345; strcpy(Class[98].Dept, “ICE"); Class[98].gender = 'M'; Class[0] = Class[98]; . . . 0 1 2 … 98 99 Shah 12345 M ICE Structures
  • 129. • In memory, every stored data item occupies one or more contiguous memory cells. • The number of memory cells required to store a data item depends on data item type. • Whenever we define a variable, the system allocates memory locations to hold the value of the variable. 14 Basic Concept
  • 130. char c = ‘A’; int x = 50; 15 Assume each memory cell is 1 byte
  • 131. int x = 50; •This statement instructs the compiler to allocate locations for the integer variable x, and put the value 50 in those locations. •During execution of the program, the system always associates the name x with the address 1380. •The value 50 can be accessed by using either name x or address 1380. 16 x  variable 50  value 1380  address Basic Concept
  • 132. Ampersand (&) operator returns address of the given variable #include <stdio.h> int main() { int x; char c; printf("Address of x variable: %xn", &x); printf("Address of c variable: %xn", &c); return 0; } Pointers
  • 133. • A pointer is a variable that stores the address of another variable • p is a pointer to variable v int *p; pointer variable declaration int v; p = &v; p stores the address of v 18 Pointers
  • 134. • The general form of a pointer variable declaration type *var_name; int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch; // pointer to a character 19 Pointers
  • 135. • Pointer variables can appear in declarations along with other variables: int i, j, a[10], b[20], *p, *q; 20 Pointers
  • 136. Declaring Pointer Variables • Few important operations, which we will do with the help of pointers very frequently  Define a pointer variable  Assign the address of a variable to a pointer  Access the value at the address available in the pointer variable • This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. 21
  • 137. 22 #include <stdio.h> int main() { int v = 20; // actual variable declaration int *ip; // pointer variable declaration ip = &v; // store address of var in pointer variable printf("Address of v variable: %xn", &v); // address stored in pointer variable printf("Address stored in ip variable: %xn", ip); // access the value using the pointer printf("Value of *ip variable: %dn", *ip); return 0; }
  • 138. The Indirection Operator • Applying the indirection operator to an uninitialized pointer variable causes undefined behavior: int *p; printf("%d", *p); /*** WRONG ***/ • Assigning a value to *p is particularly dangerous: int *p; *p = 1; /*** WRONG ***/ 23
  • 139. Pointer Assignment • Assume that the following declaration is in effect: int i, j, *p, *q; • Example of pointer assignment: p = &i; q = p; q now points to the same place as p: 24
  • 140. Pointer Assignment • If p and q both point to i, we can change i by assigning a new value to either *p or *q: *p = 1; *q = 2; • Any number of pointer variables may point to the same object. 25
  • 141. Pointer Assignment p = &i; q = &j; i = 1; *q = *p; 26
  • 142. Pointer Expressions • Like other variables, pointer variables can be used in expressions. • If p1 and p2 are two pointers, the following statements are valid: 27 sum = *p1 + *p2; prod = *p1 * *p2; prod = (*p1) * (*p2); *p1 = *p1 + 2; x = *p1 / *p2 + 5;
  • 143. #include <stdio.h> main() { int a, b; int c = 5; int *p; a = 4 * (c + 5); p = &c; b = 4 * (*p + 5); printf("a = %dnb = %dn", a, b); }
  • 144. Program: Finding the Largest and Smallest Elements in an Array • The program uses a function named max_min to find the largest and smallest elements in an array. • Prototype for max_min: void max_min(int a[], int n, int *max, int *min); • Example call of max_min: max_min(b, N, &big, &small); • When max_min finds the largest element in b, it stores the value in big by assigning it to *max. • max_min stores smallest element of b in small by assigning it to *min. 29
  • 145. • The program will read 10 numbers into an array, pass it to the max_min function, and print the results: Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31 Largest: 102 Smallest: 7 30 Program: Finding the Largest and Smallest Elements in an Array
  • 146. 31 #include <stdio.h> #define N 10 void max_min(int a[], int n, int* max, int* min); int main(void) { int b[N], i, big, small; printf("Enter %d numbers: ", N); for (i = 0; i < N; i++) scanf_s("%d", &b[i]); max_min(b, N, &big, &small); printf("Largest: %dn", big); printf("Smallest: %dn", small); return 0; }
  • 147. 32 void max_min(int a[], int n, int* max, int* min) { int i; *max = *min = a[0]; for (i = 1; i < n; i++) { if (a[i] > *max) *max = a[i]; else if (a[i] < *min) *min = a[i]; } }
  • 148. Pointers as Return Values Functions are allowed to return pointers: #include <stdio.h> int* max(int *a, int *b) { if (*a > *b) return a; else return b; } int main(void) { int* p, i=10, j=20; p = max(&i, &j); printf("Largest: %dn", *p); }
  • 149. Pointers as Return Values • A function could also return a pointer to an external variable or to a static local variable. • What f will return? int* f(void) { int i=10; return &i; } 34
  • 150. Pointers as Return Values • Never return a pointer to an automatic local variable: int* f(void) { int i=10; return &i; } The variable i won’t exist after f returns. 35
  • 151. Using Arguments for Results • Pass address of variable where you want result stored • This solves the mystery of why ‘&’ with argument to scanf scanf("%d ", &data); read a decimal integer and store in data 36
  • 152. Pointer Arithmetic • C allows you to perform some arithmetic operations on pointers. • Consider <datatype> *ptrn; //datatype can be int, double or long 37
  • 153. char* mychar; short* myshort; long* mylong; ++mychar; ++myshort; ++mylong; Assume: char takes 1 byte short takes 2 bytes long takes 4 bytes Pointers point to the memory locations 1000, 2000, and 3000, respectively
  • 154.
  • 155. • We can use const to document that a function won’t change an object whose address is passed to the function. void f(const int *p) { *p = 0; /*** WRONG ***/ } Attempting to modify *p is an error that the compiler will detect. 40 Using const to Protect Arguments
  • 156. Pointers and Arrays • When an array is declared, • The compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations. • The base address is the location of the first element (index 0) of the array. 41
  • 157. Pointers and Arrays • Consider the declaration: int x[5] = {1, 2, 3, 4, 5}; Suppose that the base address of x is 2500, and each integer requires 4 bytes. 42 Element Value Address x[0] 1 2500 x[1] 2 2504 x[2] 3 2508 x[3] 4 2512 x[4] 5 2516 Both x and &x[0] have the value 2500
  • 158. Pointers and Arrays • Following statements are equivalent p = x; p = &x[0]; • We can access successive values of x by using p++ or p-- to move from one element to another • Relationship between p and x: p = &x[0] = 2500 p+1 = &x[1] = 2504 p+2 = &x[2] = 2508 p+3 = &x[3] = 2512 p+4 = &x[4] = 2516 43 Element Value Address x[0] 1 2500 x[1] 2 2504 x[2] 3 2508 x[3] 4 2512 x[4] 5 2516
  • 159. Pointers and Arrays Another example short sArr[5] = {1,2,3,4,5}; float fArr[5] = {0.5f, 1.5f, 2.5f, 3.5f, 4.5f}; short *sp = sArr; float *fp = fArr; // both lines output sp: 1, 2, 3, 4, 5 printf("sp: %d, %d, %d, %d, %dn", *sp, *(sp + 1), *(sp + 2), *(sp + 3), *(sp + 4)); printf("sp: %d, %d, %d, %d, %dn", sp[0], sp[1], sp[2], sp[3], sp[4]);
  • 160. 45 #include <stdio.h> float avg(int array[], int size) { int *p, sum = 0; p = array; for (int i = 0; i < size; i++) sum = sum + *(p + i); return ((float)sum / size); } void main() { int x[5] = {10,20,30,40,50}; printf("nAverage is %f", avg(x, 5)); }
  • 161. Pointers and struct • To access members of a structure using pointers, we use the  operator #include <stdio.h> struct person { int age; float weight; }; int main() { struct person* personPtr, person1; personPtr = &person1; printf("Enter age: "); scanf_s("%d", &personPtr->age); printf("Enter weight: "); scanf_s("%f", &personPtr->weight); printf("Displaying:n"); printf("Age: %dn", personPtr->age); printf("weight: %f", personPtr->weight); return 0; }
  • 162. Pointers, Functions, and struct #include <stdio.h> struct person { int age; float weight; }; void display(struct person *p) { printf("Displaying:n"); printf("Age: %dn", p->age); printf("weight: %f", p->weight); } int main() { struct person* personPtr, person1; personPtr = &person1; printf("Enter age: "); scanf_s("%d", &personPtr->age); printf("Enter weight: "); scanf_s("%f", &personPtr->weight); display(personPtr); return 0; }
  • 163. 48 C File Input and Output
  • 164. C File Input and Output • Types of Files  Text files  Text file is human readable because everything is stored in terms of text.  Binary files  In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. 49
  • 165. • File Operations  Creating a new file  Opening an existing file  Closing a file  Reading from and writing information to a file 50 C File Input and Output
  • 166. • File Operations  When working with files, you need to declare a pointer of type FILE FILE *fptr;  The FILE structure is the basic data type used when handling files with the standard C library. 51 C File Input and Output
  • 167. • File Operations  In order to work with a file, we must open it first, using the fopen function. ptr = fopen("fileopen","mode"); Example fopen("E:cprogramFile_A.txt","w"); 52 C File Input and Output
  • 168.
  • 169.
  • 170. • File Operations  Once we are done working with the file, we need to close it fclose(fptr); // fptr is a file pointer associated with the file to be closed  fclose returns 0 on success or -1 on failure 55 C File Input and Output
  • 171.
  • 172. • Character input functions  fgetc() returns one character from a file  fgets() gets a string from the file ending at newline or end of file  fscanf() works like the original scanf function  Return values:  Next character on success  EOF on error or end-of-file 57 C File Input and Output
  • 173. • Character output functions  fputc() writes one character to a file  fputs() writes a string to a file  fprintf() enables printf output to be written to any file  Return values:  On success the character that was written  EOF on error 58 C File Input and Output
  • 174. Writing to a text file #include <stdio.h> #include <stdlib.h> int main() { int num; FILE* fptr; int error = fopen_s(&fptr, "C:UsersShahDocumentsFile_A.txt", "w"); if (error != 0) { printf("Error!"); exit(1); } printf("Enter num: "); scanf_s("%d", &num); fprintf(fptr, "%d", num); fclose(fptr); return 0; }
  • 175. Reading from a text file #include <stdio.h> #include <stdlib.h> int main() { int num; FILE* fptr; int error = fopen_s(&fptr,"C:UsersShahDocumentsFile_A.txt", "r"); if (error!=0) { printf("Error! opening file"); exit(1); } fscanf_s(fptr, "%d", &num); printf("Value of n=%d", num); fclose(fptr); return 0; }
  • 176. C program to write a sentence to a file 61 #include <stdio.h> #include <stdlib.h> int main() { char sentence[1000]; FILE* fptr; int error = fopen_s(&fptr, "C:UsersShahDocumentsFile_A.txt", "w"); if (error != 0) { printf("Error! opening file"); exit(1); } printf("Enter a sentence:n"); gets(sentence); fprintf(fptr, "%s", sentence); fclose(fptr); return 0; }
  • 177. C program to read a line from a file #include <stdio.h> #include <stdlib.h> int main() { FILE* fptr; char str[60]; int error = fopen_s(&fptr, "C:UsersShahDocumentsFile_A.txt", "a+"); if (error != 0) { printf("Error! opening file"); exit(1); } if (fgets(str, 60, fptr) != NULL) { puts(str); } fclose(fptr); return 0; }
  • 178. • Output functions  fwrite function is used to write records to a file. A record may be an array or a structure. fwrite( ptr, int size, int n, FILE *fp ) ptr : reference of an array or a structure stored in memory. size : total number of bytes to be written. n : number of times a record will be written. fp: file where the records will be written. 63 C File Input and Output
  • 179. • Input functions  fread() function is used to read bytes form a file. fread( ptr, int size, int n, FILE *fp ) ptr : reference of an array or a structure where data will be stored after reading. size : total number of bytes to be read from file. n : number of times a record will be read. fp: file where the records will be read. 64 C File Input and Output
  • 180. Writing and reading structures #include <stdio.h> #include <stdlib.h> #include <string.h> struct person { int id; char fname[20]; char lname[20]; };
  • 181. Writing and reading structures int main() { FILE* outfile; // open file for writing int error = fopen_s(&outfile, "C:UsersShahDocumentsFile_A.txt", "w"); if (error != 0) { printf("Error! opening file"); exit(1); } struct person input1 = { 1, "rohan", "sharma" }; struct person input2 = { 2, "mahendra", "dhoni" }; // write struct to file fwrite(&input1, sizeof(struct person), 1, outfile); fwrite(&input2, sizeof(struct person), 1, outfile); if (fwrite != 0) printf("contents to file written successfully !n"); else printf("error writing file !n"); // close file fclose(outfile); }
  • 182. Writing and reading structures #include <stdio.h> #include <stdlib.h> #include <string.h> struct person { int id; char fname[20]; char lname[20]; };
  • 183. Writing and reading structures int main() { FILE* infile; struct person input; // open file for writing int error = fopen_s(&infile, "C:UsersShahDocumentsFile_A.txt", “r"); if (error != 0) { printf("Error! opening file"); exit(1); } // read file contents till end of file while (fread(&input, sizeof(struct person), 1, infile)) printf("id = %d name = %s %sn", input.id, input.fname, input.lname); // close file fclose(infile); return 0; }
  • 184. Writing and reading structures #include <stdio.h> struct student { char name[50]; int height; }; int main() { struct student stud1[3], stud2[3]; FILE* fptr; int error = fopen_s(&fptr, "C:UsersShahDocumentsFile_B.txt", "w"); if (error != 0) { printf("Error! opening file"); exit(1); } for (int i = 0; i < 3; ++i) { printf("Enter name: "); gets(stud1[i].name); printf("Enter height: "); scanf_s("%d", &stud1[i].height); getchar(); } fwrite(stud1, sizeof(stud1), 1, fptr); fclose(fptr);
  • 185. Writing and reading structures error = fopen_s(&fptr, "C:UsersShahDocumentsFile_B.txt", "r"); fread(stud2, sizeof(stud2), 1, fptr); for (int i = 0; i < 3; ++i) { printf("Name: %s Height: %dn", stud2[i].name, stud2[i].height); } fclose(fptr); }
  • 186. C library functions • The C library function strtok breaks string into a series of tokens using the delimiter. char *strtok(char *string, const char *delimiter) • Parameters • string: The contents of this string are modified and broken into smaller strings or tokens. • delimiter: This is the C string containing the delimiters. • Return value • This function returns a pointer to the first token found in the string. • A null pointer is returned if there are no tokens left to retrieve.
  • 187. #include <string.h> #include <stdio.h> int main(void) { char string[] = "A string of tokens and some more tokens"; char seps[] = " "; char* token = NULL; char* next_token = NULL; printf("Tokens:n"); // Establish string and get the first token: token = strtok_s(string, seps, &next_token); // While there are tokens in "string" while ((token != NULL) ) { // Get next token: printf(" %sn", token); token = strtok_s(NULL, seps, &next_token); } }
  • 188.  The material in these slides is taken from different sources including: o The Linux Programming Interface by Michael Kerrisk o Advanced Programming in the Unix Environment by W. Richard Stevens o Data and Computer Communications by William Stallings o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/ Acknowledgements
  • 189. File Management Sayed Chhattan Shah Associate Professor of Computer Science Mobile Grid and Cloud Computing Lab Department of Information Communication Engineering Hankuk University of Foreign Studies Korea www.mgclab.com
  • 190. File Management  Users and Groups o Every user has a unique login name and a corresponding user ID o These are defined in the system password file which also includes  Group ID the numeric group ID of the first of the groups of which the user is a member  Home directory the initial directory into which the user is placed after logging in  Login shell the name of the program to be executed to interpret user commands
  • 191. File Management  Users and Groups o Users are organized into groups o Each group is identified by a single line in the system group file which includes following information:  Group name  Group ID  User list a comma-separated list of login names of users who are members of this group o Superuser with ID 0 and name root has special privileges within the system
  • 192. File Management  The kernel maintains a single hierarchical directory structure to organize all files in the system o In Microsoft Windows each disk device has its own directory hierarchy  At the base of this hierarchy is the root directory named /
  • 193. File Management  Everything is a file in Linux o Programs can use disk files, printers, and other devices in exactly the same way  Each file is marked with a type o Ordinary data files o Devices o Pipes o Sockets o Directories  Each file has an associated user ID and group ID that define the owner of the file and the group to which it belongs.
  • 194. File Management  A directory is a special file whose contents take the form of a table of filenames coupled with references to the corresponding files  This filename-plus-reference association is called a link o Files may have multiple links, and thus multiple names, in the same or in different directories  Every directory contains at least two entries o . which is a link to the directory itself and .. which is a link to its parent directory
  • 195. File Management  A pathname describes the location of a file within the single directory hierarchy o An absolute pathname begins with a / and specifies the location of a file with respect to the root directory o A relative pathname specifies the location of a file relative to a process’s current working directory o Each process has a current working directory  A file is a sequence of m bytes
  • 196. File Management  File descriptor uniquely represents an opened file in operating system o Each running program has a number of file descriptors associated with it o File descriptors are small integers that you can use to access open files or devices  When a program starts, it usually has three of these descriptors already opened.  File descriptor 0 with the standard input of a process  File descriptor 1 with the standard output  File descriptor 2 with the standard error  You can associate other file descriptors with files and devices by using the open system call
  • 197. File Management  Registers are high speed storage areas in the CPU o All data must be stored in a register before it can be processed.
  • 198. File Management  System call o Request to the operating system to perform a task  that the process does not have permission to perform Linux kernel structure
  • 199. File Management  Few points related to system calls o A system call changes the processor state from user mode to kernel mode o The set of system calls is fixed o Each system call is identified by a unique number o Each system call may have a set of arguments that specify information to be transferred from user space to kernel space and vice versa Linux kernel structure
  • 200. File Management  From a programming point of view, invoking a system call looks much like calling a C function but behind the scenes, several steps occur during the execution of a system call o The application program makes a system call by invoking a wrapper function in C library o The wrapper function must make all of the system call arguments available to the system call routine  These arguments are passed to the wrapper via the stack, but the kernel expects them in specific registers  The wrapper function copies the arguments to these registers o The wrapper function copies system call number into a specific CPU register EAX
  • 201. File Management o The wrapper function executes a trap machine instruction (int 0x80), which causes the processor to switch from user mode to kernel mode and execute code pointed to by location 0x80 (128 decimal) of the system’s trap vector o In response to the trap to location 0x80, the kernel invokes its system_call() routine to handle the trap. This handler:  Saves register values onto the kernel stack  Checks the validity of the system call number  Invokes the appropriate system call service routine  Restores register values from kernel stack and places system call return value on the stack  Returns to the wrapper function, simultaneously returning the processor to user mode
  • 203. File Management  The flags argument is a bit mask that specifies the access mode for the file, using one of the constants shown below  One and only one of the five constants must be specified. o O_RDONLY Open for reading only. o O_WRONLY Open for writing only. o O_RDWR Open for reading and writing. o O_EXEC Open for execute only. o O_SEARCH Open for search only. It applies to directories.
  • 204. File Management  The following constants are optional: o O_APPEND Append to the end of file on each write. o O_CREAT Create the file if it doesn’t exist. This option requires a third argument to the open function — the mode, which specifies the access permission bits of the new file. o O_DIRECTORY Generate an error if path doesn’t refer to a directory. o O_EXCL Generate an error if O_CREAT is also specified and the file already exists. o O_TRUNC If the file exists and if it is successfully opened for either write-only or read–write, truncate its length to 0.
  • 205. File Management  When open() is used to create a new file, the mode bit-mask argument specifies the permissions to be placed on the file.  If the open() call doesn’t specify O_CREAT, mode can be omitted.
  • 208. File Management  Errors from open() o If an error occurs while trying to open the file open() returns –1 and errno identifies the cause of the error  The following are some possible errors • EACCES » The file permissions don’t allow the calling process to open the file in the mode specified by flags. • EISDIR » The specified file is a directory, and the caller attempted to open it for writing. • EMFILE » The process resource limit on the number of open file descriptors has been reached.
  • 209. File Management • ENFILE » The system-wide limit on the number of open files has been reached. • ENOENT » The specified file doesn’t exist, and was not specified • EROFS » The specified file is on a read-only file system and the caller tried to open it for writing. • ETXTBSY » The specified file is an executable file that is currently executing.
  • 210. File Management  count argument specifies the maximum number of bytes to read.  buffer argument supplies the address of the memory buffer into which the input data is to be placed.  ssize_t data type is a signed integer type used to hold a byte count or a –1 error indication.
  • 213.
  • 214. File Management  count is the number of bytes to write from buffer  buffer is the address of the data to be written  ssize_t data type is a signed integer type used to hold a byte count or a –1 error indication
  • 215.
  • 216. File Management  Program simply prints a message to the standard output
  • 217. File Management  This program copies the first 128 bytes of the standard input to the standard output. It copies all of the input if there are fewer than 128 bytes.
  • 218. File Management  If you run the program, you should see the following: o In the first execution, you create some input for the program using echo, which is piped to your program. o In the second execution, you redirect input from a file.
  • 219. File Management  This program copy one file to another, character by character.
  • 220. File Management  Modified program which copies the files in 1K blocks
  • 221. File Management  The close() system call closes an open file descriptor, freeing it for subsequent reuse by the process.  When a process terminates, all of its open file descriptors are automatically closed.
  • 222. File Management  For each open file, kernel records a file offset which is the current position for the next file operation o The first byte of the file is at offset 0 o On open file offset points to the beginning of the file o After reading or writing m bytes file offset moves m bytes forward
  • 223. File Management  The lseek() system call adjusts the file offset of the open file referred to by the file descriptor, according to the values specified in offset and whence.
  • 224. File Management  offset argument specifies a value in bytes.  whence argument indicates the base point from which offset is to be interpreted o SEEK_SET  The file offset is set offset bytes from the beginning of the file. o SEEK_CUR  The file offset is adjusted by offset bytes relative to the current file offset.
  • 226. File Management  Program that copies the contents of the existing file named in its first command line argument to the new file named in its second command-line argument
  • 227. File Management Program that copies the contents of the existing file named in its first command line argument to the new file named in its second command-line argument
  • 228. File Management  Following system call performs a range of control operations on an open file descriptor.  The cmd argument can specify a wide range of operations  Third argument can be of different types or it can be omitted
  • 229. File Management  Following system calls allow applications to seek and perform I/O atomically
  • 231. File Management  The set of buffers to be transferred is defined by the array iov  The integer count specifies the number of elements in iov
  • 232. File Management  Each element of iov is a structure of the following form:
  • 237. File Management  The Standard IO Library o It provides a versatile interface to low-level IO system calls and many sophisticated functions for formatting output and scanning input o It also takes care of the buffering requirements for devices o Three file streams are automatically opened when a program is started  stdin  stdout  stderr
  • 238. File Management If successful returns a pointer to a FILE object If fails returns NULL
  • 240. File Management  The fgetc function returns the next byte as a character from a file stream  The getchar function reads the next character from the standard input  The fputc function writes a character to an output file stream and returns the value it has written or EOF on failure.  The putchar function writes a single character to the standard output
  • 241. File Management  stdio library maintains an internal buffer within the FILE structure and the low-level system calls are made only when the buffer fills  Write programs using system calls and stdio library functions and check performance
  • 242. File Management  The fgets function reads a string from an input file stream. o fgets writes characters to the string pointed to by s until a newline is encountered, n-1 characters have been transferred, or the end of file is reached. o When it successfully completes, fgets returns a pointer to the string s  The gets function is similar to fgets, except that it reads from the standard input
  • 243. File Management  Formatted Input and Output
  • 247.
  • 248.
  • 250. File Management  chmod system call is used to change the permissions on a file or directory  A superuser can change the owner of a file using the chown system call
  • 251. File Management  You can create and remove directories using the mkdir and rmdir system calls  The rmdir system call removes directories, but only if they are empty
  • 252. File Management  A program can navigate directories using chdir system call in much the same way as a user moves around the file system.  A program can determine its current working directory by calling the getcwd function.  The getcwd function writes the name of the current directory into the given buffer, buf. o It returns NULL if the directory name would exceed the size of the buffer given as the parameter size. It returns buf on success.
  • 253. File Management  Errors o Many of the system calls and functions can fail for a number of reasons. o When they fail, they indicate the reason for their failure by setting the value of the external variable errno o Program must inspect the errno variable immediately after the function giving problems because it may be overwritten by the next function called
  • 254. File Management  Errors o The values and meanings of the errors are listed in the header file errno.h o They include
  • 255. File Management  The strerror function maps an error number into a string describing the type of error that has occurred  The perror function also maps the current error, as reported in errno, into a string and prints it on the standard error stream.
  • 256. File Management  File Sharing o The kernel uses three data structures to represent an open file  Every process has an entry in the process table. Within each process table entry is a table of open file descriptors. Associated with each file descriptor are • The file descriptor flags • A pointer to a file table entry  The kernel maintains a file table for all open files. Each file table entry contains • The file status flags for the file, such as read, write, and append • The current file offset • A pointer to the v-node table entry for the file  Each open file has a v-node structure that contains information about the type of file and pointers to functions that operate on the file. • For most files, the v-node also contains the i-node for the file
  • 257. File Management  Tables for a single process that has two different files open: o one file is open on standard input o other is open on standard output
  • 258. File Management  The first process has the file open on descriptor 3 and that the second process has that same file open on descriptor 4.  Each process that opens the file gets its own file table entry, but only a single v-node table entry is required for a given file.
  • 259. File Management  File Sharing o The UNIX System supports the sharing of open files among different processes o Each process has its own file table entry with its own current file offset. o Works fine for multiple processes that are reading the same file. o Unexpected results can arise when multiple processes write to the same file.
  • 260.
  • 261.  The material in these slides is taken from different sources including: o The Linux Programming Interface by Michael Kerrisk o Advanced Programming in the Unix Environment by W. Richard Stevens o Data and Computer Communications by William Stallings o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/ Acknowledgements
  • 262. Process Management Sayed Chhattan Shah Associate Professor of Computer Science Mobile Grid and Cloud Computing Lab Department of Information Communication Engineering Hankuk University of Foreign Studies Korea www.mgclab.com
  • 263.  Process o A program in execution o A process is an instance of an executing program o A process is an abstract entity to which system resources are allocated in order to execute a program Process Management
  • 264.  A program is a file containing a range of information that describes how to construct a process at run time o Machine-language instructions  Encode the algorithm of the program o Program entry-point address  Location of the instruction at which execution of the program should commence o Data  Values used to initialize variables and also literal constants used by the program o Shared and dynamic library information  One program may be used to construct many processes Process Management
  • 265.  As a process executes it changes state  A process may be in one of the following states: o New The process is being created. o Running Instructions are being executed. o Waiting The process is waiting for some event to occur such as an I/O completion. o Ready The process is waiting to be assigned to a processor. o Terminated The process has finished execution Process Management
  • 266.
  • 267.  Process Control Block  A data structure used by operating system to store all the information about a process.  When a process is created, the operating system creates a corresponding process control block. Process Management
  • 268.  PCB includes o Unique identifier associated with this process o Process state such as waiting and running state o Program counter stores address of next instruction to be executed for this process o CPU registers vary in number and type, depending on the computer architecture o CPU-scheduling information includes a process priority and scheduling related info o Memory-management information may include such items as the value of the base and limit registers and the page tables o IO status includes list of IO devices allocated to process, a list of open files, and so on o Accounting information may include the amount of processor time and clock time used, time limits, account numbers, and so on Process Management
  • 269.  Process scheduler selects an available process for program execution on CPU  As processes enter the system they are put into a job queue which consists of all processes in the system  The processes that are residing in main memory and are ready and waiting to execute are kept on a list called the ready queue  The processes that are waiting for an I/O device are kept on a list called the device or blocked queue  Processes migrate among the various queues Process Management
  • 270. Dispatch Timeout Event Wait Event Occurs Ready Queue Blocked Queue Admit Release Processor Dispatch Release Ready Queue Admit Processor Timeout (a) Single blocked queue
  • 271. Figure 3.8 Queuing Model for Figure 3.6 Event 1 Wait Event 2 Wait Event n Wait Event Occurs Dispatch Release Ready Queue Admit Processor Timeout Event 1 Queue Event 1 Occurs Event 2 Occurs Event n Occurs Event 2 Queue Event n Queue (a) Single blocked queue (b) Multiple blocked queues
  • 272.  Context Switch o When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch o Context of a process represented in the PCB o Context switch time is overhead  System does no useful work while switching  More complex the OS and the PCB longer the context switch Process Management
  • 273.  The steps in a full process switch are: save the context of the processor save the context of the processor update the process control block of the process currently in the Running state update the process control block of the process currently in the Running state move the process control block of this process to the appropriate queue move the process control block of this process to the appropriate queue select another process for execution select another process for execution update the process control block of the process selected update the process control block of the process selected update memory management data structures update memory management data structures restore the context of the processor to that which existed at the time the selected process was last switched out restore the context of the processor to that which existed at the time the selected process was last switched out If the currently running process is to be moved to another state such as Ready, Blocked, etc. then the OS must make substantial changes in its environment Process Management
  • 275.
  • 276. Process Management The Linux kernel limits process IDs to being less than or equal to 32,767. When a new process is created, it is assigned the next sequentially available process ID.
  • 278.  Memory layout of a process on Linux o The memory allocated to each process is composed of a number of parts, usually referred to as segments.  Text segment contains the machine-language instructions of the program run by the process.  Initialized data segment contains global and static variables that are explicitly initialized.  Uninitialized data segment contains global and static variables that are not explicitly initialized.  Stack is a dynamically growing and shrinking segment containing stack frames.  One stack frame is allocated for each currently called function. » A frame stores the function’s local variables arguments, and return value.  Heap is an area from which memory can be dynamically allocated at run time. Process Management
  • 281. The space labeled argv, environ at the top of this diagram holds the program command-line arguments
  • 282.  Programs that require more memory than is physically available Virtual Memory Management
  • 283.  Code needs to be in memory to execute, but entire program rarely used o Error code, unusual routines, large data structures  Entire program code not needed at same time Virtual Memory Management
  • 284.  Consider ability to execute partially-loaded program o Program no longer constrained by limits of physical memory o Program and programs could be larger than physical memory Virtual Memory Management
  • 285.  Virtual memory is a technique that allows the execution of processes which are not completely available in memory Virtual Memory Management
  • 286.  Virtual memory involves the separation of logical memory as perceived by users from physical memory o This separation allows an extremely large virtual memory to be provided for programmers when only a smaller physical memory is available Virtual Memory Management
  • 287.  A virtual memory scheme splits the memory used by each program into small, fixed-size units called pages  RAM is divided into a series of page frames of the same size  At any one time, only some of the pages of a program need to be resident in physical memory page frames  Copies of the unused pages of a program are maintained in the swap area and loaded into physical memory only as required o Swap area is a reserved area of disk space used to supplement RAM Virtual Memory Management
  • 288.  The kernel maintains a page table for each process which describes the location of each page in the process’s virtual address space o Each entry in the page table either indicates the location of a virtual page in RAM or indicates that it currently resides on disk. Virtual Memory Management
  • 289. When a process references a page that is not currently resident in physical memory, a page fault occurs, at which point the kernel suspends execution of the process while the page is loaded from disk into memory. Virtual Memory Management
  • 290.  The Stack and Stack Frames o The stack grows and shrinks linearly as functions are called and return. o Each time a function is called, an additional frame is allocated on the stack, and this frame is removed when the function returns. o Since functions can call one another, there may be multiple frames on the stack. Virtual Memory Management
  • 291.  The Stack and Stack Frames o Each stack frame contains the following information:  Function arguments and local variables  Call linkage information  Each function uses certain CPU registers, such as the program counter, which points to the next machine-language instruction to be executed. Each time one function calls another, a copy of these registers is saved in the called function’s stack frame so that when the function returns, the appropriate register values can be restored for the calling function. Virtual Memory Management
  • 292.  A signal is a notification to a process that an event has occurred o Signals are sometimes described as software interrupts  One process can send a signal to another process Signals
  • 293.  The usual source of many signals sent to a process is the kernel o A hardware exception occurred, meaning that the hardware detected a fault condition that was notified to the kernel, which in turn sent a corresponding signal to the process concerned  Dividing by 0 or referencing a part of memory that is inaccessible o The user typed one of the terminal special characters that generate signals  Control C or Control Z o A software event occurred  Input became available on a file descriptor  A timer went off, the process’s CPU time limit was exceeded  A child of this process terminated. Signals
  • 294.  Signal names are defined by including the header file signal.h  They all begin with SIG Signals
  • 296.  A program can change action that occurs when the signal is delivered o The default action should occur  This is useful to undo an earlier change of the disposition of the signal to something other than its default o The signal is ignored  This is useful for a signal whose default action would be to terminate the process o A signal handler is executed  A signal handler is a function, written by the programmer, that performs appropriate tasks in response to the delivery of a signal  Notifying the kernel that a handler function should be invoked is usually referred to as installing or establishing a signal handler Signals
  • 299. Signals A simple example of a signal handler function and a main program that establishes it as the handler for the SIGINT signal. The terminal driver generates this signal when we type the terminal interrupt character such as Control C.
  • 300. Signals A program that establishes a handler for SIGINT and SIGQUIT
  • 302.  One process can send a signal to another process using the kill system call Signals
  • 305. Signals A simple signal handler that catches either of the two user-defined signals and prints the signal number The pause function suspends the calling process until a signal is received
  • 307.  In many applications, creating multiple processes can be a useful way of dividing up a task o For example, a network server process may listen for incoming client requests and create a new child process to handle each request o The server process continues to listen for further client connections. Process Creation
  • 308.  The fork system call creates a new process, the child, which is an almost exact duplicate of the calling process, the parent Process Creation
  • 309.  Return of PID and zero allows both the parent and child to determine which is which Process Creation
  • 310.  The key point to understanding fork is to realize that after it has completed its work, two processes exist and in each process, execution continues from the point where fork returns.  The two processes are executing the same program text, but they have separate copies of the stack, data, and heap segments. o The child’s stack, data, and heap segments are initially exact duplicates of the corresponding parts the parent’s memory. o After the fork, each process can modify the variables in its stack, data, and heap segments without affecting the other process. Process Creation
  • 312. This program creates a child that modifies the copies of global and automatic variables that it inherits
  • 313.  A process may terminate in two general ways. o Abnormal termination o Normal termination using _exit system call  The status argument given to _exit defines the termination status of the process, which is available to the parent of this process when it calls wait  A termination status 0 = process completed successfully and a nonzero status = process terminated unsuccessfully  EXIT_SUCCESS (0) and EXIT_FAILURE (1) Process Termination
  • 314.  Exit Handlers o An exit handler is a programmer-supplied function that is registered at some point during the life of the process and is then automatically called during normal process termination via exit o Exit handlers are not called if a program calls _exit directly or if the process is terminated abnormally by a signal. Process Management
  • 316.
  • 317.  Waiting on a Child Process o In many applications where a parent creates child processes, it is useful for the parent to be able to monitor the children to find out when and how they terminate o The wait system call waits for one of the children of the calling process to terminate and returns the termination status of that child in the buffer pointed to by status Process Management
  • 319.
  • 320. The main program creates two child processes to execute the same printing loop and display a message before exit. For the parent process, after creating two child processes, it enters the wait state by executing the system call wait. Once a child exits, the parent starts execution and the ID of the terminated child process is returned in PID so that it can be printed. There are two child processes and thus two wait system calls, one for each child process.
  • 321.
  • 322.  Waiting on a Child Process o Parent does not have to wait immediately after creating all child processes. It may do some other tasks. o Check eClass for an example Process Management
  • 323.  The wait system call has a number of limitations o If a parent process has created multiple children, it is not possible to wait for the completion of a specific child  Parent can only wait for the next child that terminates. o If no child has yet terminated, wait always blocks o Using wait, we can find out only about children that have terminated  It is not possible to be notified when a child is stopped by a signal or when a stopped child is resumed by delivery of a signal. Process Management
  • 324.
  • 325.  The status value returned by wait and waitpid allows us to distinguish the events for the child Process Management
  • 326. This program creates a child which immediately exits.
  • 327.
  • 328. This program illustrates the use of waitpid to wait for a process to end.
  • 329.  Orphans and Zombies o The lifetimes of parent and child processes are usually not the same o Who becomes the parent of an orphaned child?  The orphaned child is adopted by init whose process ID is 1.  init is ancestor of all processes Process Management
  • 330.  Orphans and Zombies o What happens to a child that terminates before its parent has had a chance to perform a wait?  The kernel deals with this situation by turning the child into a zombie.  Most of the resources held by the child are released back to the system to be reused by other processes.  The only part of the process that remains is an entry in the kernel’s process table recording the child’s process ID, termination status, and resource usage statistics.  When the parent does perform a wait, the kernel removes the zombie Process Management
  • 331.  Executing a Shell Command o The system function allows the calling program to execute an arbitrary shell command o system returns 127 if a shell can’t be started to run the command and -1 if another error occurs. Otherwise, system returns the exit code of the command. Process Management
  • 332. The program returns from the call to system when the ps command has finished. ps command display all processes
  • 333.  A thread is a lightweight process  Running multiple threads simultaneously is called multithreading Thread Management
  • 334.  A single process can contain multiple threads o All of these threads are independently executing the same program, and they all share the same global memory, including the initialized data, uninitialized data, and heap segments. Thread Management
  • 335.  Difference between the fork system call and the creation of new threads o When a process executes a fork call, a new copy of the process is created with its own variables and its own PID. o This new process is scheduled independently, and executes almost independently of the process that created it. o When we create a new thread in a process, the new thread of execution gets its own stack and hence local variables but shares global variables, file descriptors, signal handlers, and its current directory state with the process that created it. Thread Management
  • 336.
  • 337.  Following are some advantages of using threads o Processes require more time for context switching as they are more heavy. Threads require less time for context switching as they are lighter than processes. o Because threads can share common data, they do not need to use interprocess communication. o Threads only need a stack and registers therefore, threads are cheap to create. o Sometimes it is very useful to make a program appear to do two things at once.  The classic example is to perform a real-time word count on a document while still editing the text. One thread can manage the user’s input and perform editing. The other, which can see the same document content, can continuously update a word count variable. Thread Management
  • 338.  Threads also have drawbacks o Writing multithreaded programs requires very careful design. o Debugging a multithreaded program is much, much harder than debugging a single-threaded one because the interactions between the threads are very hard to control. Thread Management
  • 339.  Besides global memory, threads also share a number of other attributes o Process ID and parent process ID o Process group ID o Open file descriptors o Process credentials such as user and group IDs o File system–related information: current working directory and root directory Thread Management
  • 340.  Among the attributes that are distinct for each thread are the following: o Thread ID o Alternate signal stack o The errno variable o Real-time scheduling policy and priority o Local variables and function call linkage information Thread Management
  • 341.  Compiling Pthreads programs o programs that use the Pthreads API must be compiled with the cc –pthread option Thread Management
  • 342. Thread Management  thread returns the thread id  attr Set to NULL if default thread attributes are used.  void * (*start ) pointer to the function to be threaded. Function has a single argument: pointer to void.  *arg pointer to argument of function. To pass multiple arguments, send a pointer to a structure
  • 346. Thread Management  Joining with a Terminated Thread
  • 347.
  • 348.
  • 349. Threads share global and static variables.
  • 350. Thread Management  Thread examples on eClass o Example demonstrates how to pass a simple integer to each thread o Returning a value from thread o Returning a value from thread 2
  • 351.
  • 352.  The material in these slides is taken from different sources including: o The Linux Programming Interface by Michael Kerrisk o Advanced Programming in the Unix Environment by W. Richard Stevens o Data and Computer Communications by William Stallings o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/ Acknowledgements
  • 353. Interprocess Communication Sayed Chhattan Shah Associate Professor of Computer Science Mobile Grid and Cloud Computing Lab Department of Information Communication Engineering Hankuk University of Foreign Studies Korea www.mgclab.com
  • 354.  Interprocess communication (IPC) enables processes to communicate with each other to share information Interprocess Communication
  • 355.  A taxonomy of UNIX IPC facilities Interprocess Communication
  • 356. Pipes  Oldest and perhaps simplest form of UNIX IPC  Half duplex o Data flows in only one direction  Only usable between processes with a common ancestor o Usually parent-child
  • 357. Pipes  Creating and using pipe
  • 359. Pipes  Pipes are anonymous o No name in file system  How do two processes share a pipe?
  • 363. A process creates pipe Writes to pipe Reads from pipe
  • 364. Create pipe Create child Parent writes to pipe Child reads from pipe Program to illustrate pipe system call shared by parent and child
  • 365. Two pipes are used First pipe to send input string from parent Second pipe to send a string from child
  • 366. Two pipes are used First pipe to send input string from parent Second pipe to send a string from child
  • 367. Two pipes are used First pipe to send input string from parent Second pipe to send a string from child
  • 368.
  • 369. FIFO  FIFOs == pipe with name in file system
  • 370. FIFO  Any process can open and use FIFO o FIFO to be used for communication between unrelated processes  Once a FIFO has been opened, we use the same I/O system calls as are used with pipes and other files
  • 371. C program to implement one side of FIFO This side writes first, then reads
  • 372. C program to implement one side of FIFO This side reads first, then writes
  • 373. Message Queues  Message queues allow processes to exchange data in the form of messages o The reader receives whole messages, as written by the writer. It is not possible to read part of a message, leaving the remainder in the queue, or to read multiple messages at a time.  In addition to containing data, each message has an integer type o Messages can be retrieved from a queue in first-in, first-out order or retrieved by type  A message queue is a linked list of messages stored within the kernel and identified by a message queue identifier.
  • 380. C Program for Message Queue Writer Process
  • 381. C Program for Message Queue Reader Process
  • 382. Message Queues  Client-Server Programming with Message Queues
  • 383. Message Queues  Client-Server Programming with Message Queues o A client-server application that uses one message queue per client o The application is a simple file server o The client sends a request message to the server’s message queue asking for the contents of a named file o The server responds by returning the file contents as a series of messages to the client’s private message queue
  • 384. Message Queues  Client-Server Programming with Message Queues
  • 386.
  • 387.  The material in these slides is taken from different sources including: o The Linux Programming Interface by Michael Kerrisk o Advanced Programming in the Unix Environment by W. Richard Stevens o Data and Computer Communications by William Stallings o Guide to Network Programming accessible at https://beej.us/guide/bgnet/html/ Acknowledgements
  • 388. Network Interprocess Communication Sayed Chhattan Shah Associate Professor of Computer Science Mobile Grid and Cloud Computing Lab Department of Information Communication Engineering Hankuk University of Foreign Studies Korea www.mgclab.com
  • 389.
  • 390.  TCP/IP Protocol Architecture o Referred to as TCP/IP protocol suite o Comprises a large collection of protocols used on the Internet o It is commonly known as TCP/IP because the foundational protocols in the suite are the Transmission Control Protocol (TCP) and the Internet Protocol (IP) Network Interprocess Communication
  • 391. Provides ccess to the TCP/IP environment for users and also provides distributed information services. Application Transfer of data between end points. May provide error control, flow control, congestion control, reliable delivery. Transport Shield higher layers from details of physical network configuration. Provides routing. May provide QoS, congestion control. Internet Logical interface to network hardware. May be stream or packet oriented. May provide reliable delivery. Network Access/ Data Link Transmission of bit stream; specifies medium, signal encoding technique, data rate, bandwidth, and physical connector. Physical Twisted pair, optical fiber, satellite, terrestrial microwave Ethernet, WiFi, ATM, frame relay IPv4, IPv6 TCP, UDP SMTP, FTP, SSH, HTTP ARP ICMP, OSPF, RSVP
  • 392.  Physical layer is responsible for carrying individual bits in a frame across the link  Concerned with issues like:  Characteristics of transmission medium  Nature of the signals Network Interprocess Communication
  • 393.  Network access layer covers the exchange of data between an end system and the network that it is attached to  Concerned with:  Access to and routing data across a network for two end systems attached to the same network Network Interprocess Communication
  • 394.  Internet layer implements procedures needed to allow data to travel across multiple interconnected networks  Implemented in end systems and routers Network Interprocess Communication
  • 395. • Transport layer may provide reliable end-to- end service or merely an end-to-end delivery service without reliability mechanisms Transmission Control Protocol • Most commonly used protocol to provide this functionality TCP Network Interprocess Communication
  • 396.  Application layer contains the logic needed to support the various user applications  A separate module is needed for each different type of application that is peculiar to that application Network Interprocess Communication
  • 397. Router J TCP IP Physical Physical IP NAP 1 NAP 2 Physical Physical Network Access Protocol #1 Host A App X App Y TCP IP Network Access Protocol #2 Host B App Y App X Network 1 Network 2 Global internet address 1 2 2 4 6 3 Subnetwork attachment point address Logical connection (e.g., virtual circuit) Logical connection (TCP connection) Port Figure 2.4 TCP/IP Concepts Operation of TCP and IP Subnetwork Subnetwork
  • 398.  Two levels of addressing are needed o Each host must have a unique global internet address o Each process with a host must have an address known as a port that is unique within the host Network Interprocess Communication
  • 399.  TCP is the transport layer protocol  TCP provides reliable, ordered and error checked delivery of data between applications running on computers connected to a local area network or Internet  Flow control and congestion control mechanism Network Interprocess Communication
  • 400.  User Datagram Protocol o Alternative to TCP o Does not guarantee delivery, preservation of sequence, or protection against duplication o Enables a procedure to send messages to other procedures with a minimum of protocol mechanism o Adds port addressing capability to IP o Includes a checksum to verify that no error occurs in the data Network Interprocess Communication
  • 401.  IP address  The source and destination address fields in the IP header each contain a 32-bit global internet address, generally consisting of a network identifier and a host identifier Three different notations in IPv4 addressing Network Interprocess Communication
  • 402.
  • 403.  Network Classes  The address is coded to allow a variable allocation of bits to specify network and host to accommodate both small and large networks Network Interprocess Communication
  • 404.  Routers forward packets based on only the network portion of the address  The host portion does not matter to the routers because all hosts on the same network will be sent in the same direction  This makes the routing tables much smaller Network Interprocess Communication
  • 405.  A taxonomy of UNIX IPC facilities Interprocess Communication
  • 406. Sockets  Sockets are a method of IPC that allow data to be exchanged between applications, either on the same host or on different hosts connected by a network  Each application creates a socket o A socket is one end of the communication channel between two applications  Each socket exists in a domain  Domain determines: o Method of identifying socket o Range of communication  Processes on a single host  Across a network
  • 407. Sockets  Modern operating systems support at least the following domains o UNIX domain (AF_UNIX)  Communication on single host  Address == file system pathname o IPv4 domain (AF_INET)  Communication on IPv4 network  Address = IPv4 address (32 bit) + port number o IPv6 domain (AF_INET6)  Communication on IPv6 network  Address = IPv6 address (128 bit) + port number
  • 408. Sockets  Two main sockets types available in all domains o Stream (SOCK_STREAM) o Datagram (SOCK_DGRAM)
  • 409. Sockets  Stream sockets o SOCK_STREAM o Byte stream o Connection-oriented  Like a two-party phone call o Reliable, ordered, unduplicated o Uses TCP protocol
  • 410. Sockets  Datagram sockets o SOCK_DGRAM o Message-oriented o Connection-less  Like a postal system o Unreliable, duplicated, out of order o Uses UDP protocol
  • 411.
  • 412.
  • 414. Sockets  Binding a socket to an address This construct holds the information for socket address
  • 415. Sockets  IPv4 socket addresses o This construct holds the information about the address family, port number, Internet address, and the size of the struct sockaddr.
  • 416. Sockets  Network byte order o IP addresses and port numbers are integer values o Different hardware architectures store the bytes of a multibyte integer in different orders
  • 417. Sockets  Network byte order o Functions for converting integers in either direction between host and network byte order
  • 418. Sockets  Network byte order o Following functions allow conversion of both IPv4 and IPv6 addresses between binary form and dotted-decimal or hex-string notation
  • 419. Sockets  Following functions are used to get and set options on sockets  setsockopt (server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))