code_junkie

How do you spawn another process in C?

Nov 10th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. int main()
  4. {
  5. system("echo HAI");
  6.  
  7. return 0;
  8. }
  9.  
  10. #include <stdio.h>
  11.  
  12. int main()
  13. {
  14. int entry = 1;
  15. char line[200];
  16. FILE* output = popen("/usr/bin/ls -1 /usr/man", "r");
  17. while ( fgets(line, 199, output) )
  18. {
  19. printf("%5d: %s", entry++, line);
  20. }
  21. }
  22.  
  23. 1: cat1
  24. 2: cat1b
  25. 3: cat1c
  26. 4: cat1f
  27. 5: cat1m
  28. 6: cat1s
  29. ...
  30.  
  31. int system(const char *string);
  32.  
  33. QString program = "./yourspawnedprogram";
  34. QProcess * spawnedProcess = new QProcess(parent);
  35. spawnedProcess->start(program);
  36. // or spawnedProcess->startDetached(program);
  37.  
  38. #include <unistd.h>
  39. #include <stdio.h>
  40.  
  41. int main()
  42. {
  43. printf( "hi 2 un" );
  44. int mypid = fork();
  45.  
  46. if( 0 == mypid )
  47. printf( "lol childn" );
  48. else
  49. printf( "lol parentn" );
  50.  
  51. return( 0 );
  52. }
Add Comment
Please, Sign In to add comment