网络编程第六次作业(2024.8.15)

1.

#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>

sem_t *space = NULL;
sem_t *data  = NULL;

void func(){
	sem_close(space);
	sem_close(data);
	printf("%p  %p\n",space,data);
	space=NULL;
	data=NULL;
	sem_unlink("/my_spsce");
	sem_unlink("/my_data");
	//未解除映射,直接退出,也相当于断连了
	//不过申请的共享内存仍在
	exit(0);
}


int main(int argc, const char *argv[]){
	//连接信号
	signal(SIGINT,&func);
	//创建key值和获取共享内存的ID
	key_t key = ftok("./",1);
	//共享内存的大小,必须是偶数
	int shm_id = shmget(key,4, IPC_CREAT | 0666);
	//将这个共享内存映射至本进程的虚拟空间
	char *p  = shmat(shm_id, NULL,0);//p就是共享内存的首地址
//	bzero(p,4);
	//打开有名信号量
	space = sem_open("/my_spsce", O_CREAT,0777,1);
	data  = sem_open("/my_data", O_CREAT,0777,0);
	while(space!=NULL&&data!=NULL){
		//把数据-1
		sem_wait(space);
		fgets(p,2,stdin);
		//空间+1
		sem_post(data);
	}
	/*执行不了,crtl+c信号发出给信号量置空,除非能在v操作与循环之间按下*/
//	shmdt(p);
//	printf("end\n");
//	p=NULL;
//	return 0;
}

#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>

sem_t *space = NULL;
sem_t *data  = NULL;

void func(){
	sem_close(space);
	sem_close(data);
	printf("%p  %p\n",space,data);
	space=NULL;
	data=NULL;
	sem_unlink("/my_spsce");
	sem_unlink("/my_data");
	exit(0);
}


int main(int argc, const char *argv[]){
	//连接信号
	signal(SIGINT,&func);
	//创建key值和获取共享内存的ID
	key_t key = ftok("./",1);
	//共享内存的大小,必须是偶数
	int shm_id = shmget(key,4, IPC_CREAT | 0666);
	//将这个共享内存映射至本进程的虚拟空间
	char *p  = shmat(shm_id, NULL,0);//p就是共享内存的首地址
	//打开有名信号量
	space = sem_open("/my_spsce", O_CREAT,0777,1);
	data  = sem_open("/my_data", O_CREAT,0777,0);
	while(space!=NULL&&data!=NULL){
		//把数据-1
		sem_wait(data);
		fprintf(stderr,p);
		//空间+1
		sem_post(space);
	}	
//	shmdt(p);
//	printf("end\n");
//	p=NULL;
//	return 0;
}

2.

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

#define FIFO_PATH1 "./my_fifo1"
#define FIFO_PATH2 "./my_fifo2"

//创建有名管道
int fifo_creat(char* path){
	if(access(path,F_OK)){        //返回值0代表存在
		int fifo = mkfifo(path,0777);
		if(-1 == fifo){
			perror("创建失败:\n");
			return -1;
		}
	}
	return 0;
}

//打开管道文件
int fifo_open(char* path){
	int ofifo = open(path,O_RDONLY);
	if(-1 == ofifo){
		perror("打开管道文件失败:\n");
		return -1;
	}
	return ofifo;
}

int fifo_open2(char* path){
	int ofifo = open(path,O_WRONLY);
	if(-1 == ofifo){
		perror("打开管道文件失败:\n");
		return -1;
	}
	return ofifo;
}

//往管道写
void* fifo_write(void* fileid){
	int fd = *((int*)fileid);
	char data[300] = {0};
	while(strcmp(data,"end\n")){
		printf("请输入您想传输的数据!\n");
		memset(data,0,sizeof(data));
		fgets(data,sizeof(data),stdin);
		write(fd,data,strlen(data));
		printf("1thread:%s\n",data);	
	}
	return NULL;
}

int main(int argc, const char *argv[])
{
	//创建有名管道
	fifo_creat(FIFO_PATH2);
	//打开管道文件
	int ofifo1 = fifo_open(FIFO_PATH1);
	int ofifo2 = fifo_open2(FIFO_PATH2);
	if(ofifo1==-1 ||ofifo2==-1)
		return 0;
	//线程写入
	pthread_t tid;
	if(pthread_create(&tid,NULL,fifo_write,&ofifo2)){
		printf("线程创建失败!\n");
		return 0;
	}
	//读取管道数据并输出
	char data[300] = {0};
	while(strcmp(data,"end\n")){
		memset(data,0,sizeof(data));
		read(ofifo1,data,sizeof(data)-1);
		printf("progress1:%s\n",data);
	}

	return 0;
}
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>

#define FIFO_PATH1 "./my_fifo1"
#define FIFO_PATH2 "./my_fifo2"

//创建有名管道
int fifo_creat(char* path){
	if(access(path,F_OK)){        //返回值0代表存在
		int fifo = mkfifo(path,0777);
		if(-1 == fifo){
			perror("创建失败:\n");
			return -1;
		}
	}
	return 0;
}

//打开管道文件
int fifo_open(char* path){
	int ofifo = open(path,O_RDONLY);
	if(-1 == ofifo){
		perror("打开管道文件失败:\n");
		return -1;
	}
	return ofifo;
}

int fifo_open2(char* path){
	int ofifo = open(path,O_WRONLY);
	if(-1 == ofifo){
		perror("打开管道文件失败:\n");
		return -1;
	}
	return ofifo;
}

//从管道读取
void* fifo_write(void* fileid){
	int fd = *((int*)fileid);
	char data[300] = {0};
	while(strcmp(data,"end\n")){
		memset(data,0,sizeof(data));
		read(fd,data,sizeof(data)-1);
		printf("2thread:%s\n",data);
	}
	return NULL;
}

int main(int argc, const char *argv[])
{
	//创建有名管道
	fifo_creat(FIFO_PATH1);
	fifo_creat(FIFO_PATH2);
	//打开管道文件
	int ofifo1 = fifo_open2(FIFO_PATH1);
	int ofifo2 = fifo_open(FIFO_PATH2);
	if(ofifo1==-1 ||ofifo2==-1)
		return 0;
	//线程读出
	pthread_t tid;
	if(pthread_create(&tid,NULL,fifo_write,&ofifo2)){
		printf("线程创建失败!\n");
		return 0;
	}
	//往管道写入
	char data[300] = {0};
	while(strcmp(data,"end\n")){
		memset(data,0,sizeof(data));
		printf("请输入您想传输的数据!\n");
		fgets(data,sizeof(data),stdin);
		write(ofifo1,data,strlen(data));
		printf("progress2:%s\n",data);
	}

	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值