栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux之进程通信

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux之进程通信

Linux之进程通信 管道编程实战

无名管道:

父进程进行写,子进程读。

#include
#include
#include
#include
#include
int main()
{
        int fd[2];
        int pid;
        char buf[128];
        if(pipe(fd) == -1){
                printf("Create pipe failedn");

        }
        pid = fork();
        if(pid < 0){
                printf("Create child failedn");

        }
        else if(pid > 0){
                sleep(3);
                printf("This is fathern");
                close(fd[0]);
                write(fd[1],"Hello My friend!n",strlen("Hello My friendn"));
                wait(NULL);

        }else{
                printf("This is childn");
                close(fd[1]);
                read(fd[0],buf,128);
                printf("The info are %sn",buf);
                exit(0);
        }


        return 0;
}
创建管道

利用mkfifo创建管道

函数原型:

NAME
mkfifo, mkfifoat - make a FIFO special file (a named pipe)

SYNOPSIS
#include
#include

   int mkfifo(const char *pathname, mode_t mode);

   #include            
   #include 

   int mkfifoat(int dirfd, const char *pathname, mode_t mode);

DEscriptION
mkfifo() makes a FIFO special file with name pathname. mode specifies the FIFO’s permissions. It is modified by the process’s umask in the usual way: the permissions of the created file are (mode &
~umask).

   A  FIFO  special  file is similar to a pipe, except that it is created in a different way.  Instead of
   being an anonymous communications channel, a FIFO special file is entered into the filesystem by call‐
   ing mkfifo().

   once you have created a FIFO special file in this way, any process can open it for reading or writing,
   in the same way as an ordinary file.  However, it has to be open at both  ends  simultaneously  before
   you  can  proceed  to  do  any  input or output operations on it.  Opening a FIFO for reading normally
   blocks until some other process opens the same FIFO for writing, and vice versa.  See fifo(7) for non‐
   blocking handling of FIFO special files.
#include
#include
#include
#include
#include

int main()
{
        if(mkfifo("./file",0600)== -1 && errno ==EEXIST){
                printf("mkfifo failedn");
                perror("Why");
        }else{
                printf("mkfifo successn");

        }
        return 0;

}
消息队列

发送方:

#include
#include
#include
#include

struct msgbuf{
        long mtype;
        char mtext[128];

};

int main()
{
        struct msgbuf sendBuf = {111,"This is message from queuen"};
        key_t key = ftok(".",'z');
        printf("key = %xn",key);
        int msgId = msgget(key,IPC_CREAT|0777);
    	//msgget创建消息队列
        if(msgId == -1){
                printf("get queue failedn");
        }

        msgsnd(msgId,&sendBuf,strlen(sendBuf.mtext),0);
    	//发送
        struct msgbuf readBuf;
        msgrcv(msgId,&readBuf,sizeof(readBuf.mtext),112,0);
    	//接收
        printf("return from get:%sn",readBuf.mtext);
        msgctl(msgId,IPC_RMID,NULL);
        return 0;
}

接收方:

#include
#include
#include
#include
#include

struct msgbuf{
        long mtype;
        char mtext[128];

};

int main()
{
        struct msgbuf readBuf;
        key_t key = ftok(".",'z');
        printf("key = %xn",key);
        int msgId = msgget(key,IPC_CREAT|0777);
        if(msgId == -1){
                printf("get queue failedn");
        }
        msgrcv(msgId,&readBuf,sizeof(readBuf.mtext),112,0);
    	//接收
        printf("read from queue:%sn",readBuf.mtext);
        struct msgbuf sendBuf = {111,"Thank youn"};
        msgsnd(msgId,&sendBuf,strlen(sendBuf.mtext),0);
    	//发送

        msgctl(msgId,IPC_RMID,NULL);
        return 0;
}   
共享内存

函数原型:

NAME
shmget - allocates a System V shared memory segment

SYNOPSIS
#include
#include

   int shmget(key_t key, size_t size, int shmflg);

