#include <head.h>
int main(int argc, const char *argv[])
{
/*your code*/
key_t key = ftok("./", 'g');
int shmid = shmget(key, 32, IPC_CREAT|0777);
void* shmaddr = shmat(shmid, NULL, 0);
printf("%d\n",shmid);
int *flag = (int *)shmaddr;
*flag = 0;
char *str = (char *)shmaddr + 4;
strcat(str, "123456");
while (1)
{
if (*flag == 1)
{
printf("%s\n",str);
*flag = 0;
}
}
return 0;
}
#include <head.h>
int main(int argc, const char *argv[])
{
/*your code*/
key_t key = ftok("./", 'g');
int shmid = shmget(key, 32, IPC_CREAT|0777);
void *shmaddr = shmat(shmid, NULL, 0);
printf("%d\n",shmid);
int *flag = (int *)shmaddr;
char *head = (char *)shmaddr+4;
char *end=head+5;
int i=0;
char c;
while (1)
{
if(*flag==0){
char *p=head;
char *w=end;
while (w>p)
{
c=*p;
*p=*w;
*w=c;
w--;
p++;
}
*flag=1;
}
}
return 0;
}
#include <head.h>
struct msbuf
{
long mtype;
char mtext[128];
};
int main(int argc, const char *argv[])
{
key_t key = ftok("./", 251);
printf("key=%#x\n", key);
int msqid = msgget(key, IPC_CREAT|0777);
printf("msqid=%d\n", msqid);
struct msbuf a;
while (1)
{
memset(&a,0,sizeof(a));
a.mtype = 1;
printf("A进程请发送>>>>");
fgets(a.mtext,sizeof(a.mtext),stdin);
a.mtext[strlen(a.mtext)-1]='\0';
msgsnd(msqid,&a,sizeof(a.mtext),0);
if(strcmp(a.mtext,"quit")==0)
break;
memset(&a,0,sizeof(a));
msgrcv(msqid,&a,sizeof(a.mtext),2,0);
if(strcmp(a.mtext,"quit")==0)
break;
printf("A收到消息>>>%s\n",a.mtext);
}
return 0;
}
#include <head.h>
struct msbuf
{
long mtype;
char mtext[128];
};
int main(int argc, const char *argv[])
{
key_t key = ftok("./", 251);
printf("key=%#x\n", key);
int msqid = msgget(key, IPC_CREAT|0777);
printf("msqid=%d\n", msqid);
struct msbuf a;
while (1)
{
memset(&a,0,sizeof(a));
msgrcv(msqid,&a,sizeof(a.mtext),1,0);
if(strcmp(a.mtext,"quit")==0)
break;
printf("B收到消息>>>%s\n",a.mtext);
memset(&a,0,sizeof(a));
a.mtype = 2;
printf("B进程请发送>>>>");
fgets(a.mtext,sizeof(a.mtext),stdin);
a.mtext[strlen(a.mtext)-1]='\0';
msgsnd(msqid,&a,sizeof(a.mtext),0);
if(strcmp(a.mtext,"quit")==0)
break;
}
return 0;
}
#include <head.h>
struct msbuf
{
long mtype;
char mtext[128];
};
int msqid;
struct msbuf a;
void *callback(void *arg)
{
while (1)
{
memset(&a, 0, sizeof(a));
a.mtype = 1;
fgets(a.mtext, sizeof(a.mtext), stdin);
a.mtext[strlen(a.mtext) - 1] = '\0';
msgsnd(msqid, &a, sizeof(a.mtext), 0);
if (strcmp(a.mtext, "quit") == 0)
exit(0);
}
}
int main(int argc, const char *argv[])
{
key_t key = ftok("./", 251);
printf("key=%#x\n", key);
msqid = msgget(key, IPC_CREAT | 0777);
printf("msqid=%d\n", msqid);
pthread_t tid;
pthread_create(&tid, NULL, callback, NULL);
while (1)
{
memset(&a, 0, sizeof(a));
msgrcv(msqid, &a, sizeof(a.mtext), 2, 0);
if (strcmp(a.mtext, "quit") == 0)
exit(0);
printf("A收到消息>>>%s\n", a.mtext);
}
pthread_cancel(tid);
return 0;
}
#include <head.h>
struct msbuf
{
long mtype;
char mtext[128];
};
int msqid;
struct msbuf a;
void *callback(void *arg)
{
while (1)
{
memset(&a, 0, sizeof(a));
a.mtype = 2;
fgets(a.mtext, sizeof(a.mtext), stdin);
a.mtext[strlen(a.mtext) - 1] = '\0';
msgsnd(msqid, &a, sizeof(a.mtext), 0);
if (strcmp(a.mtext, "quit") == 0)
exit(0);
}
}
int main(int argc, const char *argv[])
{
key_t key = ftok("./", 251);
printf("key=%#x\n", key);
msqid = msgget(key, IPC_CREAT | 0777);
printf("msqid=%d\n", msqid);
pthread_t tid;
pthread_create(&tid, NULL, callback, NULL);
while (1)
{
memset(&a, 0, sizeof(a));
msgrcv(msqid, &a, sizeof(a.mtext), 1, 0);
if (strcmp(a.mtext, "quit") == 0)
exit(0);
printf("B收到消息>>>%s\n", a.mtext);
}
pthread_cancel(tid);
return 0;
}