目录
1、什么是共享内存?
共享内存本质上就是内存中的一块区域,用于进程间通信使用。所有访问这块空间的程序都能访问改数据。
2、实现思路

3、upload和download代码
3.1、头文件
#ifndef __COMMON_H__
#define __COMMON_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#define IPC_PATH "./" // IPC路径
#define IPC_MASK 1 // IPC标识
#define MSG_LEN 256 // 消息长度
#define PAGE_SIZE 4096 // 一页的大小,共享内存设置大小时,最好以当前系统的整数页好
typedef struct demo_inf
{
int shm_id; // 共享内存id
char *shm_p; // 共享内存地址
char *file_path; // upload file path
long other_id; // download id
} SDI, *P_SDI;
#endif
3.2、upload
#include "common.h"
P_SDI Upload_Init(char *file_path);
int Running_upload(P_SDI p_sdi);
void get_file_size(int signum);
int Upload_Free(P_SDI p_sdi);
int Upload_Free(P_SDI p_sdi)
{
free(p_sdi);
printf("free 成功!\n");
return 0;
}
void get_file_size(int signum)
{
printf("get_file_size signal\n");
}
void file_upload_continue(int signum);
void file_upload_continue(int signum)
{
if (signum == SIGUSR2)
{
printf("file download continue!\n");
}
if (signum == SIGALRM)
{
printf("the last download\n");
}
}
P_SDI Upload_Init(char *file_path)
{
if (file_path == (char *)NULL)
{
printf("will upload file path is NULL..\n");
return (P_SDI)-1;
}
P_SDI p_sdi = (P_SDI)malloc(sizeof(SDI));
if (p_sdi == (P_SDI)NULL)
{
perror("malloc error");
return (P_SDI)-1;
}
// 创建Key值
key_t key = ftok(IPC_PATH, IPC_MASK);
if (key == -1)
{
perror("ftok error");
return (P_SDI)-1;
}
else
{
printf("key = %d\n", key);
}
p_sdi->file_path = file_path;
printf("请输入接收方的PID : \n");
scanf("%ld", &p_sdi->other_id);
// 创建共享内存ID
if ((p_sdi->

最低0.47元/天 解锁文章
746

被折叠的 条评论
为什么被折叠?



