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 :
|
|