Last active
May 3, 2017 06:55
-
-
Save tzutalin/ad4277e8ee24392b9ea23765452d6528 to your computer and use it in GitHub Desktop.
Test flock
This file contains hidden or 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 <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/sem.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/sem.h> | |
#include <sys/ipc.h> | |
#include <iostream> | |
static int sem_id = 0; | |
static int semaphore_p() | |
{ | |
// 对信号量做减1操作,即等待P(sv) | |
struct sembuf sem_b; | |
sem_b.sem_num = 0; | |
sem_b.sem_op = -1;//P() | |
sem_b.sem_flg = SEM_UNDO; | |
if (semop(sem_id, &sem_b, 1) == -1) | |
{ | |
fprintf(stderr, "semaphore_p failed\n"); | |
return 0; | |
} | |
return 1; | |
} | |
static int semaphore_v() | |
{ | |
struct sembuf sem_b; | |
sem_b.sem_num = 0; | |
sem_b.sem_op = 1; // V() | |
sem_b.sem_flg = SEM_UNDO; | |
if (semop(sem_id, &sem_b, 1) == -1) | |
{ | |
fprintf(stderr, "semaphore_v failed\n"); | |
return 0; | |
} | |
return 1; | |
} | |
void lockUnLock() { | |
sem_id = semget((key_t) 1234, 1, 0666 | IPC_CREAT); | |
int i = 0; | |
semaphore_p(); | |
std::cout << "The file was locked " << std::endl; | |
std::cout << "please input a number to unlock the file " << std::endl; | |
scanf("%d", &i); | |
semaphore_v(); | |
} | |
int main() { | |
lockUnLock(); | |
return 0; | |
} |
This file contains hidden or 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 <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/file.h> | |
#include <iostream> | |
void lockUnLock() { | |
int fd, i; | |
char path[] = "test.txt"; | |
fd = open(path, O_WRONLY | O_CREAT); | |
if (fd != -1) { | |
std::cout << "open file " << path << std::endl; | |
std::cout << "Please input a number to lock the file" << path << std::endl; | |
scanf("%d", &i); | |
if (flock(fd, LOCK_EX) == 0) { | |
std::cout << "The file was locked " << std::endl; | |
} else { | |
std::cout << "The file was not locked " << std::endl; | |
} | |
std::cout << "please input a number to unlock the file " << std::endl; | |
scanf("%d", &i); | |
if (flock(fd, LOCK_UN) == 0) { | |
std::cout << "The file was unlocked. " << std::endl; | |
} else { | |
std::cout << "The file was no unlocked. " << std::endl; | |
} | |
close(fd); | |
} else { | |
std::cout << "Cannot open file " << path << std::endl; | |
} | |
} | |
int main() { | |
lockUnLock(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment