linux操作系统编程——共享内存的读写(采用信号机制进行同步互斥)

来源:互联网 发布:字符串数组 strstr 编辑:程序博客网 时间:2024/05/01 12:10

程序要求:

   创建一个写端和一个读端,写端写入数据后读端才开始读,读端读完数据后,写端才可以开始写,这样的同步采用信号机制实现,并且写端与读端打开顺序不同也能实现功能;

程序如下:

(1)、write.c(写端)

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <errno.h>#include <signal.h>typedef struct{    pid_t pid;    char buf[1024];}memory;void write_func(int sig_no){    return ;}int main(int argc, const char *argv[]){    key_t key;    memory *p = NULL;    int shmid;    pid_t pid;    int create_flag = 0;    signal(SIGUSR1, write_func);    if ((key = ftok(".", 'a')) < 0)    {        perror("failed to get key");        exit(-1);    }    if ((shmid = shmget(key, sizeof(memory), 0666 | IPC_CREAT | IPC_EXCL)) < 0)    {        if (errno == EEXIST)        {                   if ((shmid = shmget(key, sizeof(memory), 0666)) < 0)            {                perror("failed to shmget memory");                exit(-1);            }            if ((p = shmat(shmid, NULL, 0)) == (void *)(-1))            {                perror("failed to shmat share memory");                exit(-1);            }            pid = p->pid;            p->pid = getpid();            kill(pid, SIGUSR1);        }        else        {            perror("failed to shmget");            exit(-1);        }    }    else    {        create_flag = 1;        if ((p = shmat(shmid, NULL, 0)) == (void *)(-1))        {            perror("failed to shmat memory");            exit(-1);        }        p->pid = getpid();        pause();        pid = p->pid;    }    while(1)    {        printf(">");        fgets(p->buf, sizeof(p->buf), stdin);        p->buf[strlen(p->buf) - 1] = 0;        kill(pid, SIGUSR1);        if (strncmp(p->buf, "quit", 4) == 0)            break;    }    if (create_flag == 1)    {        if (shmdt(p) < 0)        {            perror("failed to shmdt memory");            exit(-1);        }        if (shmctl(shmid, IPC_RMID, NULL) == -1)        {               perror("failed to delete share memory");            exit(-1);        }    }    return 0;}
(2)、read.c(读端)

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <signal.h>#include <errno.h>typedef struct{    pid_t pid;    char buf[1024];}memory;void read_func(int sig_no){    return ;}int main(int argc, const char *argv[]){    int pid;    key_t key;    int shmid;    memory *p = NULL;    int create_flag = 0;    signal(SIGUSR1, read_func);    if ((key = ftok(".", 'a')) < 0)    {        perror("failed to get key");        exit(-1);    }    if ((shmid = shmget(key, sizeof(memory), 0666 | IPC_CREAT | IPC_EXCL)) < 0)    {        if (errno == EEXIST)        {            if ((shmid = shmget(key, sizeof(memory), 0666)) < 0)            {                perror("failed to create share memory");                exit(-1);            }                        if ((p = shmat(shmid, NULL, 0)) == (void *)(-1))            {                perror("failed to shmat");                exit(-1);            }            pid = p->pid;            p->pid = getpid();            kill(pid , SIGUSR1);        }        else        {            perror("failed to shmget");            exit(-1);        }    }    else    {        create_flag = 1;        if ((p = shmat(shmid, NULL, 0)) == (void *)(-1))        {            perror("failed to shmat");            exit(-1);        }        p->pid = getpid();        pause();        pid = p->pid;    }        while(1)    {        pause();        if (strncmp(p->buf, "quit", 4) == 0)            break;        printf("recv: %s\n", p->buf);    }    if (create_flag == 1)    {        if (shmdt(p) < 0)        {            perror("failed to shmdt");            exit(-1);        }        if (shmctl(shmid, IPC_RMID, NULL) == -1)        {            perror("failed to delete share memory");            exit(-1);        }    }    return 0;}

原创粉丝点击