Top Banner
1 Tehran Polytechnic University Process and Inter-Process Communication By : Amir Hossein Payberah [email protected]
53

Process and Inter-Process Communication - SICS

Feb 11, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Process and Inter-Process Communication - SICS

1

Tehran

Polytechnic

University

Processand

Inter-Process Communication

By : Amir Hossein [email protected]

Page 2: Process and Inter-Process Communication - SICS

2

Tehran

Polytechnic

University

Contents

What is a Process? Process Control Process Relationship Inter- Process Communication

Page 3: Process and Inter-Process Communication - SICS

3

Tehran

Polytechnic

University

What is a Process? Program

An executable file Process

An instance of a program that is being executed by the OS.

Every process has a unique ID (PID).

Page 4: Process and Inter-Process Communication - SICS

4

Tehran

Polytechnic

University

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.

Page 5: Process and Inter-Process Communication - SICS

5

Tehran

Polytechnic

University

Process Statuses Running

The process is either running or it is ready to run.

Waiting The process is waiting for an event or for a

resource.

Stopped The process has been stopped, usually by

receiving a signal.

Zombie This is a halted process which, for some

reason, still has a task_struct data structure in the task vector.

Page 6: Process and Inter-Process Communication - SICS

6

Tehran

Polytechnic

University

Type of Processes Interactive Process

Initiated from (and controlled by) a shell Daemon Process

Run in the background until required

Page 7: Process and Inter-Process Communication - SICS

7

Tehran

Polytechnic

University

Related Commands ps

Report process status. pstree

Display a tree of processes. nice

Run a program with modified scheduling priority.

renice Alter priority of running process.

kill Send signal to a process.

Page 8: Process and Inter-Process Communication - SICS

8

Tehran

Polytechnic

University

Related Commands (Cont.) top

Display top CPU processes. jobs

List the active jobs. bg

Place a job in background (similar to &); fg

Place a job in foreground. Ctrl+z

Stopped a process. Ctrl+c

Terminate a process.

Page 9: Process and Inter-Process Communication - SICS

9

Tehran

Polytechnic

University

Contents

What is a Process? Process Control Process Relationship Inter- Process Communication

Page 10: Process and Inter-Process Communication - SICS

10

Tehran

Polytechnic

University

Process Control fork and vfork exit wait and waitpid exec signal kill

Page 11: Process and Inter-Process Communication - SICS

11

Tehran

Polytechnic

University

fork

forkParent Child

PID1 PID2

Page 12: Process and Inter-Process Communication - SICS

12

Tehran

Polytechnic

University

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 is a copy of the parent.

The child gets a copy of the parent’s data space, heap and stack.

The parent and child don’t share these portions of memory.

Page 13: Process and Inter-Process Communication - SICS

13

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).

Page 14: Process and Inter-Process Communication - SICS

14

Tehran

Polytechnic

University

fork Samplemain(){

int pid;pid = fork();

if (pid < 0)// error

else if (pid == 0)//child

else//parent

}

Page 15: Process and Inter-Process Communication - SICS

15

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.

Page 16: Process and Inter-Process Communication - SICS

16

Tehran

Polytechnic

University

vfork int = vfork(); It has the same calling sequence and

same return values as fork. The child doesn’t copy the parent data

space. The child runs in the address space of the

parent. With vfork, child runs first, then parent

runs.

Page 17: Process and Inter-Process Communication - SICS

17

Tehran

Polytechnic

University

exit Normal termination

Executing a return from the main function. Calling the exit function. Calling the _exit function.

Abnormal termination Calling abort. Receives certain signals.

Page 18: Process and Inter-Process Communication - SICS

18

Tehran

Polytechnic

University

exit (cont.)

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.

Page 19: Process and Inter-Process Communication - SICS

19

Tehran

Polytechnic

University

Termination Conditions Parent terminate before the child

The init process becomes the parent process of any process whose parent terminated.

Page 20: Process and Inter-Process Communication - SICS

20

Tehran

Polytechnic

University

Termination Conditions Child terminate before the parent

The child is completely disappeared, but the parent wouldn’t 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.

Page 21: Process and Inter-Process Communication - SICS

21

Tehran

Polytechnic

University

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.

Page 22: Process and Inter-Process Communication - SICS

22

Tehran

Polytechnic

University

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

doesn’t have any child process)

Page 23: Process and Inter-Process Communication - SICS

23

Tehran

Polytechnic

University

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: wait can block, while waitpid has an option that

prevents it from blocking. waitpid doesn’t wait for the first child to terminate (it

can control which process it waits for)

Page 24: Process and Inter-Process Communication - SICS

24

Tehran

Polytechnic

University

exec 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 doesn’t change across an exec.

Page 25: Process and Inter-Process Communication - SICS

25

Tehran

Polytechnic

University

exec functions 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[]);

Page 26: Process and Inter-Process Communication - SICS

