+ All Categories
Home > Documents > System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

Date post: 15-Jan-2016
Category:
Upload: jaylan-edwards
View: 222 times
Download: 0 times
Share this document with a friend
25
System V IPC (InterProcess Communic ation) Messages Queue, Shared Memory, and Semaphores
Transcript
Page 1: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

System V IPC(InterProcess Communication)

Messages Queue, Shared Memory, and Semaphores

Page 2: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

Messages• Four system call

– msgget( ), msgsnd( ), msgrcv( ), and msgctl( )

• msgqid = msgget(key, flag)– Create a new message queue (entry) or to ret

rieve an existing message queue– key: a name chosen by user, represent a mes

sage queue.– Flag: such as create flag bit with different per

mission– Return a kernel –chosen descriptor which poi

nts to the message queue

Page 3: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 4: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

• Each message queue (or IPC entry) has a permissions structure– Pointers to the first and last messages on a lin

ked list– The number of messages and the total numbe

r of data bytes on the linked list– The process ID of the last processes to send

and receive messages– Time stamps of the last msgsnd, msgrc, and

msgctl operation

Page 5: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

• msgsnd (msgqid, msg, count, flag)

Page 6: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

• count = msgrcv(id, msg, maxcount, type, flag)– Id: message descriptor– Msg: the address of a user structure (message)– Maxcount: the size of the msg– Type: the message type that user wants to read– Flag: specifies what the kernel should do if no me

ssage are on the queue– Count: the number of bytes returned to the user

• Msgctl(id, cmd, mstatbuf)– Query the status of a message descriptor, set its

status, and remove a message queue

Page 7: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 8: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

Shared Memory• Shmget: create a new region of shared memory

or returns an existing one– Shmid = shmget (key, size, flag)

• Shmat: logically attacheds a shared memory to the virtual address space of a process– Virtaddr = shmat (id, addr, flags)

• Shmdt: detaches a shared memory from the virtual address– Shmdt(addr)

• Shmctl: manipulate various parameters associated with the shared memory– Shmctl9id, cmd, shmstatbuf)

Page 9: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 10: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 11: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 12: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

Semaphores• Id=semget (key, nsems, flag)

– nsems: represent the number of the semaphore element

Page 13: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

• Oldval = Semop(id, oplist, nsops)– Oplist: point to an array of semaphore operation

• include sem_num, operation (positive or negative), flag

– Nsops: indicate the size of operation array– Oldval: the value of the semaphore before operation

• The kernel change the value of a semaphore according to the value of the operation– Operation = 0, if element is 0, continue, else sleep– Operation is positive: increase the semaphore, awaken all pr

ocess that are waiting the semaphore– Operation is negative:

• if operation + element > 0, element – operation, and continue• If operation + element < 0, sleep• If operation + element = 0, wakeup a process which wait the element

to 0

Page 14: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

• Semctl(id, number, cmd, arg)– Number: the number of semaphore elements r

equired to do the cmd operation– Cmd: retrieve or set control parameters (permi

ssions and others)– Set one or all semaphore values in a set– Read the semaphore values– Arg: is interpreted based on the value of cmd.

Union semunion{int val;struct semid_ds *semstat;unsigned short *array;

}arg;

Page 15: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 16: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 17: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

The program is a.outUser executes it three timesIn the following sequence:a.out &a.out a &a.out b &

Page 18: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 19: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

Undo flag

• Dangerous situations could occur– If a process does a semaphore oper

ation, locking some resource, and then exit without resetting the semaphore value (ex: program error, receipt a signal, sudden termination, core down)

• To avoid such problems– Process can set the SEM_UNDO fla

g in the semop call; when it exits, the kernel reverses the effect of every semaphore operation the process has done.

Page 20: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

ptrace(cmd, pid, addr, data)• Cmd: read/write data, ..• Addr: a virtual address to be read/written in the tra

ced process• Data: an integer value to be written (write to addr)• P42: how do system calls actually work?

– Use interrupt 0x80 which provides the argument of sys_call_num and sys_call_args

– If the process which PF_TRACESYS is set (means traced process), the system call sends a SIGTRAP signal to the parent process and calls the scheduler. The traced process is interrupted until the parent process reactivates it (the ptrace() system call is called again)

Page 21: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

• pid = Fork() system call– Only way for a user to create a new process– The return of the fork system call

• In the parent process, pid is the child process ID

• In the child process, pid is 0

– Kernel does the following sequence of operations for fork• Allocate a slot in the process table for the new process

• Assign a unique ID number to the child process

• Make a logical copy of the context of the parent process (text region is shared between the parent and child process).

• Increment file and inode table counters for files associated with the process

• Return the ID number of the child to the parent process, and a 0 value to the child process

Page 22: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 23: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Page 24: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

Process tracing• Debugger forks a child process• Child process invokes the ptrace syst

em call thus kernel sets a trace bit in the child process table entry

• The child process execs the traced program

• The kernel executes the exec call – Due to the trace-bit is set, the kernel s

ends the “trap” signal to the parent which wake up the parent from the wait call.

• The parent call ptrace to do the read/write operation of the traced process

• The traced process wakeup and do the operation of ptrace call and continue execution until execute the system call

Page 25: System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.

Recommended