2024年8月15日 通信

1.使用线程解决管道通信不能同时双端不能同时进行读写的问题

1)player1.c

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
int open_ret1;
int open_ret2;

void* rcv(void* arg)
{
    while (1)
    {
		char *r_buf = malloc(100);
		memset(r_buf,0,100);
        ssize_t r_count = read(open_ret2, r_buf, 100);
        if (r_count > 0)
		{
            printf("player2: %s\n", r_buf); 
        }
    }
    return NULL;
}

int main(int argc, const char *argv[])
{
    // 创建管道文件
    int fifo_ret1 = mkfifo("./fifo1", 0777);
    if (errno != EEXIST) 
    {
        perror("creat fifo failed");
    }

    // 创建管道文件
    int fifo_ret2 = mkfifo("./fifo2", 0777);
    if (fifo_ret2 == -1)
   
    
    if (errno != EEXIST)  
    {
        perror("creat fifo failed");
    }

    // 打开管道文件
    open_ret1 = open("./fifo1", O_RDWR);
    if (open_ret1 == -1)
    {
        perror("文件打开失败");
        return -1;
    }

    // 打开管道文件
    open_ret2 = open("./fifo2", O_RDWR);
    if (open_ret2 == -1)
    {
        perror("文件打开失败");
        return -1;
    }

    // 创建线程
    pthread_t pid;
    pthread_create(&pid, NULL, rcv, NULL);
   

    // 写入数据
    while (1)
    {
		char *w_buf = malloc(100);

        memset(w_buf, 0, 100);
        fgets(w_buf, 100, stdin); 
        write(open_ret1, w_buf, strlen(w_buf));
        if (strcmp(w_buf, "stop\n") == 0) 
        {
            break;
        }
    }

       return 0;
}

2)plaer2.c

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
int open_ret1;
int open_ret2;

void* rcv(void* arg)
{
    while (1)
    {
		char *r_buf = malloc(100);
		memset(r_buf,0,100);
        ssize_t r_count = read(open_ret1, r_buf, 100);
        if (r_count > 0)
		{
            printf("player1: %s\n", r_buf); 
        }
    }
    return NULL;
}

int main(int argc, const char *argv[])
{
    // 创建管道文件
    int fifo_ret1 = mkfifo("./fifo1", 0777);
    if (errno != EEXIST) 
    {
        perror("creat fifo failed");
    }

    // 创建管道文件
    int fifo_ret2 = mkfifo("./fifo2", 0777);
    if (fifo_ret2 == -1)
   
    
    if (errno != EEXIST)  
    {
        perror("creat fifo failed");
    }

    // 打开管道文件
    open_ret1 = open("./fifo1", O_RDWR);
    if (open_ret1 == -1)
    {
        perror("文件打开失败");
        return -1;
    }

    // 打开管道文件
    open_ret2 = open("./fifo2", O_RDWR);
    if (open_ret2 == -1)
    {
        perror("文件打开失败");
        return -1;
    }

    // 创建线程
    pthread_t pid;
    pthread_create(&pid, NULL, rcv, NULL);
   

    // 写入数据
    while (1)
    {
		char *w_buf = malloc(100);

        memset(w_buf, 0, 100);
        fgets(w_buf, 100, stdin); 
        write(open_ret2, w_buf, strlen(w_buf));
        if (strcmp(w_buf, "stop\n") == 0) 
        {
            break;
        }
    }

       return 0;
}

2.解析posix不能使用键盘ctrl+c退出循环输出的问题

1)rcvsig.c

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <signal.h>

int shm_id;
sem_t *space;
sem_t *data;
char *p;

void func(int a)
{
    // 断开共享内存
    shmdt(p);
    
    // 删除共享内存段
    shmctl(shm_id, IPC_RMID, NULL);
    
    // 关闭并删除信号量
    sem_close(space);
    sem_unlink("/my_spsce");
    sem_close(data);
    sem_unlink("/my_data");
    
    // 退出程序
    exit(0);
}

int main()
{   
    key_t key = ftok("./",2);
    
    shm_id = shmget(key,2, IPC_CREAT | 0666);
    p  = shmat(shm_id, NULL, 0);
    space = sem_open("/my_spsce", O_CREAT, 0666, 0);
    data = sem_open("/my_data", O_CREAT, 0666, 1);
    
    signal(SIGINT, func);
    
    while(1)
    {   
        sem_wait(data);
        fprintf(stderr, "%s", p);
        sem_post(space);
    }
    
    return 0;
}

2)sndsig.c

#include <stdio.h>
#include <fcntl.h>              
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>           
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <semaphore.h>

int main()
{
	
	key_t key = ftok("./",2);
	
	//创建共享内存的IPC对象
	int shmid = shmget(key, 2,IPC_CREAT | 0666);
	
	//创建有名信号量并初始化
	sem_t *space = sem_open("/my_spsce", O_CREAT,0666, 1);
	sem_t *data = sem_open("/my_data", O_CREAT,0666, 0);
	
	
	char *p = shmat(shmid, NULL, 0);
	
	char *msg = "0123456789";
	int i = 0;
	
	while(1)
	{
		sem_wait(space);
		
		memcpy(p, msg+i, 1);
		
	
		sem_post(data);
		
		i = (i+1)%10;//(0+1)%10
	}
	
	shmdt(p);
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值