#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdio.h>
#include<errno.h>
#define QKEY (key_t) 0111
#define QPERM 0660
#define MAXOBN 50
#define MAXPRIOR 10
struct q_entry{
long mtype;
char mtext[MAXOBN+1];
};
int init_queue(void) {
int queue_id;
if ((queue_id=msgget(QKEY, IPC_CREAT|QPERM))==-1)
printf("msgget failed\n");
return(queue_id);
}
int serve(void) {
int mlen, r_qid;
struct q_entry r_entry;
if ((r_qid=init_queue())==-1)
return(-1);
for (;;) {
if ((mlen=msgrcv(r_qid, &r_entry, MAXOBN, (-1*MAXPRIOR), MSG_NOERROR)) == -1) {
printf("msgrcv failed\n");
return(-1);
}
else {
r_entry.mtext[mlen]='\0';
printf("\n priority : %ld name: %s\n", r_entry.mtype, r_entry.mtext);
}
}
}
main(){
serve();
exit(0);
}
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdio.h>
#include<errno.h>
#define QKEY (key_t) 0111
#define QPERM 0660
#define MAXOBN 50
#define MAXPRIOR 10
struct q_entry{
long mtype;
char mtext[MAXOBN+1];
};
int init_queue(void) {
int queue_id;
if ((queue_id=msgget(QKEY, IPC_CREAT|QPERM))==-1)
printf("msgget failed\n");
return(queue_id);
}
int enter(char *objname, int priority) {
int len, s_qid;
struct q_entry s_entry;
if ((len=strlen(objname)) > MAXOBN) {
printf("name too long\n");
return(-1);
}
if (priority > MAXPRIOR || priority <= 0) {
printf("invalid priority lavel\n");
return(-1);
}
if ((s_qid=init_queue())==-1)
return(-1);
s_entry.mtype =(long)priority;
strncpy (s_entry.mtext, objname, MAXOBN);
if (msgsnd(s_qid, &s_entry, len, 0)==-1) {
printf("msgsnd faild\n");
return(-1);
}
else
return(0);
}
main(int argc, char **argv){
int i;
for (i=1; i<argc; i++)
enter(argv[i], argc-i);
exit(0);
}
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdio.h>
#include<errno.h>
#define QKEY (key_t) 0111
#define QPERM 0660
struct q_entry{
long mtype;
int data;
};
int init_queue(void) {
int queue_id;
queue_id=msgget(QKEY, IPC_CREAT|QPERM);
return(queue_id);
}
int serve(void) {
int r_qid;
struct q_entry r_entry;
r_qid=init_queue();
for (;;) {
msgrcv(r_qid, &r_entry, sizeof(int), 0, 0);
printf("\n priority : %ld name: %d\n", r_entry.mtype, r_entry.data);
}
}
main(){
serve();
exit(0);
}
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdio.h>
#include<errno.h>
#define QKEY (key_t) 0111
#define QPERM 0660
struct q_entry{
long mtype;
int data;
};
int init_queue(void) {
int queue_id;
queue_id=msgget(QKEY, IPC_CREAT|QPERM);
return(queue_id);
}
int enter(int data, int priority) {
int s_qid;
struct q_entry s_entry;
s_qid=init_queue();
s_entry.mtype =(long)priority;
s_entry.data=data;
msgsnd(s_qid, &s_entry, sizeof(int), 0);
return(0);
}
main(){
int i, data;
for (i=0; i<3; i++){
scanf("%d", &data);
enter(data, i+1);
}
exit(0);
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
#define SEMPERM 0600
union semun{
int val;
struct semid_ds *buf;
ushort *array;
};
main() {
int status=0, semid;
union semun arg;
if ((semid=semget((key_t) 0123, 1, SEMPERM|IPC_CREAT|IPC_EXCL))==-1) {
if (errno==EEXIST)
semid=semget((key_t) 0123, 1, 0);
}
else {
arg.val=1;
status=semctl(semid, 0, SETVAL, arg);
}
if (semid==-1 || status==-1) {
perror("initsem failed");
exit (1);
}
exit(0);
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
#include <stdio.h>
#define SEMPERM 0600
union semun{
int val;
struct semid_ds *buf;
ushort *array;
};
int initsem(key_t semkey) {
int status=0, semid;
if ((semid=semget(semkey, 1, SEMPERM|IPC_CREAT|IPC_EXCL))==-1) {
if (errno==EEXIST)
semid=semget(semkey, 1, 0);
}
else {
union semun arg;
arg.val=1;
status=semctl(semid, 0, SETVAL, arg);
}
if (semid==-1 || status==-1) {
perror("iniitsem failed");
return (-1);
}
return (semid);
}
int p(int semid) {
struct sembuf p_buf;
p_buf.sem_num=0;
p_buf.sem_op=-1;
p_buf.sem_flg=SEM_UNDO;
if (semop(semid, &p_buf, 1) ==-1) {
printf("p(semid) failed");
exit(1);
}
return(0);
}
int v(int semid) {
struct sembuf v_buf;
v_buf.sem_num=0;
v_buf.sem_op=1;
v_buf.sem_flg=SEM_UNDO;
if (semop(semid, &v_buf, 1) ==-1) {
printf("v(semid) failed");
exit(1);
}
return(0);
}
void handlesem(key_t skey);
main(){
int i;
key_t semkey=0x111;
for (i=0; i<3; i++) {
if (fork()==0)
handlesem(semkey);
}
for (i=0; i<3; i++)
wait(0);
exit(0);
}
void handlesem(key_t skey) {
int semid;
pid_t pid=getpid();
if ((semid=initsem(skey)) < 0)
exit(1);
printf("process %d before critical section\n", pid);
p(semid);
printf("process %d in critical section\n", pid);
sleep(10);
printf("process %d leaving critical section\n", pid);
v(semid);
printf("process %d exiting\n", pid);
exit(0);
}