码迷,mamicode.com
首页 > 其他好文 > 详细

60>>lockf

时间:2015-10-30 10:50:12      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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\n");
        exit(-1);
    }
    else if( pid == 0 )
    {
        while(1)
        {
            if( lockf(1,1,0) < 0 )
            {
                printf("lockf on error\n");
                exit(-1);
            }
            sleep(1);
            printf("this is in child!\n");
            sleep(1);
            printf("this is in child!\n");

            if( lockf(1,0,0) <0 )
            {
                printf("lockf off error\n");
                exit(-1);                    
            }
            sleep(1);
        }
        printf("this is child end\n");
        exit(-1);
    }
    //wait(&retval);
    while(1)
    {
        if( lockf(1,1,0) < 0 )
        {
            printf("lockf on error\n");
            exit(-1);
        }
        sleep(1);
        printf("this is in parent!\n");
        sleep(1);
        printf("this is in parent!\n");

        if( lockf(1,0,0) <0 )
        {
            printf("lockf off error\n");
            exit(-1);                    
        }
        sleep(1);
    }
    printf("this is parent process end\n");
    return 0;
}

 

60>>lockf

标签:

原文地址:http://www.cnblogs.com/yulaoqiannian/p/4922521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!