fork download
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include <string.h> /*strtok strcpy*/
  4. #include<malloc.h> /*malloc*/
  5. #include <sys/types.h> /* pid_t */
  6. #include <sys/wait.h> /* waitpid */
  7. #include <unistd.h> /* _exit, fork */
  8.  
  9. int main()
  10. {
  11. int i=0;
  12. char *cuvinte[256]; // words
  13. char comanda[256]; //command
  14.  
  15. printf("Introduceti comanda: "); //enter the command
  16. fgets(comanda,sizeof(comanda),stdin); // read the command
  17. char *c = strtok(comanda," "); // split it into tokens
  18.  
  19. while(c!=0)
  20. {
  21. cuvinte[i] = malloc( strlen( c ) + 1 ); // alocate memory for the array
  22. strcpy(cuvinte[i++],c); //copying tokens into array
  23. printf("%s\n",c); // printing them
  24. c=strtok(NULL, " ,.!?");
  25. }
  26. printf("Sunt %d elemente stocate in array! \n\n",i); //no of tokens stored
  27. printf("Primul cuvant este: %s \n\n\n",cuvinte[0]); //should print the first word
  28.  
  29.  
  30. /*i got stucked here because of the tokens */
  31.  
  32. /*face un proces copil*/
  33. pid_t pid=fork();
  34. if (pid==0) { /* procesul copil*/
  35. static char *argv[]={"/bin/uname","-a",NULL};
  36. execv(argv[0],argv);
  37. exit(127); /*in caz ca execv da fail*/
  38. }
  39. else { /* pid!=0; proces parinte */
  40. waitpid(pid,0,0); /* asteapta dupa copil */
  41. }
  42. //getch();
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 3740KB
stdin
uname -a
stdout
Linux checker 2.6.34 #6 SMP Fri Jan 21 15:21:52 CET 2011 i686 GNU/Linux
Introduceti comanda: uname
-a
Sunt 2 elemente stocate in array! 

Primul cuvant este: uname