作业1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <pthread.h>
#include <errno.h>
int msqid=0;
pthread_t tid1,tid2;
struct msgbuf
{
long mtype;
char mtext[128];
};
void* Fun_Msgsnd(void* arg)
{
struct msgbuf snd;
while(1)
{
//确认要发送到消息队列中的信息包的数据类型和内容
snd.mtype=22;
// getchar();
// printf("请输入消息内容:");
scanf("%s",snd.mtext);
getchar();
//将信息包发送到消息队列中
if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
{
perror("msgsnd");
return NULL;
}
// printf("发送成功\n");
if(0==strcmp(snd.mtext,"quit"))
{
break;
}
}
pthread_cancel(tid2);
pthread_exit(NULL);
}
void* Fun_Msgrcv(void* arg)
{
struct msgbuf rcv;
while(1)
{
//读取A类型的数据信息的内容,并打印,阻塞形式
if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),11,0)<0)
{
if(errno!=43)
{
// printf("errno=%d\n",errno);
perror("msgrcv");
return NULL;
}
}
printf("B说:%s\n",rcv.mtext);
if(0==strcmp(rcv.mtext,"quit"))
{
pthread_cancel(tid1);
if(-1==msgctl(msqid,IPC_RMID,NULL))
{
perror("msgctl");
return NULL;
}
break;
}
}
exit(0);
// pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//用ftok函数,计算key值
key_t key=ftok("./",1);
if(key<0)
{
perror("ftok");
return -1;
}
// printf("key=%#x\n",key);
//通过key值找到对应的消息队列
msqid=msgget(key,IPC_CREAT|0664);
if(msqid<0)
{
perror("msgget");
return -1;
}
// printf("msgget success, msgqid=%d\n",msqid);
if(pthread_create(&tid1,NULL,Fun_Msgsnd,NULL)!=0)
{
printf("line=%d\n",__LINE__);
printf("pthread_create failed!\n");
return -1;
}
if(pthread_create(&tid2,NULL,Fun_Msgrcv,NULL)!=0)
{
printf("line=%d\n",__LINE__);
printf("pthread_create failed!\n");
return -1;
}
pthread_join(tid2,NULL);
// pthread_join(tid1,NULL)
printf("对话结束!\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <pthread.h>
#include <errno.h>
int msqid=0;
pthread_t tid1,tid2;
struct msgbuf
{
long mtype;
char mtext[128];
};
void* Fun_Msgsnd(void* arg)
{
struct msgbuf snd;
while(1)
{
//确认要发送到消息队列中的信息包的数据类型和内容
snd.mtype=11;
// getchar();
// printf("请输入消息内容:");
scanf("%s",snd.mtext);
getchar();
//将信息包发送到消息队列中
if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
{
perror("msgsnd");
return NULL;
}
// printf("发送成功\n");
if(0==strcmp(snd.mtext,"quit"))
{
pthread_cancel(tid2);
break;
}
}
pthread_exit(NULL);
}
void* Fun_Msgrcv(void* arg)
{
struct msgbuf rcv;
while(1)
{
//读取A类型的数据信息的内容,并打印,阻塞形式
if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),22,0)<0)
{
perror("msgrcv");
return NULL;
}
printf("A说:%s\n",rcv.mtext);
if(0==strcmp(rcv.mtext,"quit"))
{
pthread_cancel(tid1);
if(-1==msgctl(msqid,IPC_RMID,NULL))
{
perror("msgctl");
return NULL;
}
break;
}
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//用ftok函数,计算key值
key_t key=ftok("./",1);
if(key<0)
{
perror("ftok");
return -1;
}
// printf("key=%#x\n",key);
//通过key值找到对应的消息队列
msqid=msgget(key,IPC_CREAT|0664);
if(msqid<0)
{
perror("msgget");
return -1;
}
// printf("msgget success, msgqid=%d\n",msqid);
if(pthread_create(&tid1,NULL,Fun_Msgsnd,NULL)!=0)
{
printf("line=%d\n",__LINE__);
printf("pthread_create failed!\n");
return -1;
}
if(pthread_create(&tid2,NULL,Fun_Msgrcv,NULL)!=0)
{
printf("line=%d\n",__LINE__);
printf("pthread_create failed!\n");
return -1;
}
pthread_join(tid1,NULL);
printf("对话结束!\n");
return 0;
}
作业2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int argc, const char *argv[])
{
key_t key=ftok("./",6);
if(key<0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n",key);
int shmid=shmget(key,64,IPC_CREAT|0664);
if(shmid<0)
{
perror("shmget");
return -1;
}
printf("shmid=%d\n",shmid);
void* addr=shmat(shmid,NULL,0);
if((void*)-1==addr)
{
perror("shmat");
return -1;
}
printf("addr=%p\n",addr);
char* buf=(char*)addr+4;
while(1)
{
if(1==*(int*)addr)
{
int i=0;
int j=strlen(buf)-1;
while(i<j)
{
char t=*(buf+i);
*(buf+i)=*(buf+j);
*(buf+j)=t;
i++;j--;
}
*(int*)addr=0;
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
key_t key=ftok("./",6);
if(key<0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n",key);
int shmid=shmget(key,64,IPC_CREAT|0664);
if(shmid<0)
{
perror("shmget");
return -1;
}
printf("shmid=%d\n",shmid);
void* addr=shmat(shmid,NULL,0);
if((void*)-1==addr)
{
perror("shmat");
return -1;
}
printf("addr=%p\n",addr);
*(int*)addr=0;
char*buf=(char*)addr+4;
strcpy(buf,"1234567");
while(1)
{
if(0==*(int*)addr)
{
printf("%s\n",(char*)addr+4);
*(int*)addr=1;
sleep(1);
}
}
return 0;
}