多线程同步机制

1, spinlock

适合短暂的等待,不值得休眠的那种短暂.

  jmp 11b
12:
get_lock:
    lock bts $0, 0x6000
    jc get_lock
  lock incw smp_cpus
  mov $smp_cpus, %bx
  lock btr $0, 0x6000    /*release lock*/
  mov 0(%bx), %si

  ...

smp_ap_boot_code_end:

这是每个核启动后,根据smp_cpus获取自己的核编号,从而在屏幕上定位不同的"点",然后循环递增其ascii码.

2, atomic_t

这是linux内核提供的原子数,相应的有atomic_inc/dec()一系列方法.

x86上,其内部实现依赖于lock指令前缀.

3,semaphore

sem_init(); sem_wait(); sem_post(); sem_destroy();

如果想fork()后仍然使用,需要利用共享内存区.

另外每次post之后,只唤醒队列里的一个进程.因为等待队列里的进程肯定都是因为sem_wait()进入的,他们自然都会调用sem_post()叫"下一个".

SO上的一篇特别好:

https://stackoverflow.com/questions/16400820/how-to-use-posix-semaphores-on-forked-processes-in-c

How to use POSIX semaphores on forked processes in C?

I want to fork multiple processes and then use a semaphore on them. Here is what I tried:

sem_init(&sem, 1, 1);   /* semaphore*, pshared, value */
.
.
.
if(pid != 0){ /* parent process */
    wait(NULL); /* wait all child processes */

    printf("\nParent: All children have exited.\n");
    .
    .
    /* cleanup semaphores */
    sem_destroy(&sem);
    exit(0);
}
else{ /* child process */
    sem_wait(&sem);     /* P operation */
    printf("  Child(%d) is in critical section.\n",i);
    sleep(1);
    *p += i%3;  /* increment *p by 0, 1 or 2 based on i */
    printf("  Child(%d) new value of *p=%d.\n",i,*p);
    sem_post(&sem);     /* V operation */
    exit(0);
}.....

一个高赞回答:

The problem you are facing is the misunderstanding of sem_init() function. When you read the manual page you will see this:

The pshared argument indicates whether this semaphore is to be shared between the threads of a process, or between processes.

If you are done reading up to this point, you will think that the non-zero value of pshared will make the semaphore inter-process semaphore. However, this is wrong. You should continue reading and you‘ll understand that you have to locate the semaphore in a shared memory region. To do that, several functions can be used as you can see below:

If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)). (Since a child created by fork(2) inherits its parent‘s memory mappings, it can also access the semaphore.) Any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), etc.

I find this approach as a more complicated approach than others, therefore I want to encourage people to use sem_open() instead of sem_init().

Below you can see a complete program illustrates the following:

  • How to allocate shared memory and use shared variables between forked processes.
  • How to initialize a semaphore in a shared memory region and is used by multiple processes.
  • How to fork multiple processes and make the parent wait until all of its children exit.
#include <stdio.h>          /* printf()                 */
#include <stdlib.h>         /* exit(), malloc(), free() */
#include <sys/types.h>      /* key_t, sem_t, pid_t      */
#include <sys/shm.h>        /* shmat(), IPC_RMID        */
#include <errno.h>          /* errno, ECHILD            */
#include <semaphore.h>      /* sem_open(), sem_destroy(), sem_wait().. */
#include <fcntl.h>          /* O_CREAT, O_EXEC          */

int main (int argc, char **argv){
    int i;                        /*      loop variables          */
    key_t shmkey;                 /*      shared memory key       */
    int shmid;                    /*      shared memory id        */
    sem_t *sem;                   /*      synch semaphore         *//*shared */
    pid_t pid;                    /*      fork pid                */
    int *p;                       /*      shared variable         *//*shared */
    unsigned int n;               /*      fork count              */
    unsigned int value;           /*      semaphore value         */

    /* initialize a shared variable in shared memory */
    shmkey = ftok ("/dev/null", 5);       /* valid directory name and a number */
    printf ("shmkey for p = %d\n", shmkey);
    shmid = shmget (shmkey, sizeof (int), 0644 | IPC_CREAT);
    if (shmid < 0){                           /* shared memory error check */
        perror ("shmget\n");
        exit (1);
    }

    p = (int *) shmat (shmid, NULL, 0);   /* attach p to shared memory */
    *p = 0;
    printf ("p=%d is allocated in shared memory.\n\n", *p);

    /********************************************************/

    printf ("How many children do you want to fork?\n");
    printf ("Fork count: ");
    scanf ("%u", &n);

    printf ("What do you want the semaphore value to be?\n");
    printf ("Semaphore value: ");
    scanf ("%u", &value);

    /* initialize semaphores for shared processes */
    sem = sem_open ("pSem", O_CREAT | O_EXCL, 0644, value);
    /* name of semaphore is "pSem", semaphore is reached using this name */

    printf ("semaphores initialized.\n\n");

    /* fork child processes */
    for (i = 0; i < n; i++){
        pid = fork ();
        if (pid < 0) {
        /* check for error      */
            sem_unlink ("pSem");
            sem_close(sem);
            /* unlink prevents the semaphore existing forever */
            /* if a crash occurs during the execution         */
            printf ("Fork error.\n");
        }
        else if (pid == 0)
            break;                  /* child processes */
    }

    /******************************************************/
    /******************   PARENT PROCESS   ****************/
    /******************************************************/
    if (pid != 0){
        /* wait for all children to exit */
        while (pid = waitpid (-1, NULL, 0)){
            if (errno == ECHILD)
                break;
        }

        printf ("\nParent: All children have exited.\n");

        /* shared memory detach */
        shmdt (p);
        shmctl (shmid, IPC_RMID, 0);

        /* cleanup semaphores */
        sem_unlink ("pSem");
        sem_close(sem);
        /* unlink prevents the semaphore existing forever */
        /* if a crash occurs during the execution         */
        exit (0);
    }

    /******************************************************/
    /******************   CHILD PROCESS   *****************/
    /******************************************************/
    else{
        sem_wait (sem);           /* P operation */
        printf ("  Child(%d) is in critical section.\n", i);
        sleep (1);
        *p += i % 3;              /* increment *p by 0, 1 or 2 based on i */
        printf ("  Child(%d) new value of *p=%d.\n", i, *p);
        sem_post (sem);           /* V operation */
        exit (0);
    }
}
 

