• lockf


    lockf( fd, mode, size );

    mode 为 1 时表示加锁,为 0 时表示解锁。

    #include<stdio.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<stdlib.h>
    
    int main()
    {
        pid_t pid;
        int retval;
        char buf[6] = "hello";
        if( (pid = fork() )< 0 )
        {
            printf("fork error
    ");
            exit(-1);
        }
        else if( pid == 0 )
        {
            while(1)
            {
                if( lockf(1,1,0) < 0 )
                {
                    printf("lockf on error
    ");
                    exit(-1);
                }
                sleep(1);
                printf("this is in child!
    ");
                sleep(1);
                printf("this is in child!
    ");
    
                if( lockf(1,0,0) <0 )
                {
                    printf("lockf off error
    ");
                    exit(-1);                    
                }
                sleep(1);
            }
            printf("this is child end
    ");
            exit(-1);
        }
        //wait(&retval);
        while(1)
        {
            if( lockf(1,1,0) < 0 )
            {
                printf("lockf on error
    ");
                exit(-1);
            }
            sleep(1);
            printf("this is in parent!
    ");
            sleep(1);
            printf("this is in parent!
    ");
    
            if( lockf(1,0,0) <0 )
            {
                printf("lockf off error
    ");
                exit(-1);                    
            }
            sleep(1);
        }
        printf("this is parent process end
    ");
        return 0;
    }
  • 相关阅读:
    多姿多彩的线程
    字典操作
    字符串语法
    购物车
    列表常用语法
    整数划分问题
    计算N的阶层
    判断是否是素数
    快速排序
    冒泡排序
  • 原文地址:https://www.cnblogs.com/little-snake/p/4922521.html
Copyright © 2020-2023  润新知