0% found this document useful (0 votes)
59 views

Process Is Not Same As Program!: Aup Notes 1 / 5

The document discusses the key concepts of processes and interprocess communication (IPC) in Unix systems. It defines processes as programs that are currently executing, as distinguished from programs which are passive code stored on disk. Processes run in either user mode or kernel mode. IPC mechanisms allow processes to communicate and coordinate, including pipes for unidirectional communication, shared memory for fast data transfer, and semaphores to synchronize access to shared resources. The fork() system call duplicates a process, with the parent and child returning different values to distinguish themselves.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Process Is Not Same As Program!: Aup Notes 1 / 5

The document discusses the key concepts of processes and interprocess communication (IPC) in Unix systems. It defines processes as programs that are currently executing, as distinguished from programs which are passive code stored on disk. Processes run in either user mode or kernel mode. IPC mechanisms allow processes to communicate and coordinate, including pipes for unidirectional communication, shared memory for fast data transfer, and semaphores to synchronize access to shared resources. The fork() system call duplicates a process, with the parent and child returning different values to distinguish themselves.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

aup notes 1 / 5

Jai Gajanan
TOPIC:
Typical arrangement of a user process (refer notes)
Process = text + data + stack

Unix = file + process

Program = is a executable file

Process = is an instance of a program that is being executed by OS


fork() is only a system call to create a new process in unix!
So
** Notes about fork() system call see BOX at the end of the doc

Process is program in execution.


Process is not same as program!
• Program is a passive entity stored in disk
• Program (code) is just part of the process
(learn to use ps, kill, top, vmstat commands with options whenever you are in
lab?) & (visit n see the /proc directory contents)

Process two types 1) Preemptive and 2) Non preemptive


And runs in two modes 1) User Mode? and 2) Kernel Mode?

System call = by using it any active process obtain services from the kernel.

A system call is implemented in the kernel. When a program makes a system call, the
arguments are packaged up and handed to the kernel, which takes over execution of the program
until the call completes.
A system call isn’t an ordinary function call, and a special procedure is required to transfer
control to the kernel. Low-level I/O functions such as open and read are examples of system calls on
Unix/ Linux.
The system calls forms the most basic interface between programs and the kernel. Each call
presents a basic operation or capability.
To watch system call run command like
$strace hostname <enter>

Kernel = is OS, provides services such as file system, mem mgt, CPU scheduling, device
I/O for the programs

What is signal?
Informs the process occurrence of asynchronous event. It is also
called as “S/W interrupt”
Signal can be sent 1) By one process to another or 2) By kernel to
the process

Reading : What is signal? How and when are signals sent? ( page# 44)

[email protected] n  9422883141


aup notes 2 / 5
INPUT & OUTPUT in Unix
There are two methods available to do I/O under Unix for doing input and output:
i) Unix system calls for I/O
ii) Standard I/O Library

i) Unix system calls for I/O : open, read, creat, write, close, lseek, dup, dup2… etc

the syntax for them is as below

int open(char *pathname, int oflag, [, int mode] ) ;


O_RDONLY
O_WRONLY
O_RDWR
O_NDELAY
O_APPEND
O_CREAT
O_TRUNC
O_EXCL

int creat(char *pathname, int mode);

int close(int filedescriptor);

int read(int fd, char *buff, unsigned int nbytes);

int write(int fd, char *buff, unsigned int nbytes);

long lseek(int fd, long *offet, int whence);


if whence is 0, the files position is set to the offset bytes
from the beginning of the file.
if whence is 1, the files position is set to its current
position plus the offset can be +ve or –ve.
if whence is 2, the file’s position is set to the size of the
file plus the offset. The offset can be +ve or –ve.

Now what about dup and dup2 ? read from the book page# 41

ii) Standard I/O Library


int fcntl(int fd, int cmd, int arg);
The fcntl is used to change the properties of the file that is already open. The fcntl system
call is the access point for several advanced operations on file descriptors. The first argument to
fcntl is an open file descriptor, and the second is a value that indicates which operation is to be
performed. For some operations, fcntl takes an additional argument. The most useful fcntl
operations, file locking. See the fcntl man page for information about the others. The fcntl
system call allows a program to place a read lock or a write lock on a file, somewhat analogous to
the mutex locks!

int cmd : F_DUPFD/ F_SETD/ F_FETFD/ ….. read page#42

int ioctl(int fd, unsigned long request, char arg);