另外关注sem_open()这个函数.

原文地址:https://www.cnblogs.com/xiang-yin/p/12114209.html

时间: 2024-07-31 14:35:07

多线程同步机制的相关文章

Java多线程同步机制

Java的多线程同步机制和其他语言开发的是一样的,在当前线程中,遇到某个事件时,等待另一个线程运行结束或者另一个线程的事件,然后再决定如何处理. 本例来自书上的实例,精简了代码,调整了部分逻辑,使得看起来更加简洁明了.已经运行通过. 代码如下: package SwingExample; import java.awt.BorderLayout; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPro

多进程间通信方式和多线程同步机制总结

多进程之间通信方式: 文件映射:本地之间 共享内存:本地之间 匿名管道:本地之间 命名管道:跨服务器 邮件槽:一对多的传输数据,通常通过网络向一台Windows机器传输 剪切板:本地之间 socket:跨服务器 多线程之间通信方式: 全局变量 自定义消息响应 多线程之间同步机制:           临界区:不可以跨进程,忘记解锁会无限等待,要么存在要么没有,多线程访问独占性共享资源 互斥量:可以跨进程,忘记解锁会自动释放,要么存在要么没有 事件:又叫线程触发器,不可以跨进程,要么存在要么没有,

Linux 多线程同步机制:互斥量、信号量、条件变量

互斥量:互斥量提供对共享资源的保护访问,它的两种状态:lock和unlock,用来保证某段时间内只有一个线程使用共享资源,互斥量的数据类型是pthread_mutex_t主要涉及函数:pthread_mutex_lock() pthread_mutex_trylock() pthread_mutex_unlock()Pthreaf_mutex_init() pthread_mutex_destroy()lock与unlock之间所锁定的区域为临界区域(如果只加锁不解锁程序会阻塞等待)信号量:信号

python多线程同步机制Semaphore

#!/usr/bin/env python # -*- coding: utf-8 -*- """ Python 线程同步机制:Semaphore """ import time import threading import random # 信号量同步基于内部计数器,每调用一次acquire(),计数器减1:每调用一次release(),计数器加1.当计数器为0时,acquire()调用被阻塞. sema = threading.Semaph

Linux多线程同步机制

http://blog.163.com/he_junwei/blog/static/19793764620141711130253/ http://blog.csdn.net/h_armony/article/details/6766505  一.互斥锁 尽管在Posix Thread中同样可以使用IPC的信号量机制来实现互斥锁mutex功能,但显然semphore的功能过于强大了,在Posix Thread中定义了另外一套专门用于线程同步的mutex函数. 1. 创建和销毁 有两种方法创建互斥

多线程同步机制的几种方法

Critical SectionCritical section(临界区)用来实现“排他性占有”.适用范围是单一进程的各线程之间.它是: 一个局部性对象,不是一个核心对象. 快速而有效率. 不能够同时有一个以上的 critical section 被等待. 无法侦测是否已被某个线程放弃. 可以实现线程间互斥,不能用来实现同步. SemaphoreSemaphore 被用来追踪有限的资源.它是:一个核心对象. 没有拥有者. 可以具名,因此可以被其他进程开启. 可以被任何一个线程释放(release

python多线程同步机制condition

#!/usr/bin/env python# -*- coding: utf-8 -*- import threadingimport time def customer(cond): t = threading.currentThread() with cond: # wait()方法创建了一个名为waiter的锁,并且设置锁的状态为locked.这个waiter锁用于线程间的通讯 cond.wait() print '{}: Resource is available to consumer

python多线程同步机制Lock

#!/usr/bin/env python# -*- coding: utf-8 -*- import threadingimport time value = 0lock = threading.Lock() def add(): global value with lock: new_value = value + 1 time.sleep(0.001) value = new_value if __name__ == '__main__': threads = [] for i in ra

Java多线程的同步机制(synchronized)

一段synchronized的代码被一个线程执行之前,他要先拿到执行这段代码的权限,在 java里边就是拿到某个同步对象的锁(一个对象只有一把锁): 如果这个时候同步对象的锁被其他线程拿走了,他(这个线程)就只能等了(线程阻塞在锁池 等待队列中). 取到锁后,他就开始执行同步代码(被synchronized修饰的代码):线程执行完同步代码后马上就把锁还给同步对象,其他在锁池中 等待的某个线程就可以拿到锁执行同步代码了.这样就保证了同步代码在统一时刻只有一个线程在执行. 众所周知,在Java多线程