Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inter-process Communication:

Similar presentations


Presentation on theme: "Inter-process Communication:"— Presentation transcript:

1 Inter-process Communication:
Unit- IV Inter-process Communication:

2 Contents: Inter-Process Communication Process Tracing Pipes Sockets
System V IPC Multiprocessor systems

3 IPC: Independent processes & co-operating processes
Communicating two processes related or unrelated. Purposes for IPC Information Sharing Data Transfer Synchronization Event Notification IPC mechanism: Signal Message Passing Pipes Sockets Shared memory

4 Process Tracing ptrace(cmd, pid, addr, data)
pid is the process ID addr refers to a location cmd: r/w, intercept, set or delete watchpoints, resume the execution Data: interpreted by cmd Used by debuggers sdb or dbx Global trace data structure

5 Debugging Process : if ((pid = fork() ) == 0) { //child traced process
ptrace(0, 0, 0, 0) ; exec("name of traced process here") ; // debugger process continues here for (;;) wait( (int *) 0) ; read (input for tracing instructions) ptrace(cmd,pid,addr,data) ; if (quitting trace) break; }

6 Process Tracing drawbacks and limitations
- child must allow tracing explicitly by invoking ptrace; parent can control the execution of its direct children only; extremely inefficient (requires several context switches); cannot control a process already running; - security problems when tracing a setuid programs (user can obtain super user privileges) – to avoid this UNIX disables tracing such programs;

7 Pipes :

8 Pipes A unidirectional, FIFO, unstructured data stream of fixed maximum size. int pipe (int * filedes) filedes[0] for read, filedes[1] for write Data P Fig 6-1 Data flow through a pipe.

9 Pipes Applications: in shell – passing output of one program to another program – e.g. cat fil1 file2 | sort Limitations: cannot be used for broadcasting; Data in pipe is a byte stream No way to distinguish between several readers or writers Named Pipes: Use mknod(),mkfifo()

10 Sockets :

11 Sockets : A socket is defined as an endpoint for communication
Concatenation of IP address and port Communication needs a pair of sockets

12 Socket Functions : sd = socket (format, type, protocol) ;
bind (sd, address, length) ; connect (sd, address, length) ; listen (sd, qlength) nsd = accept (sd, address, addrlen) ; count = send (sd, msg, iength, flags) ;

13 Socket Functions : count = recv (sd, buf, length, flags) ;
shutdown (sd, mode) getsockname(sd, name, length) ;

14 System V IPC Messages: send formatted data streams to arbitrary process Shared memory: share parts of virtual address space Semaphores: synchronize execution

15 Common properties : Table whose entries describes all instances of mechanism Each entry contains user chosen numeric key Each mech. contains get system call IPC_CREAT bit in flag: new entry with given key IPC_CREAT and IPC_EXCL bit in flag: error if entry for given key is present To find index into table index=descriptor modulo (no of entries) Permission structure: UID, GID Status information: PID and time of last access/modification Control system call: set/return status info, remove entry

16 Messages : Msgqid=msgget(key,flag) Fields of queue structure
- ptr to 1st and last msg on linked list - no of msg & total no of data byte on linked list - max no of bytes of data byte on linked list - process id of last process - timestamp of last system call

17

18 msgqid=msgget(key,flag)
When msgget called search msg queue to find id with given key if no entry allocate new queue struct initialize it return identifier else check permission & return

19 Msgsnd(msgqid,msg,count,flag)
Msgqid-descriptor of msg queue returned by msgget Msg- ptr to structure int type and char array Count- size of data array Flag-action taken if it runs of internal buffer space

20

21 Count=msgrcv(id,msg,maxcount,type,flag)
id- msg descriptor Msg-address of user structure to contain rcv msg Maxcount-size of data array in msg Type-msg type user wants to read Flag- what kernel should do if no msg are on the queue Count- no of bytes returned to user

22

23 Msgctl(id,cmd,mstatbuf)
Id-msg descriptor Cmd- type of command Mstatbuf- address of user data structure that will contain control parameters or result of query

24 Shared Memory : Sharing part of virtual address space for reading and writing. Shmget() Shmat() Shmdt() Shmctl()

25

26 Shmid=shmget(key,size,flag)
Size-no of bytes in region Search for given key if entry present & permission return descriptor for entry if no entry & IPC_CREAT flag verify size & allocate region using allocreg saves permission modes, size, ptr to region table allocate mem when attach region

27 virtaddr=shmat(id,addr,flags)
Id-returned by shmget Addr-virtual address where user wants to attach Flags-whether region read only, etc. Virtaddr- Virtual address where kernel attached region

28

29 Shared Memory detach and control :
shmdt(addr) addr- address returned by shmat shmctl(id,cmd,shmstatbuf) id-shared mem table entry cmd- type of operation shmstatbuf-address of user level structure that contain status information

30 Semaphores :

31 Semaphore Functions: id=semget(key,count,flag)
No of semaphore entries Time of last semop and semctl Oldval=semop(id,oplist,count); Oplist format:- Sem_op Sem_no Flags Semctl(id,number,cmd,arg) Args:-union Sem_val Sem_ds Array ptr

32 Change in Semaphore value a/c to operation value:
If Positive: - Increment semaphore Wake up processes waiting for sem value to increase If 0: - If sem value=0 :- continue with other operation with array Else put process to sleep (process waiting for sem value to be 0) If Negative: - Decrements semaphore by op value If semaphore = 0 Wakeup all processes waiting for sem to be 0 Else process goes to sleep (process waiting for sem value to increase)

33

34 Flag bits : IPC_CREAT IPC_EXCL IPC_NOWAIT IPC_RMID IPC_PRIVATE
MSG_NOERROR SEM_UNDO

35 Multiprocessor configuration

36 Multiprocessor configuration
Greater throughput as processes run concurrently. Each CPU executes independently but all of them execute one copy of kernel. Semantics for system call are same. Drawback: Several process execute simultaneously in kernel mode causes integrity problems.

37

38 Problem of multiprocessor system
In uniprocessor Unix system integrity of kernel data structure is maintained by two policies. Kernel cannot preempt a process and switch context to another process while executing in kernel mode. Kernel masks out interrupts when executing a critical region of code .

39 There are 3 methods for preventing such corruption
Execute all critical activity on one processor Serialize access to critical regions of code with locking primitives. Redesign algorithms to avoid contention of data structure.

40 Master Slave Arch :

41 Solution with master and slave processors
One processor called master can execute in kernel mode and other called slave execute only in user mode. Master processor- handle all system calls and interrupt. Slave processor-execute process in user mode and inform master when makes system calls.

42

43

44 Drawback: Corruption of kernel data structure possible in scheduler algorithm because it does not protect against having a process selected for execution on two processor. Master can specify the slave processor on which the process should execute (more than one process can be assign –load balancing problem). Kernel can allow only one processor to execute the scheduling loop at a time.

45 Mechanism for setting lock :
Solutions with semaphore : Mechanism for setting lock : Mechanism for setting lock :

46

47 Solution with semaphore
Partition the kernel into critical region such that at most one processor can execute code in critical region at a time. Operations on semaphores: Initialization :- to a nonnegative value. P operation :- decrements the value of semaphore. If value of semaphore < 0 (process that did the p goes to sleep) V operation :- I increments the value of semaphore. If the value of semaphore >= 0 (one process that has been sleeping as a result of p operation wakes up.) Conditional P (CP) :- Decrements the value of semaphore True: if its value > 0 False: if value <= 0

48 Implementation of semaphore
Dijkstra- possible to implement semaphore without sp. m/c instruction. Pprim locks semaphore by checking value of array val. When lock sem. It checks to see if processor already locked(val=2) or if processor with lower ID trying to lock(val=1). If either cond true, resets entry =1 & tries again

49

50

51 Locking Mechanism: Following sequence protects a resource
Pprim(semaphore) Use resource here Vprim (semaphore) Disadvantage:- loops are slow so performance down IBM atomic compare & swap instructions. AT & T 3B20 –atomic read & clear instructions.

52

53

54

55 Algorithms implemented with semaphore:
Buffer allocation Wait Driver-locking scheme Dummy process

56 Buffer allocation Data structure for buffer allocation
Buffer Header HQ of buffer Free List Buffers In multiprocessor two process could manipulate the linked list of hash queue, the semaphore for the hash queue permits only one processor at a time to manipulate the linked list. Free list requires a semaphore as several process could corrupt it.

57 Wait Parent should miss a zombie child as it executes the wait algorithm. Parent must not sleep waiting forever. Multiprocessor algorithm wait { for(;;) search all child process; if (status of child is zombie) return; P(zombie_semaphore); /* initialized to 0*/ } Perform V while exiting.

58 Drivers : Avoided inserting semaphore into driver code.
P & V operations at the driver entry points. P(driver_semaphore); Open(driver); V(driver_semaphore): Same semaphore for all entry points. Different semaphore for each driver.

59 Dummy process : In uniprocessor, if no process are ready to run, kernel idles in the context of the process that last run. In multi processor, Kernel can not idle in context of the process executed most recently on the processor. Create a dummy process per processor. When processor has no work to do context switch is done to the dummy process & it idles the context of dummy process . It consists of kernel stack only and can not be scheduled.

60 The Tunis system User interface compatible to Unix.
Written in Concurrent Euclid. Solves the mutual exclusion problem. Kernel process are activated by queuing messages for input. Concurrent Euclid implements monitor to prevent corruption of queues.

61 Performance limitation:
Methods to implement multiprocessor configuration. 1: Master – slave 2: Semaphore The implementation of multiprocessor Unix system generalizes to any no of processor but throughput will not increase at linear rate with no of processors. 1: Increased memory contention in h/w . 2: Wait longer period of time to gain semaphore. 3: Master processor becomes bottleneck.


Download ppt "Inter-process Communication:"

Similar presentations


Ads by Google