IPC Programming (cont)
Faculty of Computer Science and Engineering
Ho chi Minh city University of Technology
SinhVienZone.com
/>
IPC
Communication
Transferring message
Sharing information
Mechanisms:
Pipe
Signal
Message queue
Shared memory
Socket
RPC/RMI
SinhVienZone.com
Synchronization
Solving confliction
Processing order
Mechanisms:
Lock file
Semaphore
Mutex (pthread)
/>Faculty of Computer Science and Engineering - HCMUT
IPC Programming
Shared memory
Semaphore
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
SystemV IPC
$ipcs
------Shared Memory Segments -------key
shmid owner perms bytes
0x00000000 65536 root
644
110592
nattch
11
------Semaphore Arrays -------key
semid owner perms
nsems
------Message Queues -------key
msqid owner perms
used-bytes
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
status
dest
messages
Shared memory
xy
Process 1
Process 1
Process 2
Process 2
xy
xy
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Shared memory
See all shared memory segments
ipcs
ipcs –a
ipcs -m
Remove a shared memory segment
ipcrm shm shm_id
ipcrm -m shm_id
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Shared memory
Allow a lot of processes using the same memory segment
Minimum/Maximum shared memory segment size is
1byte/4MB
Maximum number of shared memory segments: 4096
Usage
Shared memory segment must be created first
Attach shared memory segment to process’s address space
before using
Detach shared memory segment from process’s address
space after finishing using it
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Operations on shared
memory
shmget()
shmat()
shmdt()
shmctl()
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
shmget()
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key,int size,int shmflg);
key: key of shared memory segment
size: size of shared memory segment (bytes)
shmflg: IPC_CREAT, IPC_EXCL or with its permission
Example
shm_id = shmget(123, 4096, IPC_CREAT | 0660)
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Create IPC object key
#include <sys/types.h>
#include <sys/ipc.h>
key_t ftok(const char *path, int id);
path: refer to an existing, accessible file
id: project identifier
Return value:
key_t value: if successful
-1: if fail
Example
key = ftok(“/tmp/file_123”, 321);
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
shmat()
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void *shmat(int shmid,void *shmaddr,int shmflg);
shmid: shared memory ID returned from shmget() function
shmaddr: attaching address
shmflg: SHM_RDONLY or 0
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
shmdt()
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int shmdt(void *shmaddr);
shmaddr: shared memory address (returned from shmat()
function)
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
shmctl()
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int shmctl(int shmid,int cmd,struct shmid_ds *buf);
shmid: shared memory ID returned from shmget() function
cmd: IPC_STAT, IPC_SET and IPC_RMID
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Example
Create a shared memory segment with 128 bytes size
Create two processes to use this shared memory segment
The first process writes 2 integer numbers into this shared
memory
The second process reads from this shared memory, adds
these two numbers and writes the sum back to shared
memory
The first process reads from shared memory to get the sum
and print out this sum to screen
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Example
int main() {
int *shm, shmid, k;
shmid = shmget(IPC_PRIVATE,128,IPC_CREAT|0666);
shm = (int*) shmat(shmid,0,0);
if(fork()==0) { /*child*/
shm[0]=111;
shm[1]=999;
sleep(3);
pintf("Process %d reads: Sum = %d“,getpid(),shm[2]);
shmdt((void *)shm);
shmctl(shmid, IPC_RMID, (struct shmid_ds *)0);
}
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Example
else {
/*parent*/
sleep(1);
printf("Process %d writes to shared memory ...\n",
getpid());
shm[2]=shm[0]+shm[1];
shmdt((void *)shm);
}
return(0);
}
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
IPC Programming
Shared memory
Semaphore
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
SystemV IPC
$ipcs
------Shared Memory Segments -------key
shmid owner perms bytes
0x00000000 65536 root
644
110592
nattch
11
------Semaphore Arrays -------key
semid owner perms
nsems
------Message Queues -------key
msqid owner perms
used-bytes
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
status
dest
messages
Semaphore
See all shared semaphore
ipcs
ipcs –a
ipcs -s
Remove a semaphore
ipcrm sem semid
ipcrm -s semid
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Operations on semaphore
semget()
semop()
semctl()
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
semget()
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int semget(key_t key, int nsems, int semflg);
key: key of semaphore
nsems: number of semaphore in the semaphore set
semflag: IPC_CREAT, IPC_EXCL or with its permission
Example
sem_id1=semget(IPC_PRIVATE,8,IPC_CREAT|0600);
sem_id2=semget(123,1,IPC_CREAT|IPC_EXCL|0660);
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
Hàm semop()
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int semop(int semid, struct sembuf *sops,
size_t nsops);
semid: semaphore set ID returned from semget() function
sops: a pointer to an array of nsops sembuf structures, each
sembuf specifies an operation on a specific semaphore
nsops: number of struct sembuf to be performed
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
struct sembuf
struct sembuf {
ushort sem_num;
short sem_op;
short sem_flg;
}
/* semaphore number */
/* semaphore operation */
/* operation flags */
sem_num: the order of this semaphore in the semaphore set
sem_op: change the semaphore value
sem_flg:
IPC_NOWAIT: non-blocking mode
SEM_UNDO: undo operation
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
semctl()
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int semctl(int semid, int semnum, int cmd);
int semctl(int semid, int semnum, int cmd,
union semun arg);
union semun{
int val;
struct semid_ds *buf;
ushort *array;
};
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT
semctl() - cmd argument
Commands on semaphore set
IPC_STAT: get semaphore information
IPC_SET : set semaphore’s ownership and permissions
IPC_RMID: remove the semaphore set immediately
Commands on individual semaphore
GETVAL: get the value of the semnum-th semaphore
SETVAL: set value of the semnum-th semaphore to arg.val
GETPID: get PID of process that executed the last semop call for the
semnum-th semaphore of the set
GETNCNT : get number of processes waiting for semval to increase
GETZCNT: get number of processes waiting for semval to become zero
Commands on all semaphores
SETALL: set values for all semaphores of the set
GETALL: get values of all semaphores of the set
SinhVienZone.com
/>Faculty of Computer Science and Engineering - HCMUT