前提・実現したいこと
簡単なshellのようなものを実装したいのですが、うまくいきません。
発生している問題・エラーメッセージ
segmentation fault
該当のソースコード
C
1#include<stdio.h> 2#include<unistd.h> 3#include<sys/wait.h> 4#include<string.h> 5 6 7int main(int argc,char argv[]){ 8 9 char command[100]; 10 printf(">"); 11 fgets(command,100,stdin); 12 13 14 p=strtok(command," "); 15 while(p!=NULL){ 16 p=strtok(NULL," "); 17 } 18 19 pid_t pid = fork(); 20 if (pid > 0 ){ 21 22 int ret; 23 waitpid(pid,&ret,0); 24 25 } 26 else{ 27 char* cmd[]={p[0],p[1],NULL}; 28 execvp(cmd[0], cmd); 29 } 30 31}
試したこと
うまく文字列を空白で切り分けられているか、試したのですがsegmentation faultになっていました。
C
1#include<stdio.h> 2#include<unistd.h> 3#include <string.h> 4 5int main(){ 6 char command[200]; 7 char *p; 8 printf(">"); 9 fgets(command,200,stdin); 10 11 char *tp = strtok(command," "); 12 while(tp!=NULL){ 13 tp=strtok(NULL, " "); 14 15} 16 printf("%s\n", tp); 17 } 18
よろしくお願いいたします。
どの辺がマルチプロセスなのでしょうか?
回答4件
あなたの回答
tips
プレビュー