0% found this document useful (0 votes)
38 views

IPC Programming (Cont) : Faculty of Computer Science and Engineering Ho Chi Minh City University of Technology

The document provides information about inter-process communication (IPC) mechanisms such as shared memory and semaphores. It discusses the key functions used for shared memory such as shmget(), shmat(), shmdt(), and shmctl(). It also covers the functions used for semaphores including semget(), semop(), and semctl(). Examples are given for creating and using shared memory and semaphores to synchronize access to critical sections between processes.

Uploaded by

Duong Thien
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

IPC Programming (Cont) : Faculty of Computer Science and Engineering Ho Chi Minh City University of Technology

The document provides information about inter-process communication (IPC) mechanisms such as shared memory and semaphores. It discusses the key functions used for shared memory such as shmget(), shmat(), shmdt(), and shmctl(). It also covers the functions used for semaphores including semget(), semop(), and semctl(). Examples are given for creating and using shared memory and semaphores to synchronize access to critical sections between processes.

Uploaded by

Duong Thien
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

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)

Faculty of Computer Science and Engineering - HCMUT

IPC Programming
Shared memory
Semaphore

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

Faculty of Computer Science and Engineering - HCMUT

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

Remove a shared memory segment


ipcrm shm shm_id
ipcrm -m shm_id

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 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()

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)

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);
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

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)

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

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
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);
}

Faculty of Computer Science and Engineering - HCMUT

IPC Programming
Shared memory
Semaphore

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

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

Faculty of Computer Science and Engineering - HCMUT

Operations on semaphore
semget()
semop()
semctl()

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);
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

sembuf specifies an operation on a specific semaphore


nsops: number of struct sembuf to be performed

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
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;
};
Faculty of Computer Science and Engineering - HCMUT

semctl() - cmd argument


Commands on semaphore set
IPC_STAT: get semaphore information
IPC_SET : set semaphores 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

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

Faculty of Computer Science and Engineering - HCMUT

#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 p(int sem){


struct sembuf pbuf;
pbuf.sem_num=0;
pbuf.sem_op=-1; /* decrease semaphores value */
pbuf.sem_flg=SEM_UNDO;
if (semop(sem,&pbuf,1)==-1) {
perror("semop"); exit(1);
}
}
void v(int sem){
struct sembuf vbuf;
vbuf.sem_num=0;
vbuf.sem_op=1;
vbuf.sem_flg=SEM_UNDO;
if (semop(sem,&vbuf,1)==-1) {
perror("semop"); exit(1);
}
}
Faculty of Computer Science and Engineering - HCMUT

int semrel(int semid){


return semctl(semid,0,IPC_RMID,0);
}

void func(int sem) {


while(1) {
p(sem);
/* enter critical section */
printf("%d Do something in CS\n",getpid());
sleep(5);
v(sem);
/* exit critical section */
printf("%d Out of CS\n",getpid());
sleep(1);
}
}

void main() {
int sem=seminit();;
if (fork()==0) func(sem);
else func(sem);
semrel(sem);
}

Faculty of Computer Science and Engineering - HCMUT

Questions???

You might also like