Guest User

Untitled

a guest
Mar 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. main thread pid is 14608
  2. child thread pid is 14610
  3.  
  4. main thread pid is 3615
  5. child thread pid is 3615
  6.  
  7. #include <pthread.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10.  
  11. void* thread_function (void* arg)
  12. {
  13. fprintf (stderr, "child thread pid is %dn", (int) getpid ());
  14. /* Spin forever. */
  15. while (1);
  16. return NULL;
  17. }
  18.  
  19. int main ()
  20. {
  21. pthread_t thread;
  22. fprintf (stderr, "main thread pid is %dn", (int) getpid ());
  23. pthread_create (&thread, NULL, &thread_function, NULL);
  24. /* Spin forever. */
  25. while (1);
  26. return 0;
  27. }
  28.  
  29. long tid = syscall(SYS_gettid);
  30. printf("%ldn", tid);
  31.  
  32. #include <pthread.h>
  33. #include <stdio.h>
  34. #include <unistd.h>
  35. #include <syscall.h>
  36.  
  37. void* thread_function (void* arg)
  38. {
  39. long tid = syscall(SYS_gettid);
  40. printf("child thread TID is %ldn", tid);
  41. fprintf (stderr, "child thread pid is %dn", (int) getpid ());
  42. /* Spin forever. */
  43. while (1);
  44. return NULL;
  45. }
  46.  
  47. int main ()
  48. {
  49. pthread_t thread;
  50. long tid = syscall(SYS_gettid);
  51. printf("main TID is %ldn", tid);
  52. fprintf (stderr, "main thread pid is %dn", (int) getpid ());
  53. pthread_create (&thread, NULL, &thread_function, NULL);
  54. /* Spin forever. */
  55. while (1);
  56. return 0;
  57. }
  58.  
  59. main TID is 17963
  60. main thread pid is 17963
  61. thread TID is 17964
  62. child thread pid is 17963
  63.  
  64. // simple program to create threads that simply sleep
  65. // compile in debian jessie with apt-get install build-essential
  66. // and then g++ -O4 -Wall -std=c++0x -pthread threads2.cpp -o threads2
  67. #include <string>
  68. #include <iostream>
  69. #include <thread>
  70. #include <chrono>
  71.  
  72. // how many seconds will the threads sleep for?
  73. #define SLEEPTIME 100
  74. // how many threads should I start?
  75. #define NUM_THREADS 25
  76.  
  77. using namespace std;
  78.  
  79. // The function we want to execute on the new thread.
  80. void threadSleeper(int threadid){
  81. // output what number thread we've created
  82. cout << "task: " << threadid << "n";
  83. // take a nap and sleep for a while
  84. std::this_thread::sleep_for(std::chrono::seconds(SLEEPTIME));
  85. }
  86.  
  87. void main(){
  88. // create an array of thread handles
  89. thread threadArr[NUM_THREADS];
  90. for(int i=0;i<NUM_THREADS;i++){
  91. // spawn the threads
  92. threadArr[i]=thread(threadSleeper, i);
  93. }
  94. for(int i=0;i<NUM_THREADS;i++){
  95. // wait for the threads to finish
  96. threadArr[i].join();
  97. }
  98. // program done
  99. cout << "Donen";
  100. return;
  101. }
  102.  
  103. UID PID PPID LWP C NLWP STIME TTY TIME CMD
  104. debian 689 687 689 0 1 14:52 ? 00:00:00 sshd: debian@pts/0
  105. debian 690 689 690 0 1 14:52 pts/0 00:00:00 -bash
  106. debian 6217 690 6217 0 1 15:04 pts/0 00:00:00 screen
  107. debian 6218 6217 6218 0 1 15:04 ? 00:00:00 SCREEN
  108. debian 6219 6218 6219 0 1 15:04 pts/1 00:00:00 /bin/bash
  109. debian 6226 6218 6226 0 1 15:04 pts/2 00:00:00 /bin/bash
  110. debian 6232 6219 6232 0 26 15:04 pts/1 00:00:00 ./threads2
  111. debian 6232 6219 6233 0 26 15:04 pts/1 00:00:00 ./threads2
  112. debian 6232 6219 6234 0 26 15:04 pts/1 00:00:00 ./threads2
  113. debian 6232 6219 6235 0 26 15:04 pts/1 00:00:00 ./threads2
  114. debian 6232 6219 6236 0 26 15:04 pts/1 00:00:00 ./threads2
  115. debian 6232 6219 6237 0 26 15:04 pts/1 00:00:00 ./threads2
  116. debian 6232 6219 6238 0 26 15:04 pts/1 00:00:00 ./threads2
  117. debian 6232 6219 6239 0 26 15:04 pts/1 00:00:00 ./threads2
  118. debian 6232 6219 6240 0 26 15:04 pts/1 00:00:00 ./threads2
  119. debian 6232 6219 6241 0 26 15:04 pts/1 00:00:00 ./threads2
  120. debian 6232 6219 6242 0 26 15:04 pts/1 00:00:00 ./threads2
  121. debian 6232 6219 6243 0 26 15:04 pts/1 00:00:00 ./threads2
  122. debian 6232 6219 6244 0 26 15:04 pts/1 00:00:00 ./threads2
  123. debian 6232 6219 6245 0 26 15:04 pts/1 00:00:00 ./threads2
  124. debian 6232 6219 6246 0 26 15:04 pts/1 00:00:00 ./threads2
  125. debian 6232 6219 6247 0 26 15:04 pts/1 00:00:00 ./threads2
  126. debian 6232 6219 6248 0 26 15:04 pts/1 00:00:00 ./threads2
  127. debian 6232 6219 6249 0 26 15:04 pts/1 00:00:00 ./threads2
  128. debian 6232 6219 6250 0 26 15:04 pts/1 00:00:00 ./threads2
  129. debian 6232 6219 6251 0 26 15:04 pts/1 00:00:00 ./threads2
  130. debian 6232 6219 6252 0 26 15:04 pts/1 00:00:00 ./threads2
  131. debian 6232 6219 6253 0 26 15:04 pts/1 00:00:00 ./threads2
  132. debian 6232 6219 6254 0 26 15:04 pts/1 00:00:00 ./threads2
  133. debian 6232 6219 6255 0 26 15:04 pts/1 00:00:00 ./threads2
  134. debian 6232 6219 6256 0 26 15:04 pts/1 00:00:00 ./threads2
  135. debian 6232 6219 6257 0 26 15:04 pts/1 00:00:00 ./threads2
  136. debian 6260 6226 6260 0 1 15:04 pts/2 00:00:00 ps -eLf
Add Comment
Please, Sign In to add comment