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 />

time_t msg_rtime; /* time of last msgrcv */<br />

time_t msg_ctime; /* time of last msgctl */<br />

/* that changed <strong>the</strong> above */<br />

}<br />

! Message queue<br />

structures in kernel<br />

msqid<br />

msg-perrn<br />

msg_first<br />

msg_last<br />

…<br />

…<br />

msg_ctime<br />

link<br />

type=100<br />

length=l<br />

data_<br />

link<br />

type=200<br />

length=2<br />

data_<br />

link<br />

type=300<br />

length=3<br />

data<br />

A new message is created, or an existing message queue is accessed <strong>with</strong> msgget system call.<br />

int msgget(key_t key, int msgflag);<br />

msgflag<br />

0400 MSG_R read by owner<br />

0200 MSG_W write by owner<br />

0040 MSG_R >> 3 read by group<br />

0020 MSG_W >> 3 write by group<br />

0004 MSG_R >> 6 read by world<br />

0002 MSG_W >> 6 write by world<br />

IPC_CREAT & IPC_EXCL<br />

#include <br />

#include <br />

#include <br />

int msgsnd(int msqid, struct msgbuf *ptr, int length, int flag);<br />

struct msgbuf {<br />

long mtype; /* message type, must be > 0 */<br />

char mtext[l]; /* message data */ ;<br />

}<br />

- The data mtext can be binary data or text.<br />

- The kernel does not interpret <strong>the</strong> contents of <strong>the</strong> message at all, so cooperating processes could define <strong>the</strong>ir own<br />

structure.<br />

- The length is in bytes.<br />

- The flag can be set to IPC_NOWAIT or zero.<br />

int msgrcv(int msqid, struct msgbuf *ptr, int length, long msgtype, int flag);<br />

If MSG_NOERROR bit in flag is set, than data of received message is greater than length.<br />

specify which message on queue is returned:<br />

- if msgtype = 0 <strong>the</strong>n first message on queue<br />

- if msgtype > 0 <strong>the</strong>n first <strong>with</strong> a type = msgtype<br />

- if msgtype < 0 <strong>the</strong>n first message <strong>with</strong> lowest type

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

Saved successfully!

Ooh no, something went wrong!