SE Operating System Manual+Programs-rpc 10 prog
SE Operating System Manual+Programs-rpc 10 prog
Bhusawal
Department of Computer Science & Engineering
Class: S.E Sem-IV
Theory:
A shell is not an operating system. It is a way to interface with the operating system and
run commands.
What is BASH?
Bash is a shell written as a free replacement to the standard Bourne Shell (/bin/sh)
originally written by Steve Bourne for UNIX systems.
It has all of the features of the original Bourne Shell, plus additions that make it easier to
program with and use from the command line.
Since it is Free Software, it has been adopted as the default shell on most Linux systems.
“\” vs. “/”: In DOS, the forward-slash “/” is the command argument delimiter, while
the backslash “\” is a directory separator. In Linux/UNIX, the “/” is the directory
separator, and the “\” is an escape character. More about these special characters in a
minute!
Filenames: The DOS world uses the “eight dot three” filename convention, meaning
that all files followed a format that allowed up to 8 characters in the filename, followed
by a period (“dot”), followed by an option extension, up to 3 characters long (e.g.
FILENAME.TXT). In UNIX/Linux, there is no such thing as a file extension. Periods can
be placed at any part of the filename, and “extensions” may be interpreted differently by
all programs, or not at all.
Special Characters
Before we continue to learn about Linux shell commands, it is important to know that
there are many symbols and characters that the shell interprets in special ways. This
means that certain typed characters: a) cannot be used in certain situations, b) may be
used to perform special operations, or, c) must be “escaped” if you want to use them in a
normal way.
Charact Description
er
\ Escape character. If you want to reference a special character, you must
“escape” it with a backslash first.
Example: /usr/src/linux
. Current directory. Can also “hide” files when it is the first character in a
filename.
.. Parent directory
~ User's home directory
* Represents 0 or more characters in a filename, or by itself, all files in a
directory.
hello22.txt
[] Can be used to represent a range of values, e.g. [0-9], [A-Z], etc.
Example: ls | more
> Redirect output of a command into a new file. If the file already exists,
over-write it.
Executing Commands
The Command PATH:
Most common commands are located in your shell's “PATH”, meaning that you can just
type the name of the program to execute it.
Your shell's “PATH” variable includes the most common program locations, such as
To execute commands that are not in your current PATH, you have to give the complete
location of the command.
Examples: /home/bob/myprogram
Command Syntax
Commands can be run by themselves, or you can pass in additional arguments to
make them do different things. Typical command syntax can look something like
this:
command [-argument] [-argument] [--argument] [file]
Examples: ls List files in current directory
ls -l Lists files in “long” format
ls -l --color As above, with colorized output
cat filename Show contents of a file
cat -n filename Show contents of a file, with line numbers
Getting Help
When you're stuck and need help with a Linux command, help is usually only a few
keystrokes away! Help on most Linux commands is typically built right into the
commands themselves, available through online help programs (“man pages” and
“info pages”), and of course online.
Info Pages
Some programs, particularly those released by the Free Software Foundation, use info
pages as their main source of online documentation. Info pages are similar to man
page, but instead of being displayed on one long scrolling screen, they are presented
in shorter segments with links to other pieces of information. Info pages are accessed
with the “info” command, or on some Linux distributions, “pinfo” (a nicer info
browser).
For example: info df Loads the “df” info page.
Navigating the Linux File system
The Linux file system is a tree-like hierarchy hierarchy of directories and files. At the
base of the file system is the “/” directory, otherwise known as the “root” (not to be
confused with the root user). Unlike DOS or Windows filesystems that have multiple
“roots”, one for each disk drive, the Linux filesystem mounts all disks somewhere
underneath the / file system. The following table describes many of the most common
Linux directories.
The Linux Directory Layout
Directory Description
The nameless base of the filesystem. All other directories, files,
drives, and devices are attached to this root. Commonly (but
incorrectly) referred to as the “slash” or “/” directory. The “/” is just
a directory separator, not a directory itself.
/bin Essential command binaries (programs) are stored here (bash, ls,
mount, tar, etc.)
/boot Static files of the boot loader.
/dev Device files. In Linux, hardware devices are acceessd just like other
files, and they are kept under this directory.
/etc Host-specific system configuration files.
/home Location of users' personal home directories (e.g. /home/susan).
/lib Essential shared libraries and kernel modules.
/proc Process information pseudo-filesystem. An interface to kernel data
structures.
/root The root (superuser) home directory.
/sbin Essential system binaries (fdisk, fsck, init, etc).
/tmp Temporary files. All users have permission to place temporary files
here.
/usr The base directory for most shareable, read-only data (programs,
libraries, documentation, and much more).
/usr/bin Most user programs are kept here (cc, find, du, etc.).
/usr/include Header files for compiling C programs.
/usr/lib Libraries for most binary programs.
/usr/local “Locally” installed files. This directory only really matters in
environments where files are stored on the network. Locally-
installed files go in
/usr/local/bin, /usr/local/lib, etc.). Also often used for software
packages installed from source, or software not officially shipped
with the distribution.
/usr/sbin Non-vital system binaries (lpd, useradd, etc.)
/usr/share Architecture-independent data (icons, backgrounds, documentation,
terminfo, man pages, etc.).
/usr/src Program source code. E.g. The Linux Kernel, source RPMs, etc.
/usr/X11R6 The X Window System.
/var Variable data: mail and printer spools, log files, lock files, etc.
Commands for Navigating the Linux Filesystems
The first thing you usually want to do when learning about the Linux filesystem is
take some time to look around and see what's there! These next few commands will:
a) Tell you where you are,
b) take you somewhere else, and c) show you what's there. The following table
describes the basic operation of the pwd, cd, and ls commands, and compares them to
certain DOS commands that you might already be familiar with.
Finding Things
The following commands are used to find files. “ls” is good for finding files if you
already know approximately where they are, but sometimes you need more powerful
tools such as these:
Linux Description
Command
which Shows the full path of shell commands found in your path. For
example, if you want to know exactly where the “grep” command is
located on the filesystem, you can type “which grep”. The output
should be something like: /bin/grep
whereis Locates the program, source code, and manual page for a command
(if all information is available). For example, to find out where “ls”
and its man page are, type: “whereis ls” The output will look
something like:
ls: /bin/ls /usr/share/man/man1/ls.1.gz
locate A quick way to search for files anywhere on the filesystem. For
example, you can find all files and directories that contain the name
“mozilla” by typing: locate mozilla
find A very powerful command, but sometimes tricky to use. It can be
used to search for files matching certain patterns, as well as many
other types of searches. A simple example is:
find . -name \*mp3
This example starts searching in the current directory “.” and all
sub- directories, looking for files with “mp3” at the end of their
names.
Informational Commands
The following commands are used to find out some information about the user or the
system.
Linux Description
Command
clear Clear the screen
echo Display text on the screen. Mostly useful when writing shell scripts.
For example: echo “Hello World”
more Display a file, or program output one page at a time. Examples:
more mp3files.txt ls -
la | more
less An improved replacement for the “more” command. Allows you to
scroll backwards as well as forwards.
grep Search for a pattern in a file or program output. For example, to
find out which TCP network port is used by the “nfs” service, you
can do this:
grep “nfs” /etc/services
This looks for any line that contains the string “nfs” in the file
“/etc/services” and displays only those lines.
lpr Print a file or program output. Examples:
lpr mp3files.txt - Print the mp3files.txt file
ls -la | lpr - Print the output of the “ls -la” command.
sort Sort a file or program output. Example: sort mp3files.txt
su “Switch User”. Allows you to switch to another user's account
temporarily. The default account to switch to is the root/superuser
account. Examples:
su - Switch the root account
su - - Switch to root, and log in with root's environment
su larry- Switch to Larry's account
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________
Theory:
Listing Files
To list the files and directories stored in the current directory, use the following
command −
$ls
Here is the sample output of the above command −
$ls
1 -
Regular file, such as an ASCII text file, binary executable, or hard link.
2 b
Block special file. Block input/output device file such as a physical
hard drive.
3 c
Character special file. Raw input/output device file such as a physical
hard drive.
4 d
Directory file that contains a listing of other files and directories.
5 l
Symbolic link file. Links on any regular file.
6 p
Named pipe. A mechanism for interprocess communications.
7 s
Socket used for interprocess communication.
Metacharacters
Metacharacters have a special meaning in Unix. For example, * and ? are
metacharacters. We use * to match 0 or more characters, a question mark (?)
matches with a single character.
For Example −
$ls ch*.doc
Displays all the files, the names of which start with ch and end with .doc −
ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch04-1.doc ch040.doc ch05.doc ch06-2.doc
ch01-2.doc ch02-1.doc c
Here, * works as meta character which matches with any character. If you want to
display all the files ending with just .doc, then you can use the following command
−
$ls *.doc
Hidden Files
An invisible file is one, the first character of which is the dot or the period
character (.). Unix programs (including the shell) use most of these files to store
configuration information.
Some common examples of the hidden files include the files −
.profile − The Bourne shell ( sh) initialization script
.kshrc − The Korn shell ( ksh) initialization script
.cshrc − The C shell ( csh) initialization script
.rhosts − The remote shell conZiguration Zile
To list the invisible files, specify the -a option to ls −
$ ls -a
Creating Files
You can use the vi editor to create ordinary files on any Unix system. You simply
need to give the following command −
$ vi filename
The above command will open a file with the given filename. Now, press the
keyi to come into the edit mode. Once you are in the edit mode, you can start
writing your content in the file as in the following program −
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
Once you are done with the program, follow these steps −
Press the key esc to come out of the edit mode.
Press two keys Shift + ZZ together to come out of the file completely.
You will now have a file created with filename in the current directory.
$ vi filename
$
Editing Files
You can edit an existing file using the vi editor. We will discuss in short how to
open an existing file −
$ vi filename
Once the file is opened, you can come in the edit mode by pressing the key iand
then you can proceed by editing the file. If you want to move here and there inside
a file, then first you need to come out of the edit mode by pressing the key Esc.
After this, you can use the following keys to move inside a file −
l key to move to the right side.
h key to move to the left side.
k key to move upside in the file.
j key to move downside in the file.
So using the above keys, you can position your cursor wherever you want to edit.
Once you are positioned, then you can use the i key to come in the edit mode. Once
you are done with the editing in your file, press Esc and finally two keys Shift +
ZZ together to come out of the file completely.
Copying Files
To make a copy of a file use the cp command. The basic syntax of the command is
−
$ cp source_file destination_file
Following is the example to create a copy of the existing file filename.
$ cp filename copyfile
$
You will now find one more file copyfile in your current directory. This file will
exactly be the same as the original file filename.
Renaming Files
To change the name of a file, use the mv command. Following is the basic syntax −
$ mv old_file new_file
The following program will rename the existing file filename to newfile.
$ mv filename newfile
$
The mv command will move the existing file completely into the new file. In this
case, you will find only newfile in your current directory.
Deleting Files
To delete an existing file, use the rm command. Following is the basic syntax −
$ rm filename
Caution − A Zile may contain useful information. It is always recommended to be
careful while using this Delete command. It is better to use the -i option along
with rm command.
Following is the example which shows how to completely remove the existing
file filename.
$ rm filename
$
You can remove multiple files at a time with the command given below −
$ rm filename1 filename2 filename3
$
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________
Aim: Shell Script programming using the commands grep, awk, and sed
Theory:
The grep Command
The grep command searches a file or files for lines that have a certain pattern. The syntax
is −
$grep pattern file(s)
The name "grep" comes from the ed (a Unix line editor) command g/re/pwhich means
“globally search for a regular expression and print all lines containing it”.
A regular expression is either some plain text (a word, for example) and/or special
characters used for pattern matching.
The simplest use of grep is to look for a pattern consisting of a single word. It can be used
in a pipe so that only those lines of the input files containing a given string are sent to the
standard output. If you don't give grep a filename to read, it reads its standard input;
that's the way all filter programs work −
$ls -l | grep "Aug"
-rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$
There are various options which you can use along with the grep command −
Sr.No. Option & Description
1 -v
Prints all lines that do not match pattern.
2 -n
Prints the matched line and its line number.
3 -l
Prints only the names of files with matching lines (letter "l")
4 -c
Prints only the count of matching lines.
5 -i
Matches either upper or lowercase.
Let us now use a regular expression that tells grep to find lines with "carol", followed by
zero or other characters abbreviated in a regular expression as ".*"), then followed by
"Aug".−
Here, we are using the -i option to have case insensitive search −
$ls -l | grep -i "carol.*aug"
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$
Sed Command
SED command in UNIX is stands for stream editor and it can perform lot’s of function on
file like, searching, find and replace, insertion or deletion. Though most common use of
SED command in UNIX is for substitution or for find and replace. By using SED you can
edit files even without opening it, which is much quicker way to find and replace
something in file, than first opening that file in VI Editor and then changing it.
SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).
SED command in unix supports regular expression which allows it perform
complex pattern matching.
Syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Example:
Consider the below text file as an input.
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Sample Commands
Replacing or substituting string : Sed command is mostly used to replace the text in a
file. The below simple sed command replaces the word “unix” with “linux” in the file.
$sed 's/unix/linux/' geekfile.txt
Output :
Awk Command:
1. AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines
2. Useful For:
(a) Transform data files
(b) Produce formatted reports
3. Programming Constructs:
(a) Format output lines
(b) Arithmetic and string operations
(c) Conditionals and loops
Syntax:
awk options 'selection _criteria {action }' input-file >
output-file
Options:
-f program-file : Reads the AWK program source from the
file
program-file, instead of from the
first command line argument.
-F fs : Use fs for the input field separator
Consider the following text file as the input file for all cases below.
3. Spliting a Line Into Fields : For each record i.e line, the awk command
splits the record delimited by whitespace character by default and stores it
in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and
$4 respectively. Also, $0 represents the whole line.
ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
In the above example, $1 and $4 represents Name and Salary fields
respectively.
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge
Shri Sant Gadge Baba College of Engineering & Technology,
Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : 4_____________________ Date :___________________________________
Theory:
Basic Terms must related to CPU scheduling
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from
time of submission of the process to the time of completion of the process(Wall
clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process
has been waiting in the ready queue to acquire get control on the CPU.
In simple terms, the duration for which a process gets control of the CPU is the
CPU burst time and the concept of gaining control of the CPU is the CPU burst.
FCFS CPU SCHEDULING ALGORITHM: For FCFS scheduling algorithm, read the
number of processes/jobs in the system, their CPU burst times. The scheduling is
performed on the basis of arrival time of the processes irrespective of their other
parameters. Each process will be executed according to its arrival time. Calculate
the waiting time and turnaround time of each of the processes accordingly.
Disadvantages: This scheduling method is nonpreemptive, that is, the process will
run until it finishes. Because of this nonpreemptive scheduling, short processes
which are at the back of the queue have to wait for the long process at the front to
finish.
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge
Theory:
Basic Terms must related to CPU scheduling
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from
time of submission of the process to the time of completion of the process(Wall
clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process
has been waiting in the ready queue to acquire get control on the CPU.
In simple terms, the duration for which a process gets control of the CPU is the
CPU burst time and the concept of gaining control of the CPU is the CPU burst.
SJF CPU SCHEDULING ALGORITHM: For SJF scheduling algorithm, read the
number of processes/jobs in the system, their CPU burst times. Arrange all the jobs
in order with respect to their burst times. There may be two jobs in queue with the
same execution time, and then FCFS approach is to be performed. Each process will
be executed according to the length of its burst time. Then calculate the waiting
time and turnaround time of each of the processes accordingly.
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge
Theory:
Basic Terms must related to CPU scheduling
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from
time of submission of the process to the time of completion of the process(Wall
clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process
has been waiting in the ready queue to acquire get control on the CPU.
In simple terms, the duration for which a process gets control of the CPU is the
CPU burst time and the concept of gaining control of the CPU is the CPU burst.
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge
Theory:
Optimal page replacement algorithm has the lowest page-fault rate of all algorithms and
will never suffer from Belady's anomaly. The basic idea is to replace the page that will not
be used for the longest period of time. Use of this page-replacement algorithm guarantees
the lowest possible page fault rate for a fixed number of frames. Unfortunately, the
optimal page-replacement algorithm is difficult to implement, because it requires future
knowledge of the reference string.
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________
Aim: Concurrent programming; use of process system calls (fork and v-fork).
Theory:
fork Function
An existing process can create a new one by calling the fork function.
#include <unistd.h>
pid_t fork(void);
The new process created by fork is called the child process. This function is called
once but returns twice. The only difference in the returns is that the return value in
the child is 0, whereas the return value in the parent is the process ID of the new
child. The reason the child's process ID is returned to the parent is that a process
can have more than one child, and there is no function that allows a process to
obtain the process IDs of its children. The reason fork returns 0 to the child is that
a process can have only a single parent, and the child can always call getppid to
obtain the process ID of its parent. (Process ID 0 is reserved for use by the kernel,
so it's not possible for 0 to be the process ID of a child.)
Both the child and the parent continue executing with the instruction that follows
the call to fork. The child is a copy of the parent. For example, the child gets a copy
of the parent's data space, heap, and stack. Note that this is a copy for the child; the
parent and the child do not share these portions of memory.
vfork Function
The function vfork has the same calling sequence and same return values as fork.
But the semantics of the two functions differ. The vfork function originated with
2.9BSD. Some consider the function a blemish, but all the platforms covered in this
book support it. In fact, the BSD developers removed it from the 4.4BSD release,
but all the open source BSD distributions that derive from 4.4BSD added support
for it back into their own releases. The vfork function is marked as an obsolete
interface in Version 3 of the Single UNIX Specification. The vfork function is
intended to create a new process when the purpose of the new process is to exec a
new program (step 2 at the end of the previous section). The bare-bones shell in
the program from Figure 1.7 is also an example of this type of program. The vfork
function creates the new process, just like fork, without copying the address space
of the parent into the child, as the child won't reference that address space; the
child simply calls exec (or exit) right after the vfork. Instead, while the child is
running and until it calls either exec or exit, the child runs in the address space of
the parent. This optimization provides an efficiency gain on some paged virtual-
memory implementations of the UNIX System. (As we mentioned in the previous
section, implementations use copy-on-write to improve the efficiency of a fork
followed by an exec, but no copying is still faster than some copying.) Another
difference between the two functions is that vfork guarantees that the child runs
first, until the child calls exec or exit. When the child calls either of these functions,
the parent resumes. (This can lead to deadlock if the child depends on further
actions of the parent before calling either of these two functions.)
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge
Shri Sant Gadge Baba College of Engineering & Technology,
Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : 9_____________________ Date :___________________________________
Theory: A typical UNIX process can be thought of as having a single thread of control:
each process is doing only one thing at a time. With multiple threads of control, we can
design our programs to do more than one thing at a time within a single process, with
each thread handling a separate task. This approach can have several benefits.
We can simplify code that deals with asynchronous events by assigning a separate
thread to handle each event type. Each thread can then handle its event using a
synchronous programming model. A synchronous programming model is much
simpler than an asynchronous one.
Multiple processes have to use complex mechanisms provided by the operating
system to share memory and file descriptors. Threads, on the other hand,
automatically have access to the same memory address space and file descriptors.
Some problems can be partitioned so that overall program throughput can be
improved. A single process that has multiple tasks to perform implicitly serializes
those tasks, because there is only one thread of control. With multiple threads of
control, the processing of independent tasks can be interleaved by assigning a
separate thread per task. Two tasks can be interleaved only if they don't depend on
the processing performed by each other.
Similarly, interactive programs can realize improved response time by using
multiple threads to separate the portions of the program that deal with user input
and output from the other parts of the program.
Thread Equality:
#include <pthread.h>
int pthread_equal(pthread_t tid1, pthread_t tid2);
Returns: nonzero if equal, 0 otherwise
Thread self
#include <pthread.h>
pthread_t pthread_self(void);
Returns: the thread ID of the calling thread
This function can be used with pthread_equal when a thread needs to identify data
structures that are tagged with its thread ID. For example, a master thread might place
work assignments on a queue and use the thread ID to control which jobs go to each
worker thread. A single master thread places new jobs on a work queue. A pool of three
worker threads removes jobs from the queue. Instead of allowing each thread to process
whichever job is at the head of the queue, the master thread controls job assignment by
placing the ID of the thread that should process the job in each job structure. Each worker
thread then removes only jobs that are tagged with its own thread ID.
The traditional UNIX process model supports only one thread of control per process.
Conceptually, this is the same as a threads-based model whereby each process is made up
of only one thread. With pthreads, when a program runs, it also starts out as a single
process with a single thread of control. As the program runs, its behavior should be
indistinguishable from the traditional process, until it creates more threads of control.
Additional threads can be created by calling the pthread_create function.
#include <pthread.h>
The memory location pointed to by tidp is set to the thread ID of the newly created thread
when pthread_create returns successfully. The attr argument is used to customize various
thread attributes. We'll set this to NULL to create a thread with the default attributes.
The newly created thread starts running at the address of the start_rtn function. This
function takes a single argument, arg, which is a typeless pointer. If you need to pass more
than one argument to the start_rtn function, then you need to store them in a structure
and pass the address of the structure in arg. When a thread is created, there is no
guarantee which runs first: the newly created thread or the calling thread. The newly
created thread has access to the process address space and inherits the calling thread's
floating-point environment and signal mask; however, the set of pending signals for the
thread is cleared.
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________
Theory:
signal Function
Signals are a way for a process to be notified of asynchronous events.
Some examples:
a timer you set has gone off (SIGALRM)
SIGTSTP stop signal.
SIGINTR interrupt signal.
some I/O you requested has occurred (SIGIO)
a user resized the terminal ”window” (SIGWINCH)
a user disconnected from the system (SIGHUP)
The simplest interface to the signal features of the UNIX System is the signal function.
#include <signal.h>
void (*signal(int signo, void (*func)(int)))(int);
Here:
signo is the name of the signal.
(*func): SIG_IGN which requests that we ignore the signal signo OR SIG_DFL which
requests that we accept the default action for signal signo OR the address of a
function which should catch or handle a signal.
The signo argument is just the name of the signal. The value of func is (a) the constant
SIG_IGN, (b) the constant SIG_DFL, or (c) the address of a function to be called when the
signal occurs. If we specify SIG_IGN, we are telling the system to ignore the signal.
(Remember that we cannot ignore the two signals SIGKILL and SIGSTOP.) When we
specify SIG_DFL, we are setting the action associated with the signal to its default value.
When we specify the address of a function to be called when the signal occurs, we are
arranging to "catch" the signal. We call the function either the signal handler or the signal-
catching function.
The prototype for the signal function states that the function requires two arguments and
returns a pointer to a function that returns nothing (void). The signal function's first
argument, signo, is an integer. The second argument is a pointer to a function that takes a
single integer argument and returns nothing. The function whose address is returned as
the value of signal takes a single integer argument (the final (int)). In plain English, this
declaration says that the signal handler is passed a single integer argument (the signal
number) and that it returns nothing. When we call signal to establish the signal handler,
the second argument is a pointer to the function. The return value from signal is the
pointer to the previous signal handler.
The signal function is defined by ISO C, which doesn't involve multiple processes, process
groups, terminal I/O, and the like. Therefore, its definition of signals is vague enough to be
almost useless for UNIX systems.
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________