UNIX IPC Unix Semaphore Unix Message queue 1
UNIX SEMAPHORE: Unix semaphore is not a single variable but an array of non-negative integer variables. Number of non-negative values: 1 to some system defined max. Data Structures maintained by Kernel: Struct semid_ds Struct ipc_perm sem_perm; Struct sem * sem_base; Ushort sem_nsems; Ushort sem_otime; //time of last sem op. Ushort sem_ctime; //time of last changed. 2
Struct ipc_perm ushort uid; //owner s userid ushort gid; //owner s groupid ushort cuid; //creator s userid ushort cgid; ushort mode: ushort seq; //slot usage sequence no. key_t key; 3
Struct sem Ushort semval; /* semaphore value >= 0 */ Ushort sempid; /*pid of last process performing operation on semaphore */ Ushort semncnt; /* ##of process waiting for semaphore value to become > cval */ Ushort semzent; /* # of process waiting for semaphore value to become 0 */ 4
A semaphore in System V consist of The value of the semaphore The pid of the last process to manipulate the semaphore The number of processes waiting for the semaphore value to increase The number of processes waiting for the semaphore value to equal to 0 5
Semaphore Calls System Calls: #include <sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> int semget(key_t key, int nsems, int semflag) 6
Semflag value is a combination of the constant shown below: Numeric Symbolic Description O400 SEM_R Read by owner 0200 SEM_A Alter by owner 0040 SEM_R >> 3 Read by Group 0020 SEM_A >> 3 Alter by Group 0004 SEM_R >> 6 Read by World 0002 SEM_A >> 6 Read by World IPC_CREAT IPC_EXCL Flag argument key does not exist Key Exist No special flag Error OK IPC CREAT OK, crates new one OK IPC_CREAT IPC_EXCL OK, creates new one Error 7
int semop( int semid, struct sembuf **opsptr, unsigned int nops); Struct sembuf Ushort sem_num; //semaphore number. Short sem_op; //semaphore operation Short sem_flag; //operation flag nops: number of perations 8
sem_op: +ve : the value of Sem_val is added to the semaphore s current value. This corresponds to the release of resources that a semaphore controls. Semop: 0 : the caller wants to wait until the semaphore s value becomes zero. Sem_op: < 0 : the caller wants to wait until the semaphore s value becomes greater than or equal to the absolute value of sem_op. this corresponds to the allocation of resources. The return value from semop is zero if OK else -1. 9
Undo operation: Per process undo header Semaphore id Semaphore num Adjustment value 10
int semctl( int semid, int semnum, int cmd, union semun arg) union semun int val. //used for SETVAL only Struct semid_ds *buff; //used for IPC_STAT and IPC_SET Ushort *array; // used for IPC_GETALL and IPC_SETALL arg; cmd : IPC_RMID : to remove the semaphore GETVAL : SETVAL Semnum: a specific semaphore no. 11
File locking with semaphore #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #define SEMKEY 123456789L #define PERMS O666 static struct sembuf op_lock[2] = 0, 0, 0, 0, 1, SEM_UNDO; static struct sembuf op_unlock[1] = 0, -1, (IPC_NOEAIT SEM_UNOD); int simid = -1; my_lock(int fd) if( semod < 0) if( semid = semget(semkey,1,ipc_creat PERMS)) < 0) printf( sem get error ); exit(10; if( semop(semid,&0p_lock[0],2) < 0) printf( sem lock error ); 12
my_unlock(int fd) if( semop(semid, &op_unlock[0],1) < 0) printf( sem unlock error ); exit(1); 13
Unix Message Queue: Struct msg Struct msg *next; Long type; Int length; Char data[max]; So, a Unix message consists of Type Length Data if length is greater than zero Pointer to the next message 14
Struct msqid_ds struct ipc_perm msg_perm; struct msg *msg_first; struct msg *mag_last; ushort msg_cbytes; ushort msg_qnum; ushort msg_qbytes; ushort msg_lspid; ushort msg_lrpid; time_t msg_stime; time_t msg_rtime; time_t msg_ctime; 15
Message queue System call: #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> int msgget( key_t key, int msgflag); The return value is msqid or -1 an error occurs Once a message queqe is opened with msgget, we can put a message on the queue using msgsnd system call. 16
. int msgsnd( int msqid, struct msgbuf *ptr, int length, int flag); struct msgbuf long mtype; char mtext[512]; If some cooperating processes wanted to exchange messages consisting of a shor integer followed by an 8-byte character array, they can define their own structure as: Typedef struct My_msgbuf Long mtype; Short mshort; Char mchar[8]; Message; Length: length of the message in bytes. This is the length of the user-defined data that follows th long integer message type. The length can be zero. Flag: IPC_NOWAIT or 0. message is read form a message queue using the msgrcv system call. 17
3. int msgrcv( int msgid, struct msgbuf *ptr, int length, long mtype, int flag); length: specifies the size of the data portion of the structure pointed to by ptr. This is the maximum amount of data that is returned by the system call. Is the MSG_NONERROR bit in the flag argument is set, this specifies that if the actual data portion of the received message is greater than length, just truncate the data portion and return without error. Not specifying the IPC_NOERROR flag cause an error. The long msgtype argument specifies which message on the queue is desired. Msgtype = 0: The first message on the queue is returned. Msgtype > 0: The first message with a type equal to msgtype is returned. Msgtype < 0: The first message with the lowest type that is less than or equal to the absolute value to msgtype is returned. 18
int msgctl( int msgid, int cmd, struct msqid_ds *buff); cmd: IPC-RMID. 19