0% found this document useful (0 votes)
69 views20 pages

Today's Topics: - System V Interprocess Communication (IPC) Mechanism

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 20

Today’s topics

•System V Interprocess communication


(IPC) mechanism
–Message Queues
–Semaphores
–Shared Memory
• Summary of processes related concepts

COP5570 – Advanced Unix Programming Florida State University


Why other IPC mechanisms

• Pipes/sockets
• FIFO semantics
• Signals: sending flags
• Sometimes, we want something beyond FIFO
• FIFO with tags (message queue)
• File semantics: the content is always there unless it
is modified explicitly. (shared memory)
•Once concurrency is allowed in shared data, we will
need a way to protect (lock) the data. (semaphore)

COP5570 – Advanced Unix Programming Florida State University


Message queues

• What are they?


– Similar to the FIFO pipes, except that
a tag (type) is matched when
reading/writing.
• Allowing cutting in line (I am only
interested in a particular type of message)
• Equivalent to merging of multiple FIFO
pipes in one.

COP5570 – Advanced Unix Programming Florida State University


Message queues

• Creating a message queue:


– int msgget(key_t key, int msgflag);

– Key can be any large number. But to


avoiding using conflicting keys in
different programs, use ftok() (the
key master).
• key_t ftok(const char *path, int id);
– Path point to a file that the process can stat
– Id: project ID, only the last 8 bits are used

COP5570 – Advanced Unix Programming Florida State University


• Message queue.
– A linked list of messages stored within the
kernel and identified by a message queue
identifier.
• Every message has a type field, and a nonnegative
length, and the actual data bytes.
• Msgsnd puts a message at the end of the queue
• Msgrcv gets a message, may not follow FIFO order
(can be based on type)
• Has resource limits: MSGMAX, MSGMNB,
MSGMNI, MSGTQL.

COP5570 – Advanced Unix Programming Florida State University


• Message queue operations
Int msgget(key_t, int flag)
Int msgctl(int msgid, int cmd, struct msgid_ds *buf)
Int msgsnd(int msgid, const void *ptr, size nbytes,
int flag);
Int msgrcv(int msgid, void *ptr, size_t nbytes, long
type, int flag);

• Performance advantage is no longer there


in newer systems (compared with pipe)

COP5570 – Advanced Unix Programming Florida State University


Shared Memory
Common chunk of read/write memory
among processes
MAX

Shared Memory
(unique key)
Create
ptr ptr
Attach 0 Attach
Proc. 1 Proc. 2

ptr ptr ptr

Proc. 3 Proc. 4 Proc. 5


COP5570 – Advanced Unix Programming Florida State University
Creating Shared Memory

int shmget(key_t key, size_t size, int shmflg);

Example:
key_t key;
int shmid;

key = ftok(“<somefile>", ‘A');

shmid = shmget(key, 1024, 0644 | IPC_CREAT);

Here’s an example: shm_create.c.

COP5570 – Advanced Unix Programming Florida State University


Attach and Detach
Shared Memory
void *shmat(int shmid, void *shmaddr, int shmflg);
int shmdt(void *shmaddr);

Example:
key_t key;
int shmid;
char *data;

key = ftok("<somefile>", ‘A');


shmid = shmget(key, 1024, 0644);
data = shmat(shmid, (void *)0, 0);

shmdt(data);
Here’s an shm_attach.c

COP5570 – Advanced Unix Programming Florida State University


Deleting Shared Memory

int shmctl(int shmid, int cmd, struct shmid_ds *buf);

shmctl(shmid, IPC_RMID, NULL);

Example: Shm_delete.c

COP5570 – Advanced Unix Programming Florida State University


Command-line IPC control

• ipcs
– Lists all IPC objects owned by the user

• ipcrm
– Removes specific IPC object

COP5570 – Advanced Unix Programming Florida State University


Semaphores

• Managing concurrent access to shared memory segment.

• Using Semaphores

– Creation: semget( … )

– Incr/Decr/Test-and-set : semop(…)

– Deletion: semctl(semid, 0, IPC_RMID, 0);

Online tutorial
http://www.ecst.csuchico.edu/~beej/guide/ipc/semaphores.html

COP5570 – Advanced Unix Programming Florida State University


• Process environment
– Command line arguments: argc, argv
– environ and getenv()
– getpid(), getuid(), getppid()
– How does a program access the second
(command line) argument?
– How does a process access the variable you
set in shell using commands such as “setenv
TERM vt100”?
– How does a process know its parent’s process
id? How does a parent know its children’s
process ids?

Florida State University


• Process management
– fork, exit, wait, waitpid, execv
– How can a parent process know
whether its child has executed
successfully?
– How to determine whether execv runs
a command successfully?

Florida State University


• File operations:
– What are the related data structures
for file operations?
– open, close, read write, unlink, dup.
– How to redirect the standard
input/output/error?

Florida State University


• Inter-process communication:
– Pipe
– What kind of processes can
communicate with pipes?
– How to implement “ps | grep xyuan |
more”?
– Message queue
– Shared memory
– semaphore

Florida State University


• Inter-process communication:
– Signal
– What is the typical default action for a
signal?
– Blocking/unblocking a signal
• sigset manipulation
– sigfillset, sigemptyset, sigaddset, sigdelset,
sigismember
• the sigprocmask system call
– Install signal handler (can ignore a signal
or use default handler)
• signal
• sigaction
– Sending signal: kill, alarm

Florida State University


• Terminal I/O
– canonical mode
– noncanonical mode
• tcgetattr, tcsetattr
• termios data structure

Florida State University


• Process group/session/control
terminal
– Related to job control
• Who gets to access the keyboard? Who to
send signal generated from the keyboard.
– Foreground and background processes
– Joining a group or creating a group:
setpgid
– Making a group foreground or
background
• tcgetpgrp/tcsetpgrp

Florida State University


Realizing shell/make commands

• IO redirection
• Pipes
• Background execution
• “arrow” functions
• ‘cd’ command
– Why is this command different from other
commands?
• PATH environment variable.

COP5570 – Advanced Unix Programming Florida State University

You might also like