[問題] fork的問題
看板C_and_CPP (C/C++)作者play2004 (156941)時間16年前 (2009/02/09 15:57)推噓5(5推 0噓 12→)留言17則, 3人參與討論串1/2 (看更多)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char b[200];
pid_t pid;
char *bb[]={"ls","pwd","cal","exit"};
int i,n;
n=strlen(b)-1;
while(b!="exit\n")
{
if((pid=fork())<0)
printf("fork error");
if(pid==0)
{
printf("commond:\n\"ls\",\"pwd\",\"cal\",\"exit\" ");
fgets(b,200,stdin);
fflush(stdin);
for(i=0;i<4;i++)
{
if(strncmp(b,bb[i],n)==0)
execlp(bb[i],bb[i],0);
}
}
else
wait();
}
return 0;
}
問題在不管我輸入捨到fgets他都只會幫我執行 ls的能力
不知道為什麼? 請知道的大大幫解一下 謝謝...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.161.191.212
※ 編輯: play2004 來自: 118.161.191.212 (02/09 16:00)
推
02/09 16:00, , 1F
02/09 16:00, 1F
→
02/09 16:01, , 2F
02/09 16:01, 2F
→
02/09 18:03, , 3F
02/09 18:03, 3F
推
02/09 20:42, , 4F
02/09 20:42, 4F
推
02/09 20:43, , 5F
02/09 20:43, 5F
→
02/09 23:23, , 6F
02/09 23:23, 6F
推
02/10 00:38, , 7F
02/10 00:38, 7F
→
02/10 00:38, , 8F
02/10 00:38, 8F
→
02/10 00:38, , 9F
02/10 00:38, 9F
→
02/10 00:39, , 10F
02/10 00:39, 10F
→
02/10 00:40, , 11F
02/10 00:40, 11F
→
02/10 00:40, , 12F
02/10 00:40, 12F
→
02/10 00:40, , 13F
02/10 00:40, 13F
→
02/10 00:40, , 14F
02/10 00:40, 14F
→
02/10 00:41, , 15F
02/10 00:41, 15F
推
02/10 09:58, , 16F
02/10 09:58, 16F
→
02/10 15:06, , 17F
02/10 15:06, 17F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章