study psix

添加检测cttl+c的信号检测函数
检测到后删除信号量

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.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 <sys/stat.h>
#include <semaphore.h>
#include <signal.h>

sem_t *space,*data;

void func(int flag)
{
    sem_close(space);
    sem_close(data);
    sem_unlink("/my_space");
    sem_unlink("/my_data");
    exit(0);

}


int main(int argc,char const *argv[])
{
    signal(SIGINT,func);
    key_t key =ftok(".",1);
    //create shm
    int shmid=shmget(key,1024,IPC_CREAT|0666);
    if(-1==shmid)
    {
        perror("shmget erro");
        exit(1);
    }
    //create posix有名信号量
    sem_t *space = sem_open("/my_space",O_CREAT,0666,1);//创建一个名为my_space的有名信号量
    sem_t *data = sem_open("/my_data",O_CREAT,0666,0);//创建一个名为my_data的有名信号量
    
    char *p=shmat(shmid,NULL,0);//将shm映射到进程空间
    if(NULL==p)
    {
        perror("shmat erro");
        exit(1);
    }

    while (1)
    {
       
        sem_wait(space);
        //如果空间不为空,且数据为0,说明没有数据所以发送数据
        printf("please input:");
        fgets(p,1024,stdin);
        sem_post(data);
        if(!strncmp(p,"quit",4))
        {
            break;
        }
        
    }
    shmdt(p);
    return 0;
}


jack.c

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "stdlib.h"
#include "string.h"
#include "errno.h"
#include "fcntl.h"
#include "sys/stat.h"
#include <pthread.h>

#define FIFOPATH_W "/home/sumu/myfifo_w"
#define FIFOPATH_R "/home/sumu/myfifo_r"

void thread_read(void *flag)
{
    //检测文件是否存在
    if(access(FIFOPATH_R,F_OK)==-1)
    {
        //创建有名管道
        if(mkfifo(FIFOPATH_R,0666)<0)
        {
            perror("mkfifo");
            exit(1);
        }
    }
    int fd_r=open(FIFOPATH_R,O_RDWR);
    if (-1==fd_r)
    {
        perror("open");
        exit(1);
    }
    char *r_buf=malloc(1024);
    while(1)
    {
        
        memset(r_buf,0,1024);
        read(fd_r,r_buf,1024);
        printf("rose:");
        fputs(r_buf,stdout);
        if(strncmp(r_buf,"quit",4)==0)
        {
            break;
        }
    }
}

int main(int argc,char const *argv[])
{
    //检测文件是否存在
    if(access(FIFOPATH_W,F_OK)==-1)
    {
        //创建有名管道
        if(mkfifo(FIFOPATH_W,0666)<0)
        {
            perror("mkfifo");
            exit(1);
        }
    }
   
    int fd_w=open(FIFOPATH_W,O_RDWR);
    if (-1==fd_w)
    {
        perror("open");
        exit(1);
    }
    pthread_t tid;
    pthread_create(&tid,NULL,(void *)thread_read,NULL);
    char *w_buf=malloc(1024);
    while(1)
    {
        puts("please input:");
        memset(w_buf,0,1024);
        fgets(w_buf,1024,stdin);
        write(fd_w,w_buf,strlen(w_buf));
        if(strncmp(w_buf,"quit",4)==0)
        {
            break;
        }
    }
      
}

rose.c

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "stdlib.h"
#include "string.h"
#include "errno.h"
#include "fcntl.h"
#include "sys/stat.h"
#include <pthread.h>

#define FIFOPATH_R "/home/sumu/myfifo_w"
#define FIFOPATH_W "/home/sumu/myfifo_r"

void thread_read(void *flag)
{
    //检测文件是否存在
    if(access(FIFOPATH_W,F_OK)==-1)
    {
        //创建有名管道
        if(mkfifo(FIFOPATH_W,0666)<0)
        {
            perror("mkfifo");
            exit(1);
        }
    }
   
    int fd_w=open(FIFOPATH_W,O_RDWR);
    if (-1==fd_w)
    {
        perror("open");
        exit(1);
    }
    char *w_buf=malloc(1024);
    while(1)
    {
        puts("please input:");
        memset(w_buf,0,1024);
        fgets(w_buf,1024,stdin);
        write(fd_w,w_buf,strlen(w_buf));
        if(strncmp(w_buf,"quit",4)==0)
        {
            break;
        }
    }
}

int main(int argc,char const *argv[])
{
    //检测文件是否存在
    if(access(FIFOPATH_R,F_OK)==-1)
    {
        //创建有名管道
        if(mkfifo(FIFOPATH_R,0666)<0)
        {
            perror("mkfifo");
            exit(1);
        }
    }
    int fd=open(FIFOPATH_R,O_RDWR);
    if (-1==fd)
    {
        perror("open");
        exit(1);
    }
    pthread_t tid;
    pthread_create(&tid,NULL,(void *)thread_read,NULL);
    char *r_buf=malloc(1024);
    while(1)
    {
        
        memset(r_buf,0,1024);
        while(read(fd,r_buf,1024)==0);
        printf("jack:");
        fputs(r_buf,stdout);
        if(strncmp(r_buf,"quit",4)==0)
        {
            break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值