IO进程(学习)2024.8.23

目录

信号灯集

概念

操作步骤

 函数接口

1.创建或打开信号灯集

 2.初始化或删除信号灯集

3.pv操作

4.操作命令

消息队列

特点

步骤

函数接口

1.创建或打开消息队列

2.添加消息

3.读取消息

4.删除消息队列

5.操作命令

信号灯集

概念

信号灯(semaphore),也叫信号量。它是不同进程间或一个给定进程内部不同线程间同步的机制;System V信号灯集是一个或者多个信号灯的一个集合。其中的每一个都是单独的计数信号灯。而Posix信号灯指的是单个计数信号灯。
通过信号灯集实现共享内存的同步操作

操作步骤

1.创建key值

2.创建或打开信号灯集        semget

3.初始化信号灯集        semctl

4.pv操作        semop

5.删除信号灯集        semctl

 函数接口

1.创建或打开信号灯集

#include <sys/sem.h>
int semget(key_t key, int nsems, int semflg);
功能:创建/打开信号灯
参数:key:ftok产生的key值
    nsems:信号灯集中包含的信号灯数目
    semflg:信号灯集的访问权限,通常为IPC_CREAT |0666
返回值:成功:信号灯集ID
       失败:-1

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

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

    semid = semget(key, 2, IPC_CREAT | IPC_EXCL | 0666);
    if (semid <= 0)
    {
        if (errno == EEXIST)
        {
            semid = semget(key, 2, 0666);
        }
        else
        {
            perror("创建信号灯集失败");
            return -1;
        }
    }
    printf("%d\n", semid);

    return 0;
}

 2.初始化或删除信号灯集

int semctl ( int semid, int semnum,int cmd,…/*union semun arg*/);
功能:信号灯集合的控制(初始化/删除)
参数:semid:信号灯集ID
        semnum: 要操作的集合中的信号灯编号
        cmd:
                GETVAL:获取信号灯的值,返回值是获得值
                SETVAL:设置信号灯的值,当cmd为SETVAL时,需要传递共用体
                IPC_RMID:从系统中删除信号灯集合
返回值:成功 0
            失败 -1
共用体格式:
union semun

{
        int val;        /* 信号量的初值 */
        struct semid_ds *buf;        /* Buffer for IPC_STAT, IPC_SET */
        unsigned short  *array;        /* Array for GETALL, SETALL */
        struct seminfo  *__buf;        /* Buffer for IPC_INFO(Linux-specific) */
};

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

union semun
{
    int val;               /* 信号量的初值 */
    struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
    unsigned short *array; /* Array for GETALL, SETALL */
    struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
};

// 初始化
void seminit(int semid, int num, int val)
{
    union semun sem;
    sem.val = val;
    semctl(semid, num, SETVAL, sem);
}

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

    semid = semget(key, 2, IPC_CREAT | IPC_EXCL | 0666);
    if (semid <= 0)
    {
        if (errno == EEXIST)
        {
            semid = semget(key, 2, 0666);
        }
        else
        {
            perror("创建信号灯集失败");
            return -1;
        }
    }
    else // 初始化(只需要在创建时初始化即可,避免重复初始化)
    {
        seminit(semid, 0, 0);
        seminit(semid, 1, 1);
    }

    printf("sem1:%d\n", semctl(semid, 0, GETVAL));
    printf("sem2:%d\n", semctl(semid, 1, GETVAL));

    return 0;
}

3.pv操作

int semop ( int semid, struct sembuf  *opsptr,  size_t  nops);
功能:对信号灯集合中的信号量进行PV操作
参数:semid:信号灯集ID
           opsptr:操作方式
           nops:  要操作的信号灯的个数 1个
返回值:成功 :0
               失败:-1
结构体格式:
struct sembuf {
        short  sem_num; // 要操作的信号灯的编号
        short  sem_op;  //    0 :  等待,直到信号灯的值变成0
                                 //   1  :  释放资源,V操作
                                //   -1 :  分配资源,P操作                    
        short  sem_flg; // 0(阻塞),IPC_NOWAIT, SEM_UNDO
};
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>

