• 카테고리

    질문 & 답변
  • 세부 분야

    시스템

  • 해결 여부

    미해결

VIM 1강에서 사용하셨던 예제를 따라하고 싶은데 실습 파일을 주실 수 있을까요?

21.03.08 13:48 작성 조회수 100

0

VIM 1강에서 사용하셨던 예제를 따라하고 싶은데 실습 파일을 주실 수 있을까요?

답변 3

·

답변을 작성해보세요.

0

audibly님의 프로필

audibly

질문자

2021.03.09

감사합니다^^

0

cat <<EOF > file_flock.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/file.h>

#define FLOCK_FILENAME		".flock_test"

static void print_usage(const char *progname)
{
	printf("%s (sh|ex)\n", progname);
}

int main(int argc, char **argv)
{
	int ops;
	int fd;

	if (argc < 2) {
		print_usage(argv[0]);
		return -1;
	}
	
	if (!strcmp(argv[1], "sh")) {
		ops = LOCK_SH;
	} else if (!strcmp(argv[1], "ex")) {
		ops = LOCK_EX;
	} else {
		print_usage(argv[0]);
		return -1;
	}

	fd = open(FLOCK_FILENAME, O_RDWR | O_CREAT, 0644);
	if (fd == -1) {
		perror("open()");
		return -1;
	}

	/* lock */
	printf("trying to grab the lock\n");
	flock(fd, ops);

	printf("got it. waiting for enter...\n");
	/* wait */
	getc(stdin);

	/* unlock */
	flock(fd, LOCK_UN);

	close(fd);
	return 0;
}
EOF

0

안녕하세요. 질문자님.

반드시 이 파일을 사용하셔야 하는 것은 아니지만, 요청하셔서 올려드립니다. 아래의 코드를 터미널에 그대로 붙여넣기 하면 file_flock.c 파일이 생성될 것입니다. 반드시 cat으로 시작하는 라인부터 마지막 줄의 EOF 다음 빈 줄도 반드시 입력하셔야 합니다.

cat <<EOF > file_flock.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/file.h>

#define FLOCK_FILENAME ".flock_test"

static void print_usage(const char *progname)

{

printf("%s (sh|ex)\n", progname);

}

int main(int argc, char **argv)

{

int ops;

int fd;

if (argc < 2) {

print_usage(argv[0]);

return -1;

}

if (!strcmp(argv[1], "sh")) {

ops = LOCK_SH;

} else if (!strcmp(argv[1], "ex")) {

ops = LOCK_EX;

} else {

print_usage(argv[0]);

return -1;

}

fd = open(FLOCK_FILENAME, O_RDWR | O_CREAT, 0644);

if (fd == -1) {

perror("open()");

return -1;

}

/* lock */

printf("trying to grab the lock\n");

flock(fd, ops);

printf("got it. waiting for enter...\n");

/* wait */

getc(stdin);

/* unlock */

flock(fd, LOCK_UN);

close(fd);

return 0;

}

EOF

아니시면 파일 내용만 복사하셔서 리눅스 데스크탑의 텍스트 편집기를 이용하셔도 됩니다.

해보시고 잘 안되시면 다시 답글 남겨주세요!

그럼 수고하세요~~