DEscriptION
shmget() returns the identifier of the System V shared memory segment associated with the value of the argument key. A new shared memory segment,with size equal to the value of size rounded up to a multiple of PAGE_SIZE, is created if key has the value IPC_PRIVATE or key isn’t IPC_PRIVATE, no shared memory segment corresponding to key exists, and IPC_CREAT is specified in shmflg.

   If shmflg specifies both IPC_CREAT and IPC_EXCL and a shared memory  segment already exists for key, then shmget() fails with errno set to EEXIST.  (This is analogous to the effect of the combination O_CREAT | O_EXCL for open(2).)

NAME
shmat, shmdt - System V shared memory operations

SYNOPSIS
#include
#include

   void *shmat(int shmid, const void *shmaddr, int shmflg);

   int shmdt(const void *shmaddr);

DEscriptION
shmat()
shmat() attaches the System V shared memory segment identified by shmid to the address space of the calling process. The attaching address is specified by shmaddr with one of the following criteria:

   *  If  shmaddr  is NULL, the system chooses a suitable (unused) page-aligned address to attach the segment.

   *  If shmaddr isn't NULL and SHM_RND is  specified  in  shmflg,  the  attach  occurs at the address equal to shmaddr rounded down to the nearest multiple of SHMLBA.

   *  Otherwise, shmaddr must be a page-aligned address  at  which  the  attach occurs.

NAME
shmat, shmdt - System V shared memory operations

SYNOPSIS
#include
#include

   void *shmat(int shmid, const void *shmaddr, int shmflg);

   int shmdt(const void *shmaddr);

DEscriptION
shmat()
shmat() attaches the System V shared memory segment identified by shmid to
the address space of the calling process. The attaching address is speci‐
fied by shmaddr with one of the following criteria:

   *  If  shmaddr  is NULL, the system chooses a suitable (unused) page-aligned
      address to attach the segment.

   *  If shmaddr isn't NULL and SHM_RND is  specified  in  shmflg,  the  attach
      occurs at the address equal to shmaddr rounded down to the nearest multi‐
      ple of SHMLBA.

   *  Otherwise, shmaddr must be a page-aligned address  at  which  the  attach
      occurs.

NAME
shmctl - System V shared memory control

SYNOPSIS
#include
#include

   int shmctl(int shmid, int cmd, struct shmid_ds *buf);

DEscriptION
shmctl() performs the control operation specified by cmd on the System V shared memory segment whose identifier is given in shmid.

   The  buf  argument  is  a  pointer  to  a  shmid_ds  structure,  defined  in  as follows:

       struct shmid_ds {
           struct ipc_perm shm_perm;    
           size_t          shm_segsz;   
           time_t          shm_atime;   
           time_t          shm_dtime;   
           time_t          shm_ctime;   
           pid_t           shm_cpid;    
           pid_t           shm_lpid;    
           shmatt_t        shm_nattch;  
           ...
       };

写:

#include
#include
#include
#include
#include
#include

int main()
{
        int shmid;
        char *shmaddr;

        key_t key;
        key = ftok(".",1);
        shmid = shmget(key,1024*4,IPC_CREAT|0666);
        if(shmid == -1){
                printf("shmget is okn");
                exit(-1);
        }
        shmaddr = shmat(shmid,0,0);
        printf("shmat okn");
        strcpy(shmaddr,"I want to be the best people!n");
        sleep(5);
        shmdt(shmaddr);
        shmctl(shmid,IPC_RMID,0);
        printf("quitn");
        return 0;
}

读:

#include
#include
#include
#include
#include
#include

int main()
{
        int shmid;
        char *shmaddr;

        key_t key;
        key = ftok(".",1);
        shmid = shmget(key,1024*4,0);
        if(shmid == -1){
                printf("shmget is okn");
                exit(-1);
        }
        shmaddr = shmat(shmid,0,0);
        printf("shmat okn");
        printf("data:%sn",shmaddr);

        shmdt(shmaddr);
        printf("quitn");
        return 0;
}

共享内存的查看:

ipcs -m

共享内存的删除:

ipcrm -m (shmid值)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/297561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号