• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

            路漫漫,長修遠,我們不能沒有錢
            隨筆 - 173, 文章 - 0, 評論 - 257, 引用 - 0
            數據加載中……

            Linux 進程互斥鎖

               最開始打算用semaphore實現,

            sem_t waitNonEmpty;
            int empty=0; 
            sem_init(&waitNonEmpty, 1, empty); /*初始化信號量*/

            sem_wait(&waitNonEmpty);             /*是否有"非空資源"*/
            sem_post(&waitNonEmpty);              /*生成一個"非滿資源"*/
            sem_destroy(&waitNonEmpty);

            后來在fc3 redhat7.3下man了sem_init, 第二個參數int pshared
            The pshared
                   argument indicates whether the semaphore is local to the  current  pro-
                   cess  ( pshared is zero) or is to be shared between several processes (
                   pshared is not zero). LinuxThreads currently does not support  process-
                   shared  semaphores,  thus  sem_init always returns with error ENOSYS if
                   pshared is not zero.

            最邪惡的現狀出來了, 這倆系統沒有實現sem的進程間通信, 如果設置not zero, 會返回ENOSYS的錯誤...
            后來找了個redhat Enterprise 4 看了下文檔, 貌似又支持了....

            做個記號. 一會兒看看flock
            fcntl 是唯一符合posix的文件鎖

            結構體flock的指針

            struct flcok {
              short int l_type; /* 鎖定的狀態*/

              //這三個參數用于分段對文件加鎖,若對整個文件加鎖,則:l_whence=SEEK_SET,l_start=0,l_len=0;
              short int l_whence;/*決定l_start位置*/
              off_t l_start; /*鎖定區域的開頭位置*/
              off_t l_len; /*鎖定區域的大小*/

              pid_t l_pid; /*鎖定動作的進程*/
            };


            l_type 有三種狀態:
            F_RDLCK 建立一個供讀取用的鎖定
            F_WRLCK 建立一個供寫入用的鎖定
            F_UNLCK 刪除之前建立的鎖定


            int fcntl(int fd, int cmd, struct flock *lock);
            int cmd
              //F_SETLKW對加鎖操作進行阻塞,
              //F_SETLK不對加鎖操作進行阻塞,立即返回

                當使用F_SETLK宏,不進行阻塞加鎖操作時。兩個進程有對文件加鎖失敗的情況出現。
                當使用F_SETLKW宏,進行阻塞加鎖操作時。兩個進程沒有文件加鎖失敗的情況。但是根據進程的輸出日志,可以明顯看出A進程對文件操作時,B進程停止對文件操作。
                測試過程中打開文件使用O_RDWR | O_CREAT | O_APPEND模式,并對文件加寫鎖。鎖對打開文件沒有影響,但是對另一進程加鎖和寫操作有影響。
              加鎖對同一進程中的多線程無效。同一進程中的多線程對同一文件加鎖,后加鎖的線程會覆蓋前一個線程加的文件鎖。
              fcntl文檔中寫到,一個進程對一個文件的區域只能加一個類型的鎖



            實現代碼如下:
            //filelock.h
            #ifndef __FILE_LOCK_HPP__
            #define __FILE_LOCK_HPP__

             
            #ifdef __cplusplus
            extern "C" {
            #endif

             
            int file_lock(int fd);
            int file_unlock(int fd);
             
            #ifdef __cplusplus
            }
            #endif
             
            #endif //__FILE_LOCK_HPP__
             

            ------------------------華麗分割線-----------------------------
            //filelock.cpp
             
            #include <fcntl.h>
            #include <unistd.h>
             
            #include "filelock.h";
             
            int file_lock(int fd){
              struct flock s_flock;
              s_flock.l_type = F_WRLCK;
             
              s_flock.l_whence = SEEK_SET;
              s_flock.l_start = 0;
              s_flock.l_len = 0;
              s_flock.l_pid = getpid();
              
              //F_SETLKW對加鎖操作進行阻塞,
              //F_SETLK不對加鎖操作進行阻塞,立即返回
              return fcntl(fd, F_SETLKW, &s_flock);
            }
             
             
            int file_unlock(int fd){
              return fcntl(fd, F_SETLKW, F_UNLCK);
            }
             

            ---------------------------------華麗的分割線--------------------------
            test.cpp
             
            #include "filelock.h"

            #include <sys/types.h>
            #include <sys/stat.h>
            #include <fcntl.h>

            #include <unistd.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <sys/wait.h>
            #include <errno.h>
             
             
            int main(int argc,char *argv[]) {
              int pid = -1;
             
              int i = 5;
              while(i--){
                if ((pid = fork()) < 0) { //fork出錯
                  puts("fork1 error");
                } else if (pid > 0) {//父進程
                  sleep(5);

                  if (waitpid(pid, NULL, 0) < 0)
                    puts("waitpid error");

                } else {//子進程
                  sleep(1);
                  int li_file = 0;
                  int li_lck_st = -1;
                  li_file = open("tt.txt", O_WRONLY|O_CREAT, 0777);
                  if( li_file < 0 ) {
                    printf("file open error\n");
                  }
                  printf("li_file=[%d] pid=[%d]\n", li_file , getpid() );
                  li_lck_st = file_lock(li_file);
                  sleep(5);
                  printf("li_lck_st=%d pid =%d\n", li_lck_st, getpid() );
                  file_unlock(li_file);
                  close(li_file);
                  printf("close file pid=%d unlock\n", getpid());
                  return 0;
                }
              }
              return 0;
            }
             

            ------------------------------華麗分割線---------------------------
            //makefile

            #########################################################
            #Project: test
            #By: Khan.Lau
            #Description: test
            #########################################################
             
            CC = gcc
            CXX = g++
            STRIP = strip
            AR = ar

            PROJ = $(shell pwd)
            DLLPATH = $(shell find $(PROJ) -name *.so*)

            LIBPATH = /usr
             
            INCS = -I$(LIBPATH)/include \
                -I$(PROJ)/src/inc \
                -I$(PROJ)/src


            CXXINCS = -I$(LIBPATH)/include \
                -I$(LIBPATH)/include/c++/4.3 \
                -I$(LIBPATH)/include/c++/4.3/backward \
                -I$(PROJ)/src/inc \
                -I$(PROJ)/src

             
            CFLAGS = -g -Wall $(CXXINCS) 
            #
            CONFIGURATION = release
            #
            BIN = test.exe
            #LIB = lib/HuaWeiBsc.a
             
            #包路徑
            LIBS = -L$(PROJ)/lib  \
                -L$(LIBPATH)/lib -lz
             
            SRCS = $(wildcard \
                src/lib/*.cpp \
                src/kernel/*.cpp)

             
            OBJBINS = $(SRCS:.cpp=.o)
             
             
            .c.o:
                $(CC) -o $@ -c $< $(CFLAGS)
             
            .cpp.o:
                $(CXX) -o $@ -c $< $(CFLAGS) 
             
             
            COLORH=echo -en "\033[1;32;40m"
            COLORN=echo -en "\033[m"
             
             
            all : $(BIN)

            $(BIN) : $(OBJBINS)
                $(CXX) -o $(CONFIGURATION)/$(BIN) $(OBJBINS) $(LIBS)
                @$(COLORN)     $(shell echo "$(BIN) build complete.")
             
            #$(LIB) : $(OBJBINS)
            #    ar rc $(LIB) $(OBJBINS)

             
            clean :
                rm -f $(OBJBINS)
                rm -f $(BIN)


            -----------------------------華麗分割線--------------------------
            測試環境 ubuntu 8.10,  redhat 7.3, redhat fc3  redhat enterprise 4.6
            結果:
            khan@khan-laptop:~/project/filelock/release$ ./test.exe
            li_file=[3] pid=[10959]
            li_lck_st=0 pid =10959
            close file pid=10959 unlock
            li_file=[3] pid=[10961]
            li_lck_st=0 pid =10961
            close file pid=10961 unlock
            li_file=[3] pid=[10964]
            li_lck_st=0 pid =10964
            close file pid=10964 unlock
            li_file=[3] pid=[10966]
            li_lck_st=0 pid =10966
            close file pid=10966 unlock
            li_file=[3] pid=[10969]
            li_lck_st=0 pid =10969
            close file pid=10969 unlock

            posted on 2008-12-24 10:45 Khan 閱讀(8271) 評論(3)  編輯 收藏 引用 所屬分類: GCC/G++ 、跨平臺開發

            評論

            # re: Linux 進程互斥鎖  回復  更多評論   

            雖然沒有仔細看你寫的代碼,但從設計思路上也能看出一些端倪出來。

            flock實現的文件鎖實際上是一個建議鎖。也就是意味著在這個鎖很容易
            收到外界的干擾。如果用于鎖定的文件被某人/某進程刪除了,那使用
            這個文件鎖的程序就很容易出現紊亂。這點也需要考慮。

            從你man出來的信息來看
            LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.
            這說的應該指的是LinuxThread吧,指的不是進程。
            sema設計的初衷就是為了讓進程間進行通信,信號量如果不能共享那也就不能進行通信是吧。所以,我認為你應該放心的去用sema。至少是目前版本的大多數的linux系統都沒有問題。這僅僅是俺一家之言,如有不對之處請斧正。
            2008-12-24 17:32 | lymons

            # re: Linux 進程互斥鎖  回復  更多評論   

            我這里沒有使用flock 直接使用了fcntl, 貌似flock也是用fcntl來實現的.
            我猜測 LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.這句可能說LinuxThreads 這部分負責實現semaphores, 但是當前的版本沒有實現進程間共享信號燈的部分,

            實際我在我系統里面測試, 確實是沒有返回ENOSYS, 但是sem_wait在semaphore大于0的情況下這里根本就沒有阻塞
            2008-12-24 17:50 | Khan.Lau

            # re: Linux 進程互斥鎖  回復  更多評論   

            flock就是用fcntl系統調用實現的。
            因為在linux內核里fcntl就是按照advise模式來實現,所以他一直是一個建議鎖。

            我建議你可以使用sys v的信號量,在Linux下這畢竟比posix的更常用一些。
            如,使用semget(2)來創建信號量,利用semop(2)來做PV操作。
            2008-12-25 10:43 | lymons
            熟妇人妻久久中文字幕| 久久人人爽人人精品视频| 国内高清久久久久久| 亚洲AV日韩AV永久无码久久| 久久亚洲国产成人精品性色| 91精品国产色综久久| 久久综合五月丁香久久激情| 久久精品人人做人人爽电影| 亚洲国产精品久久久久久| 色天使久久综合网天天| 久久精品水蜜桃av综合天堂| 久久久久久久国产免费看| 国产亚洲欧美精品久久久| 亚洲欧洲精品成人久久曰影片| 国内精品久久久人妻中文字幕| 久久精品二区| 久久香蕉国产线看观看99| 色欲久久久天天天综合网精品| 精品久久久久久久中文字幕| 精品久久久久香蕉网| 久久精品国产男包| 无码人妻久久一区二区三区蜜桃| 2020久久精品国产免费| 日韩欧美亚洲综合久久 | 久久久久久无码国产精品中文字幕 | 国产一区二区三区久久精品| 久久婷婷色综合一区二区| 亚洲精品蜜桃久久久久久| 无码任你躁久久久久久久| 久久精品国产亚洲网站| 久久久久亚洲av无码专区喷水| 中文字幕无码久久精品青草| 久久亚洲高清综合| 亚洲国产一成久久精品国产成人综合| 精品一久久香蕉国产线看播放| 2021久久国自产拍精品| 99久久久精品免费观看国产| 久久精品人人做人人爽电影蜜月| 欧洲人妻丰满av无码久久不卡| 久久国产免费直播| 亚洲精品美女久久久久99|