IO进程(学习)2024.8.22

信号

信号函数

信号处理函数

#include <signal.h>
sighandler_t signal(int signum, sighandler_t handler);
功能:信号处理函数
参数:signum:要处理的信号
           handler:信号处理方式
                        SIG_IGN:忽略信号
                        SIG_DFL:执行默认操作
                        handler:捕捉信号  
                        void handler(int sig){} //函数名可以自定义
返回值:成功:设置之前的信号处理方式
               失败:-1

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void handler(int sig)
{
    if (sig == SIGQUIT)
    {
        printf("是SIGQUIT信号\n");
    }
    else
    {
        printf("是其他信号\n");
    }
}

int main(int argc, char const *argv[])
{
    // signal(SIGINT, SIG_DFL); // 执行默认操作

    // signal(SIGINT, SIG_IGN); // 忽略信号

    signal(SIGINT, handler); // 捕捉信号

    pause();
    return 0;
}

信号的处理过程

程序运行在用户空间时->进程由于系统调用或中断进入内核->转向用户空间执行信号处理函数->信号处理函数完毕后进入内核->返回用户空间继续执行程序

练习:用信号的知识实现司机和售票员问题。
1.售票员捕捉SIGINT(代表开车)信号,向司机发送SIGUSR1信号,司机打印(let's gogogo)
2.售票员捕捉SIGQUIT(代表停车)信号,向司机发送SIGUSR2信号,司机打印(stop the bus)
3.司机捕捉SIGTSTP(代表到达终点站)信号,向售票员发送SIGUSR1信号,售票员打印(please get off the bus)
4.司机等待售票员下车,之后司机再下车。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

pid_t pid;  // 售票员的进程ID
pid_t ppid; // 司机的进程ID

// 售票员的信号处理函数
void shoupiaoyuan(int sig)
{
    if (sig == SIGINT)
    {
        kill(ppid, SIGUSR1); // 向司机发送SIGUSR1信号
    }
    if (sig == SIGQUIT)
    {
        kill(ppid, SIGUSR2); // 向司机发送SIGUSR2信号
    }
    if (sig == SIGUSR1)
    {
        printf("Please get off the bus!\n");
        exit(0);
    }
}

// 司机的信号处理函数
void siji(int sig)
{
    if (sig == SIGUSR1)
    {
        printf("Let's gogogo!\n");
    }
    if (sig == SIGUSR2)
    {
        printf("Stop the bus!\n");
    }
    if (sig == SIGTSTP)
    {
        kill(pid, SIGUSR1); // 向售票员发送SIGUSR1信号
        wait(NULL);
        exit(0);
    }
}

int main()
{
    ppid = getpid();
    pid = fork();
    if (pid < 0)
    {
        perror("forka创建失败");
        return -1;
    }

    if (pid == 0) // 子进程:售票员
    {
        // 等待信号
        while (1)
        {
            signal(SIGTSTP, SIG_IGN);      // 忽略SIGTSTP信号
            signal(SIGINT, shoupiaoyuan);  // 捕捉信号SIGINT
            signal(SIGQUIT, shoupiaoyuan); // 捕捉信号SIGQUIT
            signal(SIGUSR1, shoupiaoyuan); // 捕捉信号SIGUSR1
            pause();
        }
    }
    else // 父进程:司机
    {
        // 等待信号
        while (1)
        {
            signal(SIGINT, SIG_IGN);  // 忽略SIGINT
            signal(SIGQUIT, SIG_IGN); // 忽略SIGQUIT
            signal(SIGUSR1, siji);    // 捕捉信号SIGUSR1
            signal(SIGUSR2, siji);    // 捕捉信号SIGUSR2
            signal(SIGTSTP, siji);    // 捕捉信号SIGTSTP
            pause();
        }
    }
    return 0;
}

共享内存

概念

共享内存指的是操作系统在物理内存中申请一块空间,应用程序可以映射到这块空间,进行直接读写操作

特点

1.共享内存是一种最为高效的进程间通信方式,进程可以直接读写内存,而不需要任何数据的拷贝
2.为了在多个进程间交换信息,内核专门留出了一块内存区,可以由需要访问的进程将其映射到自己的私有地址空间
3.进程就可以直接读写这一内存区而不需要进行数据的拷贝,从而大大提高的效率
4.由于多个进程共享一段内存,因此也需要依靠某种同步机制,如互斥锁和信号量等

步骤

1.创建唯一key值        ftok
2.创建或打开共享内存        shmget
3.映射共享内存        shmat
4.取消映射        shmdt
5.删除共享内存        shmctl

函数接口

创建key值

#include <sys/types.h>
#include <sys/ipc.h>

key_t ftok(const char *pathname, int proj_id);
功能:创建key值
参数:pathname:文件名
           proj_id:取整型数的低8位数值(一般是写一个字符 'a')
返回值:成功:key值
              失败:-1
key值是根据pathname的inode号和proj_id的低8位组合而成的。

 

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>

int main(int argc, char const *argv[])
{
    key_t key;
    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }
    printf("key值为:%#x\n", key);
    return 0;
}

创建或打开共享内存

#include <sys/shm.h>

int shmget(key_t key, size_t size, int shmflg);
功能:创建或打开共享内存
参数:
        key  键值
        size   共享内存的大小
                             创建                      检测错误             创建共享内存的时候的权限
        shmflg   IPC_CREAT       |       IPC_EXCL       |             0666  
返回值:成功   shmid  共享内存的id
              出错    -1

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>

