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

📄 file.cpp

📁 一个演示多线程中共享文件操作符的例子。file是主线程
💻 CPP
字号:
#include <cstdio>#include <sys/stat.h>#include <cerrno>#include <fcntl.h>#include <unistd.h>#include <iostream>#include <sys/types.h>using namespace std;int main(int argc, char *argv[]){   int fd;   int n;   const char*mark = "This is parent process write.";   char *str;   if (argc != 2){      cout << "Usage: ex1 <filename> " << endl;      exit(0);   }   fd = open(argv[1],O_RDWR | O_CREAT | O_TRUNC ,   S_IRUSR | S_IWUSR);     if(fd == -1){      perror("open error");      exit(1);   }   n=fcntl(fd,F_GETFD,0);   if (n == -1)    {      perror("fcntl F_GETFD error");      exit(1);   }   n = write(fd,mark, strlen(mark));   if (n == -1)     {      perror("write mark error");      exit(1);    }//   if (fork()== 0)      n =sprintf(str,"%d",fd);   if (n == -1)     {      perror("sprintf error");      exit(1);    }  execlp("./child", str,NULL);   return 0;}

⌨️ 快捷键说明

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