//linuc command ===>man ====>pipe,mkfifo,kill&signal,semget,shmget,msgget,inet_addr //三次握手四次挥手
//client==>connect req,
//server==>ack,
//client===>ack
//
//client==>req cloce
//server==>ack
//client==>ack
//server==>make sure ack
### 1 无名管道 #include <stdio.h> #include <iostream> #include <unistd.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) int main(void) { int pipefd[2]; int pfd = pipe(pipefd); println(%s,"[PROC 1]"); if(pfd == -1) { perror("pfd error"); return EPIPE; } pid_t pid = fork(); if(pid == -1) { perror(format(fork error)); } if(pid == 0) { println(%s,"child process"); write(pipefd[1],(void *)"hellomyfather\n",sizeof(format(formathellomyfather))); } if(pid > 0 ) { println(%s,"parent process"); char* buff[0xff]; read(pipefd[0],(void*)buff,sizeof(format(formathellomyfather))); println(parent recv from child %s,(char *)buff); } return 0; } ### ### 有名管道 #include <stdio.h> #include <iostream> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) int main(void) { const char *pathname = "./fifofile";//path & fileName println(%s,"[PROC 1]"); if(access(pathname,R_OK|W_OK) == F_OK) //check file isexist { println(%s,"file exist"); } int ret = mkfifo(pathname, 0666); if(!ret ) { perror("mkfifo error"); //run 1,print error cause creating a fifo file,you run 2 is ok! return -1; } int fd = open(pathname,O_RDWR); if(fd == -1) { perror("file open error"); } const char buff[0xff] = "hello process 2,i`m process 1"; write(fd,buff,sizeof(buff)); println("%s","-----------"); while(1); return 0; } ////////////////////////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <iostream> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) int main(void) { const char *pathname = "./fifofile";//path & fileName println(%s,"[PROC 2]"); int ret ; int fd = open(pathname,O_RDWR); if(fd == -1) { perror("file open error"); } char buff[0xff] ; read(fd,buff,sizeof(buff)); println(%s,buff); while(1); return 0; } ### ### 信号 #include <stdio.h> #include <iostream> #include <sys/types.h> #include <signal.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) int main(int argc,char *argv[]) { if(argc != 2) { perror("argc error ,cause itis not 2"); return -1; } int pid = atoi(argv[1]); kill(pid,SIGUSR1); println("pid is:%d",pid); while(1); return 0; } /////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <iostream> #include <sys/types.h> #include <signal.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) void sighandler (int v) { println("sig is==>%d",v); } int main(void) { sighandler_t ptr = signal(SIGUSR1,sighandler); if(ptr == SIG_ERR) { perror("gignal error"); } while(1); return 0; } ### ###消息队列 #include <stdio.h> #include <iostream> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) static key_t g_key = 119; int main(int argc,char *argv[]) { int msgid = msgget(g_key,0666|IPC_CREAT); println(msgid is %d,msgid); char buff[0xff] = "hello proc2 ,i`m proc 1"; msgsnd(msgid,buff,sizeof(buff),0); while(1); return 0; } //////////////////////////////////////////////////////////// #include <stdio.h> #include <iostream> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) static key_t g_key = 119; int main(void) { int msgid = msgget(g_key,0666|IPC_CREAT); println(msgid is %d,msgid); char buff[0xff] ; msgrcv(msgid,buff,sizeof(buff),0,0); println("RECV %s",buff); while(1); return 0; } ### ### 信号量 #include <stdio.h> #include <iostream> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) static key_t g_key = 119; int main(int argc,char *argv[]) { int semid = semget(g_key,0,0); println(semid is %d,semid); semctl(semid,0,SETVAL,99); while(1); return 0; } /////////////////////////////////////////////////////////////// #include <stdio.h> #include <iostream> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) static key_t g_key = 119; int main(void) { int semid = semget(g_key,0,0); println(semid is %d,semid); int result = semctl(semid,0,GETVAL,0); println("recv = %d",result); while(1); return 0; } ### ### 共享内存 #include <stdio.h> #include <iostream> #include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #define format(x) #x "\n" #define println(fmt, var) fprintf(stderr, format(fmt), var) static key_t g_key = 119; int main(int argc, char *argv[]) { int shid = shmget(g_key, 1024, 0666 | IPC_CREAT); char *p = (char *)shmat(shid, nullptr, 0); strcpy(p, "hellosharememory"); while (1) ; return 0; } /////////////////////////////////////////////////////////// #include <stdio.h> #include <iostream> #include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #define format(x) #x "\n" #define println(fmt, var) fprintf(stderr, format(fmt), var) static key_t g_key = 119; int main(void) { int shid = shmget(g_key, 1024, 0666 | IPC_CREAT); char *p = (char *)shmat(shid, nullptr, 0); println("recv====>%s", p); int ret = shmdt(p); if (ret < 0) { perror("shmdt error"); } ret = shmctl(shid, IPC_RMID, nullptr); if (ret < 0) { perror("shmctl error"); } println("be cleaned====%p", p); while (1) ; return 0; } ### ###套接字 #include <stdio.h> #include <iostream> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define format(x) #x "\n" #define println(fmt, var) fprintf(stderr, format(fmt), var) //1 //man nanrenaiwangluodizhi ===>man inet_addr//for head file & ip addr //2 //socket fd //3 //server ipendport //4 //bind //5 //listen //6 //client ipendport //7 //accept //8 //send & recv int main(int argc, char *argv[]) { int serverfd,clientfd; struct sockaddr_in addri,clientipendport; serverfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//域名:domain addri.sin_addr.s_addr = inet_addr("127.0.0.1"); addri.sin_family = AF_INET; addri.sin_port = htons(9000); int ret = bind(serverfd,(const struct sockaddr *)&addri,sizeof(addri)); if(ret <0 ) { perror("bind error"); } ret = listen(serverfd,10); if(ret <0 ) { perror("listen error"); } int len = sizeof(clientipendport); clientfd = accept(serverfd,( sockaddr *)&clientfd,(socklen_t *)&len); if(clientfd <0 ) { perror("accept error"); } char buf []= "hello i`m proc 1"; send(clientfd,buf,sizeof(buf),0); while (1) ; return 0; } ////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <iostream> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #define format(x) #x "\n" #define println(fmt,var) fprintf(stderr,format(fmt),var) int main(void) { int clientfd; struct sockaddr_in addri; addri.sin_addr.s_addr = inet_addr("127.0.0.1"); addri.sin_family = AF_INET; addri.sin_port = htons(9000); clientfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); connect(clientfd,(const sockaddr *)&addri,sizeof(addri)); char buf [0xff]; recv(clientfd,buf,sizeof(buf),0); println(recv is==>%s,buf); while(1); return 0; } ###
标签:总结,format,int,fmt,var,间通信,println,进程,include From: https://www.cnblogs.com/cgybokeyuan/p/16806633.html