union semun
{
    int val;               /* 信号量的初值 */
    struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
    unsigned short *array; /* Array for GETALL, SETALL */
    struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
};

// 初始化
void seminit(int semid, int num, int val)
{
    union semun sem;
    sem.val = val;
    semctl(semid, num, SETVAL, sem);
}

// PV操作
void sempv(int semid, int num, int op)
{
    struct sembuf buf;
    buf.sem_num = num;
    buf.sem_op = op;
    buf.sem_flg = 0;
    semop(semid, &buf, 1);
}

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

    semid = semget(key, 2, IPC_CREAT | IPC_EXCL | 0666);
    if (semid <= 0)
    {
        if (errno == EEXIST)
        {
            semid = semget(key, 2, 0666);
        }
        else
        {
            perror("创建信号灯集失败");
            return -1;
        }
    }
    else // 初始化(只需要在创建时初始化即可,避免重复初始化)
    {
        seminit(semid, 0, 10);
        seminit(semid, 1, 10);
    }
    // P操作
    sempv(semid, 0, 1);
    // V操作
    sempv(semid, 1, -1);

    printf("sem1:%d\n", semctl(semid, 0, GETVAL));
    printf("sem2:%d\n", semctl(semid, 1, GETVAL));

    // 删除信号灯集
    semctl(semid, 0, IPC_RMID);

    return 0;
}

4.操作命令

    ipcs  -s  :查看创建的信号灯集
    ipcrm  -s  [semid]:删除信号灯集

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

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

union semun
{
    int val;               /* 信号量的初值 */
    struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
    unsigned short *array; /* Array for GETALL, SETALL */
    struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
};

// 初始化
void seminit(int semid, int num, int val)
{
    union semun sem;
    sem.val = val;
    semctl(semid, num, SETVAL, sem);
}

// PV操作
void sempv(int semid, int num, int op)
{
    struct sembuf buf;
    buf.sem_num = num;
    buf.sem_op = op;
    buf.sem_flg = 0;
    semop(semid, &buf, 1);
}

int main(int argc, char const *argv[])
{
    key_t key;
    int semid;
    int shmid;
    char *p;
    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;
        }
    }
    p = shmat(shmid, NULL, 0);
    if (p == (char *)-1)
    {
        perror("映射共享内存失败");
        return -1;
    }
    semid = semget(key, 2, IPC_CREAT | IPC_EXCL | 0666);
    if (semid <= 0)
    {
        if (errno == EEXIST)
        {
            semid = semget(key, 2, 0666);
        }
        else
        {
            perror("创建信号灯集失败");
            return -1;
        }
    }
    else // 初始化(只需要在创建时初始化即可,避免重复初始化)
    {
        seminit(semid, 0, 0);
        seminit(semid, 1, 1);
    }

    while (1)
    {
        // P操作
        // sempv(semid, 1, -1);
        scanf("%s", p);
        // V操作
        sempv(semid, 0, 1);
        if (strcmp(p, "quit") == 0)
        {
            break;
        }
    }
    shmdt(p);
    return 0;
}

output.c:

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

union semun
{
    int val;               /* 信号量的初值 */
    struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
    unsigned short *array; /* Array for GETALL, SETALL */
    struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
};

// 初始化
void seminit(int semid, int num, int val)
{
    union semun sem;
    sem.val = val;
    semctl(semid, num, SETVAL, sem);
}

// PV操作
void sempv(int semid, int num, int op)
{
    struct sembuf buf;
    buf.sem_num = num;
    buf.sem_op = op;
    buf.sem_flg = 0;
    semop(semid, &buf, 1);
}

