由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 两个process重复了,哪里错了
相关主题
怎样读一个不断更新的文件What does int & mean? C/C++ question
C++的smart pointer注定是个二流的东西One question about Void pointer
C 语言,初学者,简单问题Why this is a dangling pointer
请教 C++的一个困惑 (operator delete)C++ Q87: What is wrong with this swap function? (转载)
菜鸟请教C问题C++class指针转换
pointer to function这个function pointer最后的那个int是什么意思?
听说pointer to member function只支持最多128个virtual functiWhy Avoid array indexing. Use pointers.
一个nested class的问题Why do I need to use "plain" pointer?
相关话题的讨论汇总
话题: sigusr1话题: flag话题: child话题: pid话题: atindex
进入Programming版参与讨论
1 (共1页)
c*****a
发帖数: 808
1
flag在main的外面.之前我用fgets加File pointer *就不会重复,感觉是因为跟
pointer有关,现在用lower level的read加 lseek(fd, atIndex, SEEK_SET)来做,感
觉fd 在parent和child不会因为另外一边改变而改变
有人指教吗
int flag =1;
int main(){
pid=fork();
if(pid<0)
error
else
if ( pid == 0 ) {
/* child*/
do{
if(flag ==0)
read and write n bytes ,atIndex +n

if XXX break;
kill(getppid (), SIGUSR1) ;
signal(SIGUSR1, shandler);
pause();
}while(1);
exit(3);
} else {
/* parent */
do{
if(flag ==1)
read and write n bytes,atIndex + n
if XXX break;
kill(pid, SIGUSR1);
signal(SIGUSR1, shandler2);
wait(&child_status);/*child=3*/
}while(1);
exit(0);
}
}/*end main*/
void shandler (void)
flag=0
void shandler2 (void)
flag=1
t****t
发帖数: 6806
2
第一, 请你自己读一遍你的中文, 看看会不会读不懂, 如果你中学语文不及格, 请说英文
第二, 你的fd在程序里完全没有出现, 是打算让人猜你写了什么吗?

【在 c*****a 的大作中提到】
: flag在main的外面.之前我用fgets加File pointer *就不会重复,感觉是因为跟
: pointer有关,现在用lower level的read加 lseek(fd, atIndex, SEEK_SET)来做,感
: 觉fd 在parent和child不会因为另外一边改变而改变
: 有人指教吗
: int flag =1;
: int main(){
: pid=fork();
: if(pid<0)
: error
: else

c*****a
发帖数: 808
3
sorry. i dont know how to type chinese on linux, i just copy and paste from
google translate.

int fd;
fd = open(filename,O_RDONLY);
this is done before using fork to create a child process.
t****t
发帖数: 6806
4
now this is better. and your question is? what's the meaning of "两个process
重复了"?

from

【在 c*****a 的大作中提到】
: sorry. i dont know how to type chinese on linux, i just copy and paste from
: google translate.
:
: int fd;
: fd = open(filename,O_RDONLY);
: this is done before using fork to create a child process.

c*****a
发帖数: 808
5
I am using lower level I/O, lseek, open and read. the output of stream gets
print out twice. instead of "JOHN\n", it becomes "JOHN\nJOHN\n".
I manage to get it working, if I pass the value of current position with 2
sighanlders between parent and child; but it is a crappy code, because I
create a lot of temp variables for swapping.
However, when I do not use lower level stuff :
FILE *fp;
fp = fopen(filename,"r");
because fp is pointer, if fp is changed on parent, fp is also changed on
child. Output will not be double.
I wonder if there is another solution.
t****t
发帖数: 6806
6
一共没几行代码, 都列出来吧, 看得累死人.

gets
2

【在 c*****a 的大作中提到】
: I am using lower level I/O, lseek, open and read. the output of stream gets
: print out twice. instead of "JOHN\n", it becomes "JOHN\nJOHN\n".
: I manage to get it working, if I pass the value of current position with 2
: sighanlders between parent and child; but it is a crappy code, because I
: create a lot of temp variables for swapping.
: However, when I do not use lower level stuff :
: FILE *fp;
: fp = fopen(filename,"r");
: because fp is pointer, if fp is changed on parent, fp is also changed on
: child. Output will not be double.

l*********s
发帖数: 5409
7
They are two processes, it is a fluke.
If you want to coordinate the two process, you need to learn IPC


【在 c*****a 的大作中提到】
: I am using lower level I/O, lseek, open and read. the output of stream gets
: print out twice. instead of "JOHN\n", it becomes "JOHN\nJOHN\n".
: I manage to get it working, if I pass the value of current position with 2
: sighanlders between parent and child; but it is a crappy code, because I
: create a lot of temp variables for swapping.
: However, when I do not use lower level stuff :
: FILE *fp;
: fp = fopen(filename,"r");
: because fp is pointer, if fp is changed on parent, fp is also changed on
: child. Output will not be double.

c*****a
发帖数: 808
8
i've done similar classic stuff with semaphore mutex or pipe. i am trying
to figure out how to do this with user defined signal handler.

【在 l*********s 的大作中提到】
: They are two processes, it is a fluke.
: If you want to coordinate the two process, you need to learn IPC
:

1 (共1页)
进入Programming版参与讨论
相关主题
Why do I need to use "plain" pointer?菜鸟请教C问题
两个面世题pointer to function
一个简单的小问题听说pointer to member function只支持最多128个virtual functi
请问可以这样定义struct吗?一个nested class的问题
怎样读一个不断更新的文件What does int & mean? C/C++ question
C++的smart pointer注定是个二流的东西One question about Void pointer
C 语言,初学者,简单问题Why this is a dangling pointer
请教 C++的一个困惑 (operator delete)C++ Q87: What is wrong with this swap function? (转载)
相关话题的讨论汇总
话题: sigusr1话题: flag话题: child话题: pid话题: atindex