int main(int argc, char const *argv[])
{
    key_t key;
    int shmid;
    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }
    shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid <= 0)
    {
        if (errno == EEXIST)
        {
            shmid = shmget(key, 64, 0666);
        }
        else
        {
            perror("创建共享内存失败");
            return -1;
        }
    }
    printf("%d\n", shmid);

    return 0;
}

映射共享内存

void  *shmat(int  shmid,const  void  *shmaddr,int  shmflg);
功能:映射共享内存,即把指定的共享内存映射到进程的地址空间用于访问
参数:
        shmid   共享内存的id号
        shmaddr   一般为NULL,表示由系统自动完成映射
                         如果不为NULL,那么有用户指定
        shmflg:SHM_RDONLY就是对该共享内存只进行读操作
                                0        可读可写
返回值:成功:完成映射后的地址,
               出错:(void *)-1的地址

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    key_t key;
    int shmid;
    char *p;

    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }
    printf("key值为:%#x\n", key);

    shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid <= 0)
    {
        if (errno == EEXIST)
        {
            shmid = shmget(key, 64, 0666);
        }
        else
        {
            perror("创建共享内存失败");
            return -1;
        }
    }
    printf("shmid号为:%d\n", shmid);

    p = shmat(shmid, NULL, 0);
    if (p == (char *)-1)
    {
        perror("映射共享内存失败");
        return -1;
    }

    strcpy(p, "hello");
    printf("%s\n", p);

    return 0;
}

取消映射

int shmdt(const void *shmaddr);
功能:取消映射
参数:要取消的地址
返回值:成功:0  
               失败:-1

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    key_t key;
    int shmid;
    char *p;

    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }
    printf("key值为:%#x\n", key);

    shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid <= 0)
    {
        if (errno == EEXIST)
        {
            shmid = shmget(key, 64, 0666);
        }
        else
        {
            perror("创建共享内存失败");
            return -1;
        }
    }
    printf("shmid号为:%d\n", shmid);

    p = shmat(shmid, NULL, 0);
    if (p == (char *)-1)
    {
        perror("映射共享内存失败");
        return -1;
    }

    strcpy(p, "hello");
    printf("%s\n", p);
    
    // 阻塞观察内存状态
    // getchar();

    // 取消映射
    shmdt(p);

    return 0;
}

删除共享内存

int  shmctl(int  shmid,int  cmd,struct  shmid_ds   *buf);
功能:(删除共享内存),对共享内存进行各种操作
参数:
        shmid   共享内存的id号
        cmd     IPC_STAT 获得shmid属性信息,存放在第三参数
                    IPC_SET 设置shmid属性信息,要设置的属性放在第三参数
                    IPC_RMID:删除共享内存,此时第三个参数为NULL即可
        struct  shmid_ds   *buf:是一个结构体指针,但我们是删除共享内存,所以并没有意义,我们直接设置为NULL就可以了
返回:成功0 
           失败-1

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    key_t key;
    int shmid;
    char *p;

    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }
    printf("key值为:%#x\n", key);

    shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid <= 0)
    {
        if (errno == EEXIST)
        {
            shmid = shmget(key, 64, 0666);
        }
        else
        {
            perror("创建共享内存失败");
            return -1;
        }
    }
    printf("shmid号为:%d\n", shmid);

    p = shmat(shmid, NULL, 0);
    if (p == (char *)-1)
    {
        perror("映射共享内存失败");
        return -1;
    }

    strcpy(p, "hello");
    printf("%s\n", p);

    // 阻塞观察内存状态
    // getchar();

    // 取消映射
    shmdt(p);

    // 删除共享内存
    shmctl(shmid, IPC_RMID, NULL);

    return 0;
}

操作命令

ipcs -m:查看系统中创建的共享内存
ipcrm -m shmid:删除创建的共享内存

练习:通过共享内存实现进程间通信,一个进程从终端输入数据(input.c),另一个进程打印数据(output.c),循环执行,当输入quit时循环结束

input.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>

struct data
{
    int flag;
    char buf[32];
};

int main()
{
    key_t key;
    int shmid;
    struct data *p = NULL;

    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }
    printf("key值为:%#x\n", key);

    shmid = shmget(key, 64, IPC_CREAT | 0666);
    if (shmid < 0)
    {
        perror("创建共享内存失败");
        return -1;
    }
    printf("shmid号为:%d\n", shmid);

    p = (struct data *)shmat(shmid, NULL, 0);
    if (p == (struct data *)-1)
    {
        perror("映射共享内存失败");
        return -1;
    }
    p->flag = 0;
    while (1)
    {
        scanf("%s", p->buf);
        p->flag = 1;
        if (strcmp(p->buf, "quit") == 0)
        {
            break;
        }
    }
    shmdt(p);
    return 0;
}

output.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
struct data
{
    int flag;
    char buf[32];
};

int main()
{
    key_t key;
    int shmid;
    struct data *p = NULL;

    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }
    printf("key值为:%#x\n", key);

    shmid = shmget(key, 64, 0666);
    if (shmid < 0)
    {
        perror("获取共享内存失败");
        return -1;
    }
    printf("shmid号为:%d\n", shmid);

    p = (struct data *)shmat(shmid, NULL, 0);
    if (p == (struct data *)-1)
    {
        perror("映射共享内存失败");
        return -1;
    }
    p->flag = 0;
    while (1)
    {
        if (p->flag == 1)
        {
            if (strcmp(p->buf, "quit") == 0)
            {
                break;
            }
            printf("读取到的数据: %s\n", p->buf);
            p->flag = 0;
        }
    }
    shmdt(p);
    shmctl(shmid, IPC_RMID, NULL);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值