The ioctl is used to change the behavior of an open file. The ioctl system call is an all-
purpose interface for controlling hardware devices. The first argument to ioctl is a file descriptor,
which should be opened to the device that you want to control. The second argument is a request
[email protected] n  9422883141
aup notes 3 / 5
code that indicates the operation that you want to perform. Various request codes are available
for different devices. Depending on the request code, there may be additional arguments
supplying data to ioctl.

** about fork() sytem call


• fork() is called once …
• … but it returns twice!!
– Once in the parent and
– Once in the child
– See & note the observation in practical i.e in laboratory ( call me )
• fork() basically duplicates the parent process image
– Both processes are exactly the same after the fork() call.
• Are there any dependence between the two processes?
– Provide a way to distinguish the parent and the child.

• How to distinguish parent and child??


– Return value in child = 0
– Return value in parent = process id of child
• Return value of -1 indicates error in all UNIX system calls.

Terminating a process
• exit (int status)
– Clean up the process (e.g close all files)
– Tell its parent processes that he is dying (SIGCHLD)
– Tell child processes that he is dying (SIGHUP)
– Exit status can be accessed by the parent process.

IPC mechanism in Unix (Interprocess communication)


1. Pipes/ FIFOs
2. Shared memory
3. semaphore
4. Message queues

1. PIPES
• The oldest mechanism & common form of Unix IPC
• Half-duplex
• Can only be used between processes that have common ancestor
• Provides unidirectional communication ( one way )
• Is not associated with temporary file! ( pipe(pfd) )
FIFO’s
• Named pipes
• Allows only related processes to communicate
• Used by shell command to pass data from one shell pipeline to another without
creating intermediate temporary file
• Pass data between client and server process (mknode or mkfifo)

But what is pipe?

Run following commands on system prompt


$ls –l | less
$ps –ef | more

[email protected] n  9422883141


aup notes 4 / 5
Pipes: two types of pipes, named pipes and unnamed pipes
– name pipes:
• like a file (create a named pipe (mknod), open, read/write)
• can be shared by any process
– Unnamed pipes:
• An unnamed pipe does not associate with any physical file.
• It can only be shared by related processes (descendants of a process
that creates the unnamed pipe).
• Created using system call pipe().

• The pipe system call syntax


int pipe(int fds[2])
– The semantic: creates a pipe and returns two file descriptors fds[0] and
fds[1], both for reading and writing

a read from fds[0] accesses the data written to fds[1] (POSIX)


and a read from fds[1] accesses the data written to fds[0] (non standard).
the pipe has a limited size (64K in some systems) - cannot write
to the pipe infinitely. Writing to a pipe with no reader: broken pipe error
Reading from a pipe with no writer?

H/W
• What is the difference between open and dup?

A pipe is a communication device that permits unidirectional communication. Data written to the
“write end” of the pipe is read back from the “read end.” Pipes are serial devices; the data is always
read from the pipe in the same order it was written. Typically, a pipe is used to communicate
between two threads in a single process or between parent and child processes. In a shell, the
symbol “|” creates a pipe.
For example, this shell command see above BOX
A pipe’s data capacity is limited. If the writer process writes faster than the reader process
consumes the data, and if the pipe cannot store more data, the writer process blocks until more
capacity becomes available. If the reader tries to read but no data is available, it blocks until data
becomes available. Thus, the pipe automatically synchronizes the two processes.

2. Shared Memory:
• Fastest of all IPC mechanism
• Access to shared region of memory often controlled by semaphore
• Catch all for the shared memory operations…
• Slower than normal memory access
• Most useful of three structures
• Shared memory is accessible to many processes
• Quickest way to share information among processes
• Shared memory is persistence – we’ve to clean up (e.g. ipcrm command)

3. Semaphore:
• Main use to synchronize access to shared memory. (Dutch scientist “Dijkastra” P & V
operations)
• Is a counter to provide access to shared data for multiple processes.

To obtain shared resources, processes needs to do the following:


1. Test sema that controls the resource
2. if val of sema > 0, decrement sema and use resource & when use of resource is over
increment the sema
3. If val == 0 sleep until val > 0
[email protected] n  9422883141
aup notes 5 / 5

[ -------------------------------------------------------- ]
[ learn about following command + options while learning IPC ]
[ $ipcs –m –q –s –a ]
[ $ipcrm –m ]
[ -------------------------------------------------------- ]

4. Messages:
Read from other notes/ book. It has no performance advantage
as compared to other techniques?

Learn about the typical movement of data between client and server using shared
memory. (refer fig 3.25/ p#154) and ( fig 3.24/ p#153)

[email protected] n  9422883141

You might also like