⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p10-5.c

📁 SUN Solaris8平台下进程间通信
💻 C
字号:
#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include "err_exit.h"#include "filelock.h"int main(void){    int fd;    off_t fdpos;    /*打开文件 */    if((fd = open("tempfile",O_RDWR|O_CREAT,0666)) < 0)       err_exit("open error");    /* 对文件尾及其之后的扩充部分置写锁 */    if (WRITE_LOCK(fd,0,SEEK_END,0) == -1)        err_exit("WRITE_LOCK error");    else       printf("%d: call WRITE_LOCK(fd,0,SEEK_END,0) success\n",getpid());    /* 定位文件位置于当前文件尾,并写入8个字符 */    fdpos = lseek(fd,0L,SEEK_END);    write(fd,"8 bytes.",8);                   /* 企图释放前面设置的写锁,但实际并未释放,因为文件尾的位置已后移 */    if (UN_LOCK(fd,0L,SEEK_END,0) == -1)        err_exit("UN_LOCK error");    else       printf("%d: call UN_LOCK(fd,0,SEEK_END,0) success\n",getpid());    /* 让子进程在该区域设置读锁验证父进程是否已正确释放其写锁 */    if (vfork()== 0){   /* 子进程 */       if((fd = open("tempfile",O_RDWR|O_CREAT,0666)) < 0)          err_exit("open error");        /* 对整个文件读置锁,不能设置读锁表明父进程有锁区域未释放 */       if (READ_LOCK(fd, 0,SEEK_SET,0) == -1)          printf("%d: READ_LOCK failed \n",getpid());       else          printf("%d: READ_LOCK success\n",getpid());       exit(0);    }    /* 父进程 */    sleep(10);       /* 保证父进程后退出 */    exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -