目录
1.要求用消息队列实现AB进程对话:1)A进程发送一句话,B进程接收后打印;
2) B进程接着再发送一句话,A进程接收打印;3)重复上述步骤,当A进程或者B进程接收到quit后退出AB进程。
1.要求用消息队列实现AB进程对话:
1)A进程发送一句话,B进程接收后打印;
2) B进程接着再发送一句话,A进程接收打印;
3)重复上述步骤,当A进程或者B进程接收到quit后退出AB进程。
p1.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#define MSG_EXCEPT 020000
struct msgbuf{
long mtype;
char mtext[64];
};
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("./",1);
if(key<0)
{
perror("ftok");
return -1;
}
//创建消息队列,并获取消息队列id号
int msgid = msgget(key,IPC_CREAT | 0666);
if(msgid<0)
{
perror("msgget");
return -1;
}
//往消息队列里发送信息包
struct msgbuf mqbuf;
ssize_t res = 0;
pid_t pid = fork();
if(pid>0){
while(1)
{
memset(&mqbuf,0,sizeof(mqbuf));
//printf("请输入消息类型:");
scanf(" %ld",&mqbuf.mtype);
while(getchar()!=10);
if(mqbuf.mtype == 0)
{
break;
}
printf("请输入消息内容:");
fgets(mqbuf.mtext,sizeof(mqbuf.mtext),stdin);
mqbuf.mtext[strlen(mqbuf.mtext)-1] = '\0';
if(msgsnd(msgid,&mqbuf,sizeof(mqbuf.mtext),0)<0)
{
perror("msgsend");
return -1;
}
if(strcmp(mqbuf.mtext,"quit")==0)
{
kill(pid,9);
exit(1);
}
printf("--------------------\n");
}
}else{
while(1){
//读取消息队列里的信息
memset(&mqbuf,0,sizeof(mqbuf));
msgrcv(msgid,&mqbuf,sizeof(mqbuf.mtext),2,0);
if(strcmp(mqbuf.mtext,"quit")==0)
{
kill(getppid(),9);
exit(1);
}
printf("mtype:%ld mtext:%s\n",mqbuf.mtype,mqbuf.mtext);
}
}
return 0;
}
1.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#define MSG_EXCEPT 020000
struct msgbuf{
long mtype;
char mtext[64];
};
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("./",1);
if(key<0)
{
perror("ftok");
return -1;
}
//创建消息队列,并获取消息队列id号
int msgid = msgget(key,IPC_CREAT | 0666);
if(msgid<0)
{
perror("msgget");
return -1;
}
//往消息队列里发送信息包
struct msgbuf mqbuf;
ssize_t res = 0;
pid_t pid = fork();
if(pid==0){
while(1)
{
memset(&mqbuf,0,sizeof(mqbuf));
//printf("请输入消息类型:");
scanf(" %ld",&mqbuf.mtype);
while(getchar()!=10);
if(mqbuf.mtype == 0)
{
break;
}
printf("请输入消息内容:");
fgets(mqbuf.mtext,sizeof(mqbuf.mtext),stdin);
mqbuf.mtext[strlen(mqbuf.mtext)-1] = '\0';
if(msgsnd(msgid,&mqbuf,sizeof(mqbuf.mtext),0)<0)
{
perror("msgsend");
return -1;
}
if(strcmp(mqbuf.mtext,"quit")==0)
{
kill(getppid(),9);
exit(1);
}
printf("--------------------\n");
}
}else{
while(1){
//读取消息队列的信息
memset(&mqbuf,0,sizeof(mqbuf));
msgrcv(msgid,&mqbuf,sizeof(mqbuf.mtext),1,0);
if(strcmp(mqbuf.mtext,"quit")==0)
{
kill(pid,9);
exit(1);
}
printf("mtype:%ld mtext:%s\n",mqbuf.mtype,mqbuf.mtext);
}
}
return 0;
}
2. 创建两个进程A、B,以及一个共享内存,共享内存中存储char str="123456",在不考虑进程退出的情况下,要求如下:
1.A进程循环打印str字符串。
2.B进程循环倒置str字符串,不使用辅助数组。注意是循环倒置,要把字符串倒过来,倒回去。
3.要求A进程打印出来的结果是有序的,例如: "123456"或者654321",不允许出现"623451".,等无序情况
提示:将flag + str一起写到共享内存中,当flag=0,打印当flag=1 ,倒置
p1.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("./",'a');
if(key<0)
{
perror("ftok");
return -1;
}
//创新共享内存,并获取id
int shmid = shmget(key,32,IPC_CREAT|0666);
if(shmid<0)
{
perror("shmget");
return -1;
}
//映射到当前进程上
void* shmaddr = shmat(shmid,NULL,0);
if(shmaddr==(void*)-1)
{
perror("shmat");
return -1;
}
while(1)
{
if(*(int*)shmaddr==1)
{
char* star = (char*)shmaddr+4;
char* end = (char*)shmaddr+9;
while(end>star)
{
*star ^= *end;
*end ^= *star;
*star ^= *end;
star++;
end--;
}
*(int*)shmaddr = 0;
}
}
return 0;
}
1.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("./",'a');
if(key<0)
{
perror("ftok");
return -1;
}
//创新共享内存,并获取id
int shmid = shmget(key,32,IPC_CREAT|0666);
if(shmid<0)
{
perror("shmget");
return -1;
}
//映射到当前进程上
void* shmaddr = shmat(shmid,NULL,0);
if(shmaddr==(void*)-1)
{
perror("shmat");
return -1;
}
while(1)
{
if(*(int*)shmaddr==0)
{
printf("%s\n",(char*)shmaddr+4);
*(int*)shmaddr = 1;
}
sleep(1);
}
system("ipcs -m");
return 0;
}