父子进程的问题
时间:2011-12-14
来源:互联网
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
int main()
{
int fd=open( "a.txt ", O_RDWR|O_CREAT|O_TRUNC,0666);
if(fork()==0){
char c= 'A ';
for(; c <= 'Z '; c++){
write(fd, &c, 1);
sleep(1);
}
close(fd);
exit(0);
}
int i=0;
char str[10]={};
for(; i <26; i++){
bzero(str, sizeof(str));//memset(str, 0, sizeof(str));
sprintf(str, "%d ", i);
write(fd,str,strlen(str));//我想知道为什么打印的时候是A01B2C3D.....
难道不是交互打印的吗?
sleep(1);
}
close(fd);
}
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
int main()
{
int fd=open( "a.txt ", O_RDWR|O_CREAT|O_TRUNC,0666);
if(fork()==0){
char c= 'A ';
for(; c <= 'Z '; c++){
write(fd, &c, 1);
sleep(1);
}
close(fd);
exit(0);
}
int i=0;
char str[10]={};
for(; i <26; i++){
bzero(str, sizeof(str));//memset(str, 0, sizeof(str));
sprintf(str, "%d ", i);
write(fd,str,strlen(str));//我想知道为什么打印的时候是A01B2C3D.....
难道不是交互打印的吗?
sleep(1);
}
close(fd);
}
作者: adfsf 发布时间: 2011-12-14
交互的挺好的。
作者: qq120848369 发布时间: 2011-12-14
这里父子进程没法保证是交互写入文件的,sleep根本不能控制,如果一定要实现交叉打印的要求(A0B1C2D3这种),你需要在父子进程之间同步。
作者: xtrx 发布时间: 2011-12-14
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28