- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有这个 C 程序。我有两个进程,父子,并使用信号量使它们一次同步一个。父亲必须写 (n) 个数字,在本例中是十个,总是在打开文件的第一个字节中,儿子必须读取它。问题是,当我打印结果时,写入(父亲)的文件描述符错误,而读取(儿子)则没有这样的文件。你能帮我吗??谢谢
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define FILENAME "test.txt"
#define MUTEX "/mutex"
#define READ "/read"
#define WRITE "/write"
int main(int argc, char *argv[]){
int i, pid, n=10, fd, x;
int nread, nwrite;
char c = 'a';
sem_t *mutex, *reader, *writer;
//fd = open(FILENAME, O_CREAT | O_TRUNC, 0666);
mutex = sem_open(MUTEX, O_CREAT, 0666, 1);
reader = sem_open(READ, O_CREAT, 0666, 0);
writer = sem_open(WRITE, O_CREAT, 0666, 1);
pid = fork();
fd = open(FILENAME, O_CREAT | O_TRUNC, 0777);
if(fd < 0){
perror("Open FILE error");
exit(-1);}
if(pid == 0){ // son
do{
sem_wait(reader); // si può leggere???
sem_wait(mutex);
lseek(fd, 0, SEEK_SET);
nread = read(fd, &x, sizeof(int));
if(nread <=0)
perror("Read error");
printf("Son has read (%d byte) = %d\n", nread, x);
fflush(NULL);
sem_post(mutex);
sem_post(writer);
}
while(x != (n-1));
exit(0);
}
else{
for(i=0; i<n; i++){
sem_wait(writer); // can I write??
sem_wait(mutex);
lseek(fd, 0, SEEK_SET);
nwrite = write(fd, &c, sizeof(char));
if(nwrite <= 0)
perror("nwrite error");
printf("Father has written (%d byte) %d\n", nwrite, i);
fflush(NULL);
sem_post(mutex);
sem_post(reader); // it's possible to read
}
//wait(NULL);
}
sem_unlink(MUTEX);
sem_unlink(READ);
sem_unlink(WRITE);
//remove(FILENAME);
exit(0);
}
最佳答案
首先,您在没有指定 o_flag 的情况下打开了文件。这实际上是未定义的行为 ( "Applications shall specify exactly one of .... O_RDONLY .... O_WRONLY .... O_RDWR" ),但出于实际目的意味着文件以只读打开。
因此父级的写操作因 EBADF 而失败。无法写入只读文件!
其次, child 的错误检查不正确。 read()
成功时可能返回零,在这种情况下,perror()
查询的 errno
不能保证有意义。您的意思是检查返回值是否小于零,而不是小于或等于零。
关于c - 读取和写入操作得到错误的 fd 并且没有这样的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28484548/
正如我不断发现的那样,有各种各样的文件描述符——几乎所有的东西都是围绕文件描述符抽象出来的:常规文件、套接字、信号和计时器(例如)。所有文件描述符都只是整数。 给定一个文件描述符,是否可以知道它是什么
socket fd 长什么样子? 什么是 socket fd ?粗糙的来讲,就是网络 fd,比如我们最常见的 C/S 客户端服务端的编程模式,就是网络通信的一种方式。撇开底层
如果我调用了shutdown(fd,SHUT_RDWR),但没有调用close(fd),会发生什么? inline void CSocket::close() { if (_socket_fd
我有以下结构: struct myfds_for_nic { int fd1; int fd2; int fd3; int fd4; int fd5;
fd 是否有等价于 fseek 的东西?我已经使用 int fds 很长时间了,想使用 fseek...但我知道没有搜索功能。 提前致谢! 最佳答案 参见 POSIX 函数 lseek(2) : SY
我正在使用 Clojure 的 core.logic CLP(FD) 库(core.logic 版本 0.8.3)开发一种简单的方形打包算法。 正方形的表示方式如下: [[[x11 y11] [x12
我正在学习 linux 操作系统,我有一个关于管道的问题。 我想实现一个管道。 所以我定义了int fd[2]; 但是为什么fd[0]是读而fd[1]是写呢? 0代表stdin吗? (我认为是写)而1
我知道进程的pid,需要获取它使用的socketfd,所以在/proc/$pid/中查找fd,例如: $ ls -la /proc/1442/fd | grep socket lrwx------ 1
我正在检查当前 fatrace 的源代码。 调用fanotify获取数据值的主循环如下: res = read (fan_fd, buffer, 4096); ... data
Docker daemon documentation建议大多数设置使用以下 hosts 选项: dockerd -H fd:// 我猜 fd 代表文件描述符。我不明白 fd 如何用于套接字通信。 我
在执行之前:os.read(fd,1024) 我想检查是否会有输出,而不是挂起直到收到输出。由于 fd 是一个 int 对象,我不能这样做: os.fstat(f.fileno()).st_size
我有一个程序 ( https://github.com/raboof/connbeat ) 依赖于 /proc/[pid]/fd/* 来查找给定(网络)inode 的进程。 /proc/[pid]/f
#define STACK_SIZE (1024 * 1024) static char container_stack[STACK_SIZE]; char* const container_args
inotify file in C 我看过下面的代码用来调用 (void) inotify_rm_watch(fd, wd); (void) close(fd); 为什么不呢? inotify_rm_
下面的小 C 程序(我们称之为 pointless): /* pointless.c */ #include #include void main(){ write(STDOUT_FILENO
考虑这个代码示例: #include #include #include int main() { //this file exists and contains data: "ABCD
在父进程中close(fd[1]);, 为什么它会跳过第一个 fd[1](替换为父 STD_OUT)并在子进程中关闭 fd[1]? #define STD_INPUT 0 #define STD_OU
需要使用代理设置运行模拟器,我在命令提示符下使用以下命令来启动模拟器 emulator -avd AVD_for_3_7_WVGA_Nexus_One -http-proxy http://usern
我正在 appcelerator studio 中创建应用程序。在我向其添加 admob 模块之前,它在我的 Android 6 Lenovo a7000 上正常运行。现在我收到这些错误: [ERRO
我不明白必须如何解决以下问题。非常感谢任何帮助学习如何解决这个问题的人! Consider Relation Schema R = {ABCDEFG} with a set of Functional
我是一名优秀的程序员,十分优秀!