This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fcntl.h> | |
int fcntl (int fildes, int cmd, struct flock *lock); | |
// fcntl은 파일의 일부분을 exclusive lock 또는 shared lock의 형태로 lock을 걸어서 | |
// 파일에 대한 공유를 설정함으로써 파일의 동기화를 맞춰주는 system call이며, | |
// 파라미터로는 file descriptor, command, flock라고 하는 data structure 주소값을 넣음 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct flock { | |
...... | |
short l_type; // type of lock : F_RDLCK, F_WRLCK, F_UNLCK | |
// F_RDLCK : reader's lock | |
// F_WRLCK : writer's lock | |
// F_UNLCK : unlock | |
short l_whence; // SEEK_SET, SEEK_CUR, SEEK_END | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#define NUM_RECORDS 100 // record에는 최대 100개의 정보 기록 가능 | |
struct record { // 하나의 record에 기록되는 정보 | |
char name[20]; // account owner | |
int id; // account number | |
int balance; // 계좌 잔고 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open the master account file; | |
while (true) { | |
get the operation type and information on the account | |
switch (operation) { // user가 원하는 작업 세분화 | |
case create: // 새로운 record 생성 | |
get a writer’s lock on the record; // record 정보 update이므로 writer's lock 설정 | |
get id and name of the user; // user로부터 id와 name을 입력받음 (새로운 account 생성이므로 balance는 0) | |
reset the account information; // account 정보 update | |
release the writer’s lock; // writer's lock 해제 | |
case inquiry: // read balance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#define TRUE 1 | |
#define FALSE 0 | |
#define NUM_RECORDS 100 | |
struct record // rec-proc.c에서의 record data structure와 동일 | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fcntl.h> | |
int flock(int fd, int operation); | |
// operation은 어떤 lock을 걸지에 대한 것으로 | |
// LOCK_SH, LOCK_EX, LOCK_UN이 있음 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* condition variable creation & destruction */ | |
int pthread_cond_init (*condition, *attr); // mutex와 유사하게 condition variable에 대해서 초기화해 줄 수 있는 함수 | |
int pthread_cond_destory (*condition); // condtion variable을 제거해 줄 수 있는 함수 | |
/* conditon attribute creation & destruction */ | |
int pthread_condattr_init (*attr); // condition variable의 속성을 초기화 해 줄 수 있는 함수 | |
int pthread_condattr_destory (*attr); // condition variable의 속성을 제거해 줄 수 있는 함수 | |
pthread_cond_t *condition | |
// condition variable을 위한 pthread 변수 | |
// (type은 pthread_cond_t) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 초기화된 condition variable을 이용해서 thread들간에 condition variable에 대한 | |
signal을 주고 받는 작업을 수행하는 pthead 함수 3가지 : wait, signal, broadcast */ | |
pthread_cond_wait (condition, mutex); // argument로 condition variable과 mutex를 받음 | |
pthread_cond_signal (condition); // argument로 condition variable을 받음 | |
pthread_cond_broadcast (condition); // argument로 condition variable을 받음 | |
pthread_cond_t *condition | |
pthread_condattr_t *attr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <stdio.h> | |
#define NUM_THREADS 3 // 3개의 thread 생성 | |
#define TCOUNT 10 // 2개의 thread는 count를 10씩 증가시킴 | |
#define COUNT_LIMIT 12 // 나머지 1개의 thread는 count가 12가 될 때까지 기다림 | |
int count = 0; | |
int thread_ids[3] = {0, 1, 2}; // thread id | |
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; // mutex를 위한 변수 초기화 | |
pthread_cond_t count_threshold_cv = PTHREAD_COND_INITIALIZER; // condition variable을 위한 변수 초기화 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <pthread.h> | |
void *producer(void *); | |
void *consumer(void *); | |
#define MAX_BUF 100 | |
//shared variables | |
int buffer[MAX_BUF]; // 100개 integer를 담을 수 있는 buffer (공유 자원) | |
int count = 0; | |
int in = -1, out = -1; |
OlderNewer