[問題] 奇怪的c問題
int main(void)
{
int fd;
fd=open("hello",O_RDWR | O_CREAT, 0666);
if(fd<0)
{
perror("open");
exit(1);
}
lock_set(fd,F_WRLCK);
getchar();
lock_set(fd,F_UNLCK);
getchar();
close(fd);
exit(0);
}
如上程式,理想的情形應該是,在呼叫lock_set(fd,F_WRLCK)完之後,會因為getchar()
而等待輸入,輸入完之後街著呼叫lock_set(fd,F_UNLCK),然後在接著由於getchar()
而等待輸入,然後停止。
但我試驗完之後,發現在呼叫完lock_set(fd,F_UNLCK)之後,程式就停了 =.=。
也就是,第二個getchar()沒有作用。
但若此時,我在第二個getchar()在加入一個getchar()之後,成式就可以等待輸入了。
but,只會停一次 =.=。意思是說,第一個getchar()不知道那去了。
請問這是為捨壓 ???
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.122.79.88
討論串 (同標題文章)