C语言函数参数既做出参又做入参的代表
生活随笔
收集整理的這篇文章主要介紹了
C语言函数参数既做出参又做入参的代表
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//使用fcntl對(duì)文件進(jìn)行加鎖 #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在這里是傳入?yún)?shù)
??? if?(r==0)
????{
???????? printf("Lock?Sussess!\n");
??? }
??? else
??? {
??????? printf("Lock?Failed!\n");
??? }
??? while?(1);???????????????????????? //不能讓程序退出
??? return?0;
} //使用fcntl讀取文件鎖
{
??? 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在這里是傳出參數(shù)
??? 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;
}
#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在這里是傳入?yún)?shù)
??? if?(r==0)
????{
???????? printf("Lock?Sussess!\n");
??? }
??? else
??? {
??????? printf("Lock?Failed!\n");
??? }
??? while?(1);???????????????????????? //不能讓程序退出
??? return?0;
} //使用fcntl讀取文件鎖
#include "stdio.h"
#include "unistd.h"
{
??? 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在這里是傳出參數(shù)
??? 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;
}
轉(zhuǎn)載于:https://www.cnblogs.com/zhangwuliang/p/4179636.html
總結(jié)
以上是生活随笔為你收集整理的C语言函数参数既做出参又做入参的代表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSON.stringify()中文乱码
- 下一篇: angular 自定义指令参数详解