1:将昨天的作业(使用管道让2个终端之间互相聊天),使用消息队列去实现
2:将昨天的作业(使用管道让2个终端之间互相聊天) 使用共享内存 + 信号灯集去实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
typedef struct shmbuf{
char text[255];
}shm_t;
pthread_t pid;
void semwait(int sid,int index){
struct sembuf buf;
buf.sem_num=index;
buf.sem_op=-1;
buf.sem_flg=SEM_UNDO;
semop(sid,&buf,1);
}void sempost(int sid,int index){
struct sembuf buf;
buf.sem_num=index;
buf.sem_op=1;
buf.sem_flg=SEM_UNDO;
semop(sid,&buf,1);
}
void* func(void* arg){
int sid=*(int*)arg;
key_t key=ftok("./shm.txt",1);
if(key==-1){
perror("ftok");
return NULL;
}
int id=shmget(key,255,IPC_CREAT|0666);
if(id==-1){
perror("shmget");
return NULL;
}
shm_t* buf=shmat(id,NULL,0);
while(1){
semwait(sid,0);
printf("\033[11D#1say: %s\n",buf->text);
memset(buf,0,sizeof(buf));
sleep(1);
sempost(sid,0);
}
}
int main(int argc, const char *argv[])
{
key_t key3=ftok("./shm.txt",3);
if(key3==-1){
perror("ftok");
return -1;
}
int id=shmget(key3,255,IPC_CREAT|0666);
if(id==-1){
perror("shmget");
return -1;
}
//
key_t key2=ftok("./shm.txt",2);
if(key2==-1){
perror("ftok2");
return -1;
}
int sid=semget(key2,2,IPC_CREAT|0666);
if(sid==-1){
perror("semget");
return -1;
}
//semctl(sid,0,SETVAL,1);
//semctl(sid,1,SETVAL,1);
//
if(pthread_create(&pid,NULL,func,&sid)!=0){
perror("pthread_create");
return -1;
}
pthread_detach(pid);
shm_t* buf=shmat(id,NULL,0);
while(1){
semwait(sid,1);
printf("2#talk: ");
scanf("%s",buf->text);
sempost(sid,1);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
typedef struct shmbuf{
char text[255];
}shm_t;
pthread_t pid;
void semwait(int sid,int index){
struct sembuf buf;
buf.sem_num=index;
buf.sem_op=-1;
buf.sem_flg=SEM_UNDO;
semop(sid,&buf,1);
}void sempost(int sid,int index){
struct sembuf buf;
buf.sem_num=index;
buf.sem_op=1;
buf.sem_flg=SEM_UNDO;
semop(sid,&buf,1);
}
void* func(void* arg){
int sid=*(int*)arg;
key_t key3=ftok("./shm.txt",3);
if(key3==-1){
perror("ftok");
return NULL;
}
int id=shmget(key3,255,IPC_CREAT|0666);
if(id==-1){
perror("shmget");
return NULL;
}
shm_t* buf=shmat(id,NULL,0);
while(1){
semwait(sid,1);
printf("\033[11D#2say: %s\n",buf->text);
memset(buf,0,sizeof(buf));
sleep(1);
sempost(sid,1);
}
}
int main(int argc, const char *argv[])
{
key_t key=ftok("./shm.txt",1);
if(key==-1){
perror("ftok");
return -1;
}
int id=shmget(key,255,IPC_CREAT|0666);
if(id==-1){
perror("shmget");
return -1;
}
//
key_t key2=ftok("./shm.txt",2);
if(key2==-1){
perror("ftok2");
return -1;
}
int sid=semget(key2,2,IPC_CREAT|0666);
if(sid==-1){
perror("semget");
return -1;
}
semctl(sid,0,SETVAL,1);
semctl(sid,1,SETVAL,1);
//
if(pthread_create(&pid,NULL,func,&sid)!=0){
perror("pthread_create");
return -1;
}
pthread_detach(pid);
shm_t* buf=shmat(id,NULL,0);
while(1){
semwait(sid,0);
printf("1#talk: ");
scanf("%s",buf->text);
sempost(sid,0);
}
return 0;
}