4. 进/线程利用futex同步
进程或者线程都可以利用futex来进行同步。
对于线程,情况比较简单,因为线程共享虚拟内存空间,虚拟地址就可以唯一的标识出futex变量,即线程用同样的虚拟地址来访问futex变量。
对 于进程,情况相对复杂,因为进程有独立的虚拟内存空间,只有通过mmap()让它们共享一段地址空间来使用futex变量。每个进程用来访问futex的 虚拟地址可以是不一样的,只要系统知道所有的这些虚拟地址都映射到同一个物理内存地址,并用物理内存地址来唯一标识futex变量。
小结:
1. Futex变量的特征:1)位于共享的用户空间中 2)是一个32位的整型 3)对它的操作是原子的
2. Futex在程序low-contention的时候能获得比传统同步机制更好的性能。
3. 不要直接使用Futex系统调用。
4. Futex同步机制可以用于进程间同步,也可以用于线程间同步。
下面给出一份示例代码。
/*
* This sample show how to use futex betwen two process, and use system v
* shared memory to store data
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#if __GLIBC_PREREQ(2, 3)
#if defined FUTEX_WAIT || defined FUTEX_WAKE
#include <linux/futex.h>
#else
#define FUTEX_WAIT 0
#define FUTEX_WAKE 1
#endif
#ifndef __NR_futex
#define __NR_futex 202
#endif
#endif
#define FILE_MODE (S_IRUSR | S_IWUSR)
const char shmfile[] = "/tmp";
const int size = 100;
struct namelist
{
int id;
char name[20];
};
int
main(void)
{
int fd, pid, status;
int *ptr;
struct stat stat;
// create a Posix shared memory
int flags = O_RDWR | O_CREAT;
fd = shm_open(shmfile, flags, FILE_MODE);
if (fd < 0)
{
printf("shm_open failed, errormsg=%s errno=%d", strerror(errno), errno);
return 0;
}
ftruncate(fd, size);
ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
pid = fork();
if (pid == 0) { // child process
sleep(5);
printf("Child %d: start\n", getpid());
fd = shm_open(shmfile, flags, FILE_MODE);
fstat(fd, &stat);
ptr = (int *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
struct namelist tmp;
// store total num in ptr[0];
*ptr = 3;
namelist *cur = (namelist *)(ptr+1);
// store items
tmp.id = 1;
strcpy(tmp.name, "Nellson");
*cur++ = tmp;
tmp.id = 2;
strcpy(tmp.name, "Daisy");
*cur++ = tmp;
tmp.id = 3;
strcpy(tmp.name, "Robbie");
*cur++ = tmp;
printf("wake up parent\n");
syscall(__NR_futex ,ptr, FUTEX_WAKE, 1, NULL );
exit(0);
} else{ // parent process
printf("parent start waiting\n");
syscall(__NR_futex , ptr, FUTEX_WAIT, *(int *)ptr, NULL );
printf("parent end waiting\n");
struct namelist tmp;
int total = *ptr;
printf("\nThere is %d item in the shm\n", total);
ptr++;
namelist *cur = (namelist *)ptr;
for (int i = 0; i< total; i++) {
tmp = *cur;
printf("%d: %s\n", tmp.id, tmp.name);
cur++;
}
printf("\n");
waitpid(pid, &status, 0);
}
// remvoe a Posix shared memory from system
printf("Parent %d get child status:%d\n", getpid(), status);
return 0;
}
------------------分割线------------------
root:/home/ftpuser/ipc#g++ -o futex -lrt futex.cc
root:/home/ftpuser/ipc#./futex
parent start waiting
Child 2825: start
wake up parent
parent end waiting
There is 3 item in the shm
1: Nellson
2: Daisy
3: Robbie
Parent 2824 get child status:0