26

Tehran

Polytechnic

University

signal Signals are software interrupts. The name of signals all begin with the

three character SIG : SIGABRT

Page 27: Process and Inter-Process Communication - SICS

27

Tehran

Polytechnic

University

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

Page 28: Process and Inter-Process Communication - SICS

28

Tehran

Polytechnic

University

signal Samplemain(){

signal (SIGUSER, sig_user);while (1);

}//---------------------------------void sig_user (int signo){

if (signo == SIGUSER)printf (“receive signal\n”);

return;}

Page 29: Process and Inter-Process Communication - SICS

29

Tehran

Polytechnic

University

kill int kill (int pid, int signo); Send a signal to a process or group of

the processes.

Page 30: Process and Inter-Process Communication - SICS

30

Tehran

Polytechnic

University

Contents

What is a Process? Process Control Process Relationship Inter- Process Communication

Page 31: Process and Inter-Process Communication - SICS

31

Tehran

Polytechnic

University

Process Relationship

forkfork

fork

exec

exec

fork & exec

exec

exec

fork

Page 32: Process and Inter-Process Communication - SICS

32

Tehran

Polytechnic

University

Contents

What is a Process? Process Control Process Relationship Inter- Process Communication

Page 33: Process and Inter-Process Communication - SICS

33

Tehran

Polytechnic

University

IPC Pipe FIFO Message queue Shared memory socket

Page 34: Process and Inter-Process Communication - SICS

34

Tehran

Polytechnic

University

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

pipe

Page 35: Process and Inter-Process Communication - SICS

35

Tehran

Polytechnic

University

pipe (cont.)

int pipe (int *filedes); filedes[0] : open for reading filedes[1] : open for writing pipe command :

who | sort | lpr

Page 36: Process and Inter-Process Communication - SICS

36

Tehran

Polytechnic

University

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) mode is or’ed with S_IFIFO dev is equal 0 for FIFO.

Page 37: Process and Inter-Process Communication - SICS

37

Tehran

Polytechnic

University

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);

Page 38: Process and Inter-Process Communication - SICS

38

Tehran

Polytechnic

University

Message Queue Message queues are a linked list of

messages stored within the kernel. We don’t have to fetch messages in a

first-int, first-out order. We can fetch messages based on their type

field. A process wants to impose some

structure on the data being transferred.

Page 39: Process and Inter-Process Communication - SICS

39

Tehran

Polytechnic

University

Message Queue (cont.)

int msgget (key_t key, int msgflag); A new queue is created, or an existing

queue is open by msgget.

Page 40: Process and Inter-Process Communication - SICS

40

Tehran

Polytechnic

University

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. Struct my_msg {

long type;char data[SIZE];

}

Page 41: Process and Inter-Process Communication - SICS

41

Tehran

Polytechnic

University

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: 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.

Page 42: Process and Inter-Process Communication - SICS

42

Tehran

Polytechnic

University

Message Queue (cont.)

int msgctl (int msgid, int cmd, struct msgid_ds *buf);

The msgctl function performs various operations in a queue.

Page 43: Process and Inter-Process Communication - SICS

43

Tehran

Polytechnic

University

Shared memory

Server Client

Pipe, FIFO,message

Inputfile

Outputfile

kernel

Page 44: Process and Inter-Process Communication - SICS

44

Tehran

Polytechnic

University

Shared memory (cont.)

Server Client

Kernel

Shared memory

Inputfile

Outputfile

Page 45: Process and Inter-Process Communication - SICS

45

Tehran

Polytechnic

University

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.

Page 46: Process and Inter-Process Communication - SICS

46

Tehran

Polytechnic

University

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.

Page 47: Process and Inter-Process Communication - SICS

47

Tehran

Polytechnic

University

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.

Page 48: Process and Inter-Process Communication - SICS

48

Tehran

Polytechnic

University

Shared memory (cont.)

int shmctl (int shmid, int cmd, struct shmid_ds *buf);

The msgctl function performs various operations in a shared memory segment.

Page 49: Process and Inter-Process Communication - SICS

49

Tehran

Polytechnic

University

Semaphore Semaphores are a synchronization

primitive. To obtain a shared resource:

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.

Page 50: Process and Inter-Process Communication - SICS

50

Tehran

Polytechnic

University

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.

Page 51: Process and Inter-Process Communication - SICS

51

Tehran

Polytechnic

University

Semaphore (cont.)

int semop (int semid, struct sembuf *semop, size_t nops);

Struct sembuf {

ushort sem_num;short sem_op;shoet sem_flag;

}

Page 52: Process and Inter-Process Communication - SICS

52

Tehran

Polytechnic

University

Semaphore (cont.)

Each particular operation is specified by a sem_op value: 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 semaphore’s 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.

Page 53: Process and Inter-Process Communication - SICS

53

Tehran

Polytechnic

University

Question?