Discussion Week 13
¡ñ Message Queue
¡ñ Shared Memory
Copyright By PowCoder代写 加微信 powcoder
¡ñ Today¡¯s Tasks
Message Queue
¡ñ A component for IPC, providing an asynchronous communication protocol
¡ñ A linked list of messages stored within the kernel and identified by a
message queue identifier
¡ñ Each message is given an identification or type so that processes can select the appropriate message
¡ñ Process must share a common key in order to gain access to the queue in the first place
System calls for message queues
¡ñ ftok(): is used to generate a unique key, not required: You can also choose a key on your own
¡ñ msgget(): either returns the message queue identifier for a newly created message queue or returns the identifiers for a queue which exists with the same key value
¡ñ msgsnd(): place data onto a message queue
¡ñ msgrcv(): messages are retrieved from a queue
¡ñ msgctl(): It performs various operations on a queue, generally it is used to
destroy message queue
#include
#define MSGSZ 128
typedef struct msgbuf { long mtype;
char mtext[MSGSZ];
} mymsg_t;
Example: Sending
mymsg_t m1 = {15, ¡°hello¡±}; mymsg_t m2 = {20, ¡°goodbye¡±}; int mid;
key_t key = 100;
mid = msgget (key, 0666 | IPC_CREAT);/*u,g,o can read/write into queue*/ /* msgsnd will block if queue is full, otherwise:*/
msgsnd (mid, (void *)&m1, sizeof (m1), 0);
msgsnd (mid, (void *)&m2, sizeof (m2), 0);
/* msgsnd will not block if IPC_NOWAIT is specified*/
msgsnd (mid, (void *)&m1, sizeof (m1), IPC_NOWAIT); /*Returns -1 if cannot send (and errno = ENOMSG)*/
Example: Receiving
mymsg_t msg; int mid;
key_t key = 100;
mid = msgget (key, 0666 | IPC_CREAT);
/* read msgs with tag 15 and 20 */
/* will block if such messages are not there*/ msgrcv (mid, (void *)&msg, sizeof (msg), 20, 0); msgrcv (mid, (void *)&msg, sizeof (msg), 15, 0);
/*non-blocking:*/
res = msgrcv (mid, (void *)&msg, sizeof (msg), 30, IPC_NOWAIT); /*Returns -1 if not on queue (and errno = ENOMSG)*/
Exercise 1
¡ñ Repeatedly read user input and send it to message queue (use ctrl-D to stop).
¡ñ Repeatedly read from message queue and print the message.
Shared Memory
¡ñ Allows processes to read from and write to the same memory segment
¡ñ Changes made by one process can be viewed by anther process
¡ñ Used to provide communication among processes or avoid redundant copies
¡ñ Efficient means of passing data between programs
Shared-Memory Functions
¡ñ ftok(): is used to generate a unique key
¡ñ shmat(): attach shared memory segment to current process
¡ñ shmctl(): perform shared memory control operations, such as destroy
shared memory
¡ñ shmdt(): detach shared memory segment from calling process
¡ñ shmget(): get ID of shared memory segment with key
#include
int key = 1234;
/* access and attach to shared memory */
int shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666); void *buf = shmat(shmid, NULL, 0);
printf(“Segment ID %d\n”, shmid); printf(“Attached at %p\n”, buf);
shmdt(buf); /* detach shared memory */ shmctl(shmid, IPC_RMID, 0);
Exericise 2
¡ñ write.c
¡ñ Repeatedly read user input into shared memory.
¡ñ Read and print the content in shared memory every second until ¡°exit¡± is received.
¡ñ Quiz on Canvas: due Tuesday at 11:59 pm
¡ñ Exercises:
¡ð Submit a zip file that contains send.c recv.c write.c and read.c.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com