int main(int argc, char const *argv[])
{
    key_t key;
    int semid;
    int shmid;
    char *p;
    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;
        }
    }
    p = shmat(shmid, NULL, 0);
    if (p == (char *)-1)
    {
        perror("映射共享内存失败");
        return -1;
    }

    semid = semget(key, 2, IPC_CREAT | IPC_EXCL | 0666);
    if (semid <= 0)
    {
        if (errno == EEXIST)
        {
            semid = semget(key, 2, 0666);
        }
        else
        {
            perror("创建信号灯集失败");
            return -1;
        }
    }
    else // 初始化(只需要在创建时初始化即可,避免重复初始化)
    {
        seminit(semid, 0, 0);
        seminit(semid, 1, 1);
    }

    while (1)
    {
        // P操作
        sempv(semid, 0, -1);
        if (strcmp(p, "quit") == 0)
        {
            break;
        }
        // V操作
        // sempv(semid, 1, 1);
        printf("读取到的数据: %s\n", p);
    }

    shmdt(p);
    shmctl(shmid, IPC_RMID, NULL);
    semctl(semid, 0, IPC_RMID);

    return 0;
}

消息队列

特点

1.消息队列就是一个消息的列表。用户可以在消息队列中添加消息、读取消息等。
2.消息队列可以按照类型来发送/接收消息
3.在linux下消息队列的大小有限制。
        消息队列个数最多为16个
        消息队列总容量最多为16384字节
        每个消息内容最多为8192字节

步骤

1.创建key值

2.创建或打开消息队列        msgget

3.添加信息        msgsnd

4.读取消息        msgrcv

5.删除消息队列        msgctl

函数接口

1.创建或打开消息队列

#include <sys/msg.h>
int msgget(key_t key, int flag);
功能:创建或打开一个消息队列
参数:key值
           flag:创建消息队列的权限IPC_CREAT|IPC_EXCL|0666
返回值:成功:返回msgid值
               失败:-1

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

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

    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgid = msgget(key, 0666);
        }
        else
        {
            perror("创建消息队列失败");
            return -1;
        }
    }
    printf("%d\n", msgid);

    return 0;
}

2.添加消息

int msgsnd(int msqid, const void *msgp, size_t size, int flag); 
功能:添加消息
参数:msqid:消息队列的ID
           msgp:指向消息的指针。

常用消息结构msgbuf如下:
struct msgbuf

{
        long mtype;        //消息类型
        char mtext[N]};         //消息正文
}
          size:发送的消息正文的字节数
          flag:IPC_NOWAIT:消息没有发送完成函数也会立即返回    
                   0:直到发送完成函数才返回
返回值:成功:0
              失败:-1

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

struct msgbuf
{
    long mtype; // 消息类型
    char ch;    // 消息正文
    int num;
};

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

    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgid = msgget(key, 0666);
        }
        else
        {
            perror("创建消息队列失败");
            return -1;
        }
    }
    // 添加消息
    struct msgbuf msg = {100, 'a', 10};
    msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);

    return 0;
}

3.读取消息

int msgrcv(int msgid,  void* msgp,  size_t  size,  long msgtype,  int  flag);
功能:读取消息
参数:msgid:消息队列的ID
           msgp:存放读取消息的空间
           size:接受的消息正文的字节数
           msgtype:0:接收消息队列中第一个消息。
                        大于0:接收消息队列中第一个类型为msgtyp的消息.
                        小于0:接收消息队列中类型值不小于msgtyp的绝对值且类型值又最小的消息。
           flag:0:若无消息函数会一直阻塞
                     IPC_NOWAIT:若没有消息,进程会立即返回ENOMSG
返回值:成功:接收到的消息的长度
               失败:-1

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

struct msgbuf
{
    long mtype; // 消息类型
    char ch;    // 消息正文
    int num;
};

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

    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgid = msgget(key, 0666);
        }
        else
        {
            perror("创建消息队列失败");
            return -1;
        }
    }
    //添加消息
    struct msgbuf msg = {100, 'a', 10};
    msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);

    // 再添加一条消息
    msg.mtype = 200;
    msg.ch = 'b';
    msg.num = 20;
    msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);

    // 读信息
    struct msgbuf msg1;
    msgrcv(msgid, &msg1, sizeof(msg1) - sizeof(long),100, 0);
    printf("%c %d\n", msg1.ch, msg1.num);

    return 0;
}

