16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Processes II<br />

System V IPC<br />

- message queues<br />

- semaphores<br />

- shared memory0<br />

Message queue Semaphore Shared memory<br />

include file sys/msg.h sys/sem.h sys/shm.h<br />

system calls<br />

to create or open msgget semget shmget<br />

for control operations msgct1 semct1 shmct1<br />

for IPC operations msgsnd semop shmat<br />

msgrcv<br />

shmdt<br />

/* */<br />

struct ipc_perm { /* */<br />

ushort uid; /* owner's user id */<br />

ushort gid; /* owner's group id */<br />

ushort cuid; /* creator's user id */<br />

ushort cgid; /* creator's group id */<br />

ushort mode; /* access modes */<br />

ushort seq; /* slot usage sequence number */<br />

ket_t key; /* key */<br />

};<br />

! Generating IPC ids<br />

char *path key_t key msgget() int id<br />

ftok()<br />

semget()<br />

char proj<br />

shrnget()<br />

! Logic flow for opening<br />

an IPC channel<br />

start here<br />

OK create new entry return ID<br />

| | no<br />

key==IPC PRIVATE? yes system table full?<br />

| no | yes<br />

Key already exists?<br />

no<br />

IPC_CREAT set?<br />

| yes<br />

yes error return<br />

CREAT & EXCL set?<br />

errno=EEXIST<br />

| no<br />

access permission? no error return<br />

| yes<br />

errno=EACCES<br />

OK return ID<br />

136<br />

yes<br />

errno= EN<strong>OS</strong>PC<br />

no<br />

errno= ENOENT<br />

Message queues<br />

There is no requirement that any process be waiting for a message to arrive on queue before some o<strong>the</strong>r process is<br />

allowed to write a message to that queue<br />

For every message queue in <strong>the</strong> system, <strong>the</strong> kernel maintains <strong>the</strong> following structure of information:<br />

#include <br />

#include /* defines ipc_perm structure */<br />

struct msqid_ds {<br />

struct ipc_perm msg_perm; /* operation perm struct */<br />

struct msg *msg_first; /* ptr to first msg on q */<br />

struct msg *msg_last; /* ptr to last msg on q */<br />

ushort msg_cbytes; /* current # of bytes on q */<br />

ushort msg_qnuro; /* current # of messages on q */<br />

ushort msg_qbytes; /* max # of bytes allowed on q */<br />

ushort msg_lspid; /* pid of last msgsnd */<br />

ushort msg_lrpid; /* pid of last msgrcv */<br />

time_t msg_stime; /* time of last msgsnd */

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!