1 #include "head.h"
2 int main(int argc, const char *argv[])
3 {
4 key_t key = ftok("/home/ubuntu/",'a');
5 if(key < 0)
6 {
7 perror("ftok");
8 return -1;
9 }
10 printf("key = %#x\n",key);
11 int shmid = shmget(key,128,IPC_CREAT|0664);
12 if(shmid < 0)
13 {
14 perror("shmget");
15 return -1;
16 }
17 printf("shmid = %d\n",shmid);
18 int semid = semget(key,2,IPC_CREAT|0664);
19 if(semid < 0)
20 {
21 perror("semget");
22 return -1;
23 }
24 void *shmaddr = shmat(shmid,NULL,0);
25 if(shmaddr == (void*)-1)
26 {
27 perror("shmat");
28 return -1;
29 }
30 printf("shmaddr = %p\n",shmaddr);
31 char *ptr = (char*)shmaddr;
32 strcpy(ptr,"1234567");
33 char *p1 = NULL;
34 char *p2 = NULL;
35 char c = 0;
36 struct sembuf sops;
37 while(1)
38 {
39 sops.sem_num = 0;
40 sops.sem_op = -1;
41 sops.sem_flg = 0;
42 if(semop(semid,&sops,1) < 0)
43 {
44 perror("semop");
45 return -1;
46 }
47
48 p1 = ptr;
49 p2 = ptr+strlen(ptr)-1;
50 while(p1 < p2)
51 {
52 c = *p1;
53 *p1 = *p2;
54 *p2 = c;
55 p1++;
56 p2--;
57 }
58 sops.sem_num = 0;
59 sops.sem_op = 1;
60 sops.sem_flg = 0;
61 if(semop(semid,&sops,1) < 0)
62 {
63 perror("semop");
64 return -1;
65 }
66 }
67 system("ipcs -m");
68
69 return 0;
70 }
71 #include "head.h"
72 int main(int argc, const char *argv[])
73 {
74 key_t key = ftok("/home/ubuntu/",'a');
75 if(key < 0)
76 {
77 perror("ftok");
78 return -1;
79 }
80 printf("key = %#x\n",key);
81 int shmid = shmget(key,128,IPC_CREAT|0664);
82 if(shmid < 0)
83 {
84 perror("shmget");
85 return -1;
86 }
87 printf("shmid = %d\n",shmid);
88 int semid = semget(key,2,IPC_CREAT|0664);
89 if(semid < 0)
90 {
91 perror("semget");
92 return -1;
93 }
94 struct sembuf sops;
95 sops.sem_num = 0;
96 sops.sem_op = 1;
97 sops.sem_flg = 0;
98 if(semop(semid,&sops,1) < 0)
99 {
100 perror("semop");
101 return -1;
102 }
103
104 void *shmaddr = shmat(shmid,NULL,0);
105 if(shmaddr == (void*)-1)
106 {
107 perror("shmat");
108 return -1;
109 }
110 printf("shmaddr = %p\n",shmaddr);
111 char *ptr = (char*)shmaddr;
112 while(1)
113 {
114
115 sops.sem_num = 0;
116 sops.sem_op = -1;
117 sops.sem_flg = 0;
118 if(semop(semid,&sops,1) < 0)
119 {
120 perror("semop");
121 return -1;
122 }
123 printf("%s\n",ptr);
124
125 sops.sem_num = 0;
126 sops.sem_op = 1;
127 sops.sem_flg = 0;
128 if(semop(semid,&sops,1) < 0)
129 {
130 perror("semop");
131 return -1;
132
133 }
134 }
135 system("ipcs -m");
136
137 return 0;
#include "head.h"
typedef struct msgque
{
long mtype;
char mtext[128];
} Seq;
int main(int argc, const char *argv[])
{
key_t key = ftok("/home/ubuntu/",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid);
Seq snd;
while(1)
{
snd.mtype = 100;
printf("请输入消息内容>>>>>");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1] = '\0';
if(strcmp(snd.mtext,"quit") == 0)
break;
if(msgsnd(msqid,&snd,sizeof(snd.mtext),0) < 0)
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
}
Seq rcv;
ssize_t res = 0;
while(1)
{
res = msgrcv(msqid,&rcv,sizeof(rcv.mtext),101,0);
if(res < 0)
{
perror("msgrcv");
return -1;
}
printf("res=%ld mtype=%ld:mtext=%s\n",res,rcv.mtype,rcv.mtext);
}
system("ipcs -q");
return 0;
}
#include "head.h"
typedef struct msgque
{
long mtype;
char mtext[128];
} Seq;
int main(int argc, const char *argv[])
{
key_t key = ftok("/home/ubuntu/",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid);
Seq snd;
while(1)
{
snd.mtype = 101;
printf("请输入消息内容>>>>>");
fgets(snd.mtext,sizeof(snd.mtext),stdin);
snd.mtext[strlen(snd.mtext)-1] = '\0';
if(strcmp(snd.mtext,"quit") == 0)
break;
if(msgsnd(msqid,&snd,sizeof(snd.mtext),0) < 0)
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
}
Seq rcv;
ssize_t res = 0;
while(1)
{
res = msgrcv(msqid,&rcv,sizeof(rcv.mtext),100,0);
if(res < 0)
{
perror("msgrcv");
return -1;
}
printf("res=%ld mtype=%ld:mtext=%s\n",res,rcv.mtype,rcv.mtext);
}
system("ipcs -q");
return 0;
}