當(dāng)前位置:
首頁(yè) >
linux进程通讯-纯文本文件
發(fā)布時(shí)間:2023/12/9
43
豆豆
生活随笔
收集整理的這篇文章主要介紹了
linux进程通讯-纯文本文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
三)強(qiáng)制性加鎖的實(shí)現(xiàn)
1)對(duì)文件加鎖有兩種方式:勸告性鎖和強(qiáng)制性鎖.
2)勸告性鎖工作時(shí),每個(gè)進(jìn)程都要對(duì)文件進(jìn)行讀或?qū)懼罢{(diào)用lockf對(duì)文件加鎖,如果一個(gè)進(jìn)程忘記調(diào)用lockf,那么鎖協(xié)議將會(huì)被忽視
3)強(qiáng)制性鎖工作時(shí),它將使任何一個(gè)想要訪問已被加鎖的文件的進(jìn)程都堵塞在讀或?qū)戧?duì)列上.加鎖的過程是由內(nèi)核強(qiáng)制啟動(dòng)的,所以不用擔(dān)心不同步的進(jìn)程忽視勸告性鎖.
4)程序2就是勸告性鎖的典型例子,而下面的例子會(huì)演示強(qiáng)制性鎖的使用.
mount -o remount,rw,mand /dev/sdb1 /mnt/
cd /mnt/
chmod g-x messagebuf.dat
chmod g+s messagebuf.dat
修改程序2的源代碼如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <sys/stat.h>
const char *filename = "messagebuf.dat";
void error_out(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
void child(void)
{
FILE *fp = fopen(filename, "r+");
if (fp == NULL)
error_out("child:fopen");
/*int r = lockf(fileno(fp), F_LOCK, 0);
if (r == -1)
error_out("parent:lockf");*/
char buf[32];
fread(buf,sizeof(buf), 1, fp);
if (ferror(fp))
error_out("fread");
printf("child read '%s'\n", buf);
}
void parent(FILE *fp)
{
fprintf(fp, "%#x", getpid());
fflush(fp);
int r = lockf(fileno(fp), F_ULOCK, 0);
if (r == -1)
error_out("lockf:F_ULOCK");
fclose(fp);
}
int main(int argc, char *argv[])
{
int r;
int fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0666);
FILE *fp = fdopen(fd, "r+");
if (fp == NULL)
error_out("parent:fopen");
r = lockf(fileno(fp), F_LOCK, 0);
if (r == -1)
error_out("parent:lockf");
pid_t pid = fork();
if (pid == 0){
child();
exit(0);
}
else{
int status = 0;
parent(fp);
wait(&status);
printf("child status=%d\n", WEXITSTATUS(status));
}
exit(0);
}
gcc file-ipc-better.c -o file-ipc-better
./file-ipc-better
child read '0x9a0'
child status=0
我們注釋掉了子進(jìn)程的lockf函數(shù)的過程,這時(shí)如果沒有強(qiáng)制鎖,子進(jìn)程的讀操作,將會(huì)忽視父進(jìn)程的lockf,而我們加了強(qiáng)制鎖,子進(jìn)程的讀操作被阻塞了.
最終的結(jié)果同第二個(gè)程序一樣,但我們卻使用了不同的鎖機(jī)制.
最后使用文件進(jìn)行傳遞媒介就意味著你可能遇到媒介中存在的潛在不安全因素.
1)對(duì)文件加鎖有兩種方式:勸告性鎖和強(qiáng)制性鎖.
2)勸告性鎖工作時(shí),每個(gè)進(jìn)程都要對(duì)文件進(jìn)行讀或?qū)懼罢{(diào)用lockf對(duì)文件加鎖,如果一個(gè)進(jìn)程忘記調(diào)用lockf,那么鎖協(xié)議將會(huì)被忽視
3)強(qiáng)制性鎖工作時(shí),它將使任何一個(gè)想要訪問已被加鎖的文件的進(jìn)程都堵塞在讀或?qū)戧?duì)列上.加鎖的過程是由內(nèi)核強(qiáng)制啟動(dòng)的,所以不用擔(dān)心不同步的進(jìn)程忽視勸告性鎖.
4)程序2就是勸告性鎖的典型例子,而下面的例子會(huì)演示強(qiáng)制性鎖的使用.
mount -o remount,rw,mand /dev/sdb1 /mnt/
cd /mnt/
chmod g-x messagebuf.dat
chmod g+s messagebuf.dat
修改程序2的源代碼如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <sys/stat.h>
const char *filename = "messagebuf.dat";
void error_out(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
void child(void)
{
FILE *fp = fopen(filename, "r+");
if (fp == NULL)
error_out("child:fopen");
/*int r = lockf(fileno(fp), F_LOCK, 0);
if (r == -1)
error_out("parent:lockf");*/
char buf[32];
fread(buf,sizeof(buf), 1, fp);
if (ferror(fp))
error_out("fread");
printf("child read '%s'\n", buf);
}
void parent(FILE *fp)
{
fprintf(fp, "%#x", getpid());
fflush(fp);
int r = lockf(fileno(fp), F_ULOCK, 0);
if (r == -1)
error_out("lockf:F_ULOCK");
fclose(fp);
}
int main(int argc, char *argv[])
{
int r;
int fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0666);
FILE *fp = fdopen(fd, "r+");
if (fp == NULL)
error_out("parent:fopen");
r = lockf(fileno(fp), F_LOCK, 0);
if (r == -1)
error_out("parent:lockf");
pid_t pid = fork();
if (pid == 0){
child();
exit(0);
}
else{
int status = 0;
parent(fp);
wait(&status);
printf("child status=%d\n", WEXITSTATUS(status));
}
exit(0);
}
gcc file-ipc-better.c -o file-ipc-better
./file-ipc-better
child read '0x9a0'
child status=0
我們注釋掉了子進(jìn)程的lockf函數(shù)的過程,這時(shí)如果沒有強(qiáng)制鎖,子進(jìn)程的讀操作,將會(huì)忽視父進(jìn)程的lockf,而我們加了強(qiáng)制鎖,子進(jìn)程的讀操作被阻塞了.
最終的結(jié)果同第二個(gè)程序一樣,但我們卻使用了不同的鎖機(jī)制.
最后使用文件進(jìn)行傳遞媒介就意味著你可能遇到媒介中存在的潛在不安全因素.
轉(zhuǎn)載于:https://blog.51cto.com/xuanjicto/725324
總結(jié)
以上是生活随笔為你收集整理的linux进程通讯-纯文本文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决问题E: 无法获得锁 /var/li
- 下一篇: linux limit