一个照书本写的练习代码,运行出错

陈诒亮 发布于 2014/06/10 17:01
阅读 188
收藏 0
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

const char *test_file = "/tmp/test_lock";

int main(int argc, char const *argv[])
{
	
	int file_desc;
	int byte_count;
	char *byte_to_write = "A";
	struct flock region_1;
	struct flock region_2;
	int res;

	file_desc = open(test_file, O_RDWR | O_CREAT , 0666);
	if (!file_desc)
	{
		/* code */
		fprintf(stderr, "Unable to open %s for read/write\n", test_file );
		exit(EXIT_FAILURE);
	}

	for (byte_count = 0; byte_count < 100; byte_count ++)
	{
		/* code */
		(void)write(file_desc, byte_to_write, 1);
	}

	region_1.1_type = F_RDLCK;
	region_1.1_whence = SEEK_SET;
	region_1.1_start = 10;
	region_1.1_len = 20;

	region_2.1_type = F_WRLCK;
	region_2.1_whence = SEEK_SET;
	region_2.1_start = 40;
	region_2.1_len = 10;

	printf("Process %d locking file \n", getpid() );
	res = fcntl(file_desc, F_SETLK, &region_1);
	if (res == -1)
	{
		/* code */
		fprintf(stderr, "Failed to lock region_1\n");
	}
	res = fcntl(file_desc, F_SETLK, &region_2);
	if (res == -1)
	{
		/* code */
		fprintf(stderr, "Failed to lock region_2\n" );
	}

	sleep(60);
	printf("Process %d closing file \n", getpid());
	close(file_desc);

	exit(EXIT_SUCCESS);
}



代码我照着书本打了,检查了好几遍,也没看出哪里错了,也不太清楚到底提示的是什么错误!

加载中
0
未注册用户
未注册用户

1_type应该是l_type吧?(L,不是数字1)

唔,话说变量名也不能以数字开头啊

陈诒亮
陈诒亮
我草,被坑了,原来是这样l啊,看上去和1一样,呵呵,太粗心了!
0
gat205
gat205

1_type 

invalid suffix "_type",数字开头的变量名?(C全忘光了) 

0
明月惊鹊
明月惊鹊
理论上应该找光盘来复制,或者google。  纯手打往往就是找打。
返回顶部
顶部