Process and Inter-Process Communication
Tehran Polytechnic University
By : Amir Hossein Payberah
payberah@[Link]
1
Contents
What is a Process? Process Control Process Relationship Inter- Process Communication
Tehran Polytechnic University
2
What is a Process?
Program
An executable file An instance of a program that is being executed by the OS. Every process has a unique ID (PID).
Process
Tehran Polytechnic University
3
Process Management
The Unix OS is a time-sharing system. Each process is represented by a task_struct data structure. The task_vector is an array of pointers to every task_struct data structure in the system.
Tehran Polytechnic University
4
Process Statuses
Running
The process is either running or it is ready to run. The process is waiting for an event or for a resource. The process has been stopped, usually by receiving a signal. This is a halted process which, for some reason, still has a task_struct data structure in 5 the task vector.
Waiting
Stopped
Tehran Polytechnic University
Zombie
Type of Processes
Interactive Process
Initiated from (and controlled by) a shell Run in the background until required
Daemon Process
Tehran Polytechnic University
6
Related Commands
ps
Report process status. Display a tree of processes. Run a program with modified scheduling priority. Alter priority of running process. Send signal to a process.
7
pstree
nice
Tehran Polytechnic University
renice
kill
Related Commands
(Cont.)
top
Display top CPU processes. List the active jobs. Place a job in background (similar to &); Place a job in foreground. Stopped a process. Terminate a process.
8
jobs
bg
fg
Tehran Polytechnic University
Ctrl+z
Ctrl+c
Contents
What is a Process? Process Control Process Relationship Inter- Process Communication
Tehran Polytechnic University
9
Process Control
Tehran Polytechnic University
10
fork and vfork exit wait and waitpid exec signal kill
fork
PID1 PID2
Parent
fork
Child
Tehran Polytechnic University
11
fork (cont.)
int fork(); The only way a new process is created by the Unix kernel.
The new process created is called the child process. The child gets a copy of the parents data space, heap and stack. The parent and child dont share these portions of memory.
12
The child is a copy of the parent.
Tehran Polytechnic University
fork (cont.)
This function is called once, but return twice.
The process ID of the new child (to the parent).
A process can have more than one child.
0 (to the child).
Tehran Polytechnic University
13
fork Sample
main() { int pid; pid = fork(); if (pid < 0) // error else if (pid == 0) //child else //parent }
14
Tehran Polytechnic University
fork (cont.)
We never know if the child starts executing before the parent or vice versa.
This depends on the scheduling algorithm used by the kernel.
Tehran Polytechnic University
15
vfork
int = vfork(); It has the same calling sequence and same return values as fork. The child doesnt copy the parent data space.
The child runs in the address space of the parent.
Tehran Polytechnic University
With vfork, child runs first, then parent runs.
16
exit
Normal termination
Executing a return from the main function. Calling the exit function. Calling the _exit function. Calling abort. Receives certain signals.
Abnormal termination
Tehran Polytechnic University
17
exit (cont.)
Tehran Polytechnic University
int exit (int state); Sometimes we want the terminating process to be able to notify its parent how it terminated. For the exit and _exit function this is done by passing an exit status as the argument to these two functions. The parent of the process can obtain the termination status from either the wait or waitpid function.
18
Termination Conditions
Parent terminate before the child
The init process becomes the parent process of any process whose parent terminated.
Tehran Polytechnic University
19
Termination Conditions
Child terminate before the parent
The child is completely disappeared, but the parent wouldnt be able to fetch its termination status. The kernel has to keep a certain amount of information for every terminating process. The process that has terminated, but whose parent has not waited for it, is called zombie.
Tehran Polytechnic University
20
wait
When a process terminates, the parent is notified by the kernel sending the parent the SIGCHLD signal. The parent of the process can obtain the termination status from either the wait or waitpid function.
Tehran Polytechnic University
21
wait (cont.)
The process that calls wait or waitpid can:
Block (if all of its children are still running) Return immediately with the termination status of a child (if a child has terminated) Return immediately with an error (if it doesnt have any child process)
Tehran Polytechnic University
22
wait and waitpid
int wait (int *statloc); int waitpid (int pid, int *statloc, int options);
If statloc is not a null pointer, the termination status of the terminated process is stored in this location.
The difference between these two function:
Tehran Polytechnic University
wait can block, while waitpid has an option that prevents it from blocking. waitpid doesnt wait for the first child to terminate (it can control which process it waits for)
23
exec
Tehran Polytechnic University
A process cause another program to be executed by calling one of the exec functions. When a process calls one of the exec functions, that process is completely replaced by the new program. The process ID doesnt change across an exec.
24
exec functions
Tehran Polytechnic University
int execl (char *path, char *arg0, /*(char *) 0 */); int execle (char *path, char *arg0, /*(char *) 0, char *envp[] */); int execlp (char *filename, char *arg0, /*(char *) 0 */); int execv (char *pathname, char *argv0[]); int execve (char *pathname, char *argv0[], char *envp[]); int execvp (char *filename, char *envp[]);
25
signal
Signals are software interrupts. The name of signals all begin with the three character SIG : SIGABRT
Tehran Polytechnic University
26
signal (cont.)
void (*signal (int signo, void (*func) (int))) (int); Kernel do when a signal occurs:
Ignore the signal
Can not ignore : SIGKILL, SIGSTOP
Catch the signal Let the default action apply
Tehran Polytechnic University
27
signal Sample
main() { signal (SIGUSER, sig_user); while (1); } //--------------------------------void sig_user (int signo) { if (signo == SIGUSER) printf (receive signal\n); return; }
Tehran Polytechnic University
28
kill
int kill (int pid, int signo); Send a signal to a process or group of the processes.
Tehran Polytechnic University
29
Contents
What is a Process? Process Control Process Relationship Inter- Process Communication
Tehran Polytechnic University
30
Process Relationship
fork fork exec exec fork & exec exec
Tehran Polytechnic University
fork
fork exec
31
Contents
What is a Process? Process Control Process Relationship Inter- Process Communication
Tehran Polytechnic University
32
IPC
Pipe FIFO Message queue Shared memory socket
Tehran Polytechnic University
33
pipe
It provides a one-way flow of data. It is in the kernel It can only be used between processes that have a parent process in common. Parent Child
Tehran Polytechnic University
pipe
34
pipe (cont.)
int pipe (int *filedes); filedes[0] : open for reading filedes[1] : open for writing pipe command :
who | sort | lpr
Tehran Polytechnic University
35
FIFO
It is similar to a pipe. Unlike pipes, a FIFO has a name associated with it (named pipe). It uses a file as a communication way. int mknod (char *path, int mode, int dev)
Tehran Polytechnic University
mode is ored with S_IFIFO dev is equal 0 for FIFO.
36
Name Space
The set of possible names for a given type of IPC is called its name space. The name is how the client and server connect to exchange messages. key_t ftok (char *path, char proj);
Tehran Polytechnic University
37
Message Queue
Message queues are a linked list of messages stored within the kernel. We dont have to fetch messages in a first-int, first-out order.
We can fetch messages based on their type field.
Tehran Polytechnic University
A process wants to impose some structure on the data being transferred.
38
Message Queue (cont.)
int msgget (key_t key, int msgflag); A new queue is created, or an existing queue is open by msgget.
Tehran Polytechnic University
39
Message Queue (cont.)
int msgsnd(int msgid, void *ptr, size_t len, int flag); Data is placed onto a message queue by calling msgsnd; ptr points to a long integer that contains the positive integer message type, and it is immediately followed by the message data.
Tehran Polytechnic University
Struct my_msg { long type; char data[SIZE]; }
40
Message Queue (cont.)
int msgrcv (int msgid, void *ptr, sizet len, long mtype, int flag); The type argument lets us specify which message we want:
Tehran Polytechnic University
mtype == 0, the first message on the queue mtype > 0, the first message on the queue whose type equals mtype. mtype < 0, the first message on the queue whose type is the lowest value less or equal to the absolute value of mtype.
41
Message Queue (cont.)
int msgctl (int msgid, int cmd, struct msgid_ds *buf); The msgctl function performs various operations in a queue.
Tehran Polytechnic University
42
Shared memory
Client Server
Tehran Polytechnic University
Output file
Pipe, FIFO, message
kernel
Input file
43
Shared memory (cont.)
Client Shared memory Server
Tehran Polytechnic University
Output file
Kernel
Input file
44
Shared memory (cont.)
int shmget (key_t key, int size, int flag); A shared memory segment is created, or an existing one is accessed with the shmget system call.
Tehran Polytechnic University
45
Shared memory (cont.)
Char *shmat (int shmid, char *shmaddr, int shmfalg); The shmget dose not provide access to the segment for the calling process. We must attach the shared memory segment by calling the shmat system call.
Tehran Polytechnic University
46
Shared memory (cont.)
int shmdt (char *shmaddr); When a process is finished with a shared memory segment, it detaches the segment by calling the shmdt system call. This call dose not delete the shared memory segment.
Tehran Polytechnic University
47
Shared memory (cont.)
int shmctl (int shmid, int cmd, struct shmid_ds *buf); The msgctl function performs various operations in a shared memory segment.
Tehran Polytechnic University
48
Semaphore
Semaphores are a synchronization primitive. To obtain a shared resource:
Tehran Polytechnic University
Test the semaphore that controls the resource. If the value is positive the process can use the resource. The process decrements the value by 1. If the value is 0, the process goes to sleep until the value is greater than 0.
49
Semaphore (cont.)
int semget (key_t key, int nsems, int flag); This function get a semaphore ID. int semctl (int semid, int semnum, int cmd, union semun arg); The semctl function performs various operations in a semaphore.
Tehran Polytechnic University
50
Semaphore (cont.)
Tehran Polytechnic University
int semop (int semid, struct sembuf *semop, size_t nops); Struct sembuf { ushort sem_num; short sem_op; shoet sem_flag; }
51
Semaphore (cont.)
Each particular operation is specified by a sem_op value:
Tehran Polytechnic University
sem_op > 0, this correspond to the release of resources. The sem_op value is added to the current value; sem_op == 0, the caller wants to wait until the semaphores value become zero. sem_op < 0, this correspond to the allocation od resources. The caller wants to wait until the value become greater or equal the absolute value of sem_op. then the absolute value of sem_op is subtracted from the current value.
52
Question?
Tehran Polytechnic University
53