C语言的代参函数,C语言函数参数既做出参又做入参的代表

//使用fcntl对文件进行加锁

#include "stdio.h"

#include "unistd.h"

#include "fcntl.h"

int main()

{

int fd;

struct flock lk;

int r;

fd=open("a.txt", O_RDWR);

if (fd==-1)

{

fd=open("a.txt", O_RDWR|O_CREAT|O_EXCL, 0666);

if (fd==-1)

{

perror("File Open Error");

exit(2);

}

}

lk.l_type=F_WRLCK;

lk.l_whence=SEEK_SET;

lk.l_start=5;

lk.l_len=10;

r=fcntl(fd, F_SETLK, &lk);           //lk在这里是传入参数

if (r==0)

{

printf("Lock Sussess!\n");

}

else

{

printf("Lock Failed!\n");

}

while (1);                         //不能让程序退出

return 0;

}

//使用fcntl读取文件锁

#include "stdio.h"

#include "unistd.h"

#include "fcntl.h"

int main()

{

int fd;

struct flock lk={0};

int r;

fd=open("a.txt", O_RDWR);

if (fd==-1)

{

perror("Error");

exit(0);

}

r=fcntl(fd, F_GETLK, &lk);     //lk在这里是传出参数

if (r==0)

{

printf("Get Lock Success!\n");

}

if (lk.l_type==F_WRLCK)

{

printf("Write Lock!\n");

}

printf("start:%d, len:%d\n", lk.l_start, lk.l_len);

return 0;

}

原文:http://www.cnblogs.com/zhangwuliang/p/4179636.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值