Guest User

Untitled

a guest
Apr 18th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <pthread.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <syslog.h>
  10. #include <string.h>
  11.  
  12. #define NTHREADS 10
  13.  
  14. void *threadFunc(void *arg)
  15. {
  16.   printf(".");
  17.   fflush(stdout);
  18.     // Sleep 10 seconds to give all procs a chance to start and to be bound to a cpu set
  19.   sleep(10);
  20.   while(1==1)
  21.     {
  22.       int a = 400;
  23.       a * 400;
  24.       a * 300;
  25.       a / 15;
  26.     }
  27. }
  28.  
  29. int startThreads(int nthreads)
  30. {
  31.   printf("Starting %d CPU threads: ", NTHREADS);
  32.   pthread_t pta[nthreads];
  33.   for ( int i = 0; i < nthreads; i++ )
  34.   {
  35.     pthread_t bla;
  36.     pta[i] = bla;
  37.     pthread_create(&pta[i],NULL,threadFunc,NULL);
  38.   }
  39.   pthread_join(pta[0],NULL);
  40.   return 0;
  41. }
  42.  
  43. int main(void)
  44. {
  45.        
  46.     /* Our process ID and Session ID */
  47.     pid_t pid, sid;
  48.  
  49.     /* Fork off the parent process */
  50.     pid = fork();
  51.     if (pid < 0) {
  52.     exit(EXIT_FAILURE);
  53.     }
  54.     /* If we got a good PID, then
  55.     we can exit the parent process. */
  56.     if (pid > 0) { // Child can continue to run even after the parent has finished executing
  57.     exit(EXIT_SUCCESS);
  58.     }
  59.    
  60.     /* Change the file mode mask */
  61.     umask(0);
  62.            
  63.     /* Create a new SID for the child process */
  64.     sid = setsid();
  65.     if (sid < 0) {
  66.     /* Log the failure */
  67.     exit(EXIT_FAILURE);
  68.     }
  69.        
  70.     /* Change the current working directory */
  71.     if ((chdir("/")) < 0) {
  72.     /* Log the failure */
  73.     exit(EXIT_FAILURE);
  74.     }
  75.  
  76.     startThreads(NTHREADS);
  77.    
  78.     exit(EXIT_SUCCESS);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment