Linux下プロセス間通信実験
(1)無名パイプ親子プロセス間通信pipe()
(2)有名パイプ任意プロセス間通信mkfifo()
(3)信号通信signal()
(4)共有メモリによる通信shmget(),shmat()
(5)メッセージキューftok(),msgget(),msgsnd(),msgrcv()
(6)信号量semget(),semop()
- #include <unistd.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- int main()
- {
- int pipe_fd[2];
- pid_t pid;
- char buf_r[100];
- char* p_wbuf;
- int r_num;
-
- memset(buf_r,0,sizeof(buf_r));
-
- /* */
- if(pipe(pipe_fd)<0)
- {
- printf("pipe create error
");
- return -1;
- }
-
- /* */
- if((pid=fork())==0) // OR ?
- {
- printf("
");
- close(pipe_fd[1]);
- sleep(2); /* */
- if((r_num=read(pipe_fd[0],buf_r,100))>0)
- {
- printf( "%d numbers read from the pipe is %s
",r_num,buf_r);
- }
- close(pipe_fd[0]);
- exit(0);
- }
- else if(pid>0)
- {
- close(pipe_fd[0]);
- if(write(pipe_fd[1],"Hello",5)!=-1)
- printf("parent write1 Hello!
");
- if(write(pipe_fd[1]," Pipe",5)!=-1)
- printf("parent write2 Pipe!
");
- close(pipe_fd[1]);
- sleep(3);
- waitpid(pid,NULL,0); /* */
- exit(0);
- }
- return 0;
- }
(2)有名パイプ任意プロセス間通信mkfifo()
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define FIFO "/tmp/myfifo"
-
- main(int argc,char** argv)
- {
- char buf_r[100];
- int fd;
- int nread;
-
- /* */
- if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
- printf("cannot create fifoserver
");
-
- printf("Preparing for reading bytes...
");
-
- memset(buf_r,0,sizeof(buf_r));
-
- /* */
- fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
- if(fd==-1)
- {
- perror("open");
- exit(1);
- }
- while(1)
- {
- memset(buf_r,0,sizeof(buf_r));
-
- if((nread=read(fd,buf_r,100))==-1)
- {
- if(errno==EAGAIN)
- printf("no data yet
");
- }
- printf("read %s from FIFO
",buf_r);
- sleep(1);
- }
- pause(); /* , */
- unlink(FIFO); //
- }
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define FIFO_SERVER "/tmp/myfifo"
-
- main(int argc,char** argv)
- {
- int fd;
- char w_buf[100];
- int nwrite;
-
- /* */
- fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
-
- if(argc==1)
- {
- printf("Please send something
");
- exit(-1);
- }
-
- strcpy(w_buf,argv[1]);
-
- /* */
- if((nwrite=write(fd,w_buf,100))==-1)
- {
- if(errno==EAGAIN)
- printf("The FIFO has not been read yet.Please try later
");
- }
- else
- printf("write %s to the FIFO
",w_buf);
- }
(3)信号通信signal()
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- void my_func(int sign_no)
- {
- if(sign_no==SIGINT)
- printf("I have get SIGINT
");
- else if(sign_no==SIGQUIT)
- printf("I have get SIGQUIT
");
- }
- int main()
- {
- printf("Waiting for signal SIGINT or SIGQUIT
");
-
- /* */
- signal(SIGINT, my_func);
- signal(SIGQUIT, my_func);
-
- pause();/* */
- exit(0);
- }
(4)共有メモリによる通信shmget(),shmat()
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
-
- #define PERM S_IRUSR|S_IWUSR
- /* */
-
- int main(int argc,char **argv)
- {
- int shmid;
- char *p_addr,*c_addr;
-
- if(argc!=2)
- {
- fprintf(stderr,"Usage:%s
\a",argv[0]);
- exit(1);
- }
-
- /* */
- if((shmid=shmget(IPC_PRIVATE,1024,PERM))==-1)
- {
- fprintf(stderr,"Create Share Memory Error:%s
\a",strerror(errno));
- exit(1);
- }
-
- /* */
- if(fork()) //
- {
- p_addr=shmat(shmid,0,0); /* 0 , 0 */
- memset(p_addr,'\0',1024); //p_addr
- strncpy(p_addr,argv[1],1024);
- wait(NULL); // ,
- exit(0);
- }
- else //
- {
- sleep(1); // 1
- c_addr=shmat(shmid,0,0);
- printf("Client get %p
",c_addr);
- exit(0);
- }
- }
(5)メッセージキューftok(),msgget(),msgsnd(),msgrcv()
- #include <sys/types.h>
- #include <sys/msg.h>
- #include <unistd.h>
-
- struct msg_buf
- {
- int mtype;
- char data[255];
- };
-
- int main()
- {
- key_t key;
- int msgid;
- int ret;
- struct msg_buf msgbuf;
-
- key=ftok("/tmp/2",'a');
- printf("key =[%x]
",key);
- msgid=msgget(key,IPC_CREAT|0666); /* */
-
- if(msgid==-1)
- {
- printf("create error
");
- return -1;
- }
-
- msgbuf.mtype = getpid();// , int, ,
- strcpy(msgbuf.data,"test haha");
- ret=msgsnd(msgid,&msgbuf,sizeof(msgbuf.data),IPC_NOWAIT);
- if(ret==-1)
- {
- printf("send message err
");
- return -1;
- }
-
- memset(&msgbuf,0,sizeof(msgbuf));
- ret=msgrcv(msgid,&msgbuf,sizeof(msgbuf.data),getpid(),IPC_NOWAIT);
- if(ret==-1)
- {
- printf("recv message err
");
- return -1;
- }
- printf("recv msg =[%s]
",msgbuf.data);
-
- }
(6)信号量semget(),semop()