共享内存mmap(父子进程)
main.cpp
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
typedef struct{
char name[16];
int age;
}people;
const char *names[]={"tom","lily","jerry","david","garrett"};
int main(int argc, char** argv)
{
int i;
people *p_map;
p_map=(people*)mmap(NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS,-1,0);
if(fork() == 0)//儿子进程
{
sleep(2);
for(i = 0;i<5;i++)
printf("child read: the %d people's age is %d,name is %s\n",i+1,(*(p_map+i)).age,
p_map[i].name);
(*p_map).age = 100;
munmap(p_map,sizeof(people)*10); //实际上,进程终止时,会自动解除映射。
return 0;
}
else
{
for(i = 0;i<5;i++)
{
memcpy((*(p_map+i)).name, names[i],strlen(names[i]));
(*(p_map+i)).age=20+i;
}
sleep(5);
printf( "parent read: the first people's age is %d\n",(*p_map).age );
printf("umap\n");
munmap( p_map,sizeof(people)*10 );
printf( "umap ok\n" );
return 0;
}
}
makefile:
overpassb:main.o
g++ -g -o overpassb main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpassb
共享内存posix(父子进程)
main.cpp
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<signal.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
int shmID;
char * buf;
void finalize(int signo)
{
shmctl(shmID,IPC_RMID,NULL);
exit(0);
}
int main()
{
int i = 1;
key_t shmKey;
signal(SIGINT,finalize);
signal(SIGTERM,finalize);
if(fork() == 0) //子进程
{
shmKey = ftok("/dev/null",1); //可以使用任何大于0的数字,如果名字和数相同,则产生的key相同。
if(shmKey == -1)
{
printf("create key error\n");
exit(-1);
}
shmID = shmget(shmKey,20,IPC_CREAT | IPC_EXCL | 0666);
if(shmID == -1)
{
printf("create shamID error\n");
exit(-1);
}
//sleep(2); //等待父进程执行,好显示第一行为空。
while(1)
{
buf = (char *)shmat(shmID,NULL,0);//shmat负责把共享的物理内存attach到当前进程空间buf
srandom(time(NULL));
sprintf(buf,"%ld",random()%100);
shmdt(buf); //shmdt负责把共享内存从当前进程空间detach
}
}
else //父进程
{
sleep(1); //让子进程先执行,以建立内存映射。
shmKey = ftok("/dev/null",1); //可以使用任何大于0的数字,如果名字和数相同,则产生的key相同。
if(shmKey == -1)
{
printf("create key error\n");
exit(-1);
}
shmID = shmget(shmKey,20,0); //得到shmKey表示的共享内存的ID,0表示如果shmKey映射的不存在则报错。
if(shmID == -1)
{
printf("create shmID error\n");
exit(-1);
}
while(1)
{
buf = (char *)shmat(shmID,NULL,0);
printf("%d. the content of the shared memory is: %s\n",i++,buf);
shmdt(buf);
sleep(1);
}
}
return 0;
}
makefile:
overpass:main.o
g++ -g -o overpass main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpass
共享内存posix(两个进程)
main.cpp
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<signal.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
int shmID;
char * buf;
void finalize(int signo)
{
shmctl(shmID,IPC_RMID,NULL);
exit(0);
}
int main()
{
int i = 1;
key_t shmKey;
signal(SIGINT,finalize);
signal(SIGTERM,finalize);
shmKey = ftok("/dev/null",1); //可以使用任何大于0的数字,如果名字和数相同,则产生的key相同。
if(shmKey == -1)
{
printf("create key error\n");
exit(-1);
}
shmID = shmget(shmKey,20,IPC_CREAT | IPC_EXCL | 0666);
if(shmID == -1)
{
printf("create shamID error\n");
exit(-1);
}
//sleep(2); //等待父进程执行,好显示第一行为空。
while(1)
{
buf = (char *)shmat(shmID,NULL,0);//shmat负责把共享的物理内存attach到当前进程空间buf
srandom(time(NULL));
sprintf(buf,"%ld",random()%100);
shmdt(buf); //shmdt负责把共享内存从当前进程空间detach
}
return 0;
}
makefile:
overpass1:main.o
g++ -g -o overpass1 main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpass
=============================
main.cpp
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<signal.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
int shmID;
char * buf;
void finalize(int signo)
{
shmctl(shmID,IPC_RMID,NULL);
exit(0);
}
int main()
{
int i = 1;
key_t shmKey;
signal(SIGINT,finalize);
signal(SIGTERM,finalize);
sleep(1); //让子进程先执行,以建立内存映射。
shmKey = ftok("/dev/null",1); //可以使用任何大于0的数字,如果名字和数相同,则产生的key相同。
if(shmKey == -1)
{
printf("create key error\n");
exit(-1);
}
shmID = shmget(shmKey,20,0); //得到shmKey表示的共享内存的ID,0表示如果shmKey映射的不存在则报错。
if(shmID == -1)
{
printf("create shmID error\n");
exit(-1);
}
while(1)
{
buf = (char *)shmat(shmID,NULL,0);
printf("%d. the content of the shared memory is: %s\n",i++,buf);
shmdt(buf);
sleep(1);
}
return 0;
}
makefile:
overpass2:main.o
g++ -g -o overpass2 main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpass
管道通信
a进程
main.cpp
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int fd;
char buf[20] = "hello world!!!\n";
remove("/tmp/my_fifo");
if((mkfifo("/tmp/my_fifo", O_CREAT|O_RDWR|0666)) < 0)
{
perror("mkfifo");
exit(1);
}
if((fd = open("/tmp/my_fifo" , O_WRONLY)) < 0)
{
perror("open");
exit(1);
}
if((write(fd,buf,strlen(buf))) < 0)
{
perror("write");
exit(1);
}
close(fd);
return 0;
}
makefile:
overpassa:main.o
g++ -g -o overpassa main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpassa
=========================
b进程
main.cpp
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int fd;
char buf[20] = "";
if((fd = open("/tmp/my_fifo" , O_RDONLY)) < 0)
{
perror("open");
exit(1);
}
if((read(fd,buf,20)) < 0)
{
perror("read");
exit(1);
}
printf("%s" , buf);
close(fd);
return 0;
}
makefile
overpassb:main.o
g++ -g -o overpassb main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpassb
匿名管道(父子进程)
main.cpp
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <string.h>
int main()
{
int n,fd[2]; // fd[0]:read,fd[1]:write 这里的fd是文件描述符的数组,用于创建管道做准备的
int fd2[2];
pid_t pid;
char line[100]={0};
char buff[100]={0};
if(pipe(fd)<0 || pipe(fd2)<0) // 创建匿名管道
{
printf("pipe create error\n");
return -1;
}
if(fork()==0)
{
//close(fd[1]); //这里是子进程,先关闭管道的写入端,然后在管道的读出端读出数据
n= read(fd[0],line,100);
//close(fd2[0]);
write(fd2[1],"child:hello world\n",strlen("child:hello world\n")+1);
printf("\n%s",line);
return 0;
}
else
{ //这里是父进程,先关闭管道的读出端,然后在管道的写端写入“hello world"
//close(fd[0]);
write(fd[1],"parent:hello word\n",strlen("parent:hello word\n")+1);
//close(fd2[1]);
read(fd2[0],buff,100);
printf("\n%s",buff);
return 0;
}
}
makefile:
overpass:main.o
g++ -g -o overpass main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpass
信号量通信(父子进程)
main.cpp
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXNUMS 15
union semun
{
int val; /* Value for SETVAL */
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) */
};
int get_sem_val(int sid,int semnum)//取得当前信号量
{
return(semctl(sid,semnum,GETVAL,0));
}
int main(void)
{
int sem_id;
int pid;
int rc;
struct sembuf sem_op;//信号集结构
union semun sem_val;//信号量数值
//建立信号量集,其中只有一个信号量,从0开始计数,第1个信号量
sem_id=semget(IPC_PRIVATE,1,IPC_CREAT|0600);//IPC_PRIVATE私有,只有本用户使用,如果为正整数,则为公共的;1为信号集的数量,一般总为1;
if (sem_id==-1){
printf("create sem error!\n");
exit(1);
}
printf("create %d sem success!\n",sem_id);
//信号量初始化
sem_val.val=1;
rc=semctl(sem_id,0,SETVAL,sem_val);//设置信号量,0为第一个信号量,1为第二个信号量,...以此类推;SETVAL表示设置
if (rc==-1){
printf("initlize sem error!\n");
exit(1);
}
//创建进程
pid=fork();
if (pid==-1){
printf("fork error!\n");
exit(1);
}
else if(pid==0){//一个子进程,消费者,P操作
sleep(20);
for (int i=0;i<MAXNUMS;i++){
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
//P操作信号量,每次-1,当sem_id中的值减到0之后,P操作阻塞
semop(sem_id,&sem_op,1);
printf("\n%d consumer: %d",i,get_sem_val(sem_id,0));
}
}
else{//父进程,生产者
for (int i=0;i<MAXNUMS;i++){
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_id,&sem_op,1);//V操作信号量,每次+1
printf("%d producer: %d\n",i,get_sem_val(sem_id,0));
}
}
exit(0);
}
makefile:
overpass:main.o
g++ -g -o overpass main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpass
信号量通信(不同进程)
a进程
main.cpp
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXNUMS 15
union semun
{
int val; /* Value for SETVAL */
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) */
};
int get_sem_val(int sid,int semnum)//取得当前信号量
{
return(semctl(sid,semnum,GETVAL,0));
}
void sem_lock(int sem_id,struct sembuf *sem_op)
{
sem_op->sem_num=0;
sem_op->sem_op=-1;
sem_op->sem_flg=0;
//P操作信号量,每次-1,当sem_id中的值减到0之后,P操作阻塞
semop(sem_id,sem_op,1);
}
void sem_unlock(int sem_id,struct sembuf *sem_op)
{
sem_op->sem_num=0;
sem_op->sem_op=1;
sem_op->sem_flg=0;
semop(sem_id,sem_op,1);//V操作信号量,每次+1
}
int main(void)
{
int sem_id;
int pid;
int rc;
struct sembuf sem_op;//信号集结构
union semun sem_val;//信号量数值
key_t semKey;
semKey = ftok("/dev/null",1); //可以使用任何大于0的数字,如果名字和数相同,则产生的key相同。
if(semKey == -1)
{
printf("create key error\n");
exit(-1);
}
//建立信号量集,其中只有一个信号量,从0开始计数,第1个信号量
sem_id=semget(semKey,1,IPC_CREAT|0600);//IPC_PRIVATE私有,只有本用户使用,如果为正整数,则为公共的;1为信号集的数量,一般总为1;
if (sem_id==-1){
printf("create sem error!\n");
exit(1);
}
printf("create %d sem success!\n",sem_id);
//信号量初始化
sem_val.val=1;
rc=semctl(sem_id,0,SETVAL,sem_val);//设置信号量,0为第一个信号量,1为第二个信号量,...以此类推;SETVAL表示设置
if (rc==-1){
printf("initlize sem error!\n");
exit(1);
}
printf("waiting for the lock in process1\n");
sem_lock(sem_id, &sem_op);
printf("locked in process1\n");
sleep(25);
sem_unlock(sem_id,&sem_op);
printf("unlocked in process1\n");
//进程1生产者
/*
for (int i=0;i<MAXNUMS;i++){
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_id,&sem_op,1);//V操作信号量,每次+1
printf("%d producer: %d\n",i,get_sem_val(sem_id,0));
}
*/
exit(0);
}
makefile:
overpass1:main.o
g++ -g -o overpass1 main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpass
==============
b进程
main.cpp
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXNUMS 15
union semun
{
int val; /* Value for SETVAL */
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 sem_lock(int sem_id,struct sembuf *sem_op)
{
sem_op->sem_num=0;
sem_op->sem_op=-1;
sem_op->sem_flg=0;
//P操作信号量,每次-1,当sem_id中的值减到0之后,P操作阻塞
semop(sem_id,sem_op,1);
}
void sem_unlock(int sem_id,struct sembuf *sem_op)
{
sem_op->sem_num=0;
sem_op->sem_op=1;
sem_op->sem_flg=0;
semop(sem_id,sem_op,1);//V操作信号量,每次+1
}
int get_sem_val(int sid,int semnum)//取得当前信号量
{
return(semctl(sid,semnum,GETVAL,0));
}
int main(void)
{
int sem_id;
int pid;
int rc;
struct sembuf sem_op;//信号集结构
key_t semKey;
semKey = ftok("/dev/null",1); //可以使用任何大于0的数字,如果名字和数相同,则产生的key相同。
if(semKey == -1)
{
printf("create key error\n");
exit(-1);
}
//建立信号量集,其中只有一个信号量,从0开始计数,第1个信号量
sem_id=semget(semKey,1,IPC_CREAT|0600);//IPC_PRIVATE私有,只有本用户使用,如果为正整数,则为公共的;1为信号集的数量,一般总为1;
if (sem_id==-1){
printf("create sem error!\n");
exit(1);
}
printf("create %d sem success!\n",sem_id);
printf("waiting for the lock in process2\n");
sem_lock(sem_id, &sem_op);
printf("locked in process2\n");
sleep(25);
sem_unlock(sem_id,&sem_op);
printf("unlocked in process2\n");
/*
for (int i=0;i<MAXNUMS;i++){
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
//P操作信号量,每次-1,当sem_id中的值减到0之后,P操作阻塞
semop(sem_id,&sem_op,1);
printf("\n%d consumer: %d",i,get_sem_val(sem_id,0));
}
*/
exit(0);
}
makefile:
overpass2:main.o
g++ -g -o overpass2 main.o
main.o:main.cpp
g++ -g -c main.cpp -o main.o
clean:
rm -f *.o overpass