日期:2014-05-16  浏览次数:20867 次

本人初学linux,那位高手帮我修改下下面的程序,错误提示已经列出来了,谢谢。
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

//sets lock of type on descriptor fd
void setlock(int fd,int type);
int main(int argc,char *argc[])
{
int fd;
// open the file
fd = open (argv[1],O_RDWR | O_CREAT,0666);
if(fd < 0)
{
perror("open");
exit(EXIT_FAILURE);
}
//Set read lock
setlock(fd,F_RDLCK);
printf("PID %d read locked %s\n",getpid(),argv[1]);
getchar();
//unlock
setlock(fd,F_UNLCK);
printf("PID %d unlocked %s\n",getpid(),argv[1]);
getchar();

//set write lock
setlock(fd,F_WRLCK);
printf("PID %d write locked %s\n",getpid(),argv[1]);
  getchar();
close(fd);
exit(EXIT_SUCCESS);
}
void setlock(int fd,int type)
{
struct flock lock;
char msg[80];

// describe the lock we want
lock.1_whence = SEEK_SET; //???
lock.1_start = 0;
lock.1_len = 1; //lock a single byte;
while(1)
{
//这边的while循环要一直到获得锁为止才推出
lock.1_type = type;
//set the lock and return caller
// 这边的参数什么意思??
if((fcntl(fd,F_SETLK,&lock)) == 0)
{
return;
}

//find out why we couldn't set the lock;
fcntl(fd,F_GETLK,&lock);
if(lock.1_type != F_UNLCK)
{
switch(lock.1_type)
{
case (F_RDLCK):
sprintf(msg,"read lock already set by %d\n",lock.1_pid);
break;
case (F_WRLCK):
sprintf(msg,"write lock alread set by %d\n",lock.1_pid);
break;
}
puts(msg);
getchar();
}
}


}


/*
[root@localhost lockit]# make
gcc -c lockit.c
lockit.c:10: error: conflicting types for 'argc'
lockit.c:10: error: previous definition of 'argc' was here
lockit.c: In function 'main':
lockit.c:14: error: 'argv' undeclared (first use in this function)
lockit.c:14: error: (Each undeclared identifier is reported only once
lockit.c:14: error: for each function it appears in.)
lockit.c:14: error: 'O_REWR' undeclared (first use in this function)
lockit.c:42:6: error: invalid suffix "_whence" on floating constant
lockit.c: In function 'setlock':
lockit.c:42: error: expected ';' before numeric constant
lockit.c:43:6: error: invalid suffix "_start" on floating constant
lockit.c:43: error: expected ';' before numeric constant
lockit.c:44:6: error: invalid suffix "_len" on floating constant
lockit.c:44: error: expected ';' before numeric constant
lockit.c:48:7: error: invalid suffix "_type" on floating constant
lockit.c:48: error: expected ';' before numeric constant
lockit.c:58:13: error: invalid suffix "_type" on floating constant
lockit.c:58: error: used struct type value where scalar is required
lockit.c:58: error: expected ')' before numeric constant
lockit.c:60:15: error: invalid suffix "_type" on floating constant
lockit.c:60: error: expected ')' before numeric constant
lockit.c:60: error: switch quantity not an integer
lockit.c:63:53: error: invalid suffix "_pid" on floating constant
lockit.c:63: error: expected ')' before numeric constant
lockit.c:66:53: error: invalid suffix "_pid" on floating constant
lockit.c:66: error: expected ')' before numeric constant
make: *** [lockit.o] Error 1
*/

------解决方案--------------------
改完后的代码如下,运行没问题的。
C/C++ code

#include <unistd.h>
#include <sys/types.h>