普通网友 2019-05-08 10:28
浏览 50
已采纳

为什么此代码未达到竞争条件?

I have this go code which walks a directory file tree and produces MD5 hashes of every file in it and writes the result in an output file.

package main

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io"
    "os"
    "path/filepath"
    "sync"
)

func main() {

    filePath := os.Args[1]
    output := os.Args[2]
    wg := &sync.WaitGroup{}

    err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
        if !info.IsDir() {
            wg.Add(1)
            go func(path string) {
                md5Sum, _ := md5File(path)
                if err := writeToFile(path, md5Sum, output); err != nil {
                    panic(err)
                }
                wg.Done()
            }(path)
        }
        return nil
    })
    if err != nil {
        panic(err)
    }
    wg.Wait()
}

func md5File(filePath string) (string, error) {
    file, err := os.Open(filePath)
    if err != nil {
        return "", err
    }
    defer file.Close()

    hash := md5.New()
    if _, err := io.Copy(hash, file); err != nil {
        return "", err
    }

    checksum := hash.Sum(nil)

    return string(hex.EncodeToString(checksum)), nil
}

func writeToFile(filePath, md5sum, output string) error {

    file, err := os.OpenFile(output, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755)
    if err != nil {
        return err
    }

    defer file.Close()

    file.WriteString(fmt.Sprintf("%s %s
", md5sum, filePath))

    return file.Sync()
}

From my understanding it is bound to run into a race condition sometime upon writing to the output file but it never does. I mean I have executed this code countless times never facing any issues. It even produces the same result every time.

Why is that? Am I missing something?

UPDATE: When I say it is bound to face a race condition I mean when running multiple goroutines it is possible for more than one goroutine to want to write to file at the same time.

  • 写回答

1条回答 默认 最新

  • dontoften8899 2019-05-08 10:43
    关注

    When I say it is bound to face a race condition I mean when running multiple goroutines it is possible for more than one goroutine to want to write to file at the same time.

    It is not a problem to open a file multiple times. And since you explicitly use O_APPEND the writes don't interact badly with each other. To cite from man open:

    O_APPEND ...
    Before each write(2), the file offset is positioned at the end of the file, as if with lseek(2). The modification of the file offset and the write operation are performed as a single atomic step.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元