Introduction
R Lakshman naik
Program and Process
• An instance of a program executed by the operating
systems.
• Every Process has a unique process ID (PID).
• Parent process can only use the fork system call to create a
new child process
• Relationship between parent and child:
– #include <unistd.h> /* all header files are under /user/include/ */
– #include <sys/types.h> /* under /user/include/sys/ */
• pid_t getpid(void), ----- returns process ID
• pid_t getppid(void),------returns parent process ID
• pid_t getpgid(void),------returns process group ID
• uid_t getuid(void),------returns process user ID
• gid_t getgid(void),------returns group ID
• Process ID, parent process ID, process group ID, terminal
group ID, user ID, group ID, effective user ID, effective group
ID
Process Control Board
Process System Call
• Unix Kernel (operating system) provides a limited
number (60-200) of direct entry points through
which an active process can obtain services from
the kernel. These are named system calls.
– Return value: –1 means: error occurs; >=0: OK.
– Global interger value errno: contains the system error
number, See <errno.h>,<sys/errno.h>.
– When an error occurs, use perror( ) to print a message
to explain the error.
– Use %man to know more details.
File descriptor
• In Unix and related computer operating systems, a file
descriptor (FD) is an abstract indicator used to access a file or
other input/output resource, such as a pipe or network socket.
• File descriptors form part of the POSIXapplication programming
interface.
• A file descriptor is a non-negative integer, generally represented in
the C programming language as the type int (negative values being
reserved to indicate "no value" or an error condition).
Integer <unistd.h> symbolic <stdio.h> file
Name
value constant stream
0 Standard input STDIN_FILENO stdin
1 Standard output STDOUT_FILENO stdout
2 Standard error STDERR_FILENO stderr
File
• All data in Unix is organized into files.
• All files are organized into directories.
• These directories are organized into a tree-like structure
called the filesystem
• There are three basic types of files −
– Ordinary Files − An ordinary file is a file on the system that
contains data, text, or program instructions.
– Special Files − Some special files provide access to hardware
such as hard drives, CD-ROM drives, modems, and Ethernet
adapters.
– Directories − Directories store both special and ordinary files.
For users familiar with Windows or Mac OS, Unix directories are
equivalent to folders.
File operations
• Creating
• Editing
• Displaying content
• Counting words
• Copying
• Renaming
• deleting
Absolute/Relative Pathnames
• Directories are arranged in a hierarchy with root (/) at the
top.
• The position of any file within the hierarchy is described by
its pathname.
• Elements of a pathname are separated by a /.
• A pathname is absolute, if it is described in relation to root,
thus absolute pathnames always begin with a /.
– /etc/passwd
– /users/sjones/chem/notes
– /dev/rdsk/Os3
• A pathname can also be relative to your current working
directory. Relative pathnames never begin with /.
– chem/notes
– personal/res
File Permission / Access Modes
• File ownership is an important component of
Unix that provides a secure method for storing
files.
• Every file in Unix has the following attributes −
– Owner permissions
– Group permissions
– Other (world) permissions
• $ls -l /home/amrood
-rwxr-xr-- 1 root users 1024 Nov 2 00:10 myfile
drwxr-xr--- 1 root users 1024 Nov 2 00:10 mydir
Access Modes
File Access Modes Directory Access Modes
• Read
• Read – Access to a directory means that
– Grants the capability to read, the user can read the contents.
The user can look at
i.e., view the contents of the the filenames inside the
file. directory.
• Write • Write
– Access means that the user can
– Grants the capability to add or delete files from the
modify, or remove the directory.
content of the file. • Execute
– Executing a directory doesn't
• Execute really make sense, so think of
– User with execute this as a traverse permission.
permissions can run a file as a – A user must have execute access
to the bin directory in order to
program. execute the ls or
the cd command.
Changing Permissions
[Link]. Chmod operator & Description
1 +
Adds the designated permission(s) to a file or
directory.
2 -
Removes the designated permission(s) from a file or
directory.
3 =
Sets the designated permission(s).
Two commands are available to change the owner and the group of files −
•chown − The chown command stands for "change owner" and is used to change
the owner of a file.
•chgrp − The chgrp command stands for "change group" and is used to change the
group of a file.
Example
• $chmod o+wx testfile
• $ls -l testfile -rwxrwxrwx 1 root users 1024 Nov 2 00:10 testfile
• $chmod u-x testfile
• $ls -l testfile -rw-rwxrwx 1 root users 1024 Nov 2 00:10 testfile
• $chmod g = rx testfile
• $ls -l testfile -rw-r-xrwx 1 root users 1024 Nov 2 00:10 testfile
Signals
• Singals are software interrupts and occur asynchronously
at any time.
• For example, a segmentation fault produces a SIGSEGV
signal. By default, SIGSEGV dumps core + terminate the
process. SIGALRM signal is generated by using unsigned
int alarm(unsigned int seconds). Default is to terminate
the process.
– Each signal is generated from process ->process or from
kernel -> process.
– Each signal has a name defined in
“/usr/include/sys/iso/signal_iso.h”, description and default
action,
• e.g. SIGALRM; Alarm clock; Terminate. (Copy [Stevens90;P44] or
<signal.h> ).
How to generate signals
Process->process:
• System call: int kill(int pid, int sig). It is because signals can be
produced by program using kill( ) that signals are also called as
software interrupts. kill( ) is not only used to kill a process, but also
used in most cases to send a signal sig to a process pid. (Copy
[Stevens90;P45]).
• Command: kill –9 pid, Generate SIGTERM. Don’t confuse kill
command with system call of the same name.
kernel -> process:
• Keyboard: control-C or DeleteSIGINT. Control-backslashSIGQUIT.
Control-ZSIGTSTP.
• Hardware: SIGSEGV or SIGFPE for Floating Point Arithmetic errors.
• Software condition: Special software conditions are noticed by
kernel such as SIGURG for out-of-band data on a socket.