IPC Programming (Cont) : Faculty of Computer Science and Engineering Ho Chi Minh City University of Technology
IPC Programming (Cont) : Faculty of Computer Science and Engineering Ho Chi Minh City University of Technology
IPC
Communication
Transferring message
Sharing information
Mechanisms:
Pipe
Signal
Message queue
Shared memory
Socket
RPC/RMI
Synchronization
Solving confliction
Processing order
Mechanisms:
Lock file
Semaphore
Mutex (pthread)
IPC Programming
Shared memory
Semaphore
SystemV IPC
$ipcs
------Shared Memory Segments -------key
shmid owner perms bytes
0x00000000 65536 root
644
110592
nattch
11
nsems
used-bytes
status
dest
messages
Shared memory
xy
Process 1
Process 1
Process 2
Process 2
xy
xy
Faculty of Computer Science and Engineering - HCMUT
Shared memory
See all shared memory segments
ipcs
ipcs a
ipcs -m
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 processs address space
before using
Detach shared memory segment from processs address
space after finishing using it
Faculty of Computer Science and Engineering - HCMUT
Operations on shared
memory
shmget()
shmat()
shmdt()
shmctl()
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)
Example
key = ftok(/tmp/file_123, 321);
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
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)
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
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
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);
}
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);
}
IPC Programming
Shared memory
Semaphore
SystemV IPC
$ipcs
------Shared Memory Segments -------key
shmid owner perms bytes
0x00000000 65536 root
644
110592
nattch
11
nsems
used-bytes
status
dest
messages
Semaphore
See all shared semaphore
ipcs
ipcs a
ipcs -s
Remove a semaphore
ipcrm sem semid
ipcrm -s semid
Operations on semaphore
semget()
semop()
semctl()
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);
Faculty of Computer Science and Engineering - HCMUT
Hm 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
struct sembuf
struct sembuf {
ushort sem_num;
short sem_op;
short sem_flg;
}
/* semaphore number */
/* semaphore operation */
/* operation flags */
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;
};
Faculty of Computer Science and Engineering - HCMUT
Example
Implement 4 semaphore functions
seminit(): create a semaphore
p (): decrease value of semaphore by 1
v (): increase value of semaphore by 1
semrel(): remove semaphore
Write a program using semaphore to solve confliction on
critical sections
#include
#include
#include
#include
#include
<sys/types.h>
<sys/ipc.h>
<sys/sem.h>
<errno.h>
<unistd.h>
union {
int val;
struct semid_ds *buf;
ushort *array;
} carg;
int seminit() {
int i, semid;
if (semid=semget(IPC_PRIVATE,1,IPC_EXCL|0666)==-1)
return(-1);
carg.val=1;
if (semctl(semid,0,SETVAL,carg)==-1) return(-1);
return semid;
}
Faculty of Computer Science and Engineering - HCMUT
void main() {
int sem=seminit();;
if (fork()==0) func(sem);
else func(sem);
semrel(sem);
}
Questions???