4.删除消息队列

int msgctl ( int msgqid, int cmd, struct msqid_ds *buf );
功能:对消息队列的操作,删除消息队列
参数:msqid:消息队列的队列ID
     cmd:
        IPC_STAT:读取消息队列的属性,并将其保存在buf指向的缓冲区中。
        IPC_SET:设置消息队列的属性。这个值取自buf参数。
        IPC_RMID:从系统中删除消息队列。
     buf:消息队列缓冲区
返回值:成功:0
      失败:-1

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

struct msgbuf
{
    long mtype; // 消息类型
    char ch;    // 消息正文
    int num;
};

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

    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgid = msgget(key, 0666);
        }
        else
        {
            perror("创建消息队列失败");
            return -1;
        }
    }
    // 添加消息
    struct msgbuf msg = {100, 'a', 10};
    msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);

    // 再添加一条消息
    msg.mtype = 200;
    msg.ch = 'b';
    msg.num = 20;
    msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);

    // 读信息
    struct msgbuf msg1;
    msgrcv(msgid, &msg1, sizeof(msg1) - sizeof(long), 100, 0);
    printf("%c %d\n", msg1.ch, msg1.num);

    msgrcv(msgid, &msg1, sizeof(msg1) - sizeof(long), 200, 0);
    printf("%c %d\n", msg1.ch, msg1.num);

    // 删除消息队列
    msgctl(msgid, IPC_RMID, NULL);

    return 0;
}

5.操作命令

ipcs -q:查看消息队列
ipcrm -q msgid:删除消息队列

练习:两个进程通过消息队列进行通信,一个进程从终端输入下发的指令,另一个进程接收指令,并打印对应操作语句。
        如果输入LEDON,另一个进程输出 “开灯”
        如果输入LEDOFF,另一个进程输出 “关灯”
        如果输入BEEPON,另一个进程输出 “开蜂鸣器”
        如果输入BEEPOFF,另一个进程输出 “关蜂鸣器”

input.c:

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

struct msgbuf
{
    long mtype;   // 消息类型
    char ch[100]; // 消息正文
};

int main()
{
    key_t key;
    int msgid;
    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }

    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgid = msgget(key, 0666);
        }
        else
        {
            perror("创建消息队列失败");
            return -1;
        }
    }

    while (1)
    {
        struct msgbuf msg;
        msg.mtype = 100;
        printf("请输入信号: ");
        scanf("%s", msg.ch);
        msgsnd(msgid, &msg, sizeof(msg.ch), 0);
    }

    return 0;
}

output.c:

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

struct msgbuf
{
    long mtype;   // 消息类型
    char ch[100]; // 消息正文
};

int main()
{
    key_t key;
    int msgid;
    key = ftok("./test", 'a');
    if (key < 0)
    {
        perror("创建key值失败");
        return -1;
    }

    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgid = msgget(key, 0666);
        }
        else
        {
            perror("创建消息队列失败");
            return -1;
        }
    }
    while (1)
    {
        struct msgbuf msg1;
        msgrcv(msgid, &msg1, sizeof(msg1.ch), 100, 0);

        if (strcmp(msg1.ch, "LEDON") == 0)
        {
            printf("开灯\n");
        }
        else if (strcmp(msg1.ch, "LEDOFF") == 0)
        {
            printf("关灯\n");
        }
        else if (strcmp(msg1.ch, "BEEPON") == 0)
        {
            printf("开蜂鸣器\n");
        }
        else if (strcmp(msg1.ch, "BEEPOFF") == 0)
        {
            printf("关蜂鸣器\n");
        }
    }
    // 删除消息队列
    msgctl(msgid, IPC_RMID, NULL);

    return 0;
}
  • 29
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值