#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define SIZE (sizeof(struct msgbuf) - sizeof(long))
void *task(void *info)
{
int msgid = *(int *)info;
struct msgbuf buf;
while(1)
{
//固定读取类型5
msgrcv(msgid, &buf, SIZE, 5, 0);
printf("\033[;034m%s\033[0m\n", buf.mtext);
if(strcmp(buf.mtext, "quit") == 0)
{
puts("聊天结束");
msgctl(msgid, IPC_RMID, NULL);
raise(SIGINT);
}
}
}
int main(int argc, const char *argv[])
{
//获取key值
key_t key = ftok("./", 'k');
if(key == -1)
{
perror("create key error");
return -1;
}
//创建消息队列
int msgid = msgget(key, IPC_CREAT | 0664);
if(msgid == -1)
{
perror("create msgid error");
return -1;
}
//创建线程
pthread_t tid;
if(pthread_create(&tid, NULL, task, &msgid) != 0)
{
puts("create tid error");
return -1;
}
//固定类型10进行写入
struct msgbuf buf;
buf.mtype = 10;
while(1)
{
scanf(" %[^\n]", buf.mtext);
msgsnd(msgid, &buf, SIZE, 0);
if(strcmp(buf.mtext, "quit") == 0)
{
puts("聊天结束");
msgctl(msgid, IPC_RMID, NULL);
raise(SIGINT);
}
}
return 0;
}
#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define SIZE (sizeof(struct msgbuf) - sizeof(long))
void *task(void *info)
{
int msgid = *(int *)info;
struct msgbuf buf;
while(1)
{
msgrcv(msgid, &buf, SIZE, 10, 0);
printf("\033[;034m%s\033[0m\n", buf.mtext);
if(strcmp(buf.mtext, "quit") == 0)
{
puts("聊天结束");
msgctl(msgid, IPC_RMID, NULL);
raise(SIGINT);
}
}
}
int main(int argc, const char *argv[])
{
key_t key = ftok("./", 'k');
if(key == -1)
{
perror("create key error");
return -1;
}
int msgid = msgget(key, IPC_CREAT | 0664);
if(msgid == -1)
{
perror("create msgid error");
return -1;
}
pthread_t tid;
if(pthread_create(&tid, NULL, task, &msgid) != 0)
{
puts("create tid error");
return -1;
}
struct msgbuf buf;
buf.mtype = 5;
while(1)
{
scanf(" %[^\n]", buf.mtext);
msgsnd(msgid, &buf, SIZE, 0);
if(strcmp(buf.mtext, "quit") == 0)
{
puts("聊天结束");
msgctl(msgid, IPC_RMID, NULL);
raise(SIGINT);
}
}
return 0;
}
#include<myhead.h>
//定义系统出牌函数
void handler(int signo)
{
if(signo == SIGALRM)
{
printf("系统帮您出了一张牌\n");
//进入下一次定时器
alarm(8);
}
}
int main(int argc, const char *argv[])
{
//将SIGALRM函数进行绑定
if(signal(SIGALRM, handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
//启动一个定时器,时长为8秒数
alarm(8);
char ch = 0; //接收用户出的牌
while(1)
{
scanf("%c", &ch);
getchar();
printf("您出的牌为:%c\n", ch);
//重新启动定时器
alarm(8);
}
return 0;
}
#include<myhead.h>
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
//1、创建一个key值
key_t key = ftok("/", 't');
if(key == -1)
{
perror("ftok error");
return -1;
}
printf("key = %#x\n", key);
//2、通过key值创建共享内存段
int shmid = shmget(key, PAGE_SIZE, IPC_CREAT|0664);
if(shmid == -1)
{
perror("shmget error");
return -1;
}
printf("shmid = %d\n", shmid);
//3、将共享内存段映射到用户空间
char *addr = (char *)shmat(shmid, NULL, 0);
if(addr == (void*)-1 )
{
perror("shmat error");
return -1;
}
printf("addr = %p\n", addr); //输出映射的虚拟地址
//4、使用共享内存段
//char buf[128] = "";
while(1)
{
printf("请输入>>>>");
fgets(addr, PAGE_SIZE, stdin); //向共享内存中输入数据
addr[strlen(addr)-1] = 0; //将'\n'换成'\0'
//判断退出条件
if(strcmp(addr, "quit") == 0)
{
break;
}
}
//5、取消映射
//取消addr对共享内存的映射关系
if(shmdt(addr) == -1)
{
perror("shmdt error");
return -1;
}
//6、删除共享内存
return 0;
}
?